whatnowmax / jira_side_template_populate

// ==UserScript==
// @name jira_side_template_populate
// @description populate and templates for jira
// @version 1.6
// @author whatnowmax
// @license MIT
// @match https://tidalautomation.atlassian.net/secure/BrowseProjects.*

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


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

    //when createGlobalItem button is clicked
    document.getElementById("createGlobalItemIconButton").addEventListener("click", makeDropdownAndButton);
    //when the little icon version is clicked
    document.getElementById("createGlobalItem").addEventListener("click", makeDropdownAndButton);


    //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() {
        var _dialog = document.getElementById("create-issue-dialog");
        if (_dialog) {
            console.log("Exists!");
            clearInterval(checkExist);
            //make dropdown
            document.getElementById("create-issue-dialog").appendChild(newList);
            //make populate button
            document.getElementById("create-issue-dialog").appendChild(populateButton);
        }
    }, 100);

function makeDropdownAndButton() {
    console.log("button clicked");
    var checkExist = setInterval(function() {
        var _dialog = document.getElementById("create-issue-dialog");
        if (_dialog) {
            console.log("Exists!");
            clearInterval(checkExist);
            //make dropdown
            document.getElementById("create-issue-dialog").appendChild(newList);
            //make populate button
            document.getElementById("create-issue-dialog").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. Did you use the (make jira) button in the CRM case to get here?";
    }
    var _summary = document.getElementById("summary");
    var _pri = document.getElementById("priority-field");
    var _custVisible = document.getElementById("customfield_10145");
    var _team = document.getElementById("customfield_10173");
    var _label = document.getElementById("labels-textarea");
    var _hotfixYESfield = document.getElementById("customfield_10203-2");
    var _component = document.getElementById("components-textarea");

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

    //always the same
    var _assignee = document.getElementById("assignee-field");
    var _buildVer = document.getElementById("customfield_10200");
    var _caseNum = document.getElementById("customfield_10190");
    var _custName = document.getElementById("customfield_10175");
    var _description = document.getElementById("description");
    var _foundby = document.getElementById("customfield_10161");
    switch(newList.value) {
        case "Troubleshooting":
            console.log("troubleshooting was selected")
            //commented because already required
            //_summary.value = "T - <title of issue>";
            _pri.value = "High";
            _custVisible.value = "10012"; //10012 = not visible
            _assignee.value = "Dhiraj Srivastava";
            //_buildVer.value = "<build version>";
            _caseNum.value = "<case number>";
            _custName.value = "<customer name exactly as shown in CRM>";
            _team.value = "10045"; //10045 = TSE
            _description.value = "<required>";
            _label.value = "TSE";
            //_foundby.value = "10038"; field not present in troubleshooting escalation
            break;

        case "Hotsite Bug":
            console.log("Hotsite Bug was selected")
            _custVisible.value = "10013"; //10013 = yes visible
            _pri.value = "High";
            _assignee.value = "Dhiraj Srivastava";
            //_buildVer.value = "<build version>";
            _caseNum.value = "<case number>";
            _custName.value = "<customer name exactly as shown in CRM>";
            _team.value = "10045"; //10045 = TSE
            _description.value = "<required>";
            _label.value = "TSE";
            _hotfixYESfield.checked = "checked";
            _symptom.value = "<(Externally facing)Error message(s) or description of issue when first experienced by end users or administrators.>";
            _conditions.value = "<(Externally facing)versions and steps used to recreate issue>";
            _workaround.value = "<(Externally facing)exact steps taken to mitigate the issue fully, or partially.>";
            _foundby.value = "10038";
            break;
        case "Bug":
            console.log("Regular Bug was selected")
            _custVisible.value = "10013"; //10013 = yes visible
            //_buildVer.value = "<build version>";
            _caseNum.value = "<case number>";
            _custName.value = "<customer name exactly as shown in CRM>";
            _description.value = "<required>";
            _symptom.value = "<(Externally facing)Error message(s) or description of issue when first experienced by end users or administrators.>";
            _conditions.value = "<(Externally facing)versions and steps used to recreate issue>";
            _workaround.value = "<(Externally facing)exact steps taken to mitigate the issue fully, or partially.>";
            _foundby.value = "10038";
            break;
        case "Enhancement":
            console.log("Enhancement was selected")
            _custVisible.value = "10013"; //10013 = yes visible
            //_buildVer.value = "<build version>";
            _caseNum.value = "<case number>";
            _custName.value = "<customer name exactly as shown in CRM>";
            _description.value = "<required>";
            _symptom.value = "<copy of description with nothing INTERAL>";
            _foundby.value = "10038";
            break;
        case "Doc Bug":
             console.log("Doc bug was selected")
            _custVisible.value = "10013"; //10013 = yes visible
            //_buildVer.value = "<build version>";
            _caseNum.value = "<case number>";
            _custName.value = "<customer name exactly as shown in CRM>";
            _description.value = "<name of document, page number, heading, as well as changes to be made with explanation>";
            _symptom.value = "<copy of description with nothing INTERAL>";
            _component.value = "Documentation";
            _assignee.value = "Robin Thomas";
            _pri.value = "High";
            _label.value = "Documentation";
            _foundby.value = "10038";
            break;
        case "Tidal Repository Bug":
            _assignee.value = "Maryna Lipnitskaya";
            _custVisible.value = "10013"; //10013 = yes visible
            _description.value = "<required>";
            //_symptom.value = "<copy of description with nothing INTERAL>";
            _foundby.value = "10038";
            break;
        default:
            console.log("Something else was chosen, which is odd.");
    }
}

function populate_from_case() {
    // [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

}