Action
Send to JIRA
Extending upon the actions created by Koronics Norbert and Padraic Renaghan
- Updates the API to v3
- Error messages are no longer added to the Draft document
- Constructs a URL to the newly created ticket instead of the API payload and copies that to the clipboard
- Creates using the required Atlassian Document Format for the description
Steps
-
script
// Setup Credential object to store JIRA server info let 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"); // Prompt the user for their JIRA credentials credential.authorize(); // Construct the URL for the JIRA API endpoint const url = `https://${credential.getValue("host")}/rest/api/3/issue/`; // Get the current draft content and split it into lines let lines = draft.content.split("\n"); // The first line is the title of the JIRA ticket let title = lines[0]; // The rest of the lines are the description of the JIRA ticket let descriptionBody = lines.slice(1).join("\n"); // Construct the Atlassian Document Format (ADF) for the description let description = { "type": "doc", "version": 1, "content": [ { "type": "paragraph", "content": [ { "type": "text", "text": descriptionBody } ] } ] } // Construct the fields object for the JIRA API request let fields = { "project": { "key": credential.getValue("project")}, "issuetype": { "name": "Task" }, "summary": title, "description": description, }; // Make a POST request to the JIRA API to create a new issue let http = HTTP.create(); let response = http.request({ "url": url, "username": credential.getValue("username"), "password": credential.getValue("password"), "method": "POST", "data": { fields }, "encoding": "json", "headers": { "Content-Type": "application/json", "Accept": "application/json" } }); // Log the API response, useful for debugging console.log("JIRA API Response: " + response.statusCode + " -- " + response.responseText); if (response.success || response.statusCode == 201) { // If the request is successful, parse the response JSON and construct the URL of the new JIRA ticket // Add the ticket URL to the draft content and display a success message let r = JSON.parse(response.responseText); let ticketURL = "https://" + credential.getValue("host") + "/browse/" + r.key; let t = r.key + " \/\/ " + fields.summary + " \/\/ " + ticketURL; draft.content = t + "\n\n" + draft.content; draft.update(); app.displaySuccessMessage("Created " + r.key + " in JIRA"); app.setClipboard(ticketURL); } else { // If the request fails, display an error message app.displayErrorMessage("Failed to create JIRA ticket. Check the console for details."); context.fail("ERROR " + response.statusCode + " " + response.responseText); }
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.