floodmeadows / Jira create Welsh translation subtask

// ==UserScript==
// @name         Jira create Welsh translation subtask
// @namespace    https://openuserjs.org/users/floodmeadows
// @description  Adds button to create a subtask under the current ticket to prepare Welsh translations.
// @copyright    2023, floodmeadows (https://openuserjs.org/users/floodmeadows)
// @license      MIT
// @version      0.4
// @include      https://jira.*.uk/browse/*
// @updateURL    https://openuserjs.org/meta/floodmeadows/Jira_create_Welsh_translation_subtask.meta.js
// @downloadURL  https://openuserjs.org/install/floodmeadows/Jira_create_Welsh_translation_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 Welsh Translation 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 = "Welsh translation for " + currentIssueSummary;
    const description = `*Checklist*
# Relevant tab in Welsh spreadsheet:
# Excel file sent to Welsh Language Unit?
# With screenshots for context?
# Screenshots location (e.g. attached to this ticket / Google Drive folder etc.):
# Response expected from WLU by end of (date):
# CYF translation reference (received from WLU):
# Response received from WLU?
# Translations spreadsheet updated?
`;

    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)
        });

}