How can I get a reference to a CommitService instance in a pre-receive hook?

I'm trying to write a ScriptRunner pre-receive hook that rejects merge commits except from master.

I'm able to enumerate the refChanges, but I don't see how to get a reference to a CommitService instance so that I can get the message and parents of each commit.

Here's what I have thus far:

import com.atlassian.stash.commit.Commit
import com.atlassian.stash.commit.CommitService
import com.atlassian.stash.commit.CommitsBetweenRequest
import com.atlassian.stash.hook.HookResponse 
import com.atlassian.stash.repository.RefChange
import com.atlassian.stash.repository.Repository
import com.atlassian.stash.util.Page
import com.atlassian.stash.util.PageRequestImpl
import com.atlassian.stash.util.PageUtils
Repository repository = repository 
Collection<RefChange> refChanges = refChanges
HookResponse hookResponse = hookResponse
for (refChange in refChanges) {
  hookResponse.out().print("from ${refChange.getFromHash()} to ${refChange.getToHash()}\n")
  hookResponse.out().print(">>> ${refChange.getRefId()}\n")
  CommitsBetweenRequest request = new CommitsBetweenRequest.Builder(repository)
      .exclude(refChange.getFromHash())
      .include(refChange.getToHash())
      .build()
  try {
    Page<Commit> commits =
      commitService.getCommitsBetween(request, PageUtils.newRequest(0, 25))
  } catch (e) {
    hookResponse.out().println("DOH: ${e.getMessage()}")
    return false
  }
  for (commit in commits) {
    hookResponse.out().println(">>> ${commit.getMessage()}")
    hookResponse.out().println(">>> parents: ${commit.getParents().length}")
  }
}
return true

EDIT

Well, I can't answer my own question, apparently, but the solution is to use ComponentLocator:

import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.stash.commit.CommitService


CommitService commitService = ComponentLocator.getComponent(CommitService)

1 answer

1 accepted

1 vote

The solution is to use ComponentLocator wink

You just need to be aware that you will have to modify that import when you come to upgrade to Bitbucket server: https://scriptrunner.adaptavist.com/latest/bitbucket/releases/release-4.1.2.html

There is also a method tacked on to refChanges which will get all the commits:

refChanges.getCommits(repository).each { Commit commit -> ...

 

 

Thanks! That is useful. It would be great to add some documentation about this to https://scriptrunner.adaptavist.com/latest/bitbucket/PreReceiveHooks.html

Some of the extension methods are doc'd at https://scriptrunner.adaptavist.com/latest/bitbucket/#_condition_methods, unfortunately not that one.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Jun 12, 2018 in Bitbucket

Do you use any Atlassian products for your personal projects?

After spinning my wheels trying to get organized enough to write a book for National Novel Writing Month (NaNoWriMo) I took my affinity for Atlassian products from my work life and decided to tr...

161 views 20 8
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you