-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpr-metadata-validator.rs
More file actions
359 lines (312 loc) · 12.4 KB
/
pr-metadata-validator.rs
File metadata and controls
359 lines (312 loc) · 12.4 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
use std::{collections::BTreeMap, process::exit};
use chrono::NaiveDate;
use indexmap::IndexMap;
use maplit::btreemap;
use octocrab::Octocrab;
use regex::Regex;
use trainee_tracker::{
Error,
config::{CourseSchedule, CourseScheduleWithRegisterSheetId},
course::{get_descriptor_id_for_pr, match_prs_to_assignments},
newtypes::Region,
octocrab::{all_pages, octocrab_for_token},
pr_comments::{PullRequest, close_existing_comments, leave_tagged_comment},
prs::get_prs,
};
const ARBITRARY_REGION: Region = Region(String::new());
#[tokio::main]
async fn main() {
let Ok([_argv0, pr_url]) = <[_; _]>::try_from(std::env::args().collect::<Vec<_>>()) else {
eprintln!("Expected one arg - PR URL");
exit(1);
};
let pr = PullRequest::from_html_url(&pr_url).expect("Failed to parse PR URL");
// TODO: Fetch this from classplanner or somewhere when we have access to a useful API.
let known_region_aliases = KnownRegions(btreemap! {
"Cape Town" => vec!["South Africa", "SouthAfrica", "ZA", "ZA Cape Town"],
"Glasgow" => vec!["Scotland"],
"London" => vec![],
"North West" => vec!["NW", "Manchester"],
"Sheffield" => vec![],
"West Midlands" => vec!["WM", "WestMidlands", "West-Midlands", "Birmingham"],
});
let github_token =
std::env::var("GH_TOKEN").expect("GH_TOKEN wasn't set - must be set to a GitHub API token");
let octocrab = octocrab_for_token(github_token).expect("Failed to get octocrab");
let course_schedule = make_fake_course_schedule(pr.repo.clone());
let course = CourseScheduleWithRegisterSheetId {
name: "itp".to_owned(),
register_sheet_id: "".to_owned(),
course_schedule,
};
let result = validate_pr(
&octocrab,
course,
&pr.repo,
&pr.org,
pr.number,
&known_region_aliases,
)
.await
.expect("Failed to validate PR");
const PR_METADATA_VALIDATOR_LABEL: &str = "pr-metadata-validator";
let message = match &result {
ValidationResult::Ok => {
if let Err(err) =
close_existing_comments(&octocrab, &pr, PR_METADATA_VALIDATOR_LABEL).await
{
eprintln!("Failed to close existing comments: {:?}", err);
}
exit(0);
}
ValidationResult::CouldNotMatch => COULD_NOT_MATCH_COMMENT,
ValidationResult::BodyTemplateNotFilledOut => BODY_TEMPLATE_NOT_FILLED_IN_COMMENT,
ValidationResult::BadTitleFormat { reason } => {
&format!("{}{}", BAD_TITLE_COMMENT_PREFIX, reason)
}
ValidationResult::UnknownRegion => UNKNOWN_REGION_COMMENT,
ValidationResult::WrongFiles {
expected_files_pattern,
} => &format!("{}`{}`", WRONG_FILES, expected_files_pattern),
ValidationResult::NoFiles => NO_FILES,
};
let full_message = format!(
"{message}\n\nIf this PR is not coursework, please add the NotCoursework label (and message on Slack in #cyf-curriculum or it will probably not be noticed).\n\nIf this PR needs reviewed, please add the 'Needs Review' label to this PR after you have resolved the issues listed above."
);
eprintln!("{}", full_message);
leave_tagged_comment(
&octocrab,
&pr,
&[PR_METADATA_VALIDATOR_LABEL, &result.to_string()],
full_message,
)
.await
.expect("Failed to create comment with validation error");
let remove_label_response = octocrab
.issues(&pr.org, &pr.repo)
.remove_label(pr.number, "Needs Review")
.await;
match remove_label_response {
Ok(_) => {
println!(
"Found issues for PR #{}, notified and removed label",
pr.number
);
}
Err(octocrab::Error::GitHub { source, .. }) if source.status_code == 404 => {
println!(
"Found issues for PR #{}, notified and label already removed",
pr.number
);
// The only time this API 404s is if the label is already removed. Continue without error.
}
err => {
eprintln!("Error removing label: {:?}", err);
}
};
exit(2);
}
const COULD_NOT_MATCH_COMMENT: &str = r#"Your PR couldn't be matched to an assignment in this module.
Please check its title is in the correct format, and that you only have one PR per assignment."#;
const BODY_TEMPLATE_NOT_FILLED_IN_COMMENT: &str = r#"Your PR description contained template fields which weren't filled in.
Check you've ticked everything in the self checklist, and that any sections which prompt you to fill in an answer are either filled in or removed."#;
const BAD_TITLE_COMMENT_PREFIX: &str = r#"Your PR's title isn't in the expected format.
Please check the expected title format, and update yours to match.
Reason: "#;
const UNKNOWN_REGION_COMMENT: &str = r#"Your PR's title didn't contain a known region.
Please check the expected title format, and make sure your region is in the correct place and spelled correctly."#;
const WRONG_FILES: &str = r#"The changed files in this PR don't match what is expected for this task.
Please check that you committed the right files for the task, and that there are no accidentally committed files from other sprints.
Please review the changed files tab at the top of the page, we are only expecting changes in this directory: "#;
const NO_FILES: &str = r#"This PR is missing any submitted files.
Please check that you committed the right files and pushed to the repository"#;
#[derive(strum_macros::Display)]
enum ValidationResult {
Ok,
BodyTemplateNotFilledOut,
CouldNotMatch,
BadTitleFormat { reason: String },
UnknownRegion,
WrongFiles { expected_files_pattern: String },
NoFiles,
}
async fn validate_pr(
octocrab: &Octocrab,
course_schedule: CourseScheduleWithRegisterSheetId,
module_name: &str,
github_org_name: &str,
pr_number: u64,
known_region_aliases: &KnownRegions,
) -> Result<ValidationResult, Error> {
let course = course_schedule
.with_assignments(octocrab, github_org_name)
.await
.map_err(|err| err.context("Failed to get assignments"))?;
let module_prs = get_prs(octocrab, github_org_name, module_name, false)
.await
.map_err(|err| err.context("Failed to get PRs"))?;
let pr_in_question = module_prs
.iter()
.find(|pr| pr.number == pr_number)
.ok_or_else(|| {
anyhow::anyhow!(
"Failed to find PR {} in list of PRs for module {}",
pr_number,
module_name
)
})?
.clone();
if pr_in_question.labels.contains("NotCoursework") {
return Ok(ValidationResult::Ok);
}
let user_prs: Vec<_> = module_prs
.into_iter()
.filter(|pr| pr.author == pr_in_question.author)
.collect();
let matched = match_prs_to_assignments(
&course.modules[module_name],
user_prs,
Vec::new(),
&ARBITRARY_REGION,
)
.map_err(|err| err.context("Failed to match PRs to assignments"))?;
for pr in matched.unknown_prs {
if pr.number == pr_number {
return Ok(ValidationResult::CouldNotMatch);
}
}
let title_sections: Vec<&str> = pr_in_question.title.split("|").collect();
if title_sections.len() != 5 {
return Ok(ValidationResult::BadTitleFormat {
reason: "Wrong number of parts separated by |s".to_owned(),
});
}
if !known_region_aliases.is_known_ignoring_case(title_sections[0].trim()) {
return Ok(ValidationResult::UnknownRegion);
}
// TODO: Validate cohorts when they're known (1)
let sprint_regex = Regex::new(r"^(S|s)print \d+$").unwrap();
let sprint_section = title_sections[3].trim();
if !sprint_regex.is_match(sprint_section) {
return Ok(ValidationResult::BadTitleFormat {
reason: format!(
"Sprint part ({}) doesn't match expected format (example: 'Sprint 2', without quotes)",
sprint_section
),
});
}
if pr_in_question.title.to_ascii_uppercase() == pr_in_question.title {
return Ok(ValidationResult::BadTitleFormat {
reason: "PR title should not all be in uppercase".to_owned(),
});
}
if pr_in_question.body.contains("Briefly explain your PR.")
|| pr_in_question
.body
.contains("Ask any questions you have for your reviewer.")
|| pr_in_question.body.contains("- [ ]")
{
return Ok(ValidationResult::BodyTemplateNotFilledOut);
}
let pr_assignment_descriptor_id =
get_descriptor_id_for_pr(&matched.sprints, pr_number).expect("This PR does not exist");
// This should never error, as a PR by this point in code must have been matched
// with an assignment, and PR assignments must have an associated issue descriptor
check_pr_file_changes(
octocrab,
github_org_name,
module_name,
pr_number,
pr_assignment_descriptor_id,
)
.await
}
// Check the changed files in a pull request match what is expected for that sprint task
async fn check_pr_file_changes(
octocrab: &Octocrab,
org_name: &str,
module_name: &str,
pr_number: u64,
task_issue_number: u64,
) -> Result<ValidationResult, Error> {
// Get the Sprint Task's description of expected changes
let Ok(task_issue) = octocrab
.issues(org_name, module_name)
.get(task_issue_number)
.await
else {
return Ok(ValidationResult::CouldNotMatch); // Failed to find the right task
};
let task_issue_body = task_issue.body.unwrap_or_default();
let directory_description = Regex::new("CHANGE_DIR=(.+)\\n")
.map_err(|err| Error::UserFacing(format!("Known good regex failed to compile: {}", err)))?;
let Some(directory_regex_captures) = directory_description.captures(&task_issue_body) else {
return Ok(ValidationResult::Ok); // There is no match defined for this task, don't do any more checks
};
let directory_description_regex = directory_regex_captures
.get(1)
.expect("Regex capture failed to return string match")
.as_str(); // Only allows a single directory for now
let directory_matcher = Regex::new(directory_description_regex).map_err(|err| {
Error::UserFacing(format!(
"Failed to compile regex from {}, check the CHANGE_DIR declaration: {}",
task_issue.html_url, err
))
})?;
// Get all of the changed files
let pr_files = all_pages("changed files in pull request", octocrab, async || {
octocrab
.pulls(org_name, module_name)
.list_files(pr_number)
.await
})
.await?;
if pr_files.is_empty() {
return Ok(ValidationResult::NoFiles); // no files committed
}
// check each file and error if one is in unexpected place
for pr_file in pr_files {
if pr_file.filename == ".gitignore" {
continue; // always allow top-level gitignore changes
}
if !directory_matcher.is_match(&pr_file.filename) {
return Ok(ValidationResult::WrongFiles {
expected_files_pattern: directory_description_regex.to_string(),
});
}
}
Ok(ValidationResult::Ok)
}
struct KnownRegions(BTreeMap<&'static str, Vec<&'static str>>);
impl KnownRegions {
fn is_known_ignoring_case(&self, possible_region: &str) -> bool {
let possible_region_lower = possible_region.to_ascii_lowercase();
for (known_region, known_region_aliases) in &self.0 {
if known_region.to_ascii_lowercase() == possible_region_lower {
return true;
}
for known_region_alias in known_region_aliases {
if known_region_alias.to_ascii_lowercase() == possible_region_lower {
return true;
}
}
}
false
}
}
fn make_fake_course_schedule(module_name: String) -> CourseSchedule {
let fixed_date = NaiveDate::from_ymd_opt(2030, 1, 1).unwrap();
let mut sprints = IndexMap::new();
sprints.insert(
module_name,
std::iter::repeat_with(|| btreemap![ARBITRARY_REGION => fixed_date])
// 5 is the max number of sprints a module (currently) contains.
.take(5)
.collect(),
);
CourseSchedule {
start: fixed_date,
end: fixed_date,
sprints,
}
}