-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.js
125 lines (111 loc) · 5.06 KB
/
run.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const core = require('@actions/core');
const github = require('@actions/github');
const inputs = require('./inputs');
const review = require('./review');
const approval = require('./approval');
const label = require('./label');
const owners = require('./owners');
let extractLabelNames = function(labels) {
return labels.map(label => label.name);
};
let getConfig = function() {
const areaOwnershipFile = core.getInput('area_ownership_file');
console.log(`Parsing owners file ${areaOwnershipFile}`);
const {maintainers, areaReviewers, areaApprovers} = inputs.parseOwners(areaOwnershipFile);
console.log(`Maintainers:`, maintainers);
console.log(`Area reviewers:`, areaReviewers);
console.log(`Area approvers:`, areaApprovers);
const supportLabelRegex = core.getInput('support_label_regex');
let areaReviewersRegexList = [];
let areaApproversRegexList = [];
if (supportLabelRegex) {
areaReviewersRegexList = owners.buildRegexList(areaReviewers);
areaApproversRegexList = owners.buildRegexList(areaApprovers);
}
return {
minApprovingReviewsTotal: core.getInput('min_approving_reviews_total'),
minApprovingReviewsPerArea: core.getInput('min_approving_reviews_per_area'),
maintainers: maintainers,
areaReviewers: areaReviewers,
areaApprovers: areaApprovers,
areaReviewersRegexList: areaReviewersRegexList,
areaApproversRegexList: areaApproversRegexList,
failIfCannotBeMerged: core.getInput('fail_if_cannot_be_merged'),
labelOnSuccess: core.getInput('label_on_success'),
requireAreaLabel: core.getInput('require_area_label'),
succeedIfMaintainerApproves: core.getInput('succeed_if_maintainer_approves'),
requestReviewsFromMaintainersIfNeeded: core.getInput('request_reviews_from_maintainers_if_needed'),
ignoreIfNotLabelledWith: core.getInput('ignore_if_not_labelled_with'),
failIfNotEnoughAvailableApproversPerArea: core.getInput('require_enough_available_approvers_for_area'),
maintainersAreUniversalApprovers: core.getInput('maintainers_are_universal_approvers'),
};
};
async function run() {
try {
const token = core.getInput('token');
const config = getConfig();
const owner = github.context.repo.owner;
const repo = github.context.repo.repo;
// Get the JSON webhook payload for the event that triggered the workflow
const payload = JSON.stringify(github.context.payload, undefined, 2);
console.debug(`The event payload: ${payload}`);
const isPullRequestReview = 'review' in github.context.payload;
if (isPullRequestReview) {
console.log(`This was triggered by a pull_request_review event`);
}
const pullRequest = github.context.payload.pull_request;
const labels = extractLabelNames(pullRequest.labels);
if (pullRequest.draft) {
console.log(`Skipping PR because it is a draft`);
return;
}
if (config.ignoreIfNotLabelledWith != '' && !labels.includes(config.ignoreIfNotLabelledWith)) {
console.log(`Skipping PR because label ${config.ignoreIfNotLabelledWith} is not present`);
return;
}
const author = pullRequest.user.login;
const pullNumber = pullRequest.number;
if (!isPullRequestReview) {
const reviewersList = review.computeReviewers(
labels,
author,
config,
);
console.log(`Assigning reviewers:`, reviewersList);
await review.requestReviewers(owner, repo, pullNumber, token, reviewersList);
}
const approvals = await approval.getApprovals(owner, repo, pullNumber, token);
console.log(`Currrent approvals: ${approvals}`);
const canBeMerged = approval.canBeMerged(
labels,
author,
approvals,
config,
);
console.log(`Checking if PR can be merged: ${canBeMerged}`);
if (config.labelOnSuccess !== '') {
const hasLabel = labels.includes(config.labelOnSuccess);
if (canBeMerged) {
if (hasLabel) {
console.log(`PR already labelled with '${config.labelOnSuccess}'`);
} else {
console.log(`Labelling PR with '${config.labelOnSuccess}'`);
await label.addLabel(owner, repo, pullNumber, token, config.labelOnSuccess);
}
} else {
if (hasLabel) {
console.log(`Unlabelling PR with '${config.labelOnSuccess}'`);
await label.removeLabel(owner, repo, pullNumber, token, config.labelOnSuccess);
} else {
console.log(`PR is not labelled with '${config.labelOnSuccess}'`);
}
}
}
if (!canBeMerged && config.failIfCannotBeMerged) {
core.setFailed(`Not enough approving reviews for PR`);
}
} catch (error) {
core.setFailed(error.message);
}
}
module.exports = run;