2

Im trying to write a stash plugin that will iterate through the commits in a change set pushed to stash in a Pre Receive Repository Hook.

The API passes a Collection of refChange in the onReceive method.

public boolean onReceive(RepositoryHookContext context, Collection<RefChange> refChanges, HookResponse hookResponse)

if I make 3 commits then push I get one RefChange which looks like this

refId = refs/heads/master
fromHash = ded3e4583653f14892cc3e8a898ba74ee75e1a58 // First Commit in change set
toHash = ae017dcdadf7ca69617fb05f6905cccfe2aa4229 // Most recent commit
type = "UPDATE"

Id like to get a collection of all the commits so that I can get all the commit messages.

I'm looking at com.atlassian.stash.commit.CommitService getCommit and getCommits. I think I need to getCommitsBetween but can't quite figure out how to crate the GetCommitsBetween parameter needed from the RefChange I have.

Am I even heading down the right path here?

sam schonstal
  • 429
  • 2
  • 10

2 Answers2

4

Even though the CommitsBetweenRequest page on the Atlassian Stash API documentation is one of the few pages with an explanation, it took some trial and error to figure this out. GetCommitsBetween works but here's the trick...

Set the commitsBetweenBuilder.exclude to the starting commit in the change set and commitsBetweenBuilder.include to the ending commit hash.

CommitsBetweenRequest.Builder commitsBetweenBuilder = new CommitsBetweenRequest.Builder(context.getRepository() );
commitsBetweenBuilder.exclude(refChange.getFromHash()); //Starting with
commitsBetweenBuilder.include(refChange.getToHash()); // ending with

PageRequest pageRequest = new PageRequestImpl(0,6);

Page<Commit> commits = commitService.getCommitsBetween(commitsBetweenBuilder.build(), pageRequest);

//TODO: handle Pages
for (Commit commit : commits.getValues()) {
   hookResponse.out().println("Message = " + commit.getMessage() + "\n");
}
reduckted
  • 2,358
  • 3
  • 28
  • 37
sam schonstal
  • 429
  • 2
  • 10
  • How are you using the `commitService` ? i'm trying to figure out how to initialize it – Alex Brodov May 25 '15 at 09:37
  • I believe it gets injected via the constructor... `public class myClass implements AsyncPostReceiveRepositoryHook, RepositorySettingsValidator { private final CommitService commitService; public myClass(CommitService commitService, NavBuilder navBuilder) { this.commitService = commitService; this.navBuilder = navBuilder; }` – sam schonstal Jun 04 '15 at 17:55
  • When I use this constructor for some reason it disables my plugin completely, when I refresh my repository hooks it disappear from the list... how can I implement this? – H.Epstein Apr 18 '20 at 20:00
-2

I wasn't able to get the dependency injection working for the CommitService. Spring for some reason wasn't able to find it, when trying to run in locally ???

I did getting it working using the component locator.

CommitService commitService = ComponentLocator.getComponent(CommitService.class);
ajitksharma
  • 4,523
  • 2
  • 21
  • 40