generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoggle-protection.js
107 lines (94 loc) · 3.21 KB
/
toggle-protection.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
const { Octokit } = require('@octokit/rest');
const branchDoesNotHaveProtection = (errorObj) => {
const hasStatus = errorObj.status !== undefined;
return hasStatus && errorObj.toString() === 'HttpError: Branch not protected';
};
const transformOutputRules = (outputRules) => {
const result = {};
// result must be -> https://developer.github.com/v3/repos/branches/#update-branch-protection
// map simple boolean props from {prop: {enabled: false|true} to {prop: false | true}
const simpleBooleanProps = [
'allow_deletions',
'allow_force_pushes',
'required_linear_history',
'enforce_admins'
].forEach(booleanProp => {
if (booleanProp in outputRules) {
result[booleanProp] = outputRules[booleanProp].enabled;
}
});
// handle special object-based props
if ('required_status_checks' in outputRules) {
const { contexts_url, ...payload } = outputRules['required_status_checks'];
result['required_status_checks'] = { ...payload };
} else {
result['required_status_checks'] = null;
}
if ('required_pull_request_reviews' in outputRules) {
const { url, ...payload } = outputRules['required_pull_request_reviews'];
result['required_pull_request_reviews'] = { ...payload };
} else {
result['required_pull_request_reviews'] = null;
}
if ('restrictions' in outputRules) {
const { url, users_url, apps_url, ...payload } = outputRules['restrictions'];
result['restrictions'] = { ...payload };
} else {
result['restrictions'] = null;
}
return result;
};
const disableProtection = async function ({ owner, repo, branch, auth }) {
const octokit = new Octokit({
auth,
previews: ['luke-cage-preview']
});
// await currentProtection and then delete branchProtection
let branchProtectionRules = {};
try {
const branchProtectionResponse = await octokit.repos.getBranchProtection({
owner,
repo,
branch
});
branchProtectionRules = branchProtectionResponse.data;
// now delete rules
await octokit.repos.deleteBranchProtection({
owner,
repo,
branch
});
} catch (errorResponse) {
// be gracious about branches not actually being protected and rethrow
if (!branchDoesNotHaveProtection(errorResponse)) {
throw errorResponse;
}
}
// console.log(branchProtectionRules);
return { on: JSON.stringify(branchProtectionRules) };
};
const enableProtection = async function ({ owner, repo, branch, protectionRules, auth }) {
// console.log('Got initial rules: %o', protectionRules);
const transformedRules = transformOutputRules(protectionRules);
// console.log('real rules: %o', transformedRules);
const octokit = new Octokit({
auth,
previews: ['luke-cage-preview']
});
// set branch protection for passed rules
await octokit.repos.updateBranchProtection({
owner,
repo,
branch,
...transformedRules
});
return { nice: true };
};
const toggleProtection = async function ({ on, off, owner, repo, branch, GITHUB_TOKEN }) {
if (off) {
return disableProtection({ owner, repo, branch, auth: GITHUB_TOKEN });
} else {
return enableProtection({ owner, repo, branch, protectionRules: JSON.parse(on), auth: GITHUB_TOKEN });
}
};
module.exports = toggleProtection;