Skip to content

Instantly share code, notes, and snippets.

@tan9
Last active July 8, 2022 01:47
Show Gist options
  • Save tan9/78bb3fc107c3d1e7ef8d404e3e0a99ec to your computer and use it in GitHub Desktop.
Save tan9/78bb3fc107c3d1e7ef8d404e3e0a99ec to your computer and use it in GitHub Desktop.
Azure DevOps Toolbox
// ==UserScript==
// @name Azure DevOps Toolbox
// @source https://gist.github.com/tan9/78bb3fc107c3d1e7ef8d404e3e0a99ec
// @namespace https://gist.github.com/tan9/
// @version 0.1.1
// @description Change the new Pull Request target to repository itself, a workaround for https://developercommunity.visualstudio.com/t/ability-to-change-the-default-target-repo-when-cre/791273
// @author Pei-Tang Huang
// @downloadURL https://gist.githubusercontent.com/tan9/78bb3fc107c3d1e7ef8d404e3e0a99ec/raw/
// @updateURL https://gist.githubusercontent.com/tan9/78bb3fc107c3d1e7ef8d404e3e0a99ec/raw/
// @match https://dev.azure.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=azure.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
// case 1. where the pull request creation was created from an pasted URL
const updateTragetOnState = (state) => {
if (state.vssNavigationState?.routeId !== "ms.vss-code-web.create-pull-request-route") {
// only work on create-pull-request-route
return;
}
const sourceRepositoryId = state?.vssNavigationState?.state?.sourceRepositoryId;
const sourceRef = state?.vssNavigationState?.state?.sourceRef;
if (sourceRepositoryId && sourceRef) {
const refs = JSON.parse(sessionStorage.getItem('azure-devops-toolbox-target-adjusted-refs') || '[]');
const refKey = `${sourceRepositoryId}:${sourceRef}`;
if (refs.indexOf(refKey) != -1) {
// only adjust target once
return;
}
const targetRepositoryId = state.vssNavigationState.state.targetRepositoryId;
if (sourceRepositoryId === targetRepositoryId) {
// already set target to repository itself, nothing to do on our side
return;
}
const targetUrl = state.vssNavigationState.url.replace(targetRepositoryId, sourceRepositoryId);
console.log(`About to adjust target repository: ${targetRepositoryId} to ${sourceRepositoryId}.` );
refs.push(refKey);
sessionStorage.setItem('azure-devops-toolbox-target-adjusted-refs', JSON.stringify(refs));
window.location.href = targetUrl;
}
}
updateTragetOnState(history.state);
window.history.pushState = new Proxy(window.history.pushState, {
apply: (target, thisArg, args) => {
const targetUrl = args[2];
if (targetUrl?.indexOf('pullrequestcreate?') != -1) {
// case 2. where the pull request creation was triggered via [Create a pull request] from a source branch
const targetUrlSplit = targetUrl?.split('pullrequestcreate?');
if (targetUrlSplit.length = 2) {
const targetPrefix = targetUrlSplit[0] + 'pullrequestcreate?';
const params = new URLSearchParams(targetUrlSplit[1]);
if (params.has('targetRepositoryId') && params.has('sourceRepositoryId') &&
(params.get('targetRepositoryId') !== params.get('sourceRepositoryId'))) {
params.set('targetRepositoryId', params.get('sourceRepositoryId'));
window.location.href = targetPrefix + params;
}
}
}
if (targetUrl?.endsWith('pullrequestcreate')) {
// case 3. where the pull request creation was triggered via [New pull request] from pull request view
// TODO
}
return target.apply(thisArg, args);
},
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment