Created
July 9, 2024 00:04
-
-
Save mathmul/65734ea76e7b46440fb1d56c95cd6eea to your computer and use it in GitHub Desktop.
Convert Jira Task Title to a git branch name and copy to clipboard
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
/* | |
Played around so it works from DevTools console too. | |
EDIT: | |
After making this I went and wrote an extension for Chromium based browsers "Jira Task Title to Git Branch (Clipboard)" | |
Its code is totally free for abuse: https://github.com/mathmul/chr-ext-jira-task-title-to-git-branch | |
*/ | |
await (async function copyBranchNameToClipboard(branchType = 'feature', timeToFallbackMethod = Infinity) { | |
const getJiraTaskKey = () => | |
window.location.href.match(/\/browse\/([A-Z]+-\d+)/)?.[1] + '-' || '' | |
const slugifyText = text => text | |
.normalize('NFKD') | |
.replace(/[\u0300-\u036f]/g, '') | |
.toLowerCase() | |
.replace(/[^a-z0-9 -]/g, '-') | |
.replace(/\s+/g, '-') | |
.replace(/-+/g, '-') | |
.replace(/^-+|-+$/g, '') | |
const copyToClipboard = (text, timeToFallbackMethod) => new Promise((resolve, reject) => { | |
const storage = document.createElement('textarea') | |
storage.style.position = 'fixed' | |
storage.style.top = '0' | |
storage.style.left = '0' | |
storage.style.width = '2em' | |
storage.style.height = '2em' | |
storage.style.padding = '0' | |
storage.style.border = 'none' | |
storage.style.outline = 'none' | |
storage.style.boxShadow = 'none' | |
storage.style.background = 'transparent' | |
storage.value = text | |
document.body.appendChild(storage) | |
storage.focus() | |
storage.select() | |
storage.setSelectionRange(0, 99999) | |
const handleCopy = async () => { | |
clearTimeout(fallbackTimeout) | |
try { | |
await navigator.clipboard.writeText(storage.value) | |
resolve(storage.value) | |
} catch (err) { | |
document.execCommand('copy') | |
? resolve(storage.value) | |
: reject(err) | |
} finally { | |
document.body.removeChild(storage) | |
window.removeEventListener('focus', handleCopy) | |
} | |
} | |
const fallbackTimeout = timeToFallbackMethod <= 2_147_483_647 && setTimeout(() => { | |
console.log(`Falling back to document.execCommand after ${Math.round(timeToFallbackMethod / 1000)} seconds`) | |
if (document.execCommand('copy')) { | |
resolve(storage.value) | |
} else { | |
const err = new Error('Failed with document.execCommand after timeout') | |
console.error(err) | |
reject(err) | |
} | |
document.body.removeChild(storage) | |
window.removeEventListener('focus', handleCopy) | |
}, Math.min(timeToFallbackMethod)) | |
window.addEventListener('focus', handleCopy) | |
console.log("Hit <Tab> to give focus back to document (or we will face a DOMException)") | |
}) | |
const jiraKey = getJiraTaskKey() | |
const taskTitle = document.querySelector('h1').innerText | |
const slugifiedTitle = slugifyText(taskTitle) | |
const branchName = `${branchType}/${jiraKey}${slugifiedTitle}` | |
return await copyToClipboard(branchName, timeToFallbackMethod) | |
.then(() => `Branch name copied to clipboard: ${branchName}`) | |
.catch(err => `Failed to copy branch name: ${err}`) | |
})('feature', 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment