1

I'm trying to read a file for a pre-receive hook with the bitbucket api. The point is, I need to check some concrete file and content stuff in order to aprove or reject the commit. You can see the code here:

request.getRefChanges().stream()
.filter(change -> change.getRef().getId().equalsIgnoreCase("refs/heads/master"))
.forEach(refChange -> {
     scmService.getBulkContentCommandFactory(request.getRepository())                
     .contents(new BulkContentCommandParameters.Builder(refChange.getToHash())
            .sinceCommitId(refChange.getFromHash())
            .build(), new BulkContentCallback() {
                    @Override
                    public void onFile(@Nonnull BulkFile file, @Nonnull InputStream content) {
                        this.processFile(file, content);
                    }
                        private void processFile(BulkFile file, InputStream content) {
                            log.info("Streaming file {} with content id {}, file: {}, size: {}",
                                    file.getPath(), file.getContentId(), file.getSize());
                            try {
                                StringWriter writer = new StringWriter();
                                //IOUtils.copy(content, writer, "UTF-8");
                                log.info("file content: {}", readSB(content));
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                log.error(e.getMessage(), e);
                            }
                        }
                    }).call();});

The problem is, the InputStream I get from the onFile method of BulkContentCallback is like just giving me the first line of the file. I've tried with different implementations in the readSB method like InputStream first implementation

    StringBuilder textBuilder = new StringBuilder();
    try (Reader reader = new BufferedReader(new InputStreamReader
              (inputStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
                int c = 0;
                while ((c = reader.read()) != -1) {
                    textBuilder.append((char) c);
                }
            }

    return textBuilder.toString();

Or InputStream second implementation:

 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
 int nRead;
 byte[] data = new byte[1024];
 while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }

 buffer.flush();
 byte[] byteArray = buffer.toByteArray();

 String text = new String(byteArray, StandardCharsets.UTF_8);

 return text;

Or InputStream third implementation:

try (BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream))) {
        return buffer.lines().collect(Collectors.joining("\n"))
}

But the result is always the same, just prints the first line.

Any idea?

dcalap
  • 1,048
  • 2
  • 13
  • 37

1 Answers1

0

Detected as a bug in Bitbucket version 5.13.0 and older (tested in 5.10.1 and later versions and is not working). I just updated to 5.14.0 and works correctly.

Hope it helps.

dcalap
  • 1,048
  • 2
  • 13
  • 37