Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Script

Code Block
//// 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

  • If used in a transition:

    • Comment line 2

    • Uncomment line 4

    • Change formName in line 6 to the name of the form that has the dropdown field used in the script.

    • Change fieldName in line 7 to the name/label of the field that has the dropdown options used for different issue creation.

    • Change value inside option.label.equals() in lines 100 and 102 to the dropdown values in the field used in the script. Values must be between quotation marks.

    • Change parameters inside createIssue() in lines 101 and 103 to:

      • Issue summary (between quotation marks)

      • Issue Type (must match an issue type name and be between quotation marks)

      • Project key (between quotation marks)

    • More else if clauses can be added if more options exist in the field, patterns must be the same as lines 102 to 104. else if's can also be removed if fewer options are needed.

  • If used in the script console:

    • Change issueKey in line 2 to the issue that has the form with the field used in the script.

    • Change formName in line 6 to the name of the form that has the dropdown field used in the script.

    • Change fieldName in line 7 to the name/label of the field that has the dropdown options used for different issue creation.

    • Change value inside option.label.equals() in lines 100 and 102 to the dropdown values in the field used in the script. Values must be between quotation marks.

    • Change parameters inside createIssue() in lines 101 and 103 to:

      • Issue summary (between quotation marks)

      • Issue Type (must match an issue type name and be between quotation marks)

      • Project key (between quotation marks)

    • More else if clauses can be added if more options exist in the field, patterns must be the same as lines 102 to 104. else if's can also be removed if fewer options are needed.

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

  • All options must be on the same choice field, on the same form.

  • The code between lines 9 to 25 and 101 and to 106 may need to be amended to include other fields that have been set as required for creating issues in your instance.

  • If there are multiple copies of the same form on the issue, the script will run on the first copy of the form.

  • Issues can only be created for Standard issue types (not subtask issue types).

  • This script may not work if the form is over 32 KB. You can limit the size of your forms by using multiple smaller forms, and by including character/word limits on your text fields.

  • This script will not work on Legacy Forms.

...