Skip to content

Instantly share code, notes, and snippets.

@hsingh23
Last active May 19, 2016 14:23
Show Gist options
  • Save hsingh23/67528e4193cf84cad3f1 to your computer and use it in GitHub Desktop.
Save hsingh23/67528e4193cf84cad3f1 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Stash PR links for commits
// @namespace https://gist.githubusercontent.com/hsingh23/67528e4193cf84cad3f1/raw/stash-pullrequest-linker.user.js
// @version 1.0.9
// @downloadURL https://gist.githubusercontent.com/hsingh23/67528e4193cf84cad3f1/raw/stash-pullrequest-linker.user.js
// @updateURL https://gist.githubusercontent.com/hsingh23/67528e4193cf84cad3f1/raw/stash-pullrequest-linker.user.js
// @description Stash's commit view does not show you which pull request a commit belongs to. This script links it up as best as it can.
// @author Harsh Singh
// @match https://*/projects/*/repos/*/commits*
// @match http://*/projects/*/repos/*/commits*
// @grant none
// ==/UserScript==
/* jshint -W097 */
'use strict';
function querySelectorAllArray(query, dom) {
dom = (dom)? dom: document;
return Array.from(dom.querySelectorAll(query));
}
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
function createPullRequestLinks() {
var commitToMessageDom = {};
querySelectorAllArray(".changesetid,.commitid").map(function(changeset) {
commitToMessageDom[changeset.text] = changeset.parentElement.parentElement.querySelector(".message-subject");
});
querySelectorAllArray(".merge .message-subject").map(function(x) {
var first_match = x.title.match(/Merge pull request #(\d+)/);
if (first_match){
var pull_request_id = first_match[1];
var url = location.href.replace("commits","pull-requests") + "/"+ pull_request_id + "/overview";
if (! x.getAttribute("wrapped")) {
x.outerHTML = "<a href='" + url + "'>" + x.outerHTML + "</a>";
x.setAttribute("wrapped", "true");
}
var second_match = x.title.match(/commit \'([\d\w]{11})/);
if (second_match && second_match[1]) {
var ref_commit = second_match[1];
var message = commitToMessageDom[ref_commit];
if ( message && !message.getAttribute("wrapped")) {
message.setAttribute("wrapped", "true");
commitToMessageDom[ref_commit].outerHTML = "<a href='" + url + "'>" + commitToMessageDom[ref_commit].outerHTML + "</a>";
}
}
}
});
}
createPullRequestLinks();
var observer = new MutationObserver(debounce(createPullRequestLinks, 500));
observer.observe(document.querySelector("#commits-table tbody"), { childList: true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment