Jira: Pull Request Link Improver

Adds more convenient pull request links to Jira tickets.

  1. // ==UserScript==
  2. // @name Jira: Pull Request Link Improver
  3. // @namespace https://github.com/rybak/atlassian-tweaks
  4. // @version 11
  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. // @include https://*jira*/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-2025 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 LOG_PREFIX = '[PR Link Improver]';
  41. const PANEL_ID = 'PrLinksImproverPanel';
  42. const LIST_ID = 'PrLinksImproverList';
  43. var loadInProgress = false;
  44. const PARSE_PATH = /[/](projects|users)[/]([^/]*)[/]repos[/]([^/]*)[/].*/;
  45.  
  46. function log(...toLog) {
  47. console.log(LOG_PREFIX, ...toLog);
  48. }
  49.  
  50. function info(...toLog) {
  51. console.info(LOG_PREFIX, ...toLog);
  52. }
  53.  
  54. function warn(...toLog) {
  55. console.warn(LOG_PREFIX, ...toLog);
  56. }
  57.  
  58. function error(...toLog) {
  59. console.error(LOG_PREFIX, ...toLog);
  60. }
  61.  
  62. function getJiraMajorVersion() {
  63. return document.querySelector('meta[name="application-name"]').attributes.getNamedItem("data-version").value.split(".")[0];
  64. }
  65.  
  66. function createPanel() {
  67. var header;
  68. const jiraMajorVersion = getJiraMajorVersion();
  69. switch (jiraMajorVersion) {
  70. case "7":
  71. header = '<div class="mod-header"><h2 class="toggle-title">Pull requests</h2></div>';
  72. break;
  73. case "8":
  74. case "9":
  75. header = '<div class="mod-header"><h4 class="toggle-title">Pull requests</h4></div>';
  76. break;
  77. default:
  78. warn("Jira v" + jiraMajorVersion + " is not supported");
  79. header = '<div class="mod-header"><h4 class="toggle-title">Pull requests</h4></div>';
  80. break;
  81. }
  82. $('#viewissue-devstatus-panel')
  83. .prepend($(header +
  84. `<div class="mod-content" id="${PANEL_ID}" style="margin-bottom:1rem;"></div>`));
  85. }
  86.  
  87. function addError(errors) {
  88. for (const e of errors) {
  89. error("Error: " + e);
  90. }
  91. $(`#${PANEL_ID}`).append($(`<p>Could not load from Bitbucket. Got: ${errors}</p>`));
  92. }
  93.  
  94. function extractProjectRepoSlugsFromPr(pr) {
  95. const url = pr.url;
  96. const path = new URL(url).pathname;
  97. const matching = path.match(PARSE_PATH);
  98. const project = matching[2];
  99. const repository = matching[3];
  100. return project + '/' + repository;
  101. }
  102.  
  103. function addPrLinks(pullRequests) {
  104. $(`#${PANEL_ID}`).append($(`<ul id="${LIST_ID}" class="item-details status-panels devstatus-entry"></ul>`));
  105. const list = $(`#${LIST_ID}`);
  106. for (const pr of pullRequests) {
  107. const url = pr.url;
  108. const prId = pr.id;
  109. const li = $('<li/>').appendTo(list);
  110. const slugsPrefix = extractProjectRepoSlugsFromPr(pr) + ': ';
  111. const link = document.createElement('a');
  112. link.href = url;
  113. link.appendChild(document.createTextNode(`${prId}: ${pr.name}`));
  114. info("NAME='" + pr.name + "' issue='" + JIRA.Issue.getIssueKey() + "'");
  115. if (pr.name.includes(JIRA.Issue.getIssueKey())) {
  116. link.style.fontWeight = 'bold';
  117. }
  118. if (pr.status == "DECLINED") {
  119. link.style.textDecoration = 'line-through';
  120. }
  121. $(li).append(slugsPrefix).append(link);
  122. }
  123. }
  124.  
  125. function addPrLinksPanel() {
  126. if ($(`#${PANEL_ID}`).length) {
  127. // the PR links panel has already been created
  128. loadInProgress = false;
  129. return;
  130. }
  131. const issueId = JIRA.Issue.getIssueId();
  132. // https://community.atlassian.com/t5/Jira-questions/JIRA-REST-API-to-get-list-of-branches-related-to-a-issue/qaq-p/800389
  133. const pullRequestsUrl = `/rest/dev-status/1.0/issue/detail?issueId=${issueId}&applicationType=stash&dataType=pullrequest`;
  134. log("Loading: " + pullRequestsUrl);
  135. $.getJSON(pullRequestsUrl, data => {
  136. if ($(`#${PANEL_ID}`).length) {
  137. // the PR links panel has been created while we were getting the PR data
  138. loadInProgress = false;
  139. return;
  140. }
  141. createPanel();
  142. if (data.detail.length == 0) {
  143. addError(data.errors);
  144. loadInProgress = false;
  145. return;
  146. }
  147. const pullRequests = data.detail[0].pullRequests;
  148. addPrLinks(pullRequests);
  149. loadInProgress = false;
  150. });
  151. }
  152.  
  153. function startPrLoading() {
  154. if (!loadInProgress) {
  155. loadInProgress = true;
  156. addPrLinksPanel();
  157. } else {
  158. log("Already loading. Skipping...");
  159. }
  160. }
  161.  
  162. $(document).ready(startPrLoading);
  163. JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, startPrLoading);
  164. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址