-
Notifications
You must be signed in to change notification settings - Fork 3
/
autosquash_please.js
100 lines (84 loc) · 3.45 KB
/
autosquash_please.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const loadOptions = () => {
return Promise.all([
browser.storage.local.get('github_token'),
browser.storage.local.get('ghe_domain'),
browser.storage.local.get('ghe_token'),
]).then((results) => {
return {
GitHubToken: results[0].github_token || '',
GHEDomain: results[1].ghe_domain || '',
GHEToken: results[2].ghe_token || '',
};
});
};
const buildURL = (repositoryInfo) => {
return (repositoryInfo.Host == 'api.github.com')
? `https://${repositoryInfo.Host}/repos/${repositoryInfo.User}/${repositoryInfo.Repository}/pulls/${repositoryInfo.PullRequestNumber}/commits`
: `https://${repositoryInfo.Host}/api/v3/repos/${repositoryInfo.User}/${repositoryInfo.Repository}/pulls/${repositoryInfo.PullRequestNumber}/commits`;
};
const extractCommitMessages = (commits) => {
let messages = [];
commits.forEach((commit) => {
messages.push(commit.commit.message);
});
return messages;
};
const isFixupCommits = (message) => {
return message.match('^fixup!');
};
const isGitHubDotCom = () => {
return location.hostname === 'github.com';
};
const setRequestHeader = (request, options) => {
if (options.GitHubToken !== '' && isGitHubDotCom()) {
console.log('GitHub');
request.setRequestHeader('Authorization', `token ${options.GitHubToken}`);
} else if (options.GHEToken !== '' && location.hostname === options.GHEDomain) {
console.log('GHE');
request.setRequestHeader('Authorization', `token ${options.GHEToken}`);
} else {
console.error('[autosquash-please] You should set API token.');
}
};
loadOptions().then((options) => {
let match = null;
if (isGitHubDotCom() || location.hostname === options.GHEDomain) {
match = location.pathname.match('^/(.+)/(.+)/pull/(.+)$');
}
if (match) {
const host = (isGitHubDotCom())
? 'api.github.com'
: options.GHEDomain;
const repositoryInfo = {
Host: host,
User: match[1],
Repository: match[2],
PullRequestNumber: match[3],
};
console.log('[autosquash-please] repository info');
console.log(repositoryInfo);
const request = new XMLHttpRequest();
request.open('GET', buildURL(repositoryInfo), true);
setRequestHeader(request, options);
request.addEventListener('load', (e) => {
const commitMessages = extractCommitMessages(JSON.parse(e.target.responseText));
console.log('[autosquash-please] commit messages');
console.log(commitMessages);
if (commitMessages.find(isFixupCommits)) {
console.log('[autosquash-please] A fixup! commit is found.');
let mergeDetails = document.getElementById('partial-pull-merging');
mergeDetails.innerHTML = '<p class="merge-pr-more-commits" style="font-weight: bold; font-size: 150%; color: red;">The autosquash-please addon prevents to push merge button.</p>';
} else {
console.log('[autosquash-please] No fixup! commits.');
}
});
request.addEventListener('error', (e) => {
console.error('[autosquash-please] An error on calling GitHub API.');
console.error(e);
});
request.send();
}
}).catch((e) => {
console.error('[autosquash-please] An unhandled error occured.');
console.error(e);
});