Create Issues Based on Form Field Values.
Purpose | Hosting | Compatible Tools |
---|---|---|
Create a new issue based on the values of a choice question (checkbox, dropdown or multi-select dropdown) in a form. | Cloud | Any scripting tool, such as JMWE, Power Scripts or Scriptrunner, that uses Groovy. |
Script | //// When using this script in the script console uncomment the next line:
def issueKey = 'ISSUE-1'
//// When using this script in a transition uncomment the next line:
// def issueKey = issue.key
String formName = "Form Name"
String fieldName = "Issue Creation Dropdown Field"
Closure createIssue = { def summary, def issueTypeName, def projectKey ->
def taskType = get('/rest/api/2/issuetype').asObject(List).body.find { it['name'] == issueTypeName }['id']
post('/rest/api/2/issue')
.header('Content-Type', 'application/json')
.body(
[
fields: [
summary : summary,
project : [
key: projectKey
],
issuetype : [
id: taskType
]
]
])
.asString().body
}
Closure getFormId = { def allProperties, String name ->
for(def formProperty : allProperties){
if(formProperty.key.equals("proforma.forms")){
for(def p : formProperty.value.forms) {
if(p.name.equals(name)){
return p.id
}
}
}
}
}
Closure<String> getFieldId = { def property, String name ->
for (def question : property.value.design.questions){
if(name.equals(question.value.label)){
return question.key
}
}
}
Closure getDropdownFieldValue = { def property, String name ->
def fieldId = getFieldId(property, name)
for (def answer : property.value.state.answers){
if(answer.key.equals(fieldId)){
return answer.value.choices
}
}
}
Closure getDropdownFieldInfo = { def property, String name ->
def fieldId = getFieldId(property, name)
for (def question : property.value.design.questions){
if(question.key.equals(fieldId)){
return question.value.choices
}
}
}
def proformaPropertyKeys = []
def allIssueProperties = get('/rest/api/3/issue/' + issueKey + '/properties/')
.header('Content-Type', 'application/json')
.asObject(Map)
if (allIssueProperties.status == 200){
for(def property : allIssueProperties.body.keys){
if(property.key.contains("proforma.forms")){
proformaPropertyKeys.add(property.key)
}
}
}
def formProperties = []
if(!proformaPropertyKeys.isEmpty()){
for(def key : proformaPropertyKeys){
def property = get('/rest/api/3/issue/' + issueKey + '/properties/' + key)
.header('Content-Type', 'application/json')
.asObject(Map)
if(property.status == 200){
formProperties.add(property.body)
}
}
}
def formId
if(!formProperties.isEmpty()){
formId = getFormId(formProperties, formName)
for(def formProperty : formProperties){
if(formProperty.key.equals("proforma.forms" + ".i" + formId) && formProperty.value.schemaVersion.equals(8)){
def fieldChoice = getDropdownFieldValue(formProperty, fieldName)
def fieldInfo = getDropdownFieldInfo(formProperty, fieldName)
for(def option : fieldInfo){
if(option.label.equals("Story") && fieldChoice.contains(option.id)){
createIssue("Summary for Story", "Story", "PS")
} else if(option.label.equals("Task") && fieldChoice.contains(option.id)){
createIssue("Summary for Task", "Task", "PS")
}
}
}
}
}
|
Installation instructions |
|
Possible use cases | Use this script to create new issues based on the value selected on a form. For example, a dropdown menu in a support request form could be used to make new Bug or Feature Request issues. |
Limitations |
|