Last active
July 6, 2023 21:59
-
-
Save JonathanGawrych/dddd4c21d9380c09b82895444a27f646 to your computer and use it in GitHub Desktop.
Creates a floating badge of the sum on the tickets when you highlight multiple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Jira Sum Highlighted | |
// @author Jonathan Gawrych | |
// @match https://goreact.atlassian.net/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=atlassian.net | |
// @grant none | |
// @run-at document-idle | |
// ==/UserScript== | |
(function doit() { | |
'use strict'; | |
if (typeof GH === 'undefined') { | |
// We aren't initalized yet. Wait for a bit | |
setTimeout(doit, 1000); | |
return; | |
} | |
// Don't run on apages without the selection managaer | |
if (GH.IssueSelectionManager.prototype.handleIssueClick == null) { | |
return; | |
} | |
const hoverSelectionBadgeElement = document.createElement('SPAN'); | |
hoverSelectionBadgeElement.className = 'aui-badge'; | |
hoverSelectionBadgeElement.style.position = 'absolute'; | |
hoverSelectionBadgeElement.style.top = 0; | |
hoverSelectionBadgeElement.style.right = 0; | |
hoverSelectionBadgeElement.style.transform = 'translate(50%, -50%)'; | |
// Hook when you click on a jira ticket | |
GH.IssueSelectionManager.prototype.handleIssueClick = ((oldFn) => { | |
return function monkeyPatch(...args) { | |
const result = oldFn.apply(this, args); | |
// Get the selected issues | |
const selected = this.getSelectedIssuesInOrder(); | |
// If one or zero issues are selected, remove our summary badge | |
if (selected.length <= 1) { | |
hoverSelectionBadgeElement.remove(); | |
return result; | |
} | |
// Otherwise, we need to calculate the sum. Get all issues. | |
const issuesByKey = Object.assign.apply(Object, [{}].concat( | |
GH.BacklogModel.getAllIssueListsNew().map( | |
(list) => list.data.issueByKey | |
))); | |
// Map them to mapped to the estimations and add it together | |
const estimations = selected.map((key) => issuesByKey[key].estimateStatistic.statFieldValue.value ?? 0); | |
const sumEstimations = Math.round(estimations.reduce((a, b) => a + b) * 10) / 10; | |
hoverSelectionBadgeElement.innerText = sumEstimations; | |
const firstSelectedElement = document.querySelector(`[data-issue-key="${selected[0]}"]`); | |
firstSelectedElement.appendChild(hoverSelectionBadgeElement); | |
return result; | |
}; | |
})(GH.IssueSelectionManager.prototype.handleIssueClick); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment