Bitbucket: commit links in diff tab of PRs

Adds convenience links in PRs of Bitbucket v7.6.+

安装此脚本?
作者推荐脚本

您可能也喜欢Bitbucket: copy commit reference

安装此脚本
  1. // ==UserScript==
  2. // @name Bitbucket: commit links in diff tab of PRs
  3. // @namespace https://github.com/rybak/atlassian-tweaks
  4. // @version 16
  5. // @license MIT
  6. // @description Adds convenience links in PRs of Bitbucket v7.6.+
  7. // @author Andrei Rybak
  8. // @include https://*bitbucket*/*/repos/*/pull-requests/*
  9. // @match https://bitbucket.example.com/*/repos/*/pull-requests/*
  10. // @icon https://bitbucket.org/favicon.ico
  11. // @homepageURL https://github.com/rybak/atlassian-tweaks
  12. // @grant none
  13. // ==/UserScript==
  14.  
  15. /*
  16. * Copyright (c) 2021-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 commit links]";
  41.  
  42. function warn(...toLog) {
  43. console.warn(LOG_PREFIX, ...toLog);
  44. }
  45.  
  46. function log(...toLog) {
  47. console.log(LOG_PREFIX, ...toLog);
  48. }
  49.  
  50. const ABBREV_LEN = 8; // abbreviate commit hashes to this number of characters
  51. const BLOCK_ID = 'RybakCommitLinkDiv';
  52. const URL_ID = 'RybakCommitLinkA';
  53. const TOOLTIP_BLOCK_ID = 'RybakCommitMessageDiv';
  54. const TOOLTIP_MSG_ID = 'RybakCommitMessagePre';
  55. const parsePath = /[/](projects|users)[/]([^/]*)[/]repos[/]([^/]*)[/].*[/]commits[/]([0-9a-f]+)/
  56.  
  57. function createTooltip(message) {
  58. $('#' + BLOCK_ID).hover((e) => {
  59. $('#' + TOOLTIP_BLOCK_ID).remove(); // delete previous tooltip
  60. const tooltipHtml = $('<div id="' + TOOLTIP_BLOCK_ID + '" class="Tooltip sc-jnlKLf ghcsui sc-bZQynM WXFrO sc-EHOje cffcMV"' +
  61. 'style="z-index:800; opacity: 1; position: fixed; top: 0px; left: 0px;' + // tweaked original element.style
  62. 'max-width: 600px; width: auto;' + // override of .ghcsui for better fitting of text
  63. 'background-color: rgb(23, 43, 77); border-radius: 3px; box-sizing: border-box; color: rgb(255, 255, 255); font-size: 12px; ' + // from .WXFrO
  64. 'line-height: 1.3; padding: 2px 6px; overflow-wrap: break-word;' + // from .WXFrO
  65. 'pointer-events: none;' + // from .cffcMV
  66. '">' +
  67. '<pre id="' + TOOLTIP_MSG_ID + '" class="commit-message-tooltip" style="' +
  68. 'white-space: pre-wrap; word-break: break-word;' + // from .commit-message-tooltip
  69. '"></pre>' +
  70. '</div>');
  71. $($('.atlaskit-portal-container')[0]).append(tooltipHtml);
  72. $('#' + TOOLTIP_MSG_ID).text(message); // text added early to calculate height correctly
  73.  
  74. const width = $('#' + TOOLTIP_BLOCK_ID).outerWidth();
  75. const height = $('#' + TOOLTIP_BLOCK_ID).height();
  76. const block = $('#' + BLOCK_ID);
  77. const blockOffset = block.offset();
  78. var x = blockOffset.left;
  79. var y = blockOffset.top + block.height() + 8; // 8 is from CSS rule ".changes-scope-actions > *"
  80. const maxX = $(window).width() + window.pageXOffset;
  81. const maxY = $(window).height() + window.pageYOffset;
  82. if (x + width > maxX) {
  83. x = Math.max(maxX - width, 0);
  84. }
  85. if (y + height > maxY) {
  86. y = Math.max(maxY - height, 0);
  87. }
  88. $('#' + TOOLTIP_BLOCK_ID).css({left: x, top: y}).show();
  89. }, (e) => {
  90. $('#' + TOOLTIP_BLOCK_ID).hide();
  91. });
  92. }
  93.  
  94. function ensureCommitLink(label) {
  95. const matching = document.location.pathname.match(parsePath);
  96. if (!matching) {
  97. log(label, "No commit in the URL: " + document.location.pathname);
  98. return;
  99. }
  100. const origin = document.location.origin;
  101. const hash = document.location.hash; // add hash in case the user clicked to a different file
  102. const projectOrUser = matching[1];
  103. const project = matching[2];
  104. const repository = matching[3];
  105. /*
  106. * TODO: keep track of `commit` and avoid recreating everything if
  107. * `commit` hasn't changed.
  108. */
  109. const commit = matching[4];
  110. log(label, "Parsed " + project + "/" + repository + "/" + commit);
  111.  
  112. const url = origin + '/' + projectOrUser + '/' + project + '/repos/' + repository + '/commits/' + commit + document.location.hash;
  113. const linkText = commit.substring(0, ABBREV_LEN);
  114. log(label, "Link: " + url);
  115. log(label, "Text: " + linkText);
  116.  
  117. const prevBlock = $('#' + BLOCK_ID);
  118. if (prevBlock.length) {
  119. log(label, "Updating the link...");
  120. } else {
  121. const searchCodeButton = document.querySelector('#main .changes-scope-actions [data-testid="search-action-button-tooltip--container"] button');
  122. const container = document.createElement('div');
  123. container.id = BLOCK_ID;
  124. container.classList.add(searchCodeButton.classList[0]);
  125. const link = document.createElement('a');
  126. link.id = URL_ID;
  127. container.appendChild(link);
  128. document.querySelector('.changes-scope-actions').append(container);
  129. log(label, "Creating the link...");
  130. }
  131. $('#' + URL_ID)
  132. .attr('href', url)
  133. .text(linkText);
  134. const restApiUrl = document.location.origin + "/rest/api/1.0/" + projectOrUser + "/" + project + '/repos/' + repository + '/commits/' + commit;
  135. log(label, "Ajax...: " + restApiUrl);
  136.  
  137. $.ajax({
  138. // https://docs.atlassian.com/bitbucket-server/rest/7.6.0/bitbucket-rest.html#idp224
  139. url: (restApiUrl)
  140. }).then(data => {
  141. log(label, "Ajax response received");
  142. if (!document.getElementById(BLOCK_ID)) {
  143. warn(label, `Something happened to #${BLOCK_ID}. Re-creating...`);
  144. ensureCommitLink("[smth happened]");
  145. }
  146. createTooltip(data.message);
  147. });
  148. log(label, "Done");
  149. }
  150.  
  151. $(document).ready(function() {
  152. ensureCommitLink("[document.ready]");
  153. window.onpopstate = function(event) {
  154. ensureCommitLink("[onpopstate]");
  155. };
  156. });
  157.  
  158. })();