Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

@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

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"}
}
}'''

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

New improvements to user management in Bitbucket Cloud 👥

Hey Community! We’re willing to wager that quite a few of you not only use Bitbucket, but administer it too. Our team is excited to share that we’ll be releasing improvements throughout this month of...

3,697 views 10 16
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you