JiraRoboScript

Jira helper to preselect some fields when creating dialog is shown

Stan na 26-04-2020. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         JiraRoboScript
// @namespace    http://tampermonkey.net/
// @version      2.1
// @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/browse/*
// @grant        none
// ==/UserScript==

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

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

	// Starts listening for changes in the target HTML element of the page.
	let target = document.getElementById('jira');
	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")) {
					//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);
					//createMiracleButton(mutation.target);
				}
			}
		});

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

	function makeMiracle(dialog) {
		//load team value from parent task (rendered page), if exists
		let parentTaskTeamValue = $myJQuery("#issue-content").find("#customfield_10685-field").text().trim();
		let newTaskTeamValue = "16135";
		if (parentTaskTeamValue === "ST_SK - One Shop") {
			newTaskTeamValue = "16400";
		}

		console.log(parentTaskTeamValue);
		let agileRadio = new ElementConfig("#customfield_10267-1", true, "Agile");
		let epicInput = new ElementConfig("#customfield_10000-field", "OASK - Generic Epic", "Epic");
		let teamSelect = new ElementConfig("#customfield_10685", newTaskTeamValue, "Team"); //ST_SK - One App
		let privacyRelevanceSelect = new ElementConfig("#customfield_12605", "14042", "Privacy Relevance"); //NO

		let issueTypeToFieldMap = {
			"Technical US": [agileRadio, epicInput, teamSelect],
			"Task": [agileRadio, epicInput, teamSelect],
			"Sub-task": [agileRadio, teamSelect],
			"Story": [agileRadio, epicInput, teamSelect, privacyRelevanceSelect],
			"Bug": [agileRadio, epicInput, teamSelect]
		};

		let dialogContent = $myJQuery(dialog).find(".jira-dialog-content");
		let issueType = dialogContent.find("#issuetype-field").val();
		let prefilledFields = issueTypeToFieldMap[issueType];

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

	function ElementConfig(selector, value, readableName) {
		this.selector = selector;
		this.value = value;
		this.readableName = readableName;
		this.apply = 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 + "'");
			}
			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);
			}
		}
	}
})();