Hi,
I’m trying to read the content of a specific file in a prereceive hook in Bitbucket Server to make sure that this file has a valid content. Unfortunately, the following code does not work and I always get a NoSuchPathException when trying to read the file:
public class PrereceiveHook implements PreReceiveHook {
private final CommitService commitService;
private final ContentService contentService;
public PrereceiveHook(CommitService commitService, ContentService contentService) {
this.commitService = commitService;
this.contentService = contentService;
}
public boolean onReceive(Repository repo, Collection<RefChange> refChanges, HookResponse hookResponse) {
for (RefChange change : refChanges) {
if (change.getType() != RefChangeType.DELETE) {
CommitsBetweenRequest request = new CommitsBetweenRequest.Builder(repo)
.exclude(change.getFromHash())
.include(change.getToHash())
.build();
for (Commit commit : commitService.getCommitsBetween(request, PageUtils.newRequest(0, 999)).getValues()) {
ChangesRequest changesRequest = new ChangesRequest.Builder(repo, commit.getId()).build();
commitService.streamChanges(changesRequest, new ChangeCallback() {
@Override
public boolean onChange(@Nonnull Change change) throws IOException {
if (change.getPath().getName().equals("file.txt")) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
contentService.streamFile(repo, commit.getId(), "file.txt", new TypeAwareOutputSupplier() {
@Override
public OutputStream getStream(@Nonnull String s) throws IOException {
return os;
}
});
String fileContent = new String(os.toByteArray(), "UTF-8");
// parse file
} catch (NoSuchPathException e) {
hookResponse.err().println("failed to read file.txt");
}
}
return true;
}
});
}
}
}
return true;
}
}
Any suggestions would be much appreciated. I tried this code with Bitbucket Server versions 4.8 and 5.7, both resulting in the same error as described above even when a file called “file.txt” is part of the committed code.
Thanks,
Michael