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)