BitBucket ScriptRunner event handler unable to retrieve issues from Jira

I'm hoping someone can help me diagnose this BitBucket ScriptRunner issue.   We have an event handler on pull request creation that is supposed to look at all the issues associated with the pull request and see if those issues depend on any others in Jira.   If so, we list them in a comment and create a task that requests the user investigate before merging.  

If we just hardcode the repository and pull request IDs instead of getting them from the event, we can run the script in the console and everything works perfectly.   When it runs as an event handler, however, there’s no comment or task created and we get the message below shown in the log.   I’m wondering if authentication to Jira via the applink is a problem here or if it’s something else.   Any ideas?

Here's the error that shows up:

2017-05-10 17:17:13,162 ERROR [AtlassianEvent::thread-3] e0087637 @L9D5B6x1037x2446354x0 1t9b85q, "POST /projects/CMPOC/repos/git-hooks/pull-requests HTTP/1.0" c.o.s.bitbucket.InnerListener Event handler failed: event: com.atlassian.bitbucket.event.pull.PullRequestOpenedEvent file: <inline script> Not Found
       at ~[na:na]
       at$1.handleResponse( ~[na:na]
       at org.apache.http.impl.client.CloseableHttpClient.execute( ~[httpclient-4.5.1.jar:4.5.1]
       at org.apache.http.impl.client.CloseableHttpClient.execute( ~[httpclient-4.5.1.jar:4.5.1]
       at ~[na:na]
       at ~[na:na]
       at ~[na:na]
       at$ Source) ~[na:na]
       at ~[na:na]

And here's the script that we're using as the event handler:

import com.atlassian.applinks.api.ApplicationLinkResponseHandler
import com.atlassian.plugin.PluginAccessor
import com.atlassian.sal.api.UrlMode
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bitbucket.pull.PullRequestService
import com.atlassian.bitbucket.task.TaskService
import com.atlassian.bitbucket.task.TaskCreateRequest
import com.atlassian.bitbucket.task.TaskAnchorType
import com.atlassian.bitbucket.event.pull.PullRequestOpenedEvent
import com.atlassian.bitbucket.integration.jira.JiraIssueService
import com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketBaseScript
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import groovy.transform.BaseScript

@BaseScript BitbucketBaseScript baseScript

PullRequestOpenedEvent event = event

def pluginAccessor = ComponentLocator.getComponent(PluginAccessor.class)
def jiraIssueService = pluginAccessor.getEnabledPluginModule(ScriptRunnerImpl.PLUGIN_KEY + ":jiraIssueService").getModule() as JiraIssueService
def pullRequestService = ComponentLocator.getComponent(PullRequestService)
def taskService = ComponentLocator.getComponent(TaskService)

def pullRequest = event.getPullRequest()
def repository = pullRequest.fromRef.repository
assert pullRequest

def keys = jiraIssueService.getIssuesForPullRequest(,*.key 

def jiraLink = getJiraAppLink()
def authenticatedRequestFactory = jiraLink.createImpersonatingAuthenticatedRequestFactory()

def depkeys = keys.findResults { String key ->
        .createRequest(Request.MethodType.GET, "rest/api/latest/issue/$key?fields=issuelinks")
    	.addHeader("Accept", "application/json")
            handle: { Response response ->
                if (response.successful) {
                    new JsonSlurper().parseText(response.responseBodyAsString).fields.issuelinks.findResults { item -> 
                        if ( == "Depends" && item.outwardIssue) { item.outwardIssue.key }
                } else {
                    log.warn "Failed to look up issue $key in JIRA: " + response.responseBodyAsString
        }] as ApplicationLinkResponseHandler<Void>
def mycomment = pullRequestService.addComment(1616, 96, "WARNING:  Jira Issues involved in this pull request depend on the following: " + depkeys.flatten().join(", "))
def builder = new TaskCreateRequest.Builder()
builder.text("Check Jira issue dependencies before merging this pull request")

1 answer

1 accepted

0 votes
Accepted answer

Looking at the code here, I decided to try replacing the call to createImpersonatingAuthenticatedRequestFactory with a call to createAuthenticatedRequestFactory.   That seems to have solved the problem.

Note for others who may see this later:  the code I posted above contains a hard-coded repository ID and pull request ID when creating the comment.  I was just doing that for troubleshooting purposes.  In practice, we'll use the repository and pull request IDs that we see in the event.

Suggest an answer

Log in or Sign up to answer
Community showcase

Scrum Roles Explained: the Do's and the Don'ts

Hello Community,  Today we are going to talk about the three Scrum Roles. There is the Development Team, the Scrum Master and the Product Owner. In my opinion these three are all really impo...

130 views 2 5
Read article

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