floodmeadows / Jira create sub-tasks

// ==UserScript==
// @name         Jira create sub-tasks
// @namespace    https://openuserjs.org/users/floodmeadows
// @description  Adds buttons to create sub-tasks for Android, iOS, and Services. Each sub-task will have the same name as the main issue/ticket, but prefixed with the sub-task type (Android, iOS or Services, and
// @copyright    2021, floodmeadows (https://openuserjs.org/users/floodmeadows)
// @license      MIT
// @version      0.2.1
// @include      https://jira.*.uk/browse/*
// @updateURL    https://openuserjs.org/meta/floodmeadows/Jira_create_sub-tasks.meta.js
// @downloadURL  https://openuserjs.org/install/floodmeadows/Jira_create_sub-tasks.user.js
// @grant        none
// ==/UserScript==

/* jshint esversion: 6 */

//--- Customise these to your Jira project ----//
const jiraSubtaskIssueTypeId = 5;
//---------------------------------------------//

const debug = false;

(function() {
  'use strict';
  'use esversion: 6';

  addLinkAddSubtasks();
  addLinkCreateSubtask("Android","Android");
  addLinkCreateSubtask("iOS","iOS");
  addLinkCreateSubtask("Services","Microservices");
})();

function addLinkAddSubtasks() {
  const newElement = document.createElement("a");
  newElement.setAttribute("href","#");
  newElement.setAttribute("class","aui-button toolbar-trigger issueaction-workflow-transition");
  newElement.setAttribute("style","background-color:#ffffffff");
  const text = document.createTextNode("Add sub-tasks:");
  newElement.appendChild(text);
  const target = document.getElementById('opsbar-transitions_more');
  target.parentNode.appendChild(newElement);
}

function addLinkCreateSubtask(platformName, componentName) {
  const newElement = document.createElement("a");
  newElement.setAttribute("href","#");
  newElement.setAttribute("class","aui-button toolbar-trigger issueaction-workflow-transition");
  newElement.addEventListener("click", function(){createSubtask(platformName, componentName);});
  const text = document.createTextNode(platformName);
  newElement.appendChild(text);
  const target = document.getElementById('opsbar-transitions_more');
  target.parentNode.appendChild(newElement);
}

function createSubtask(platformName, componentName) {
    const currentIssueKey = document.getElementById("key-val").childNodes[0].nodeValue;
    const currentIssueSummary = document.getElementById("summary-val").childNodes[0].nodeValue;
    const currentUrl = new URL(document.URL);
    const jiraBaseUrl = currentUrl.protocol + '//' + currentUrl.host;
    const createIssueUrl = jiraBaseUrl + '/rest/api/latest/issue';

    // Parse the Jira project key out of the current URL. e.g. if the current issue key is "ABC-1234" then the project key will be ABC.
    const pathArr = location.pathname.split("/");
    const jiraProjectKey = pathArr[pathArr.length-1].split("-")[0];


    var headers = new Headers();
    headers.append("Content-Type", "application/json");

    const newSubtaskSummary = platformName + ": " + currentIssueSummary;
    var body = JSON.stringify({"fields":{"project":{"key": jiraProjectKey},"parent":{"key": currentIssueKey},"summary": newSubtaskSummary,"issuetype":{"id": jiraSubtaskIssueTypeId},"components" : [{"name" : componentName}]}});

    var requestOptions = {
        method: 'POST',
        headers: headers,
        body: body
    };

    fetch(createIssueUrl, requestOptions)
        .then(response => {
            console.log(response.text());
            if(!debug) window.location.assign(currentUrl);
        })
        .catch(error => console.log('error', error));

}