-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
161 lines (154 loc) · 4.45 KB
/
lib.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
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
const { Octokit } = require('@octokit/rest');
const github = require('@actions/github');
exports.approveAndMerge = async function (argv) {
const ruleId = await getBranchProtectionRuleForAlpha(argv);
if (!ruleId) {
console.error('empty ruleId for alpha!');
return;
}
const tag = await isBatchPullRequestTag(argv);
if (!tag) {
console.log('Not labeled batch_upgrade_alpha!');
return;
}
await branchProtection(argv, false, ruleId);
await merge(argv);
await branchProtection(argv, true, ruleId);
};
const isBatchPullRequestTag = async function (argv) {
const octokit = new Octokit({
auth: argv.token,
});
try {
console.log('owner', argv.owner, 'repo', argv.repo, 'pullRequest', argv.pullRequestNumber);
const pullRequestDetail = await octokit.request(
`GET /repos/kungfu-trader/${argv.repo}/pulls/${argv.pullRequestNumber}`,
{
owner: 'kungfu-trader',
repo: argv.repo,
pull_number: argv.pullRequestNumber,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
},
);
console.log('labels:', pullRequestDetail.data.labels);
for (const label of pullRequestDetail.data.labels) {
if (label.name == 'batch_upgrade_alpha') {
return true;
}
}
} catch (e) {
console.error(e);
}
return false;
};
const updateBranch = async function (argv) {
const octokit = new Octokit({
auth: argv.token,
});
try {
console.log('owner', argv.owner, 'repo', argv.repo, 'pullRequest', argv.pullRequestNumber);
const up = await octokit.request(
`PUT /repos/kungfu-trader/${argv.repo}/pulls/${argv.pullRequestNumber}/update-branch`,
{
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
},
);
console.log('updateBranch ret:', up);
} catch (e) {
console.error(e);
}
};
async function getBranchProtectionRuleForAlpha(argv) {
let ruleId = '';
try {
const octokit = github.getOctokit(argv.token);
const rulesQuery = await octokit.graphql(`
query {
repository(name: "${argv.repo}", owner: "kungfu-trader") {
branchProtectionRules(first:100) {
nodes {
id
pattern
}
}
}
}`);
for (const rule of rulesQuery.repository.branchProtectionRules.nodes) {
if (rule.pattern == 'alpha/*/*') {
ruleId = rule.id;
}
}
} catch (e) {
console.error(e);
}
return ruleId;
}
const branchProtection = async function (argv, isProtect, ruleId) {
const octokit = github.getOctokit(argv.token);
const statusCheckContexts = '["verify"]';
const mutation = `
mutation {
updateBranchProtectionRule(input: {
branchProtectionRuleId: "${ruleId}"
requiresApprovingReviews: ${isProtect},
requiredApprovingReviewCount: 1,
dismissesStaleReviews: ${isProtect},
restrictsReviewDismissals: ${isProtect},
requiresStatusChecks: ${isProtect},
requiresCodeOwnerReviews: false,
requiredStatusCheckContexts: ${statusCheckContexts},
requiresStrictStatusChecks: ${isProtect},
requiresConversationResolution: ${isProtect},
isAdminEnforced: true,
restrictsPushes: false,
allowsForcePushes: false,
allowsDeletions: false
}) { clientMutationId }
}
`;
await octokit.graphql(mutation);
};
const approve = async function (argv) {
const octokit = new Octokit({
auth: argv.token,
});
try {
const ret = await octokit.request(
`POST /repos/${argv.owner}/${argv.repo}/pulls/${argv.pullRequestNumber}/reviews`,
{
owner: argv.owner,
repo: argv.repo,
pull_number: argv.pullRequestNumber,
event: 'APPROVE',
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
},
);
console.log('approve', ret);
} catch (e) {
console.error(e);
}
};
const merge = async function (argv) {
const octokit = new Octokit({
auth: argv.token,
});
try {
const ret = await octokit.request(`PUT /repos/kungfu-trader/${argv.repo}/pulls/${argv.pullRequestNumber}/merge`, {
owner: 'kungfu-trader',
repo: argv.repo,
pull_number: 'PULL_NUMBER',
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
});
console.log('pull request', argv.pullRequestNumber, 'merge success!');
} catch (e) {
console.error(e);
}
};