Skip to content

Commit 808c8c2

Browse files
committed
Refactor non-default-branch to use functions with placeholders
1 parent 73ea2be commit 808c8c2

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
11
use crate::{config::WarnNonDefaultBranchException, github::IssuesEvent};
22

3-
const NON_DEFAULT_BRANCH: &str =
4-
"Pull requests are usually filed against the {default} branch for this repo, \
5-
but this one is against {target}. \
6-
Please double check that you specified the right target!";
7-
8-
const NON_DEFAULT_BRANCH_EXCEPTION: &str =
9-
"Pull requests targetting the {default} branch are usually filed against the {default} \
10-
branch, but this one is against {target}. \
11-
Please double check that you specified the right target!";
12-
13-
/// Returns a message if the PR is opened against the non-default branch (or the exception branch
14-
/// if it's an exception).
3+
/// Returns a message if the PR is opened against the non-default branch (or the
4+
/// exception branch if it's an exception).
155
pub(super) fn non_default_branch(
166
exceptions: &[WarnNonDefaultBranchException],
177
event: &IssuesEvent,
188
) -> Option<String> {
199
let target_branch = &event.issue.base.as_ref().unwrap().git_ref;
20-
let (default_branch, warn_msg) = exceptions
10+
11+
if let Some(exception) = exceptions
2112
.iter()
2213
.find(|e| event.issue.title.contains(&e.title))
23-
.map_or_else(
24-
|| (&event.repository.default_branch, NON_DEFAULT_BRANCH),
25-
|e| (&e.branch, NON_DEFAULT_BRANCH_EXCEPTION),
26-
);
27-
if target_branch == default_branch {
28-
return None;
14+
{
15+
if &exception.branch != target_branch {
16+
return Some(not_default_exception_branch_warn(
17+
&exception.branch,
18+
target_branch,
19+
));
20+
}
21+
} else if &event.repository.default_branch != target_branch {
22+
return Some(not_default_branch_warn(
23+
&event.repository.default_branch,
24+
target_branch,
25+
));
2926
}
30-
Some(
31-
warn_msg
32-
.replace("{default}", default_branch)
33-
.replace("{target}", target_branch),
27+
None
28+
}
29+
30+
fn not_default_branch_warn(default_: &str, target: &str) -> String {
31+
format!(
32+
"Pull requests are usually filed against the {default_} branch for this repo, \
33+
but this one is against {target}. \
34+
Please double check that you specified the right target!"
35+
)
36+
}
37+
38+
fn not_default_exception_branch_warn(default_: &str, target: &str) -> String {
39+
format!(
40+
"Pull requests targetting the {default_} branch are usually filed against the {default_} \
41+
branch, but this one is against {target}. \
42+
Please double check that you specified the right target!"
3443
)
3544
}

0 commit comments

Comments
 (0)