Get file changes in RepositoryMergeCheck

I’m trying to get list of changed files in RepositoryMergeCheck, but Collection of RefChange always empty.

My code:

@Nonnull
@Override
public RepositoryHookResult preUpdate(
    @Nonnull PreRepositoryHookContext context,
    @Nonnull PullRequestMergeHookRequest request) {
    Collection<RefChange> refChanges = request.getRefChanges();
    // ............
}

With debugger I can see my variables, and refChanges is empty (size = 0).

I suppose RepositoryMergeCheck haven’t any information about changes. I want to add some specific reviewers based on changing some specific files in PR. Is it possible (one way or another)?

1 Like

You can obtain the changeset from the pull request object with the help of PullRequestService API:

PullRequest pullRequest = request.getPullRequest();

PullRequestChangesRequest changesRequest = new PullRequestChangesRequest.Builder(pullRequest).build();
pullRequestService.streamChanges(changesRequest, new ChangeCallback() {
            @Override
            public boolean onChange(@Nonnull Change change) throws IOException {
                // >>> Process your file change here
                return false;
            }
        });

Let me know if that works for you
Kind regards, Ulrich
Izymes.com
++Remove boring from work++

1 Like

Thanks for the answer, Ulrich!

I already tried this way - unfortunately, it return only the first change.
In my case, I have 2 commits (add file one and add file two). In the onChange(@Nonnull Change change) method I can see only change for “add file one” commit.

I found solutions throw DiffContentCallback - looks terrible, but works :smiley::

CompareRef compareRefFrom = new CompareRef(request.getFromRef());
CompareRef compareRefTo = new CompareRef(request.getToRef());
CompareDiffRequest compareDiffRequest = new CompareDiffRequest.Builder()
    .fromRef(compareRefFrom)
    .toRef(compareRefTo)
    .build();

DiffContentCallback diffContentCallback = new DiffContentCallback() {
    @Override
    public void onDiffStart(@Nullable Path path, @Nullable Path path1) {
        // ... do something ...
    }

    /**
    * Not necessary
    * but can't use DiffContentCallback without overriding
    */
    @Override
    public void offerThreads(@Nonnull Stream<CommentThread> stream) {}
    @Override
    public void onBinary(@Nullable Path path, @Nullable Path path1) {}
    @Override
    public void onDiffEnd(boolean b) {}
    @Override
    public void onEnd(@Nonnull DiffSummary diffSummary) {}
    @Override
    public void onHunkEnd(boolean b) {}
    @Override
    public void onHunkStart(int i, int i1, int i2, int i3, @Nullable String s) {}
    @Override
    public void onSegmentEnd(boolean b) {}
    @Override
    public void onSegmentLine(@Nonnull String s, @Nullable ConflictMarker conflictMarker, boolean b) {}
    @Override
    public void onSegmentStart(@Nonnull DiffSegmentType diffSegmentType) {}
    @Override
    public void onStart(@Nonnull DiffContext diffContext) {}
};
compareService.streamDiff(compareDiffRequest, diffContentCallback);

And now I can catch all changes. I hope we can find a better way.