Skip to content

Instantly share code, notes, and snippets.

@GDreyV
Last active October 6, 2021 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GDreyV/d7d8b8979ba6e538bfa5c005fca94823 to your computer and use it in GitHub Desktop.
Save GDreyV/d7d8b8979ba6e538bfa5c005fca94823 to your computer and use it in GitHub Desktop.
Order rows in Jira by priority and status
// prevent double init
if (!window._customScriptLoaded) {
window._customScriptLoaded = true;
const updateTable = () => {
loadPriorities()
.then(() => {
console.log("Custom script::priorities loded");
return reorderIssues();
});
}
const reorderIssues = () => {
// copy from https://www.w3schools.com/howto/howto_js_sort_table.asp
table = document.getElementById("ghx-issues-in-epic-table");
switching = true;
while (switching) {
switching = false;
rows = table.rows;
for (i = 0; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByClassName("priority")[0];
y = rows[i + 1].getElementsByClassName("priority")[0];
const xValue = x.dataset.grade;
const yValue = y.dataset.grade;
// should we order by created date or name?
if (xValue > yValue) {
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
}
const getSortingGrade = (data) => {
/*
Blocker: 1, Critical:2, Major: 3, Cosmetic: 5
----
Closed:Done:3, Verified:Done:3,
Reopened:new:2,
Testing:indeterminate:4
*/
const priority = data.fields.priority;
const priorityGrade = parseInt(priority.id);
const status = data.fields.status;
const statusGrade = parseInt(status.statusCategory.id);
const value = priorityGrade + 10*statusGrade;
// console.log(priority.name, priorityGrade, status.name, statusGrade, value);
return value;
}
const loadPriorities = () => {
const wrmContextPath = require('wrm/context-path');
return new Promise((resolve, reject) => {
const target = AJS.$('#ghx-issues-in-epic-table tr');
const timeout = setTimeout(() => reject, 60*1000);
const totalPriorities = target.length;
let loadedPriorities = 0;
target.each(function() {
var row = this;
var issueKey = AJS.$(this).attr("data-issuekey");
AJS.$.getJSON(wrmContextPath() + '/rest/api/latest/issue/' + issueKey, function(data){
var value = data.fields.priority.iconUrl;
const name = data.fields.priority.name;
const priority = data.fields.priority;
const status = data.fields.status;
const grade = getSortingGrade(data);
var actions = AJS.$(row).find('td.issuetype');
AJS.$(actions).before('<td class="nav priority"'
+ ' data-priority="' + priority.name + '"'
+ ' data-status="' + status.name + '"'
+ ' data-grade="' + grade + '"><img src="' + priority.iconUrl + '" title="' + priority.name + '"/></td>');
if (++loadedPriorities >= totalPriorities) {
clearTimeout(timeout);
resolve();
}
});
});
});
}
window.addEventListener("load", () => updateTable(), false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment