whatnowmax / solo_jira_side_template_populate

// ==UserScript==
// @name solo_jira_side_template_populate
// @description populate and templates for jira
// @version 1.2
// @author whatnowmax
// @license MIT
// @match https://tidalautomation.atlassian.net/jira/your-work

// @run-at document-end
// @grant none
// ==/UserScript==

var checkExist_button = setInterval(function() {
   if (document.getElementById("createGlobalItem").id === "createGlobalItem") {
      console.log("Exists! 101");
      clearInterval(checkExist_button);
   } else {
       console.log("Doesn't exist!101");
   }
}, 100); // check every 100ms

    //get information from CRM case in the URL
    var _crmData = decodeURIComponent(window.location.search);
    var _crmDataArray = _crmData.split(";");
    //get rid of ? in first entry:
    _crmDataArray[0] = _crmDataArray[0].split("?")[1];


    //make button
    //var populateButton = document.createElement("BUTTON");
    //populateButton.disabled = true;
    //set button title
    var buttonTitle = document.createTextNode("Select Template First");
    if (window.location.search) {
        buttonTitle.textContent = "Data Ready - Verify Project and Issue Type are accurate, then select Template";
    } else {
        buttonTitle.textContent = "No data to load";
    }
   //populateButton.appendChild(buttonTitle);



    var newList = document.createElement("select");

    newList.appendChild(new Option("Bug Templates", "Bug Templates"));
    newList.appendChild(new Option("Troubleshooting", "Troubleshooting"));
    newList.appendChild(new Option("Hotsite Bug", "Hotsite Bug"));
    newList.appendChild(new Option("Bug", "Bug"));
    newList.appendChild(new Option("Enhancement", "Enhancement"));
    newList.appendChild(new Option("Doc Bug", "Doc Bug"));
    newList.appendChild(new Option("Tidal Repository Bug", "Tidal Repository Bug"));

    //add listener for changing the selection
    newList.addEventListener("change",template_chosen);

    //add listener for pressing populate
    //populateButton.addEventListener("click",populate_from_case);

    //create a try/catch for this to fix this jankiness
    var loop_the_button = setInterval(function() {
        console.log("first loop");
        var _dialog = document.getElementById("createGlobalItemIconButton");
        if (_dialog) {
            console.log("First Exists!");
            clearInterval(loop_the_button);
            //when createGlobalItem button is clicked
            document.getElementById("createGlobalItemIconButton").addEventListener("click", makeDropdownAndButton);
            //when the little icon version is clicked
            document.getElementById("createGlobalItem").addEventListener("click", makeDropdownAndButton);
        }
    }, 100);

    


    //this is a very wasteful loop that I'm using right now to get around the above two lines not working.
    var checkExist = setInterval(function() {
        console.log("second loop");
        var _dialog = document.getElementById("project-uid1-label");
        if (_dialog) {
            console.log("Second Exists!");
            clearInterval(checkExist);
            //make dropdown
            document.getElementById("project-uid1-label").appendChild(newList);
            //make populate button
            //document.getElementById("project-uid1-label").appendChild(populateButton);
        }
    }, 100);

function makeDropdownAndButton() {
    console.log("button clicked");
    var checkExist = setInterval(function() {
        var _dialog = document.getElementById("project-uid1-label");
        if (_dialog) {
            console.log("Exists!");
            clearInterval(checkExist);
            //make dropdown
            document.getElementById("project-uid1-label").appendChild(newList);
            //make populate button
            //document.getElementById("project-uid1-label").appendChild(populateButton);
        }
    }, 100);

    //add dropdown to dialog popup
}

function template_chosen() {
    //enable populate button if there is data to populate
    if (window.location.search) {
        buttonTitle.textContent = "Populate";
        //populateButton.disabled = false;
    } else {
        buttonTitle.textContent = "No data to load. This version of the script cannot scrub information from SF.";
    }
    var _summary = document.getElementById("summary-field-label");//
    var _pri = document.getElementById("priority-field-label");
    var _custVisible = document.getElementById("customfield_10145-field-label");
    var _team = document.getElementById("customfield_10173-field-label");
    var _label = document.getElementById("labels-field-label");
    var _hotfixYESfield = document.getElementById("customfield_10203-field-label");
    var _component = document.getElementById("components-field-label");
    var _reproduce_steps = document.getElementById("customfield_10178-field-label");
    var _regression = document.getElementById("customfield_10162-field-label");

    //Release Notes
    var _symptom = document.getElementById("customfield_10141-field-label");
    var _conditions = document.getElementById("customfield_10142-field-label");
    var _workaround = document.getElementById("customfield_10143-field-label");

    //always the same
    var _assignee = document.getElementById("assignee-field-label");
    var _buildVer = document.getElementById("customfield_10200-field-label");//
    var _caseNum = document.getElementById("customfield_10190-field-label");
    _caseNum.innerText = "Case(s) [REQUIRED]";
    var _custName = document.getElementById("customfield_10175-field-label");//
    var _description = document.getElementById("description-field-label");//
    var _tp_prob_desc = document.getElementById("customfield_10187-field-label");
    _description.innerText = "Description [REQUIRED]:";
    var _foundby = document.getElementById("customfield_10161-field-label");
    var _platform = document.getElementById("customfield_10139-field-label");
    var _urgency = document.getElementById("customfield_10181-field-label");
    var _doc_update = document.getElementById("customfield_10267-field-label");
    //old populate button
    var _ver = document.getElementById("versions-field-label");
    var _reproduced = document.getElementById("customfield_10198-field-label");
    _reproduced.innerText = "TSC Reproduced [REQUIRED]";

    var _tr_build = document.getElementById("customfield_10207-field-label");

    switch(newList.value) {
        case "Troubleshooting":
            console.log("troubleshooting was selected")
            //commented because already required
            _pri.innerText = "Priority [choose HIGH]";
            _ver.innerText = "Affects versions [REQUIRED]";
            _custVisible.innerText = "Customer Visible [choose NO]";
            _assignee.innerText = "Assignee [Choose Dhiraj S]";
            //_buildVer.value = "<build version>";
            _custName.innerText = "Customer Reported [REQUIRED]";
            _team.innerText = "Teams [choose NONE]";
            _summary.innerText = "Summary [REQUIRED]";
            _component.innerText = "Components [REQUIRED]";
            _label.innerText = "Label [Choose Escalation]";
            //from old populate button
            _buildVer.innerText = "TA Build Found In [REQUIRED]";
            _platform.innerText = "Platform [REQUIRED]";
            _urgency.innerText = "Urgency [REQUIRED]";
            _reproduced.innerText = "TSC Reproduced";
            break;

        case "Hotsite Bug":
            console.log("Hotsite Bug was selected")
            _hotfixYESfield.innerText = "Hotfix Needed [Choose YES only if permission was given]";
            _custVisible.innerText = "Customer Visible [choose YES]";
            _pri.innerText = "Priority [choose HIGH]";
            _assignee.innerText = "Choose [Dhiraj S]";
            _custName.innerText = "Customer Reported [REQUIRED]";
            _team.innerText = "Teams [Choose NONE]";
            _label.innerText = "Labels [Choose Escalation]";
            _hotfixYESfield.innerText = "Hotfix Needed [choose YES]";
            _symptom.innerText = "Symptom: <(Externally facing)Error message(s) or description of issue when first experienced by end users or administrators.>";
            _conditions.innerText = "Conditions: <(Externally facing)versions and steps used to recreate issue>";
            _workaround.innerText = "Workaround: <(Externally facing)exact steps taken to mitigate the issue fully, or partially.>";
            _foundby.innerText = "Found By [REQUIRED]";
            _buildVer.innerText = "TA Build Found In [REQUIRED]";
            _custName.innerText = "Customer Reported [REQUIRED]";
            _ver.innerText = "Version [REQUIRED]";
            _regression.innerText = "Regression [REQUIRED]";
            _platform.innerText = "Platform [REQUIRED]";
            _summary.innerText = "Summary [REQUIRED]";
            _reproduce_steps.innerText = "Steps to Reproduce [REQUIRED]";
            _component.innerText = "Components [REQUIRED]";
            _ver.innerText = "Affects versions [REQUIRED]";
            _urgency.innerText = "Urgency [REQUIRED]";
            break;
        case "Bug":
            console.log("Regular Bug was selected")
            _custVisible.innerText = "Customer Visible [choose YES]";
            //_buildVer.value = "<build version>";
            _custName.innerText = "Customer Reported [REQUIRED]";
            _symptom.innerText = "Symptom: <(Externally facing)Error message(s) or description of issue when first experienced by end users or administrators.>";
            _conditions.innerText = "Conditions: <(Externally facing)versions and steps used to recreate issue>";
            _workaround.innerText = "Workaround: <(Externally facing)exact steps taken to mitigate the issue fully, or partially.>";
            _foundby.innerText = "Found By [REQUIRED]";
            _buildVer.innerText = "TA Build Found In [REQUIRED]";
            _ver.innerText = "Version [REQUIRED]";
            _platform.innerText = "Platform [REQUIRED]";
            _summary.innerText = "Summary [REQUIRED]";
            _reproduce_steps.innerText = "Steps to Reproduce [REQUIRED]";
            _component.innerText = "Components [REQUIRED]";
            _ver.innerText = "Affects versions [REQUIRED]";
            _urgency.innerText = "Urgency [REQUIRED]";
            _component.innerText = "Components [REQUIRED]";
            _ver.innerText = "Affects versions [REQUIRED]";
            _urgency.innerText = "Urgency [REQUIRED]";
            _tp_prob_desc.innerText = "Problem Description [REQUIRED] <externally facing description>"
            break;
        case "Enhancement":
            console.log("Enhancement was selected");
            _ver.innerText = "Version [REQUIRED]";
            _component.innerText = "Components [REQUIRED]";
            _custVisible.innerText = "Customer Visible [choose YES]";
            _tp_prob_desc.innerText = "Problem Description [REQUIRED] <externally facing description>"
            _custName.innerText = "Customer Reported [REQUIRED]";
            _symptom.innerText = "Symptom: <(Externally facing)Error message(s) or description of issue when first experienced by end users or administrators.>";
            _conditions.innerText = "Conditions: <(Externally facing)versions and steps used to recreate issue>";
            _workaround.innerText = "Workaround: <(Externally facing)exact steps taken to mitigate the issue fully, or partially.>";
            _foundby.innerText = "Found By [REQUIRED]";
            _buildVer.innerText = "TA Build Found In [REQUIRED]";
            _ver.innerText = "Affects versions [REQUIRED]";
            break;
        case "Doc Bug":
             console.log("Doc bug was selected");
            _summary.innerText = "Summary [REQUIRED]";
            _tp_prob_desc.innerText = "Problem Description [REQUIRED] <externally facing description>"
            _doc_update.innerText = "Doc Updates [Choose YES]";
            _ver.innerText = "Version [REQUIRED]";
            _component.innerText = "Components [REQUIRED]";
            _custVisible.innerText = "Customer Visible [choose YES]";
            //_buildVer.value = "<build version>";
            _custName.innerText = "Customer Reported [REQUIRED]";
            _symptom.innerText = "Symptom: <(Externally facing)Error message(s) or description of issue when first experienced by end users or administrators.>";
            _conditions.innerText = "Conditions: <(Externally facing)versions and steps used to recreate issue>";
            _workaround.innerText = "Workaround: <(Externally facing)exact steps taken to mitigate the issue fully, or partially.>";
            _component.innerText = "Components [choose DOCUMENTATION]";
            _assignee.textContent = "Assignee [Choose Robin T]";
            _pri.innerText = "Priority [choose HIGH]";
            _label.innerText = "Labels [Choose Documentation]";
            _foundby.innerText = "Found By [REQUIRED]";
            _ver.innerText = "Affects versions [REQUIRED]";
            break;
        case "Tidal Repository Bug":
            console.log("TR was selected");
            _assignee.innerText = "Assignee [Choose Denis M]";
            _summary.innerText = "Summary [REQUIRED]";
            _custName.innerText = "Customer Reported [REQUIRED]";
            _ver.innerText = "Version [REQUIRED]";
            _custVisible.innerText = "Customer Visible [choose YES for Bug, NO for Troubleshooting]";
            _tr_build.innerText = "TR Build Found In [REQUIRED]";
            _tp_prob_desc.innerText = "Problem Description [REQUIRED] <externally facing description>"
            break;
        default:
            console.log("Something else was chosen, which is odd.");
    }
}

function populate_from_case() {

    //This function is defunct

    // [0] = case number (plus a ?)
    // [1] = Case title
    // [2] = Customer name
    // [3] = download link
    // [4] = build number
    // [5] = version
    // [6] = component (not including adapter)
    // [7] = adapter
    // [8] = platform
    // [9] = an equal sign for some reason

    console.log("Populate button clicked");
    console.log(_crmDataArray);

    var _summary = document.getElementById("summary");
    var _component = document.getElementById("components-textarea");

    //Release Notes won't be touched by populate button for now
    //var _symptom = document.getElementById("customfield_10141");
    //var _conditions = document.getElementById("customfield_10142");
    //var _workaround = document.getElementById("customfield_10143");

    var _buildVer = document.getElementById("customfield_10200");
    var _caseNum = document.getElementById("customfield_10190");
    var _custName = document.getElementById("customfield_10175");

    //haven't decided whether to populate description...
    //var _description = document.getElementById("description");

    //populate only
    var _downloadLink = document.getElementById("customfield_10189");
    var _ver = document.getElementById("versions-textarea");

    _caseNum.value = _crmDataArray[0];
    _summary.value = _crmDataArray[1];
    _custName.value = _crmDataArray[2];
    _downloadLink.value = _crmDataArray[3];
    _buildVer.value = _crmDataArray[4];
    _ver.value = _crmDataArray[5];
    _component.value = _component.value + " " + _crmDataArray[6] + " " + _crmDataArray[7];//check this
    //platform doesn't seem to be a blank in JIRA

}