Last active
June 28, 2021 09:47
-
-
Save Bonno/7519fe74b51615b1db302b51dc05549a to your computer and use it in GitHub Desktop.
Copy issue key and title for creating git branch
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: helper | |
// @namespace https://gist.github.com/Bonno/7519fe74b51615b1db302b51dc05549a/raw/52f500ceb603800a98c0abb15bda45372ed9361e/jira-helper.user.js | |
// @version 1.0.0 | |
// @description features: 1. copy issue key and title for creating git branch | |
// @author Rain Chen / Bonno | |
// @license MIT | |
// @match https://*.atlassian.net/secure/* | |
// @match https://*.atlassian.net/browse/* | |
// @grant none | |
// @require https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.7.1/clipboard.min.js | |
// @require https://cdnjs.cloudflare.com/ajax/libs/notify/0.4.2/notify.min.js | |
// ==/UserScript== | |
(function($, JIRA) { | |
'use strict'; | |
function addCopyButton() { | |
//console.info('addCopyButton'); | |
var container = $('#jira-issue-header-actions>div').first(); | |
if (typeof container == 'undefined') { | |
//console.info('container is undefined'); | |
} | |
if(!$('#clipboardKeyBtn').next().length) { | |
$('#clipboardKeyBtn').remove(); | |
container.prepend("<a id='clipboardKeyBtn' class='btn aui-button aui-button-primary aui-style'>Copy issue key</a>"); | |
} | |
if(!$('#clipboardBtn').next().length) { | |
$('#clipboardBtn').remove(); | |
container.prepend("<a id='clipboardBtn' class='btn aui-button aui-button-primary aui-style'>Copy issue title</a>"); | |
} | |
} | |
function getIssueFullTitle () { | |
var issueKey = getIssueKey(); | |
var issueTitle = $("h1[data-test-id='issue.views.issue-base.foundation.summary.heading']").text(); | |
return '[' + issueKey + '] ' + issueTitle; | |
} | |
function getIssueKey () { | |
var issueKey = ""; | |
if($('div[data-test-id="issue.views.issue-base.foundation.breadcrumbs.breadcrumb-current-issue-container"]').find('a').length){ | |
issueKey = $('div[data-test-id="issue.views.issue-base.foundation.breadcrumbs.breadcrumb-current-issue-container"]').find('a').text(); | |
} | |
if($('#key-val').length){ | |
issueKey = $('#key-val').text(); | |
} | |
return issueKey; | |
} | |
function handleCopyButton () { | |
//console.info('handleCopyButton'); | |
var clipboard = new Clipboard('#clipboardBtn', { | |
text: function(trigger) { | |
return getIssueFullTitle(); | |
} | |
}); | |
clipboard.on('success', function(e) { | |
$.notify("Copied to clipboard", "success"); | |
$.notify(getIssueFullTitle(), "success"); | |
}); | |
clipboard.on('error', function(e) { | |
$.notify("Access granted", "error"); | |
}); | |
var clipboardKey = new Clipboard('#clipboardKeyBtn', { | |
text: function(trigger) { | |
return getIssueKey(); | |
} | |
}); | |
clipboardKey.on('success', function(e) { | |
$.notify("Copied to clipboard", "success"); | |
$.notify(getIssueKey(), "success"); | |
}); | |
clipboard.on('error', function(e) { | |
$.notify("Access granted", "error"); | |
}); | |
} | |
function init() { | |
//console.info("init"); | |
updateHandlers(); | |
handleCopyButton(); | |
addCopyButton(); | |
JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function() { | |
updateHandlers(); | |
}); | |
} | |
function updateHandlers() { | |
//console.info('updateHandlers'); | |
addCopyButton(); | |
} | |
// init when page ready | |
document.onreadystatechange = function() { | |
if (document.readyState == "complete") { | |
//init(); | |
var checkExist = setInterval(function() { | |
if ($('#jira-issue-header-actions>div').length) { | |
//console.info("Exists!"); | |
clearInterval(checkExist); | |
init(); | |
} | |
}, 100); | |
} | |
}; | |
})(window.jQuery, window.JIRA); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment