Skip to content

Commit 1bbc04d

Browse files
refactor: use vec instead of Option<Vec>
1 parent 957bec4 commit 1bbc04d

File tree

3 files changed

+40
-41
lines changed

3 files changed

+40
-41
lines changed

src/bors/command/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub enum BorsCommand {
2323
/// Parent commit which should be used as the merge base.
2424
parent: Option<Parent>,
2525
/// The CI workflow to run.
26-
jobs: Option<Vec<String>>,
26+
jobs: Vec<String>,
2727
},
2828
/// Cancel a try build.
2929
TryCancel,

src/bors/command/parser.rs

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fn parser_try<'a>(command: &'a str, parts: &[CommandPart<'a>]) -> ParseResult<'a
140140
}
141141

142142
let mut parent = None;
143-
let mut jobs = None;
143+
let mut jobs = Vec::new();
144144

145145
for part in parts {
146146
match part {
@@ -165,18 +165,18 @@ fn parser_try<'a>(command: &'a str, parts: &[CommandPart<'a>]) -> ParseResult<'a
165165
"jobs" => {
166166
let raw_jobs: Vec<_> = value.split(',').map(|s| s.to_string()).collect();
167167
if raw_jobs.is_empty() {
168-
return Some(Err(CommandParseError::ValidationError(format!(
169-
"Try jobs must not be empty"
170-
))));
168+
return Some(Err(CommandParseError::ValidationError(
169+
"Try jobs must not be empty".to_string(),
170+
)));
171171
}
172172

173173
// rust ci currently allows specifying 10 jobs max
174174
if raw_jobs.len() > 10 {
175-
return Some(Err(CommandParseError::ValidationError(format!(
176-
"Try jobs must not have more than 10 jobs"
177-
))));
175+
return Some(Err(CommandParseError::ValidationError(
176+
"Try jobs must not have more than 10 jobs".to_string(),
177+
)));
178178
}
179-
jobs = Some(raw_jobs);
179+
jobs = raw_jobs;
180180
}
181181
_ => {
182182
return Some(Err(CommandParseError::UnknownArg(key)));
@@ -189,7 +189,7 @@ fn parser_try<'a>(command: &'a str, parts: &[CommandPart<'a>]) -> ParseResult<'a
189189

190190
/// Parses "@bors try cancel".
191191
fn parser_try_cancel<'a>(command: &'a str, parts: &[CommandPart<'a>]) -> ParseResult<'a> {
192-
if command == "try" && parts.get(0) == Some(&CommandPart::Bare("cancel")) {
192+
if command == "try" && parts.first() == Some(&CommandPart::Bare("cancel")) {
193193
Some(Ok(BorsCommand::TryCancel))
194194
} else {
195195
None
@@ -280,26 +280,28 @@ line two
280280
"#,
281281
);
282282
assert_eq!(cmds.len(), 1);
283-
assert!(matches!(
284-
cmds[0],
285-
Ok(BorsCommand::Try {
283+
insta::assert_debug_snapshot!(cmds[0], @r###"
284+
Ok(
285+
Try {
286286
parent: None,
287-
jobs: None
288-
})
289-
));
287+
jobs: [],
288+
},
289+
)
290+
"###);
290291
}
291292

292293
#[test]
293294
fn parse_try() {
294295
let cmds = parse_commands("@bors try");
295296
assert_eq!(cmds.len(), 1);
296-
assert!(matches!(
297-
cmds[0],
298-
Ok(BorsCommand::Try {
297+
insta::assert_debug_snapshot!(cmds[0], @r###"
298+
Ok(
299+
Try {
299300
parent: None,
300-
jobs: None
301-
})
302-
));
301+
jobs: [],
302+
},
303+
)
304+
"###);
303305
}
304306

305307
#[test]
@@ -312,7 +314,7 @@ line two
312314
parent: Some(Parent::CommitSha(CommitSha(
313315
"ea9c1b050cc8b420c2c211d2177811e564a4dc60".to_string()
314316
))),
315-
jobs: None
317+
jobs: Vec::new()
316318
})
317319
);
318320
}
@@ -325,7 +327,7 @@ line two
325327
cmds[0],
326328
Ok(BorsCommand::Try {
327329
parent: Some(Parent::Last),
328-
jobs: None
330+
jobs: Vec::new()
329331
})
330332
);
331333
}
@@ -351,7 +353,7 @@ line two
351353
cmds[0],
352354
Ok(BorsCommand::Try {
353355
parent: None,
354-
jobs: Some(vec!["ci".to_string(), "lint".to_string()])
356+
jobs: vec!["ci".to_string(), "lint".to_string()]
355357
})
356358
);
357359
}
@@ -405,13 +407,14 @@ line two
405407
"#,
406408
);
407409
assert_eq!(cmds.len(), 1);
408-
assert!(matches!(
409-
cmds[0],
410-
Ok(BorsCommand::Try {
410+
insta::assert_debug_snapshot!(cmds[0], @r###"
411+
Ok(
412+
Try {
411413
parent: None,
412-
jobs: None
413-
})
414-
));
414+
jobs: [],
415+
},
416+
)
417+
"###)
415418
}
416419

417420
#[test]

src/bors/handlers/trybuild.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub(super) async fn command_try_build<Client: RepositoryClient>(
3636
pr: &PullRequest,
3737
author: &GithubUser,
3838
parent: Option<Parent>,
39-
jobs: Option<Vec<String>>,
39+
jobs: Vec<String>,
4040
) -> anyhow::Result<()> {
4141
let repo = repo.as_ref();
4242
if !check_try_permissions(repo, pr, author).await? {
@@ -233,11 +233,7 @@ fn get_pending_build(pr: PullRequestModel) -> Option<BuildModel> {
233233
.and_then(|b| (b.status == BuildStatus::Pending).then_some(b))
234234
}
235235

236-
fn auto_merge_commit_message(
237-
pr: &PullRequest,
238-
reviewer: &str,
239-
jobs: Option<Vec<String>>,
240-
) -> String {
236+
fn auto_merge_commit_message(pr: &PullRequest, reviewer: &str, jobs: Vec<String>) -> String {
241237
let pr_number = pr.number;
242238
let mut message = format!(
243239
r#"Auto merge of #{pr_number} - {pr_label}, r={reviewer}
@@ -248,10 +244,10 @@ fn auto_merge_commit_message(
248244
pr_title = pr.title,
249245
pr_message = pr.message
250246
);
251-
if let Some(jobs) = jobs {
252-
for job in jobs {
253-
message.push_str(&format!("\nci-job: {}", job));
254-
}
247+
248+
// if jobs is empty, try-job won't be added to the message
249+
for job in jobs {
250+
message.push_str(&format!("\ntry-job: {}", job));
255251
}
256252
message
257253
}

0 commit comments

Comments
 (0)