-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
246 lines (219 loc) · 8.58 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const core = require('@actions/core');
const github = require('@actions/github');
const child_process = require('child_process');
const process = require('process');
// we will use 'git log --pretty=%B######' to show logs,
// and using special string '########' to seperate revisions.
const rev_separator = "#".repeat(27);
var known_tags = ['Misc', 'GC', 'MultiTenant', 'JWarmUp', 'RAS', 'JIT', 'JFR', 'Lilliput', 'Merge', 'Backport', 'Coroutine', 'Wisp', 'EagerAppCDS', 'QuickStart', 'Landing', 'CDS', 'Revert', 'Runtime'];
function tag_of(title) {
return known_tags.find(tag => title.startsWith("[" + tag + "]"));
}
function tag_of_backport(title) {
return title.startsWith("[Backport]");
}
function check_rev_titile(title) {
if (tag_of(title) == undefined) {
console.log("Unkown tag:" + title + ", Candidates: " + known_tags);
return 1;
}
return 0;
}
// check comment of single revision, git metadata lines are not included
function check_rev_comment(lines) {
console.log(">> Full commit message:")
console.log(lines);
// Check if commit title is well formed
var title = lines[0];
console.log(">> Checking title line:" + title);
if (check_rev_titile(title) != 0) {
console.log(">> Title check failed");
core.setFailed("Title check failed:" + title);
return 1;
} else {
console.log(">> Title is OK!");
}
// Check if the mandatory fields are present and well formed
console.log(">> Checking mandatory fields!");
var mand_fields = [/^[ ]*Summary[ ]*:[ ]*[\\s\\S]*/, /^[ ]*Testing[ ]*:[ ]*[\\s\\S]*/, /^[ ]*Reviewers[ ]*:[ ]*\S+/, /^[ ]*Issue[ ]*:[ ]*\S+/];
for (let i = 0; i < mand_fields.length; i++) {
const mf = mand_fields[i];
if (!lines.some(line => mf.test(line))) {
console.log("Missing mandatory field: " + mf);
core.setFailed("Missing mandatory field '" + mf + "' in git log");
return 1;
}
}
console.log(">> All mandatory fields are present");
// If this is a backport commit, impose additional requirements
if (tag_of_backport(title)) {
console.log(">> Check backport commit with additional requirements");
if (!/\[Backport\] (\d+): (.+)/.test(title)) {
console.log(">> Backport commit title is not well formed");
return 1;
}
}
if (lines.find(l => l.includes("alibaba-inc.com")) != undefined) {
console.log(">> 'alibaba-inc' string should not appear in commit message");
return 1;
}
return 0;
}
// Run git command in sub-process, show the command string and output string;
function verbose_run(cmd_string) {
console.log("Executing command: " + cmd_string);
var out_buf = child_process.execSync(cmd_string, {maxBuffer: Infinity});
var out_str = out_buf.toString();
console.log("Output:\n\"\"\"\n" + out_str + "\"\"\"");
return out_str;
}
// parse output of 'git log --pretty=raw -l1'
function parse_raw_git_log(git_raw_log) {
var comments = [];
if (git_raw_log == null) {
return;
}
var arr = git_raw_log.split(rev_separator);
arr.forEach(function(s) {
if (s.length > 0) {
comments.push(s);
}
});
return comments;
};
// Check comments of last N revisions from 'ref'
function check_last_n_revisions(ref_name, nof_revs) {
console.log("checking last " + nof_revs + " revisions on " + ref_name);
// retrieve top N revisions, using ######## (8 * #) as separator
var out_str = verbose_run("git log --pretty=%B%n" + rev_separator + " -n" + nof_revs + " " + ref_name);
// parsed comments, each element is from one revision
var comments = parse_raw_git_log(out_str);
if (comments.length <= 0) {
core.setFailed("No revision comments is parsed from " + ref_name);
}
// check each revision comments
comments.forEach(com => {
var cur_comm = [];
com.split('\n').forEach(line => {
line = line.trim();
if (line.length > 0) {
cur_comm.push(line);
}
})
if (cur_comm.length > 0 && 0 != check_rev_comment(cur_comm)) {
const DRAGONWELL_COMMIT_MESSAGE_TEMPLATE = "https://github.com/dragonwell-project/dragonwell11/blob/master/.github/commit_message_template";
console.log("Please refer to dragonwell standard commit message template at " + DRAGONWELL_COMMIT_MESSAGE_TEMPLATE)
core.setFailed("Step check comment failed!")
}
});
}
function fetch_pull_request_to_local_branch(local_branch) {
if (github.context.payload.action.endsWith("opened")) {
// at creation of pull request
// extract source SHA from the first merge commit
var out = verbose_run("git log -1 --pretty=%B " + github.context.sha);
var hashes = [];
out.split("\n").forEach(l => {
l = l.trim();
if (l.startsWith("Merge")) {
l.split(" ").forEach(w => {
// means it is a hash string
if (w.length > 24) {
hashes.push(w);
}
});
}
});
// fetch the parsed source
if (hashes[0] == undefined) {
core.setFailed("Cannot parse the correct source commit HASH!");
}
verbose_run("git fetch origin " + hashes[0] + ":" + local_branch);
} else if (github.context.payload.action === "synchronize") {
// at modification of pull request
var remote_ref = github.context.payload.after;
verbose_run("git fetch origin " + remote_ref + ":" + local_branch);
} else {
core.setFailed("Unsupported github action:" + github.context.payload.action);
}
verbose_run("git checkout -f " + local_branch);
}
function could_contain_multiple_commits(tag) {
return 'Merge' === tag || 'Backport' === tag;
}
// check pull requests
function check_pull_requests() {
if (check_rev_titile(github.context.payload.pull_request.title) != 0) {
core.setFailed("Pull request title check failed!");
}
var tag = tag_of(github.context.payload.pull_request.title);
//if (!could_contain_multiple_commits(tag) && github.context.payload.pull_request.commits != 1) {
// core.setFailed("Each pull request should contain only ONE commit!");
//}
}
// check the format of specific rev
function check_patch(rev) {
var out = verbose_run("git show " + rev);
var ln = 0;
console.log("Checking patch format:\n");
out.split("\n").forEach(line => {
ln = ln + 1;
console.log(ln + ": " + line);
// only check for newly added lines
if (line.startsWith('+') && line.endsWith(" ")) {
console.log("\ntrailing spaces found!\n");
core.setFailed("Trailing spaces in line-" + ln + "\n" + line);
}
});
}
// help debugging
function show_envs() {
console.log("Environment variables:\n", process.env);
console.log("github.context:\n", github.context);
}
function skipCodeFormat(tag) {
return 'Merge' === tag || 'Backport' === tag;
}
// entry of checker action
function do_check() {
show_envs();
try {
// only trigger for pull_requests
if (github.context.eventName === 'pull_request') {
if (github.context.payload.pull_request.title.startsWith("Merge ")) {
return;
}
if (github.context.payload.pull_request.title.startsWith("Revert ")
&& github.context.payload.pull_request.commits == 1) {
return;
}
if ('Backport' === tag_of(github.context.payload.pull_request.title)
&& github.context.payload.pull_request.commits !== 1) {
// Skip backporting multiple commits (for Wisp)
return;
}
// using a unique name for local branch
var local_branch = "local_ref_branch_" + Date.now();
// run checking
check_pull_requests();
fetch_pull_request_to_local_branch(local_branch);
check_last_n_revisions(local_branch, 1);
var tag = tag_of(github.context.payload.pull_request.title);
if (!skipCodeFormat(tag)) {
check_patch(local_branch);
}
} else {
core.setFailed("Can only be triggered on pull_request, current event=" +
github.context.eventName)
}
} catch (error) {
core.setFailed(error.message);
}
}
// for local testing purpose
function local_testing() {
show_envs();
check_last_n_revisions('local_ref_branch1581603361965', 1);
}
// local_testing();
do_check();