Greasy Fork 支持简体中文。

Jira: Pull Request Link Improver

Adds more convenient pull request links to Jira tickets.

安裝腳本?
作者推薦腳本

您可能也會喜歡 Bitbucket: commit links in diff tab of PRs

安裝腳本
  1. // ==UserScript==
  2. // @name Jira: Pull Request Link Improver
  3. // @namespace https://github.com/rybak/atlassian-tweaks
  4. // @version 10
  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-2023 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. case "9":
  65. header = '<div class="mod-header"><h4 class="toggle-title">Pull requests</h4></div>';
  66. break;
  67. default:
  68. warn("Jira v" + jiraMajorVersion + " is not supported");
  69. header = '<div class="mod-header"><h4 class="toggle-title">Pull requests</h4></div>';
  70. break;
  71. }
  72. $('#viewissue-devstatus-panel')
  73. .prepend($(header +
  74. `<div class="mod-content" id="${PANEL_ID}" style="margin-bottom:1rem;"></div>`));
  75. }
  76.  
  77. function addError(errors) {
  78. for (const e of errors) {
  79. log("Error: " + e);
  80. }
  81. $(`#${PANEL_ID}`).append($(`<p>Could not load from Bitbucket. Got: ${errors}</p>`));
  82. }
  83.  
  84. function addPrLinks(pullRequests) {
  85. $(`#${PANEL_ID}`).append($(`<ul id="${LIST_ID}" class="item-details status-panels devstatus-entry"></ul>`));
  86. const list = $(`#${LIST_ID}`);
  87. for (const pr of pullRequests) {
  88. const url = pr.url;
  89. const prId = pr.id;
  90. const li = $('<li/>').appendTo(list);
  91. var link = `<a href="${url}">${prId}: ${pr.name}</a>`;
  92. log("NAME='" + pr.name + "' issue='" + JIRA.Issue.getIssueKey() + "'");
  93. if (pr.name.includes(JIRA.Issue.getIssueKey())) {
  94. link = `<strong>${link}</strong>`;
  95. }
  96. if (pr.status == "DECLINED") {
  97. link = `<s>${link}</s>`;
  98. }
  99. $(link).appendTo(li);
  100. }
  101. }
  102.  
  103. function addPrLinksPanel() {
  104. if ($(`#${PANEL_ID}`).length) {
  105. // the PR links panel has already been created
  106. loadInProgress = false;
  107. return;
  108. }
  109. const issueId = JIRA.Issue.getIssueId();
  110. // https://community.atlassian.com/t5/Jira-questions/JIRA-REST-API-to-get-list-of-branches-related-to-a-issue/qaq-p/800389
  111. const pullRequestsUrl = `/rest/dev-status/1.0/issue/detail?issueId=${issueId}&applicationType=stash&dataType=pullrequest`;
  112. log("Loading: " + pullRequestsUrl);
  113. $.getJSON(pullRequestsUrl, data => {
  114. if ($(`#${PANEL_ID}`).length) {
  115. // the PR links panel has been created while we were getting the PR data
  116. loadInProgress = false;
  117. return;
  118. }
  119. createPanel();
  120. if (data.detail.length == 0) {
  121. addError(data.errors);
  122. loadInProgress = false;
  123. return;
  124. }
  125. const pullRequests = data.detail[0].pullRequests;
  126. addPrLinks(pullRequests);
  127. loadInProgress = false;
  128. });
  129. }
  130.  
  131. function startPrLoading() {
  132. if (!loadInProgress) {
  133. loadInProgress = true;
  134. addPrLinksPanel();
  135. } else {
  136. log("Already loading. Skipping...");
  137. }
  138. }
  139.  
  140. $(document).ready(startPrLoading);
  141. JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, startPrLoading);
  142. })();