Hello,
I am trying to write a hook that will validate the contents of a file and deny a push if the content is not correct. I have so far written the following piece of code:
@Nonnull
@Override
public RepositoryHookResult preUpdate(@Nonnull PreRepositoryHookContext context, @Nonnull RepositoryPushHookRequest request) {
final Repository repo = request.getRepository();
final Map<String, ByteArrayOutputStream> fileStreams = new HashMap<>();
request.getRefChanges().stream().forEach(refChange -> {
ChangesRequest changesReq = new ChangesRequest.Builder(repo, refChange.getToHash()).sinceId(refChange.getFromHash()).build();
commitService.streamChanges(changesReq, change ->{
String fileName = change.getPath().toString();
contentService.streamFile(repo, refChange.getToHash(), fileName, s -> {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
fileStreams.put(fileName, baos);
return baos;
});
return true;
});
});
fileStreams.entrySet().stream().forEach(entry -> {
new String(entry.getValue().toByteArray())); //This is the String I need
});
return RepositoryHookResult.accepted();
}
When changing the refChange.getToHash()
to refChange.getRef().getDisplayId()
in the contentService I will get the content but only from before the push. I need the content when the commits that are being pushed have been added but I still want to be able to reject a push. I have also tried a few other Id’s in the contentService with no success.
Thanks,
Niels