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 10.132.26.17,10.135.122.71 "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>
groovyx.net.http.HttpResponseException: Not Found
at groovyx.net.http.HTTPBuilder.defaultFailureHandler(HTTPBuilder.java:651) ~[na:na]
at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:503) ~[na:na]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:222) ~[httpclient-4.5.1.jar:4.5.1]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:164) ~[httpclient-4.5.1.jar:4.5.1]
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515) ~[na:na]
at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:285) ~[na:na]
at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:255) ~[na:na]
at groovyx.net.http.HTTPBuilder$get.call(Unknown Source) ~[na:na]
at Script8.run(Script8.groovy:41) ~[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.sal.api.net.Request
import com.atlassian.sal.api.net.Response
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(repository.id, pullRequest.id)*.key
def jiraLink = getJiraAppLink()
def authenticatedRequestFactory = jiraLink.createImpersonatingAuthenticatedRequestFactory()
def depkeys = keys.findResults { String key ->
authenticatedRequestFactory
.createRequest(Request.MethodType.GET, "rest/api/latest/issue/$key?fields=issuelinks")
.addHeader("Accept", "application/json")
.execute([
handle: { Response response ->
if (response.successful) {
new JsonSlurper().parseText(response.responseBodyAsString).fields.issuelinks.findResults { item ->
if (item.type.name == "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.anchorId(mycomment.getId())
builder.anchorType(TaskAnchorType.COMMENT)
builder.text("Check Jira issue dependencies before merging this pull request")
taskService.create(builder.build())
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.