|
1 | 1 | use crate::{config::WarnNonDefaultBranchException, github::IssuesEvent};
|
2 | 2 |
|
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). |
15 | 5 | pub(super) fn non_default_branch(
|
16 | 6 | exceptions: &[WarnNonDefaultBranchException],
|
17 | 7 | event: &IssuesEvent,
|
18 | 8 | ) -> Option<String> {
|
19 | 9 | 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 |
21 | 12 | .iter()
|
22 | 13 | .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 | + )); |
29 | 26 | }
|
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!" |
34 | 43 | )
|
35 | 44 | }
|
0 commit comments