I'm trying to make a BitBucket hook that would reject a push if it contained a file not matching a naming convention. So far, I'm able to create a PreRepositoryHook implementation that register the following callback.
public class MyPreRepositoryHook implements PreRepositoryHook<RepositoryHookRequest> {
public MyPreRepositoryHook () {
}
@Nonnull
@Override
public RepositoryHookResult preUpdate(@Nonnull PreRepositoryHookContext context,
@Nonnull RepositoryHookRequest request) {
// hook only wants commits added to the repository
context.registerCommitCallback(
new MyPreCommitCallback(),
RepositoryHookCommitFilter.ADDED_TO_REPOSITORY);
// return accepted() here, the callback gets a chance to reject the change when getResult() is called
return RepositoryHookResult.accepted();
}
// In MyPreCommitCallback
@Override
public boolean onCommitAdded(@Nonnull CommitAddedDetails commitDetails) {
Commit commit = commitDetails.getCommit();
SimpleChangeset.Builder builder = new SimpleChangeset.Builder(commit);
SimpleChangeset simpleChangeset = builder.build();
Page<Change> changes = simpleChangeset.getChanges();
}
But I am unable to get the list of files since the call to simpleChangeset.getChanges always return null.
Any help in getting a list of file names would be appreciated. Thank you.
I figured it out.
@Component
public class AltresPreRepositoryHook implements PreRepositoryHook<RepositoryHookRequest> {
private final CommitService commitService;
@Autowired
public AltresPreRepositoryHook(@ComponentImport CommitService commitService) {
this.commitService = commitService;
}
private static class AltresPreCommitCallback implements PreRepositoryHookCommitCallback {
private final RepositoryHookRequest request;
private final CommitService commitService;
private RepositoryHookResult result = RepositoryHookResult.accepted();
public AltresPreCommitCallback(RepositoryHookRequest request, CommitService commitService) {
this.request = request;
this.commitService = commitService;
}
@Nonnull
@Override
public RepositoryHookResult getResult() {
return result;
}
@Override
public boolean onCommitAdded(@Nonnull CommitAddedDetails commitDetails) {
Commit commit = commitDetails.getCommit();
ChangesRequest.Builder builder = new ChangesRequest.Builder(commit.getRepository(), commit.getId());
ChangesRequest changesRequest = builder.build();
final ChangedPathsCollector changedPathsCollector = new ChangedPathsCollector();
commitService.streamChanges(changesRequest, changedPathsCollector);
Collection<String> changedPaths = changedPathsCollector.getChangedPaths();
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.