Last active
July 27, 2022 15:14
-
-
Save loehx/409418e0ad5d46524f8830c5d76ceae3 to your computer and use it in GitHub Desktop.
Clean Branch Name and Ticket Name for JIRA Ticket View
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
// Clean Branch Name and Ticket Name for JIRA Ticket View | |
// Author: Alexander Löhn | |
// Install: | |
// 1. Download a Chrome extension for injecting JS (e.g. https://chrome.google.com/webstore/detail/inject-javascript-synced/aechnpkbeoilkginaangjabdhcknecck). | |
// 2. Copy this file and paste it into the extension. | |
// 3. Open any JIRA ticket (https://host.atlassian.net/browse/XX-123) and watch the script add 2 elements above the ticket title. | |
includejQuery(function($) { | |
$(document).ready(function() { | |
runTilSuccess(addNewHistoryEntry) | |
runTilSuccess(replaceH1); | |
runTilSuccess(insertBranchName); | |
}) | |
}) | |
function includejQuery(callback) { | |
if(window.jQuery) | |
{ | |
// jQuery is already loaded, set up an asynchronous call | |
// to the callback if any | |
if (callback) | |
{ | |
setTimeout(function() { | |
callback(jQuery); | |
}, 0); | |
} | |
} | |
else | |
{ | |
// Inject jQuery on-the-fly | |
var script = document.createElement('script'); | |
script.onload = function() { | |
jQuery.noConflict(); | |
if (callback) { | |
callback(jQuery); | |
} | |
}; | |
script.src = "http://code.jquery.com/jquery-2.1.1.min.js"; | |
document.getElementsByTagName('head')[0].appendChild(script); | |
} | |
} | |
function replaceH1() { | |
if (typeof $ === 'undefined') { | |
injectJQuery(); | |
return false; | |
} | |
if ($('#custom-ticket-title').length) { | |
return true; | |
} | |
const ticketNoAndTitle = document.title.replace(/[\[\]]/g, '').replace(' - Jira', ''); | |
$('<h3 id="custom-ticket-title" contenteditable="true" style="background-color: aquamarine; padding: 15px"></h3>') | |
.text(ticketNoAndTitle) | |
.insertAfter('#jira-issue-header'); | |
} | |
function insertBranchName() { | |
if (typeof $ === 'undefined') { | |
injectJQuery(); | |
return false; | |
} | |
if ($('#branch-name').length) { | |
return true; | |
} | |
const ticketNoAndTitle = document.title.replace(/[\[\,]]/g, '').replace(' - Jira', ''); | |
const [ticketNo, ...ticketTitle] = ticketNoAndTitle.split(' '); | |
const isBug = $('[alt="Bug"]').length > 0; | |
const type = isBug ? 'fix' : 'feat'; | |
$('<h5 id="branch-name" contenteditable="true" style="padding:15px; background-color: #ffeb3b"></h5>') | |
.text(type + '/' + ticketNo.replace(/[^\w-]/g, '') + '-' + convertToKebabCase(ticketTitle.join(' '))) | |
.insertAfter('#jira-issue-header'); | |
} | |
function injectJQuery() { | |
function l(u, i) { | |
var d = document; | |
if (!d.getElementById(i)) { | |
var s = d.createElement('script'); | |
s.src = u; | |
s.id = i; | |
d.body.appendChild(s); | |
} | |
} | |
l('//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js', 'jquery') | |
} | |
function camelize(str) { | |
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(word, index) { | |
return index === 0 ? word.toLowerCase() : word.toUpperCase(); | |
}).replace(/\s+/g, ''); | |
} | |
function convertToKebabCase(string) { | |
return string.replace(/[^\w]+(\w+)/g, '-$1').replace(/[^\w-]|^-/g, '').toLowerCase() | |
} | |
function runTilSuccess(func) { | |
if (!func()) { | |
console.log('runTilSuccess() 1000ms to next try...', func) | |
setTimeout(runTilSuccess.bind(this, func), 1000) | |
} | |
} | |
function addNewHistoryEntry() { | |
if (!window.moment) return | |
moment = window.moment | |
const history = JSON.parse(localStorage.__history || '{}') | |
const date = moment().format('dddd LL') | |
const dailyHistory = history[date] || {} | |
const description = document.title | |
.replace(' - Jira', '') | |
.replace(/[\[\]]/gi, '') | |
if (!Object.values(dailyHistory).some(d => d === description)) { | |
dailyHistory[moment().format('HH:mm')] = description | |
history[date] = dailyHistory | |
localStorage.__history = JSON.stringify(history) | |
} | |
console.clear() | |
console.log('%cTICKETS OF THE DAY', 'font-size: 20px') | |
console.log(Object.values(dailyHistory).join('\n')) | |
console.log('%cJIRA HISTORY', 'font-size: 20px') | |
console.log(JSON.stringify(history, null, 3)) | |
window.JIRA_HISTORY = history | |
return history | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment