Drafts Action Directory

JIRA Create

Posted by Padraic Renaghan @prenagha, Last update 3 months ago

Create an issue in JIRA
First line of draft is the issue summary, remaining lines are the issue description
Adjust defaults in the script code as needed
Saves JIRA issue Id and Summary back as first line of draft when complete and copies same to clipboard

Padraic Renaghan @prenagha

Install

Steps

  • script

    /*
      Create an issue in JIRA
      First line of the draft is the Issue Summary, 
      remaining lines are the Issue Description
      Padraic Renaghan 7/2/2019
    */
    let NL = "\n";
    let LOG_MARK = "-------";
    
    // First run will prompt for host, user, password
    // to JIRA server
    var credential = Credential.createWithHostUsernamePassword("jira-server", "JIRA credential");
    credential.addTextField("host", "JIRA Domain");
    credential.addTextField("project", "JIRA Project");
    credential.addTextField("username", "JIRA Username");
    credential.addPasswordField("password", "JIRA Password");
    
    // reset the credential
    // credential.forget();
    
    // make sure we have credential info
    credential.authorize();
    
    // build up JIRA issue properties
    var fields = { 
      "project": { "key": credential.getValue("project") },
      "issuetype": { "name": "Enhancement" },
      "summary": "",
      "description": "",
      "customfield_10020": { "value": "PROD" },
      "versions": [{ "name": "19.1" }],
      "assignee": { "name": credential.getValue("username") },
      "reporter": { "name": credential.getValue("username") },
      "security": { "name": "Internal Shared" },
      "labels": [ "zzz" ]
    };
    
    var lines = draft.content.split(NL);
    for (i=0; i<lines.length; i++) {
      var line = lines[i].replace(/\r/gm, "");
     if (line.indexOf(LOG_MARK) == 0) {
        break;
      } else if (fields.summary) {
        fields.description += line + NL;
      } else {
        fields.summary = line;
      }
    }
    
    var api = "https://" + credential.getValue("host") 
      + "/rest/api/latest/issue";
    
    var http = HTTP.create();
    var response = http.request({
      "url": api,
      "username": credential.getValue("username"),
      "password": credential.getValue("password"),
      "method": "POST",
      "data": { fields },
      "encoding": "json",
      "headers": { 
        "Content-Type": "application/json",
        "Accept": "application/json"
      }
    });
    
    console.log("JIRA API Response: " + response.statusCode + " -- " + response.responseText);
    
    if (response.success || response.statusCode == 201) {
      let r = JSON.parse(response.responseText);
      let t = r.key + " " + fields.summary;
      draft.content += t + "\n\n" + draft.content;
      app.displaySuccessMessage("Created " + t);
      app.setClipboard(t);
    } else {
      draft.content += NL + LOG_MARK + NL;
      context.fail("ERROR " + response.statusCode + " "
       + response.responseText);
      draft.content += "ERROR " + response.statusCode + NL
       + response.responseText + NL
       + "Request Fields: " + JSON.stringify(fields, null, 2) + NL;
    }
    draft.update();
    

Options

  • After Success Default
    Notification Info
    Log Level Info

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.