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.2
  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 titleNode = dom[124];
  30. var titleText = "";
  31. if (titleNode.nodeName == 'TITLE') {
  32. titleText = titleNode.innerText;
  33. } else { // if not the 124th one
  34. for (var node of dom) {
  35. if (node.nodeName == 'TITILE') {
  36. titleText = node.innerText;
  37. }
  38. }
  39. }
  40. //console.log("issueId:" + issueId + ", title: " + titleText);
  41. var title = titleText.trim();
  42. var end = titleText.lastIndexOf('-');
  43. if (end > 0) {
  44. title = titleText.substring(0, end).trim();
  45. }
  46. var nodeId = "JIRA-ISSUE-" + issueId;
  47. $("#"+nodeId).append(", " + title);
  48. }
  49. });
  50. }
  51.  
  52.  
  53. (function() {
  54. 'use strict';
  55.  
  56. var btn = '';
  57. var mrClass = '.merge-request-details > .detail-page-description';
  58. var diffClass = '.commits-compare-btn';
  59.  
  60. var resultBtnDom = ".not.exist.class";
  61. if ($(mrClass).length > 0) {
  62. resultBtnDom = $(mrClass);
  63. btn = '<div style="display:block"><button id="extractBtn" class="btn btn-success" style="margin-left:0px;">Extract Issues</button></div><hr/>';
  64. } else if ($(diffClass).length > 0) {
  65. resultBtnDom = $(diffClass).parent();
  66. btn = '<div style="display:block"><button id="extractBtn" class="btn btn-success" style="margin-left:20px;">Extract Issues</button></div><hr/>';
  67. } else {
  68. return;
  69. }
  70. var resultDiv = '<ul id="jira-issue-extractor-result"></ul>'
  71.  
  72. resultBtnDom.append(btn + resultDiv);
  73.  
  74. $('#extractBtn').click(function() {
  75. if (hasExact === true) return;
  76. var issues = new Map();
  77. $('[data-external-issue]').map(function() {
  78. var link = $(this).attr('href');
  79. var issueId = this.text;
  80. issues.set(issueId, {'link': link, 'issue_id': issueId});
  81. });
  82.  
  83. var uniqueItems = [];
  84. issues.forEach(function(obj) {
  85. getTitle(obj.issue_id, obj.link);
  86.  
  87. uniqueItems += '<li><a href="{0}" id="JIRA-ISSUE-{1}">{0}</a></li>'.format(obj.link, obj.issue_id);
  88. });
  89. uniqueItems += "<hr/>"
  90. console.log("uniqueItems:" + uniqueItems);
  91. // $('textarea').val(uniqueItems);
  92. if (hasExact === false) {
  93. $('#jira-issue-extractor-result').append(uniqueItems);
  94. hasExact = true;
  95. $('#extractBtn').prop('disabled', true);
  96. }
  97. });
  98.  
  99.  
  100. })();