Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 21:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×

Forums

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

Clone an issue to projects

Antonio D'Errico
Contributor
March 8, 2019

Hi everyone,

it is possible to clone an issue to one or more projects based on a projectlist?

here is an example:

an issue needs to be cloned to four jira projects. i want to execute a transition called for example "clone issue to projects". then a list comes up in which i need to define to which projects this issue needs to be cloned.

 

best

toni

2 answers

1 accepted

0 votes
Answer accepted
Antonio D'Errico
Contributor
April 4, 2019

Rest Endpoint


import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import com.atlassian.jira.component.ComponentAccessor
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovy.json.JsonSlurper

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

getProjects(httpMethod: "GET", groups: ["jira-software-users"]) { MultivaluedMap queryParams, String body ->

def query = queryParams.getFirst("query") as String


def prList = ComponentAccessor.getProjectManager().getProjectObjects()

def filtertList = prList.findAll { project -> project.getName().toLowerCase().contains(query.toLowerCase()) }


def rt = [
items : filtertList.collect { project ->
[
value: project.getName(),
html : project.getName(),
label: project.getName(),
displayName: project.getName(),
]
},
total : filtertList.size(),
footer: "Wähle das Projekt aus ...",
]

return Response.ok(new JsonBuilder(rt).toString()).build()
}

behaviour

def searchField = getFieldByName("Projekt")


searchField.convertToMultiSelect([
ajaxOptions: [
url : "BASEURL/rest/scriptrunner/latest/custom/getProjects",
query: true, // keep going back to the sever for each keystroke
minQueryLength: 1,
keyInputPeriod: 500,
formatResponse: "general",
]
]
)
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 4, 2019

Hello @Antonio D_Errico 

Great to see the complete code sample, if you think my answer helped you in your requirement please upvote/accept. thanks!

0 votes
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 8, 2019

Hello @Antonio D_Errico 

Yes this is possible. When you click on a transition then you can have a transition screen and in that transition screen you can have a variable which is a "project picker" custom field. 

Base don the values selected in the project picker custom field you can select the projects in which to clone the issues in the post-function of the transition. And once the cloning is done then you can clear the custom field for next time. 

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 8, 2019

Also, since Jira doesn't support multi-select project picker. Thus, you can create a custom field like the ones here which displays dynamically the actual list of projects

https://scriptrunner.adaptavist.com/latest/jira/behaviours-conversions.html

Antonio D'Errico
Contributor
March 8, 2019

i dont find the example for a mutliselect field for projects only for issues

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 8, 2019

The example of issues is there, you can create a similar example for projects using REST API call which you also described. Basically use REST API to fetch project list and then let user mult-select options from the list.

Antonio D'Errico
Contributor
March 13, 2019

hi @Tarun Sapra 

i am not  familiar with the rest api and the groovy. so i am a little bit lost at this moment.

i understand that i need a rest endpoint but i am not able to build it up.

how should the rest endpoint look like:

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

doSomething(
httpMethod: "GET", groups: ["jira-administrators"]
) { MultivaluedMap queryParams, String body ->
return Response.ok(new JsonBuilder([abc: 42]).toString()).build()
}

 

best regards

toni

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 14, 2019

Hello @Antonio D_Errico 

I am not talking about created a custom REST endpoint but calling the REST API of JIRA

https://scriptrunner.adaptavist.com/latest/jira/behaviours-conversions.html#_walkthrough_external_rest_service

In the above  example they are calling REST API of confluence but you can call REST API of JIRA which returns the project list.

https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/project-getAllProjects

Antonio D'Errico
Contributor
March 14, 2019

hi @Tarun Sapra

ok thank you.

the initilaiser looks like this but the field says no matches.

getFieldByName("Projekt").convertToMultiSelect([
ajaxOptions: [
url : getBaseUrl() + "/rest/api/2/project",
query: true,
formatResponse: "general"
]
])

If i open the api call over my brower i get the json output.

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 14, 2019

In the call you have to parse JSON and then it will work.

LIke in one of the examples in the link i shared they are parsing the response so you have to parse the response as well.


def response = httpBuilder.request(Method.GET, ContentType.JSON) {
uri.path = "/rest/api/2/issue/picker"
uri.query = [currentJQL: jqlQuery, query: query, showSubTasks: true, showSubTaskParent: true]

response.failure = { resp, reader ->
log.warn("Failed to query JIRA API: " + reader.errorMessages)
return
}
}

response.sections.each { section ->
section.issues.each {
// delete the image tag, because the issue picker is hard-coded
// to prepend the current instance base URL.
it.remove("img")
}
}

return Response.ok(new JsonBuilder(response).toString()).build()

Suggest an answer

Log in or Sign up to answer