floodmeadows / Jira create Welsh screenshots subtask

// ==UserScript==
// @name         Jira create Welsh screenshots subtask
// @namespace    https://openuserjs.org/users/floodmeadows
// @description  Adds button to create a subtask under the current ticket to create English and Welsh screenshots.
// @copyright    2023, floodmeadows (https://openuserjs.org/users/floodmeadows)
// @license      MIT
// @version      0.1
// @include      https://jira.*.uk/browse/*
// @updateURL    https://openuserjs.org/meta/floodmeadows/Jira_create_Welsh_screenshots_subtask.meta.js
// @downloadURL  https://openuserjs.org/install/floodmeadows/Jira_create_Welsh_screenshots_subtask.user.js
// @grant        none
// ==/UserScript==

/* jshint esversion: 6 */

//--- Customise this to your Jira project -----//
const jiraSubtaskIssueTypeId = 5;
const debug = false;
//---------------------------------------------//

(function () {
    'use strict';

    addButton("Add 'Create Welsh screenshots' Subtask");
})();

function addButton(buttonText) {
    const newElement = document.createElement("a");
    newElement.setAttribute("href", "#");
    newElement.setAttribute("class", "aui-button toolbar-trigger issueaction-workflow-transition");
    newElement.addEventListener("click", function () { createSubtask(); });
    const textNode = document.createTextNode(buttonText);
    newElement.appendChild(textNode);
    const target = document.getElementById('opsbar-opsbar-admin');
    target.appendChild(newElement);
}

function createSubtask() {
    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';

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

    // 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];

    const newSubtaskSummary = "Generate Welsh screenshot(s) for '" + currentIssueSummary + "'";
    const description = `See "Welsh QA Screenshots" section of ${currentIssueKey} for details.`;

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

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

    fetch(createIssueUrl, requestOptions)
        .then(response => {
            console.log(response.text());
            // refresh the page to get thenew subtask to show in the Sub-tasks section
            if(!debug) window.location.assign(currentUrl);
        })
        .catch(error => {
            console.log('error', error)
        });

}