Created
October 7, 2019 20:36
-
-
Save codycraven/b29ac43fbb6aaba781fbd01e835c5324 to your computer and use it in GitHub Desktop.
Bookmarklet to grab summary information for a Jira ticket
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
// This is a Jira issue status grabber to grab link, summary, and custom fields | |
// for use as a bookmarklet | |
// | |
// Right now, these custom fields only support user references. | |
// | |
// To use this bookmarklet, modify the `customFields` setting for your Jira | |
// install. Then create a new bookmark in your browser, enter `javascript:` | |
// then paste this script after javascript: (line breaks will be removed). | |
// | |
// Now when you are on a Jira issue page, click the bookmarklet you created | |
// and you should receive a success message and have a message formatted like | |
// the following: | |
// | |
// My issue summary | |
// https://myjirainstance.atlassian.net/browse/issuekey-## | |
// Custom field label 1: User 1 | |
// Custom ield label 2: User 2 | |
// Custom ield label 3: User 3 | |
// Custom ield label 4: User 4 | |
// etc | |
(() => { | |
const customFields = [ | |
"customfield_10559", | |
"customfield_10519", | |
"customfield_10555", | |
"customfield_10539", | |
"customfield_10540", | |
"customfield_10549", | |
"customfield_10541", | |
"customfield_10542", | |
"customfield_10544", | |
"customfield_10566", | |
"customfield_10547" | |
]; | |
function copyToClipboard(text) { | |
const tmp = document.createElement("textarea"); | |
tmp.value = text; | |
tmp.style.height = "0"; | |
tmp.style.overflow = "hidden"; | |
tmp.style.position = "fixed"; | |
document.body.appendChild(tmp); | |
tmp.focus(); | |
tmp.select(); | |
document.execCommand("copy"); | |
document.body.removeChild(tmp); | |
} | |
function createMessageContainer() { | |
const d = document.createElement("div"); | |
d.style.padding = "16px"; | |
d.style.borderWidth = "1px"; | |
d.style.borderStyle = "solid"; | |
d.style.left = "50vw"; | |
d.style.transform = "translate(-50%, 0)"; | |
d.style.borderColor = "white"; | |
d.style.background = "green"; | |
d.style.color = "white"; | |
d.style.position = "fixed"; | |
d.style.top = "5px"; | |
d.style.borderRadius = "16px"; | |
d.style.zIndex = "5999999"; | |
return d; | |
} | |
function showMessage(el, timeout = 5000) { | |
document.body.appendChild(el); | |
window.setTimeout(() => { | |
document.body.removeChild(el); | |
}, timeout); | |
} | |
function fetchDetails(domain, issueKey) { | |
return fetch( | |
`https://${domain}/rest/api/2/issue/${issueKey}?expand=names` | |
).then(response => { | |
return response.json(); | |
}); | |
} | |
function extractIssueKey(p) { | |
return /\/browse\/([^\\]+)/.exec(p)[1]; | |
} | |
try { | |
fetchDetails( | |
window.location.host, | |
extractIssueKey(window.location.pathname) | |
).then(({ fields, names, key }) => { | |
const p = [ | |
fields.summary, | |
`https://${window.location.host}/browse/${key.toLowerCase()}` | |
]; | |
customFields.forEach(k => { | |
if (fields[k]) { | |
p.push(`${names[k]}: ${fields[k].displayName}`); | |
} | |
}); | |
copyToClipboard(p.join("\n")); | |
const m = createMessageContainer(); | |
m.textContent = "Copied ticket information"; | |
showMessage(m); | |
}); | |
} catch (err) { | |
console.error(err); | |
const m = createMessageContainer(); | |
m.textContent = "Unable to fetch Jira issue details"; | |
m.style.background = "red"; | |
showMessage(m); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment