Skip to content

Instantly share code, notes, and snippets.

@dissolved
Last active February 21, 2020 16:05
Show Gist options
  • Save dissolved/4d3c969d1458457ad615e5cf4f56863b to your computer and use it in GitHub Desktop.
Save dissolved/4d3c969d1458457ad615e5cf4f56863b to your computer and use it in GitHub Desktop.
Proof of Concept Bookmarklet to Generate Branch Name from Jira Issue

Not a Bird

Status

  • Works on Chrome without any much error handling.
  • Doesn't work on Firefox (yet)

Installation

  1. Optionally minify the code (can do that here)
  2. Create a bookmark, start it with javascript: then append the code

Use

When viewing an issue in Jira, execute the bookmarklet. Currently there is no feedback on success, but it should copy to the clipboard a normalized branch name based on the issue following the conventions used at Arcadia.

(function() {
function reportPermission(state) {
console.log('Permission: ' + state);
}
function requestClipboardPermission() {
navigator.permissions.query({name:'clipboard'}).then(function(result) {
reportPermission(result.state);
result.onchange = function() {reportPermission(result.state); };
});
}
function displayBranchName(response){
const issueType = response.fields.issuetype.name.toLocaleLowerCase();
const summary = response.fields.summary.toLocaleLowerCase().replace(/(\W+)/g, '-');
const issueID = response.key;
// window.alert([issueType, summary, issueID].join('/'));
requestClipboardPermission();
navigator.clipboard.writeText([issueType, summary, issueID].join('/')).then(function() {
console.log('Branch Name clipped');
}, function() {
console.log('Failed to save to clipboard');
});
}
// https://regex101.com/r/VHIloe/1
const regex = /https:\/\/([^\/]*)\/(?:browse\/|jira\/.*?boards\/.*?selectedIssue=)(.*?)(?:\?|$)/;
const match = regex.exec(document.URL);
if (match && match.length == 3){
const issue_url = "https://" + match[1] + "/rest/api/3/issue/" + match[2];
fetch(issue_url).then(response => response.json()).then(displayBranchName);
} else {
window.alert("Could not extract issue. Have you selected a Jira ticket?");
}
return undefined;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment