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

Create issue in Jira, create issue links in created issue when pull request is opened in 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

I am trying to get all linked tasks when pull request is opened:

PullRequestOpenedEvent.png

Let me explain what I mean. For example, I have:

- two projects - "Project_A" and "Project_B".

- one issue of "Project_A" such as "project_A_issue_1" and 4 issues of "Project_B" such as "project_B_issue_1" and "project_B_issue_2", "project_B_issue_3" and "project_B_issue_4"

And I am creating a pull request of issues "Project_B" such as "project_B_issue_3" and "project_B_issue_4". The issue "project_B_issue_3" has links to issues:
- "project_A_issue_1"
- "project_B_issue_1" and "project_B_issue_2"

What I want is when pull request is created, then I need to read all these issues such as "project_A_issue_1", "project_B_issue_1" and "project_B_issue_2" in custom event handler "PullRequestOpenedEvent".
So I've borrowed code from here and adapted it to create task in Jira. Tasks are created perfectly.
How can all these issues ("project_A_issue_1", "project_B_issue_1" and "project_B_issue_2") can be read in these piece of code?

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 keySize = keys.size()

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

def input = '''{
"fields":
{
"project" : { "key" : "E" },
"issuetype" : { "name" : "Task" },
"summary" : "Foo summary''' + "${key}" + '''",
"description" : "''' + "Foo summary" + '''",
"components": [{ "id": "11382" }],
"reporter": {"name": "fooUser"}
}
}'''

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>
)
}





 

 

1 answer

1 accepted

0 votes
Answer accepted
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.
June 1, 2020

Our team is figured out how we can do it:
1. We've just created a trigger "Pull request created" in Jira Workflow
2. Then we've used a post function in Jira workflow and used the following code into post function:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.link.DefaultIssueLinkManager
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.util.UserManager

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

def issue_BAR = null as Issue;

// Preparing issue info to create issue
def issueService = ComponentAccessor.getIssueService()
def issueInputParameters = issueService.newIssueInputParameters()
def actIssueType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject(issue.projectObject).find {it.name == "Task"};
def componentsId = new Long[1];
componentsId[0] = 11382;
def originalEstimate = issue.originalEstimate/(60 * 4) as Long;


def userManager = ComponentAccessor.getUserManager() as UserManager
ApplicationUser user = userManager.getUserByName("fooUser");
ComponentAccessor.jiraAuthenticationContext.setLoggedInUser(user);

issueInputParameters
.setProjectId(issue.getProjectObject().getId())
.setSummary('''Foo task - "''' + issue.getSummary() + '''"''')
.setDescription('''Foo task - "''' + issue.getSummary() + '''"''')
.setIssueTypeId(issue.issueTypeId)
.setComponentIds(componentsId)
.setReporterId(user.name)
.setOriginalEstimate(originalEstimate);


log.info('1. Current issue' + issue)
def createValidationResult = issueService.validateCreate(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issueInputParameters)
if (createValidationResult.isValid())
{
// Creation of issue
def createResult = issueService.create(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), createValidationResult)
def createdIssue = createResult.getIssue()
log.info("issue2 : " + createdIssue)

// Reading BAR link
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
issueLinkManager.getInwardLinks(issue.id).each{ issueLink ->
def linkedIssue = issueLink.getSourceObject()
if (linkedIssue.getProjectObject().getKey() == 'BAR') {
issue_BAR = linkedIssue;
log.info('2. ***** LinkedIssue: *****' + linkedIssue)
log.info('2.1. ***** IssueType of LinkedIssue: *****' + linkedIssue.getIssueType().getName())
}
}
log.info('3. ***** LinkedIssue: *****' + issue_BAR)


// Setting links to other issues
ComponentAccessor.getIssueLinkManager().createIssueLink(issue.getId(), createdIssue.getId(), 10530L, 1L, ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser())
ComponentAccessor.getIssueLinkManager().createIssueLink(issue_BAR.getId(), createdIssue.getId(), 10334L, 1L, ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser())
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events