GitLab Jira Issue Extractor(DEV)

GitLab Jira Issue Exactor: You can extract jira issues in GitLab after you enable the plugin "jira-issue-tracker".

2019-04-11 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         GitLab Jira Issue Extractor(DEV)
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  GitLab Jira Issue Exactor: You can extract jira issues in GitLab after you enable the plugin "jira-issue-tracker".
// @author       tanglei.me
// @match        *://*/*
// @require      http://code.jquery.com/jquery-latest.min.js
// @grant        GM_xmlhttpRequest
// ==/UserScript==

// Please replace your gitlab url to @match

var hasExact = false;

String.prototype.format = function () {
  var args = arguments;
  return this.replace(/\{(\d+)\}/g, function (m, n) { return args[n]; });
};


function getTitle(issueId, url) {
    var details = GM_xmlhttpRequest({
        method:"GET",
        url:url,
        onload: function (res) {
            var text = res.responseText;
            var dom = $(text);
            var titleNode = dom[124];
            var titleText = "";
            if (titleNode.nodeName == 'TITLE') {
                titleText = titleNode.innerText;
            } else { // if not the 124th one
                for (var node of dom) {
                    if (node.nodeName == 'TITILE') {
                        titleText = node.innerText;
                    }
                }
            }
            //console.log("issueId:" + issueId +  ", title: " + titleText);
            var title = titleText.trim();
            var end = titleText.lastIndexOf('-');
            if (end > 0) {
                title = titleText.substring(0, end).trim();
            }
            var nodeId = "JIRA-ISSUE-" + issueId;
            $("#"+nodeId).append(", " + title);
        }
    });
}


(function() {
    'use strict';

    var btn = '';
    var mrClass = '.merge-request-details > .detail-page-description';
    var diffClass = '.commits-compare-btn';

    var resultBtnDom = ".not.exist.class";
    if ($(mrClass).length > 0) {
        resultBtnDom = $(mrClass);
        btn = '<div style="display:block"><button id="extractBtn" class="btn btn-success" style="margin-left:0px;">Extract Issues</button></div><hr/>';
    } else if ($(diffClass).length > 0) {
        resultBtnDom = $(diffClass).parent();
        btn = '<div style="display:block"><button id="extractBtn" class="btn btn-success" style="margin-left:20px;">Extract Issues</button></div><hr/>';
    } else {
        return;
    }
    var resultDiv = '<ul id="jira-issue-extractor-result"></ul>'

    resultBtnDom.append(btn + resultDiv);

    $('#extractBtn').click(function() {
        if (hasExact === true) return;
        var issues = new Map();
        $('[data-external-issue]').map(function() {
            var link = $(this).attr('href');
            var issueId = this.text;
            issues.set(issueId, {'link': link, 'issue_id': issueId});
        });

        var uniqueItems = [];
        issues.forEach(function(obj) {
            getTitle(obj.issue_id, obj.link);

            uniqueItems += '<li><a href="{0}" id="JIRA-ISSUE-{1}">{0}</a></li>'.format(obj.link, obj.issue_id);
        });
        uniqueItems += "<hr/>"
        console.log("uniqueItems:" + uniqueItems);
        // $('textarea').val(uniqueItems);
        if (hasExact === false) {
            $('#jira-issue-extractor-result').append(uniqueItems);
            hasExact = true;
            $('#extractBtn').prop('disabled', true);
        }
    });


})();