Action

JIRA Create

Posted by Koronics Norbert, Last update about 3 years ago

Uptated version:
*use api token in password field
*api url changed to v2

Original:
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

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": "Task" },
      "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": [ "DRAFTS" ]
    };
    
    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 = credential.getValue("host") 
      + "/rest/api/2/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 + " \/\/ " + r.self;
      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
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.