forked from outsideris/potential-conflicts-checker-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (50 loc) · 1.96 KB
/
index.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
const { inspect } = require('util');
const core = require('@actions/core');
const { GitHub, context } = require('@actions/github');
const signale = require('signale');
const pullRequests = require('./lib/pull-requests');
const leaveComment = require('./lib/comment');
const commentTpl = `This Pull Request may conflict if the Pull Requests below are merged first.\n\n`;
async function run() {
try {
if (context.eventName !== 'pull_request') {
throw new Error(`This action only work with pull_request event not ${context.eventName}`);
}
// This should be a token with access to your repository scoped in as a secret.
// The YML workflow will need to set myToken with the GitHub Secret Token
// myToken: ${{ secrets.GITHUB_TOKEN }}
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token#about-the-github_token-secret
const ghToken = core.getInput('ghToken');
const octokit = new GitHub(ghToken, {});
const conflictInfo = await pullRequests({ octokit });
signale.debug(inspect(conflictInfo, {depth:3}));
if (conflictInfo.conflictPrs.length > 0) {
// leave comment on current PR
const body = commentTpl +
conflictInfo.conflictPrs.map(c =>
`#${c.number}\nconflictable files: ${c.conflicts.map(f => `\`${f}\``).join(',')}`
).join('\n');
await leaveComment({
octokit,
pull_number: conflictInfo.pull_number,
body,
});
// leave comments on target PR
const promises = conflictInfo.conflictPrs.map(c => {
const body = commentTpl +
`#${conflictInfo.pull_number}\nconflictable files: ${c.conflicts.map(f => `\`${f}\``).join(',')}`;
return leaveComment({
octokit,
pull_number: c.number,
body,
});
});
await Promise.all(promises);
}
}
catch (error) {
signale.fatal(error);
core.setFailed(error.message);
}
}
run()