forked from microsoft/opensource.microsoft.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthorizationCheck.js
More file actions
63 lines (61 loc) · 2.48 KB
/
Copy pathauthorizationCheck.js
File metadata and controls
63 lines (61 loc) · 2.48 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
async function authorizationCheck({github, context}) {
const { payload, eventName } = context;
const { sender, action, pull_request, review, repository } = payload;
const { login } = sender;
console.log(`eventName=${eventName}, action=${action}, sender=${login}`);
if (pull_request) {
console.log(`pull_request ${pull_request.number}: state=${pull_request.state}, draft=${pull_request.draft}, web=${pull_request.html_url}`);
}
if (review) {
console.log(`review: state=${review.state}`);
}
const commentTrigger = '/build';
let authorized = false;
if (pull_request && !pull_request.draft && action == 'created') {
console.log('The pull request has been created.');
// Authorized to automatically build if this is a repo writer
authorized = await hasWritePermissions({ github, repository, login });
} else if (review && pull_request && !pull_request.draft && pull_request.state === 'open') {
const { body, state, commit_id } = review;
console.log(`This is a pull request review. action=${action}, state=${state}, commitId=${commit_id}`);
if (body) {
console.log('Body:');
console.log(body);
}
if (action === 'submitted' && state === 'approved') {
console.log('This pull request review submits an approval');
authorized = await hasWritePermissions({ github, repository, login });
} else if (body && body.includes(commentTrigger)) {
console.log(`Body contains the trigger phrase ${commentTrigger}`);
authorized = await hasWritePermissions({ github, repository, login });
}
}
return { authorized };
}
async function hasWritePermissions({ github, repository, login }) {
const response = await github.repos.getCollaboratorPermissionLevel({
owner: repository.owner.login,
repo: repository.name,
username: login,
});
let permission = null;
let authorizedWriter = false;
if (response && response.data && response.data.permission) {
permission = response.data.permission;
}
if (permission) {
switch (permission) {
case 'admin':
case 'write':
authorizedWriter = true;
console.log(`User is authorized to contribute to ${repository.full_name}`);
break;
default:
break;
}
} else {
console.log('Permission level for the user could not be retrieved.');
}
return authorizedWriter;
}
module.exports = authorizationCheck;