jiraBugTemplate

jira 提单自动填充模板

当前为 2019-09-27 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         jiraBugTemplate
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  jira 提单自动填充模板
// @author       mocobk
// @match        https://jira.sui.work/browse/*
// @match        https://jira.sui.work/projects/*
// @require      https://greasyfork.org/scripts/390554-getdeveloper/code/getDeveloper.js?version=736437
// @grant        none
// ==/UserScript==

// 测试环境
var env = '测试服';
// jira描述内容模板
var descContent = `

<p><img class="emoticon" src="/images/icons/emoticons/help_16.png" alt="" width="16" height="16" align="absmiddle" border="0" data-mce-src="/images/icons/emoticons/help_16.png"><strong>【问题描述】</strong></p>
<br>
<p><img class="emoticon" src="/images/icons/emoticons/information.png" alt="" width="16" height="16" align="absmiddle" border="0" data-mce-src="/images/icons/emoticons/information.png"><strong>【测试数据】</strong></p>
<br>
<p><img class="emoticon" src="/images/icons/emoticons/check.png" alt="" width="16" height="16" align="absmiddle" border="0" data-mce-src="/images/icons/emoticons/check.png"><strong>【预期结果】</strong></p>
<br>
<p><img class="emoticon" src="/images/icons/emoticons/error.png" alt="" width="16" height="16" align="absmiddle" border="0" data-mce-src="/images/icons/emoticons/error.png"><strong>【实际结果】</strong></p>

`;


// 指定次数和时间间隔重复执行
function timesInterval(handler, intervalTime, times) {
    let curTimes = 0;
    let timer = setInterval(() => {
        handler();
        curTimes++;
        if (curTimes >= times) {
            clearInterval(timer);
        }
    }, intervalTime)
}

function autoFill() {
    let defaultElement = {value: '', innerHTML: ''};

    // jira 编号
    let issueKey = $('#key-val')[0].getAttribute('data-issue-key') || defaultElement;

    // huanj
    let environment = $('#environment')[0] || defaultElement;

    // 描述
    let descIframe = $('iframe')[0];
    let description = descIframe.contentWindow.document.querySelector('#tinymce');

    let relatesTo = $('#issuelinks-issues-textarea')[0] || defaultElement;

    environment.value = env;
    relatesTo.value = issueKey;
    // 因描述信息框会刷新,所以要重复填充
    timesInterval(() => {
        if (!description.innerHTML.includes('<br>')) {
            description.innerHTML = descContent + '<br>';
        }
    }, 200, 10);

    relatesTo.focus();

    $('#create-issue-dialog .form-body')[0].scrollTop = 0;
    $('#summary')[0].focus();

}

// 网上找的一份代码,试过chrome中只有enter键是生效的
function fireKeyEvent(el, evtType, keyCode) {
    var doc = el.ownerDocument,
        win = doc.defaultView || doc.parentWindow,
        evtObj;
    if (doc.createEvent) {
        if (win.KeyEvent) {
            evtObj = doc.createEvent('KeyEvents');
            evtObj.initKeyEvent(evtType, true, true, win, false, false, false, false, keyCode, 0);
        } else {
            evtObj = doc.createEvent('UIEvents');
            Object.defineProperty(evtObj, 'keyCode', {
                get: function () {
                    return this.keyCodeVal;
                }
            });
            Object.defineProperty(evtObj, 'which', {
                get: function () {
                    return this.keyCodeVal;
                }
            });
            evtObj.initUIEvent(evtType, true, true, win, 1);
            evtObj.keyCodeVal = keyCode;
            if (evtObj.keyCode !== keyCode) {
                console.log("keyCode " + evtObj.keyCode + " 和 (" + evtObj.which + ") 不匹配");
            }
        }
        el.dispatchEvent(evtObj);
    } else if (doc.createEventObject) {
        evtObj = doc.createEventObject();
        evtObj.keyCode = keyCode;
        el.fireEvent('on' + evtType, evtObj);
    }
}

// 监听有空格输入后,延时500ms后自动按 enter 确认键
function autoInputEnter(element) {
    function handleKeydown(e){
        if (e.keyCode === 32){
            setTimeout(()=>{
                fireKeyEvent(element, 'keydown', 13);
                element.removeEventListener('keydown', handleKeydown);
            }, 500)
        }
    }
    element.addEventListener('keydown', handleKeydown);
}

// 根据匹配的开发人员姓名,自动填充经办开发
function assignDeveloper() {
    const bodyText = $('.issue-body-content')[0].innerText;
    const developer = maxTimesWord(bodyText);

    if (developer) {
        console.log('自动识别到开发', developer.name);
        let assigneeField = $('#assignee-field')[0];
        assigneeField.addEventListener(
            'click',
            () => {
                if (assigneeField.value !== developer.name){
                    // 这里因为必须要用户主动触发按键才能选择,采用空格触发
                    assigneeField.value = developer.name + ' 按空格键确认!';
                    assigneeField.focus();
                    assigneeField.setSelectionRange(developer.name.length, -1);
                    autoInputEnter(assigneeField);
                }
            })
    }
}


(function () {
    let createBtn = document.getElementById('create_link');
    // 添加创建按钮监听事件
    createBtn.addEventListener(
        'click',
        function () {
            let i = 0;
            let timer = setInterval(
                function () {
                    if ($('#create-issue-dialog').length === 1 && $('iframe')[0] && $('#issuetype-field')[0].value === '故障') {
                        autoFill();
                        clearInterval(timer);
                        assignDeveloper();
                    } else {
                        i++;
                        if (i >= 100) {
                            // 100 次轮询都未找到对象则停止
                            clearInterval(timer);
                        }
                    }
                }, 500)
        }
    )
})();