GitLab Jira Issue Extractor

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

目前为 2019-04-11 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name GitLab Jira Issue Extractor
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description GitLab Jira Issue Exactor: You can extract jira issues in GitLab after you enable the plugin "jira-issue-tracker".
  6. // @author tanglei.me
  7. // @match *://*/*
  8. // @require http://code.jquery.com/jquery-latest.min.js
  9. // @grant GM_xmlhttpRequest
  10. // ==/UserScript==
  11.  
  12. // Please replace your gitlab url to @match
  13.  
  14. var hasExact = false;
  15.  
  16. String.prototype.format = function () {
  17. var args = arguments;
  18. return this.replace(/\{(\d+)\}/g, function (m, n) { return args[n]; });
  19. };
  20.  
  21.  
  22. function getTitle(issueId, url) {
  23. var details = GM_xmlhttpRequest({
  24. method:"GET",
  25. url:url,
  26. onload: function (res) {
  27. var text = res.responseText;
  28. var dom = $(text);
  29. var nodeId = "JIRA-ISSUE-" + issueId;
  30.  
  31. if (dom.length < 124) {
  32. console.log("May be not logined in, url: " + url);
  33. console.log(dom);
  34. $("#"+nodeId).append(", Please Login in Jira");
  35. }
  36. var titleNode = dom[124];
  37. var titleText = "";
  38. if (titleNode && titleNode.nodeName == 'TITLE') {
  39. titleText = titleNode.innerText;
  40. } else { // if not the 124th one
  41. for (var i = 0; i < dom.length; i++) {
  42. var node = dom[i];
  43. if (node.nodeName == 'TITLE') {
  44. titleText = node.innerText;
  45. break;
  46. }
  47. }
  48. }
  49. //console.log("issueId:" + issueId + ", title: " + titleText);
  50. var title = titleText.trim();
  51. var end = titleText.lastIndexOf('-');
  52. if (end > 0) {
  53. title = titleText.substring(0, end).trim();
  54. }
  55. $("#"+nodeId).append(", " + title);
  56. }
  57. });
  58. }
  59.  
  60.  
  61. (function() {
  62. 'use strict';
  63.  
  64. var btn = '';
  65. var mrClass = '.merge-request-details > .detail-page-description';
  66. var diffClass = '.commits-compare-btn';
  67.  
  68. var resultBtnDom = ".not.exist.class";
  69. if ($(mrClass).length > 0) {
  70. resultBtnDom = $(mrClass);
  71. btn = '<div style="display:block"><button id="extractBtn" class="btn btn-success" style="margin-left:0px;">Extract Issues</button></div><hr/>';
  72. } else if ($(diffClass).length > 0) {
  73. resultBtnDom = $(diffClass).parent();
  74. btn = '<div style="display:block"><button id="extractBtn" class="btn btn-success" style="margin-left:20px;">Extract Issues</button></div><hr/>';
  75. } else {
  76. return;
  77. }
  78. var resultDiv = '<ul id="jira-issue-extractor-result"></ul>'
  79.  
  80. resultBtnDom.append(btn + resultDiv);
  81.  
  82. $('#extractBtn').click(function() {
  83. if (hasExact === true) return;
  84. var issues = new Map();
  85. $('[data-external-issue]').map(function() {
  86. var link = $(this).attr('href');
  87. var issueId = this.text;
  88. issues.set(issueId, {'link': link, 'issue_id': issueId});
  89. });
  90.  
  91. var uniqueItems = [];
  92. issues.forEach(function(obj) {
  93. getTitle(obj.issue_id, obj.link);
  94.  
  95. uniqueItems += '<li><a href="{0}" id="JIRA-ISSUE-{1}">{0}</a></li>'.format(obj.link, obj.issue_id);
  96. });
  97. uniqueItems += "<hr/>"
  98. console.log("uniqueItems:" + uniqueItems);
  99. // $('textarea').val(uniqueItems);
  100. if (hasExact === false) {
  101. $('#jira-issue-extractor-result').append(uniqueItems);
  102. hasExact = true;
  103. $('#extractBtn').prop('disabled', true);
  104. }
  105. });
  106.  
  107.  
  108. })();