-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathgithub.js
More file actions
176 lines (139 loc) · 4.2 KB
/
Copy pathgithub.js
File metadata and controls
176 lines (139 loc) · 4.2 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict';
const core = require('@actions/core');
const fs = require('fs');
const github = require('@actions/github');
const partition = require('lodash/partition');
const yaml = require('yaml');
const { LOCAL_FILE_MISSING } = require('./constants');
class PullRequest {
// ref: https://developer.github.com/v3/pulls/#get-a-pull-request
constructor(pull_request_paylaod) {
// "ncc" doesn't yet support private class fields as of 29 Aug. 2020
// ref: https://github.com/vercel/ncc/issues/499
this._pull_request_paylaod = pull_request_paylaod;
}
get author() {
return this._pull_request_paylaod.user.login;
}
get title() {
return this._pull_request_paylaod.title;
}
get is_draft() {
return this._pull_request_paylaod.draft;
}
}
function get_pull_request() {
const context = get_context();
return new PullRequest(context.payload.pull_request);
}
async function fetch_config() {
const context = get_context();
const octokit = get_octokit();
const config_path = get_config_path();
const useLocal = get_use_local();
let content = '';
if (!useLocal) {
const { data: response_body } = await octokit.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: config_path,
ref: context.ref,
});
content = Buffer.from(response_body.content, response_body.encoding).toString();
} else {
try {
content = fs.readFileSync(config_path).toString();
if (!content) {
throw new Error();
}
} catch (error) {
core.debug(`Error when reading local file: ${error}`);
throw new Error(LOCAL_FILE_MISSING);
}
}
return yaml.parse(content);
}
async function fetch_changed_files() {
const context = get_context();
const octokit = get_octokit();
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) {
const context = get_context();
const octokit = get_octokit();
const [ teams_with_prefix, individuals ] = partition(reviewers, (reviewer) => reviewer.startsWith('team:'));
const teams = teams_with_prefix.map((team_with_prefix) => team_with_prefix.replace('team:', ''));
return octokit.pulls.requestReviewers({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
reviewers: individuals,
team_reviewers: teams,
});
}
// https://docs.github.com/en/rest/teams/members?apiVersion=2022-11-28#list-team-members
async function get_team_members(team) {
const context = get_context();
const octokit = get_octokit();
const org = core.getInput('org') || context.payload.repository.owner.login;
const { data } = await octokit.teams.listMembersInOrg({
org,
team_slug: team,
});
return data?.map((member) => member.login);
}
/* Private */
let context_cache;
let token_cache;
let config_path_cache;
let use_local_cache;
let octokit_cache;
function get_context() {
return context_cache || (context_cache = github.context);
}
function get_token() {
return token_cache || (token_cache = core.getInput('token'));
}
function get_config_path() {
return config_path_cache || (config_path_cache = core.getInput('config'));
}
function get_use_local() {
return use_local_cache ?? (use_local_cache = core.getInput('use_local') === 'true');
}
function get_octokit() {
if (octokit_cache) {
return octokit_cache;
}
const token = get_token();
return octokit_cache = github.getOctokit(token);
}
function clear_cache() {
context_cache = undefined;
token_cache = undefined;
config_path_cache = undefined;
octokit_cache = undefined;
}
module.exports = {
get_pull_request,
fetch_config,
fetch_changed_files,
assign_reviewers,
clear_cache,
get_team_members,
};