generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.ts
163 lines (146 loc) · 4.85 KB
/
main.ts
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as core from '@actions/core';
import * as github from '@actions/github';
// eslint-disable-next-line require-jsdoc
export async function run() {
try {
const repoToken = core.getInput('repo-token', {required: true});
const issue: { owner: string; repo: string; number: number } =
github.context.issue;
core.setSecret(repoToken);
const pickOneFromPersonsOrTeam = core.getBooleanInput(
'pick-one-from-persons-or-team',
{required: false},
);
if (issue == null || issue.number == null) {
console.log('No pull request context, skipping');
return;
}
// See https://octokit.github.io/rest.js/
const client = github.getOctokit(repoToken);
const includeDraft = core.getBooleanInput('include-draft', {
required: false,
});
const pull = await client.rest.pulls.get({
owner: issue.owner,
repo: issue.repo,
pull_number: issue.number,
});
// Skip DRAFT PRs
if (pull.data.draft && !includeDraft) {
console.log('Skipped: DRAFT Pull Request, not assigning PR.');
return;
}
const skipWithNumberOfReviewers: number = Number(
core.getInput('skip-with-manual-reviewers') || Number.MAX_VALUE,
);
const numberOfReviewers = pull.data.requested_reviewers?.length || 0;
if (numberOfReviewers >= skipWithNumberOfReviewers) {
console.log(
'Skipped: Already ' +
numberOfReviewers +
' assigned reviewers, not assigning PR.',
);
return;
}
const teams = core
.getInput('teams')
.split(',')
.map((a) => a.trim());
const persons = core
.getInput('persons')
.split(',')
// filter out PR creator
.filter((user) => user !== issue.owner)
.map((a) => a.trim());
if (teams.length == 0 && persons.length == 0) {
core.setFailed('Please specify \'teams\' and/or \'persons\'');
return;
}
if (persons.length > 0) {
console.log('Picking from: ' + persons);
const reviewers = pickOneFromPersonsOrTeam ?
[persons[Math.floor(Math.random() * persons.length)]] :
persons;
console.log('Adding person(s): ' + reviewers);
const personResponse = await client.rest.pulls.requestReviewers({
owner: issue.owner,
repo: issue.repo,
pull_number: issue.number,
reviewers: reviewers,
});
console.log(
'Request Status:' +
personResponse.status +
', Persons: ' +
personResponse?.data?.requested_reviewers
?.map((r) => r.login)
.join(','),
);
}
// Making sure that org is provided
// if user turns on pick-one-from-persons-or-team
// option and to use teams
const org: string = core.getInput('org');
if (pickOneFromPersonsOrTeam && teams.length > 0 && org == null) {
core.setFailed(
'Please specify \'org\' if you want to ' +
'pick one from persons or teams and use Teams',
);
return;
}
if (teams.length > 0) {
if (pickOneFromPersonsOrTeam) {
// Picking out 1 person from first team listed
console.log('Selecting from first team provided: ' + teams[0]);
const members = await client.rest.teams.listMembersInOrg({
org: org,
team_slug: teams[0],
});
console.log(
'Request Status for getting team members:' + members.status,
);
// filter out PR author
const eligibleMembers = members.data
.filter((user) => user.login !== pull.data.user?.login)
.map((a) => a.login);
console.log('Picking reviewer from eligible members:', eligibleMembers);
const person = [
eligibleMembers[Math.floor(Math.random() * eligibleMembers.length)],
];
const personResponse = await client.rest.pulls.requestReviewers({
owner: issue.owner,
repo: issue.repo,
pull_number: issue.number,
reviewers: person,
});
console.log(
'Request Status:' +
personResponse.status +
', Person from First Team: ' +
personResponse?.data?.requested_reviewers
?.map((r) => r.login)
.join(','),
);
} else {
console.log('Adding teams: ' + teams);
const teamResponse = await client.rest.pulls.requestReviewers({
owner: issue.owner,
repo: issue.repo,
pull_number: issue.number,
team_reviewers: teams,
});
console.log(
'Request Status:' +
teamResponse.status +
', Teams: ' +
teamResponse?.data?.requested_teams?.map((t) => t.slug).join(','),
);
}
}
} catch (error) {
console.error(error);
core.setFailed('Unknown error' + error);
throw error;
}
}
run();