-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-set-destination-to-develop.user.js
77 lines (55 loc) · 2.22 KB
/
auto-set-destination-to-develop.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// ==UserScript==
// @name Auto set destination branch to develop on pull requests
// @version 0.2
// @description Auto set destination branch to develop on pull requests
// @author Pedro Estrada
// @match https://github.com/e-buildernoc/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant none
// ==/UserScript==
(function(history) {
'use strict';
const mainBranch = 'main';
const devBranch = 'develop';
var splitUrlPaths = (url) => new URL(url).pathname.split('/');
var getCompareIndex = (urlPaths) => urlPaths.indexOf('compare') + 1;
var getCompareIndexFromUrl = (url) => getCompareIndex(splitUrlPaths(url));
var extractBranches = (url) => {
let urlPaths = splitUrlPaths(url)
return urlPaths.slice(getCompareIndex(urlPaths), urlPaths.length).join('/')
}
var getBranches = (url) => {
let compareLocation = getCompareIndexFromUrl(url);
if (compareLocation === 0) return null;
const branch = extractBranches(url);
if (url.indexOf('...') > -1) {
return branch;
}
const endsWithSlash = url.endsWith('/') ? 1 : 0;
if (compareLocation + endsWithSlash < splitUrlPaths(url).length) {
return `${mainBranch}...${branch}`;
}
return null;
}
var checkUrlPath = (url) => {
if (url == null) return;
const branches = getBranches(url);
let urlPaths = splitUrlPaths(url)
let compareLocation = getCompareIndex(urlPaths);
console.log(branches);
if (branches == null) return;
const [dest, source] = branches.split('...');
if (dest === mainBranch && source !== devBranch && source !== mainBranch) {
const newCompare = `${devBranch}...${source}`;
const newUrlPath = urlPaths.slice(0, compareLocation).join('/') + '/' + newCompare;
location.pathname = newUrlPath;
}
}
history.pushState = new Proxy(history.pushState, {
apply: (target, thisArg, argList) => {
checkUrlPath(argList[2]);
return target.apply(thisArg, argList);
},
});
checkUrlPath(location.href);
})(window.history);