Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner, Bitbucket - cannot create task in Jira after pull request is created

zaharovvv_suek_ru
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 25, 2020

I am trying to create an issue in Jira when pull request is created in BitBucket:

PullRequestOpenedEvent.png


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?

 

 

 

1 answer

1 accepted

1 vote
Answer accepted
Robert Giddings _Adaptavist_
Community Champion
May 26, 2020

@zaharovvv_suek_ru ,

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

zaharovvv_suek_ru
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 27, 2020 edited

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"}
}
}'''
zaharovvv_suek_ru
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 28, 2020

@Robert Giddings _Adaptavist_ could you see, please, this question?

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, jira cloud certification, managing jira projects, jira project administration, jira cloud exam, atlassian certification, agile project management, jira workflows, jira permissions, jira training, jira cloud skills, atlassian learning

Become a Certified Jira Service Project Expert 🦸🏻‍♂️

Validate your expertise in managing Jira Service Projects for Cloud. Master configuration, optimize workflows, and manage users seamlessly. Earn global 🗺️ recognition and advance your career as a trusted Jira Service management expert.

Get Certified! ✍️
AUG Leaders

Atlassian Community Events