Jira: Pull Request Link Improver

Adds more convenient pull request links to Jira tickets.

目前為 2022-12-23 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Jira: Pull Request Link Improver
  3. // @namespace http://tampermonkey.net/
  4. // @version 6
  5. // @license MIT
  6. // @description Adds more convenient pull request links to Jira tickets.
  7. // @author Andrei Rybak
  8. // @match https://jira.example.com/browse/*
  9. // @match https://jira.example.com/browse/*
  10. // @icon https://jira.atlassian.com/favicon.ico
  11. // @homepageURL https://github.com/rybak/atlassian-tweaks
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. /*
  16. * Copyright (c) 2022 Andrei Rybak
  17. *
  18. * Permission is hereby granted, free of charge, to any person obtaining a copy
  19. * of this software and associated documentation files (the "Software"), to deal
  20. * in the Software without restriction, including without limitation the rights
  21. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  22. * copies of the Software, and to permit persons to whom the Software is
  23. * furnished to do so, subject to the following conditions:
  24. *
  25. * The above copyright notice and this permission notice shall be included in all
  26. * copies or substantial portions of the Software.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  29. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  31. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  32. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  33. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  34. * SOFTWARE.
  35. */
  36.  
  37. (function() {
  38. 'use strict';
  39.  
  40. const PANEL_ID = 'PrLinksImproverPanel';
  41. const LIST_ID = 'PrLinksImproverList';
  42. var loadInProgress = false;
  43.  
  44. function log(...toLog) {
  45. console.log('[PR Link Improver]:', ...toLog);
  46. }
  47.  
  48. function warn(...toLog) {
  49. console.warn('[PR Link Improver]:', ...toLog);
  50. }
  51.  
  52. function getJiraMajorVersion() {
  53. return document.querySelector('meta[name="application-name"]').attributes.getNamedItem("data-version").value.split(".")[0];
  54. }
  55.  
  56. function createPanel() {
  57. var header;
  58. const jiraMajorVersion = getJiraMajorVersion();
  59. switch (jiraMajorVersion) {
  60. case "7":
  61. header = '<div class="mod-header"><h2 class="toggle-title">Pull requests</h2></div>';
  62. break;
  63. case "8":
  64. header = '<div class="mod-header"><h4 class="toggle-title">Pull requests</h4></div>';
  65. break;
  66. default:
  67. warn("Jira v" + jiraMajorVersion + " is not supported");
  68. header = '<div class="mod-header"><h4 class="toggle-title">Pull requests</h4></div>';
  69. return;
  70. }
  71. $('#viewissue-devstatus-panel')
  72. .prepend($(header +
  73. `<div class="mod-content" id="${PANEL_ID}" style="margin-bottom:1rem;"></div>`));
  74. }
  75.  
  76. function addError(errors) {
  77. for (const e of errors) {
  78. log("Error: " + e);
  79. }
  80. $(`#${PANEL_ID}`).append($(`<p>Could not load from Bitbucket. Got: ${errors}</p>`));
  81. }
  82.  
  83. function addPrLinks(pullRequests) {
  84. $(`#${PANEL_ID}`).append($(`<ul id="${LIST_ID}" class="item-details status-panels devstatus-entry"></ul>`));
  85. const list = $(`#${LIST_ID}`);
  86. for (const pr of pullRequests) {
  87. const url = pr.url;
  88. const prId = pr.id;
  89. const li = $('<li/>').appendTo(list);
  90. var link = `<a href="${url}">${prId}: ${pr.name}</a>`;
  91. log("NAME='" + pr.name + "' issue='" + JIRA.Issue.getIssueKey() + "'");
  92. if (pr.name.includes(JIRA.Issue.getIssueKey())) {
  93. link = `<strong>${link}</strong>`;
  94. }
  95. if (pr.status == "DECLINED") {
  96. link = `<s>${link}</s>`;
  97. }
  98. $(link).appendTo(li);
  99. }
  100. }
  101.  
  102. function addPrLinksPanel() {
  103. if ($(`#${PANEL_ID}`).length) {
  104. // the PR links panel has already been created
  105. loadInProgress = false;
  106. return;
  107. }
  108. const issueId = JIRA.Issue.getIssueId();
  109. // https://community.atlassian.com/t5/Jira-questions/JIRA-REST-API-to-get-list-of-branches-related-to-a-issue/qaq-p/800389
  110. const pullRequestsUrl = `/rest/dev-status/1.0/issue/detail?issueId=${issueId}&applicationType=stash&dataType=pullrequest`;
  111. log("Loading: " + pullRequestsUrl);
  112. $.getJSON(pullRequestsUrl, data => {
  113. if ($(`#${PANEL_ID}`).length) {
  114. // the PR links panel has been created while we were getting the PR data
  115. loadInProgress = false;
  116. return;
  117. }
  118. createPanel();
  119. if (data.detail.length == 0) {
  120. addError(data.errors);
  121. loadInProgress = false;
  122. return;
  123. }
  124. const pullRequests = data.detail[0].pullRequests;
  125. addPrLinks(pullRequests);
  126. loadInProgress = false;
  127. });
  128. }
  129.  
  130. $(document).ready(() => {
  131. JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function() {
  132. if (!loadInProgress) {
  133. loadInProgress = true;
  134. addPrLinksPanel();
  135. } else {
  136. log("Already loading. Skipping...");
  137. }
  138. });
  139. });
  140. })();