I am trying to create an issue in Jira when pull request is created in BitBucket:
So I've borrowed code from here and adapted it to create task in Jira, but task is not created. I double checked the name of the project and type of task to be created.
Code is not throwing any exception and pull request is correctly created.
My code looks like this:
package examples.bitbucket.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.event.pull.PullRequestOpenRequestedEvent
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.transform.BaseScript
import com.atlassian.bitbucket.event.CancelableEvent
import com.atlassian.bitbucket.event.pull.PullRequestOpenedEvent
@BaseScript BitbucketBaseScript baseScript
def pluginAccessor = ComponentLocator.getComponent(PluginAccessor.class)
def jiraIssueService = ScriptRunnerImpl.getOsgiService(JiraIssueService)
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 input = new JsonBuilder(["""{
"fields": {
"project":
{
"key": "E1"
},
"summary": "REST ye merry gentlemen.",
"description": "Creating of an issue using project keys and issue type names using the REST API. The pull reuest id is ",
"issuetype": {
"name": "Develop"
}
}
}"""]).toString();
authenticatedRequestFactory
.createRequest(Request.MethodType.POST, "rest/api/2/issue/")
.addHeader("Content-Type", "application/json")
.setEntity(input)
.execute([
handle: { Response response ->
if (response.successful) {
log.debug "issue is created "
} else {
log.warn "Failed to create comment: $response.responseBodyAsStream"
}
}] as ApplicationLinkResponseHandler<Void>
)
However, issue is not created. Could you say, please, how to create issue from PullRequestOpenedEvent?
As a first step, please change the line at the bottom with the log.warn, to log.error to make sure any errors are being printed to your Bitbucket log file?
I would also change the error message to
"Failed to create issue: $response.responseBodyAsStream"
Finally, you can remove any lines mentioning jiraIssueService or JiraIssueService, as they are not needed in the creation example above.
Please let me know how you get on?
Kind regards,
Robert Giddings,
Product Manager, ScriptRunner for Bitbucket
The reason of why issue was not created was that *components* and *reporter* fields are required fields. So, I added these fields into json object and issue started to be created:
def body_req = '''{
"fields":
{
"project" : { "key" : "E" },
"issuetype" : { "name" : "Task" },
"summary" : "Hello, World!S1",
"description" : "World!S",
"components": [{ "id": "11382" }],
"reporter": {"name": "somejirauser"}
}
}'''
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Robert Giddings _Adaptavist_ could you see, please, this question?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.