This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-pull-request.js
55 lines (44 loc) · 1.79 KB
/
merge-pull-request.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
const { EOL } = require('os');
const { $, $$, exit } = require('./helpers');
const { ensureHub } = require('./environment');
const { ensureSynchronized, getDefaultBranch } = require('./repo');
function isMergeVersionScript() {
return process.env.npm_lifecycle_event === 'mergeversion';
}
function mergePullRequest(prId, remote = "origin") {
if (typeof prId !== 'string' || prId.length === 0) {
exit(`you must specify a pull request number`);
}
ensureHub();
const separator = '|';
const rawResults = $(`hub pr list -f "%I${separator}%B${separator}%H${separator}%U${separator}%sH%n"`);
const results = rawResults.split(EOL).map(line => {
const [id, baseBranch, headBranch, url, sha] = line.split(separator);
return { id, baseBranch, headBranch, url, sha };
});
const details = results.find(detail => detail.id === prId);
if (details === undefined) {
exit(`${prId} - not an open pull request number`)
}
const defaultBranch = getDefaultBranch();
if (details.headBranch.startsWith('release') && details.baseBranch !== defaultBranch && !isMergeVersionScript()) {
exit('use `npm run mergeversion` to merge release branches');
}
// CI status must be successful
$$(`hub ci-status -v ${details.sha}`);
// switch to pull request target branch
$$(`hub checkout ${details.baseBranch}`);
// bring in any changes from upstream
$$(`hub sync`);
// ensure local and remote head branch are in sync (`hub sync` only warns)
ensureSynchronized();
// Merge similar to GitHub Merge Button
$$(`hub merge ${details.url}`);
// Push merge to target branch
$$(`hub push`);
// Delete remote head branch
$$(`hub push ${remote} :${details.headBranch}`);
// Bring in any more changes and remote local head branch
$$(`hub sync`);
}
module.exports = mergePullRequest;