JiraRoboScript

Jira helper to preselect some fields when creating dialog is shown

2023/01/09のページです。最新版はこちら

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

You will need to install an extension such as Tampermonkey to install this script.

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         JiraRoboScript
// @namespace    http://tampermonkey.net/
// @version      4.2
// @description  Jira helper to preselect some fields when creating dialog is shown
// @author       Robo
// @homepage     https://greasyfork.org/sk/scripts/400374-jiraroboscript
// @require      https://code.jquery.com/jquery-3.4.1.min.js
// @match        https://pmjira.cz.tmo/*
// @grant        GM_addValueChangeListener
// @grant        GM_setValue
// @grant        GM_addStyle
// ==/UserScript==

(function () {
	'use strict';
	//due to conflicts with page scripts
	let $myJQuery = jQuery.noConflict(true);

	// Starts listening for changes in the target HTML element of the page.
	let target = document.getElementById('jira');

    if(target) {
        let bodyMutationObserver = new MutationObserver(function (mutations) {
            for (let i = 0; i < mutations.length; i++) { //faster than forEach
                let mutation = mutations[i];
                //console.log(mutation);
                if (mutation.addedNodes.length > 0
                    && (mutation.addedNodes[0].id === "create-issue-dialog" || mutation.addedNodes[0].id === "create-subtask-dialog")) {
                    //add inner dialog mutationObserver
                    initializeDialogMutationObserver(mutation.addedNodes[0]);
                }
            }
        });

        bodyMutationObserver.observe(target, {
            childList: true
        });
    }


    ////////////////

	function initializeDialogMutationObserver(dialog) {
		let dialogMutationObserver = new MutationObserver(function (mutations) {
			for (let i = 0; i < mutations.length; i++) { //faster than forEach
				let mutation = mutations[i];
				console.log(mutation);
				if (mutation.target.className.includes("jira-dialog-content-ready") || (mutation.addedNodes.length > 0 && mutation.addedNodes[0].className.includes("aui-dialog2-footer"))) {
					//dialog is ready
					//don't disconnect observer, because changing project type cause reload of inner content and dialog panel class change is triggered
					//makeMiracle(mutation.target);
					createMiracleButtons(mutation.target);
				}
			}
		});

		dialogMutationObserver.observe(dialog, {
			childList: true,
            attributes: true,
			attributeFilter: ["class"]
		});
	}

    function createMiracleButtons(dialog) {
		let dialogHeader = $myJQuery(dialog).find(".jira-dialog-core-heading h2");
		if (dialogHeader.find(".miracleButton").length === 0) {
			let oneAppMiracleButton = $myJQuery("<a class='aui-button aui-button-primary aui-style miracleButton' title='Prefil form' href='#' style='margin-left:20px;'>OneApp miracle</a>")
            .on("click", {team:"oneApp"}, miracleButtonHandler);
            let oneShopMiracleButton = $myJQuery("<a class='aui-button aui-button-primary aui-style miracleButton' title='Prefil form' href='#' style='margin-left:20px;'>OneShop miracle</a>")
            .on("click", {team:"oneShop"}, miracleButtonHandler);
            let debtMiracleButton = $myJQuery("<a class='aui-button aui-button-primary aui-style miracleButton' title='Prefil form' href='#' style='margin-left:20px;'>Tech debt labels</a>")
            .on("click", {labels:["S2S"]}, debtButtonHandler);
			dialogHeader.append(oneAppMiracleButton);
			dialogHeader.append(oneShopMiracleButton);
            dialogHeader.append(debtMiracleButton);

            $myJQuery(dialog).find("#create-issue-submit").on("click", verifyInputs);
		}
	}

	function miracleButtonHandler(event) {
        let dialog = $myJQuery(event.target).parents(".jira-dialog-core");
        let dialogContent = (dialog).find(".jira-dialog-core-content");

        makeMiracle(dialogContent, event.data.team);
	}

    function makeMiracle(dialogContent, team) {
		let prefilledFields = getPrefilledFieldsByIssueType(dialogContent, team);

		prefilledFields.forEach(function(prefilledField) {
            prefilledField.check(dialogContent);
			prefilledField.apply(dialogContent);

            //epic hack, fill hidden select with selected option
            if (prefilledField.readableName == "Epic") {
                let epicHiddenSelect = dialogContent.find("#customfield_10000");
                epicHiddenSelect.find("option").remove();
                if (prefilledField.value == "OA - Tech. Debts") {
                    epicHiddenSelect.append("<option value='key:DGSTR-434' title='undefined' selected='selected'>OA - Tech. Debts</option>");
                } else {
                    epicHiddenSelect.append("<option value='key:DGSTR-433' title='undefined' selected='selected'>OS - Tech. Debts</option>");
                }

                dialogContent.find("#customfield_10000-field, #customfield_10000-single-select .drop-menu").one("click", function() {
                    dialogContent.find("#customfield_10000-field").val("");
                    dialogContent.find("#customfield_10000 option").remove();
                });
            }
		});
	}

    function debtButtonHandler(event) {
        let dialog = $myJQuery(event.target).parents(".jira-dialog-core");
        let dialogContent = (dialog).find(".jira-dialog-core-content");

        addLabels(dialogContent, event.data.labels);
	}

    function addLabels(dialogContent, labels) {
        let labelsTextArea = dialogContent.find("#labels-textarea");
        labels.forEach(function(label){
            //set value, simulate click and then lost focus
            labelsTextArea.val(label).click().blur();
        });
    }

    function verifyInputs(event) {
        let dialog = $myJQuery(event.target).parents(".jira-dialog-core");
        let dialogContent = (dialog).find(".jira-dialog-core-content");

        let prefilledFields = getPrefilledFieldsByIssueType(dialogContent);

        let project = dialogContent.find("#project").val();
        let issueType = dialogContent.find("#issuetype-field").val();
        if (project == "11701") { //agile project
            if(!confirm("You should use Digital SK Tribe project. Continue?")) {
               return false;
            }

            if (issueType == "Sub-task") {
                if(!confirm("I am not sure that you realy need 'Sub-Task' type, maybe 'Technical sub-task' will be better. Continue?")) {
                    return false;
                }
            }
        }

        let fieldsFilled = true;
        prefilledFields.forEach(function(prefilledField) {
            if(!prefilledField.check(dialogContent)) {
                fieldsFilled = false;
                alert("Please fill value in " + prefilledField.readableName);
            }
		});

        return fieldsFilled;
    }

    function getPrefilledFieldsByIssueType(dialogContent, team) {
        let newTaskAgileTeamValue;
        let epic;

        if (team == "oneApp") {
            newTaskAgileTeamValue = "SQUAD: OneApp 1 (DGSTR)";
            epic = "OA - Tech. Debts";
        } else {
            newTaskAgileTeamValue = "SQUAD: OneShop 1 (DGSTR)";
            epic = "OS - Tech. Debts";
        }

        let descriptionTextArea = new ElementConfig("#description", null, "Description"); //empty
        let componentsTextArea = new ElementConfig("#components-textarea", null, "Components"); //empty

        let agileRadio = new ElementConfig("#customfield_10267-1", true, "Agile");
		let epicInput = new ElementConfig("#customfield_10000-field", epic, "Epic");
        let agileTeamSelect = new ElementConfig("#customfield_14700-ms-field", newTaskAgileTeamValue, "Agile Team");

		let issueTypeToFieldMap = {
			//Technical US
            "10111": [descriptionTextArea, agileRadio, epicInput, agileTeamSelect],
            //Task
            "10112": [descriptionTextArea, agileRadio, epicInput, agileTeamSelect],
			//Sub-task
            "10113": [descriptionTextArea, agileRadio, agileTeamSelect],
            //Technical sub-task
            "10228": [descriptionTextArea],
			//Story
            "10001": [descriptionTextArea, agileRadio, epicInput, agileTeamSelect],
			//Bug
            "10110": [descriptionTextArea, agileRadio, epicInput, agileTeamSelect]
		};

		let issueType = dialogContent.find("#issuetype").val();
		return issueTypeToFieldMap[issueType];
    }

	function ElementConfig(selector, value, readableName) {
		this.selector = selector;
		this.value = value;
		this.readableName = readableName;
		this.apply = function (parentElement) {
			if (this.value == null) {
                return;
            }
            let element = parentElement.find(this.selector);
			if (element.is(":checkbox, :radio")) {
				element.prop('checked', this.value);
			} else if (element.is("input, textarea")) {
				element.val(this.value);
			} else if (element.is("select")) {
				element.val(this.value);
				//scroll to selected
				setTimeout(function () {
					let optionTop = element.find("option:selected").offset().top;
					let selectTop = element.offset().top;
					element.scrollTop(element.scrollTop() + (optionTop - selectTop));
				}, 100);
			}
		};
        this.check = function (parentElement) {
			let element = parentElement.find(this.selector);
			if (element.parents(".qf-form.qf-configurable-form").length === 1 && element.parents(".qf-field-active").length === 0) {
				alert("Missing required field '" + this.readableName + "'");
                return false;
			}
			if (element.is(":checkbox, :radio")) {
				return parentElement.find("[name='"+element.attr("name")+"']:checked").length != 0;
			} else if (element.is("input, textarea")) {
				return element.val().length != 0;
			} else if (element.is("select")) {
				return element.val().length != 0;
			} else {
                return true;
            }
		}
	}
})();