-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.js
107 lines (82 loc) · 3.14 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
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
'use strict';
const core = require('@actions/core');
const github = require('@actions/github');
const yaml = require('yaml');
const fetch_other_group_members = require('./fetch_other_group_members');
const identify_reviewers = require('./identify_reviewers');
const should_request_review = require('./should_request_review');
const context = github.context;
const token = core.getInput('token');
const config_path = core.getInput('config');
const octokit = github.getOctokit(token);
async function run() {
core.info('Fetching configuration file from the base branch');
let config;
try {
config = await fetch_config();
} catch (error) {
if (error.status === 404) {
core.warning('No configuration file is found in the base branch; terminating the process');
return;
}
throw error;
}
const title = context.payload.pull_request.title;
const is_draft = context.payload.pull_request.draft;
if (!should_request_review({ title, is_draft, config })) {
core.info('Matched the ignoring rules; terminating the process');
return;
}
core.info('Fetching changed files in the pull request');
const changed_files = await fetch_changed_files();
core.info('Identifying reviewers based on the changed files and the configuration');
const author = context.payload.pull_request.user.login;
const reviewers_based_on_files = identify_reviewers({ config, changed_files, excludes: [ author ] });
core.info('Adding other group membres to reviwers if group assignment feature is on');
const reviwers_from_same_teams = fetch_other_group_members({ config, author });
const reviewers = [ ...new Set([ ...reviewers_based_on_files, ...reviwers_from_same_teams ]) ];
if (reviewers.length === 0) {
core.info('Matched no reviweres; terminating the process');
return;
}
core.info(`Requesting review to ${reviewers.join(', ')}`);
await assign_reviewers(reviewers);
}
async function fetch_config() {
const { data: response_body } = await octokit.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: config_path,
ref: context.payload.pull_request.base.ref, // base branch name the branch is going into
});
const content = Buffer.from(response_body.content, response_body.encoding).toString();
return yaml.parse(content);
}
async function fetch_changed_files() {
const changed_files = [];
const per_page = 100;
let page = 0;
let number_of_files_in_current_page;
do {
page += 1;
const { data: response_body } = await octokit.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
page,
per_page,
});
number_of_files_in_current_page = response_body.length;
changed_files.push(...response_body.map((file) => file.filename));
} while (number_of_files_in_current_page === per_page);
return changed_files;
}
async function assign_reviewers(reviewers) {
return octokit.pulls.requestReviewers({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
reviewers,
});
}
run().catch((error) => core.setFailed(error));