Skip to content

Instantly share code, notes, and snippets.

@codycraven
Last active August 20, 2021 18:06
Show Gist options
  • Save codycraven/ebbb81dc387a606611832927f150fa80 to your computer and use it in GitHub Desktop.
Save codycraven/ebbb81dc387a606611832927f150fa80 to your computer and use it in GitHub Desktop.
TamperMonkey Jira Portfolio Cloud Child Issue Augmentation
// ==UserScript==
// @name Jira child issue status
// @namespace https://cravencode.com/
// @version 0.1
// @description Adds child status to JIRA parent issues
// @match https://*.atlassian.net/browse/*
// @copyright 2018, Cody Craven
// ==/UserScript==
(function() {
'use strict';
function getIssueKey() {
const m = document.querySelector("meta[name='ajs-issuekey']");
if (!m) {
throw "Page is not an issue";
}
return m.getAttribute("content");
}
function getBaseURL() {
const m = document.querySelector("meta[name='ajs-jira-base-url']");
if (!m) {
throw "Can not find base URL";
}
return m.getAttribute("content");
}
function getChildTable() {
const t = document.querySelector(".jpo-child-issue-table");
if (!t) {
throw "Issue has no children";
}
return t;
}
function childrenIssues() {
return fetch(`${getBaseURL()}/rest/api/2/search?jql="Parent Link"=${getIssueKey()}`)
.then(response => response.json())
.catch(error => { throw "API query for children failed" });
}
function addStatusToRow(statusEl, rowEl) {
const td = document.createElement("td");
td.appendChild(statusEl);
rowEl.appendChild(td);
}
function addAssigneeToRow(assignee, rowEl) {
const td = document.createElement("td");
if (assignee) {
td.textContent = assignee.displayName;
}
else {
const em = document.createElement("em");
em.textContent = "Unassigned";
td.appendChild(em);
}
rowEl.appendChild(td);
}
function createStatusEl(status) {
const s = document.createElement("span");
switch(status) {
case "Open":
s.className = "jira-issue-status-lozenge aui-lozenge jira-issue-status-lozenge-blue-gray jira-issue-status-lozenge-new aui-lozenge-subtle jira-issue-status-lozenge-max-width-medium";
break;
case "Done":
s.className = "jira-issue-status-lozenge aui-lozenge jira-issue-status-lozenge-green jira-issue-status-lozenge-done aui-lozenge-subtle jira-issue-status-lozenge-max-width-medium";
break;
default:
s.className = "jira-issue-status-lozenge aui-lozenge jira-issue-status-lozenge-yellow jira-issue-status-lozenge-indeterminate aui-lozenge-subtle jira-issue-status-lozenge-max-width-medium";
}
s.textContent = status;
return s;
}
function findIssueEl(issueKey, issueLinks) {
for (let i = 0; i < issueLinks.length; i++) {
if (issueLinks[i].textContent == issueKey) {
return issueLinks[i];
}
}
}
try {
const childTable = getChildTable();
const children = childrenIssues().then(response => {
const issueLinks = childTable.getElementsByTagName("a");
response.issues.forEach(issue => {
const linkEl = findIssueEl(issue.key, issueLinks);
if (linkEl) {
const rowEl = linkEl.parentNode.parentNode;
addStatusToRow(createStatusEl(issue.fields.status.name), rowEl);
addAssigneeToRow(issue.fields.assignee, rowEl);
}
});
});
} catch (e) {
console.error("Jira child issue status script failed:", e);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment