JIRA Create Branch Name

Adds a shortcut to create a branch name from a ticket and adds it to the clipboard. Usage: [Control] + [Alt] + Click on a ticket on the board.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         JIRA Create Branch Name
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Adds a shortcut to create a branch name from a ticket and adds it to the clipboard. Usage: [Control] + [Alt] + Click on a ticket on the board.
// @author       cartok
// @match        https://*.atlassian.net/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=atlassian.net
// @grant        none
// @license      MIT

// ==/UserScript==
(function() {
    const DELIMITER = '-'

    const itemQueries = {
        sprintItem: target => {
            const wrapper = target.closest('.ghx-issue')
            if(!wrapper){
                console.warn('[user-script] Could not find ticket wrapper on sprint board')
                return
            }

            const prefix = (() => {
                const element = wrapper.querySelector('.ghx-highlighted-field')
                // not every task is related to an epic
                if(!element) return 'feature'
                return element.textContent.trim().toLowerCase().replace(/\s+/g, '-')
            })()

            const id = (() => {
                const element = wrapper.querySelector('a.ghx-key')
                if(!element) console.warn(`[user-script] Could not find ticket's issue key element`)
                return wrapper.querySelector('a.ghx-key').href.replace(/.*\/(.*)$/, '$1')
            })()

            const name = (() => {
                const element = wrapper.querySelector('.ghx-summary')
                if(!element) console.warn(`[user-script] Could not find ticket's summary element`)
                return element.textContent.trim().toLowerCase()
            })()

            return { prefix, id, name }
        },
        backglogItem: target => {
            const wrapper = target.closest('.js-issue')
            if(!wrapper){
                console.warn('[user-script] Could not find ticket wrapper on backlog')
                return
            }

            const prefix = (() => {
                const element = wrapper.querySelector('.ghx-label[data-epickey]')
                // not every task is related to an epic
                if(!element) return 'feature'
                return element.textContent.trim().toLowerCase().replace(/\s+/g, '-')
            })()

            const id = (() => {
                const element = wrapper.querySelector('a.ghx-key')
                if(!element) console.warn(`[user-script] Could not find ticket's issue key element`)
                return element.href.replace(/.*\/(.*)$/, '$1')
            })()

            const name = (() => {
                const element = wrapper.querySelector('.ghx-summary')
                if(!element) console.warn(`[user-script] Could not find ticket's summary element`)
                return element.textContent.trim().toLowerCase()
            })()

            return { prefix, id, name }
        }
    }

    function toBranchName ({ prefix, id, name }) {
        name = name
            .replace(/^(\[.*?\])/m, '')
            .replace(/^[^\w]+/, '')
            .replace(/[^\w]+$/, '')
            .replace(/[^\w]+/g, DELIMITER)

        return `${prefix}/${id}-${name}`
    }

    document.addEventListener('click', async event => {
        event.stopPropagation()
        if(event.ctrlKey && event.altKey){
            event.preventDefault()
        }


        // first check targets, do not override any other possible bindings
        const queryResults = Object.values(itemQueries).map(query => query(event.target)).find(Boolean)
        if(!queryResults || !event.ctrlKey || !event.altKey) return

        // do not open the ticket in a new tab when clicking on the link
        event.preventDefault()

        const branchName = toBranchName(queryResults)
        await navigator.clipboard.writeText(branchName)
        console.log(`[user-script] Branchname '${branchName}' copied to clipboard`)
    }, false)
})();