Skip to content

Commit f3c4479

Browse files
authored
Merge pull request #1922 from Urgau/extract-out-of-assign
Extract non-default-branch and modifies-submodules warnings out of the `assign` handler
2 parents cf8e16e + 4c602da commit f3c4479

File tree

5 files changed

+122
-61
lines changed

5 files changed

+122
-61
lines changed

src/handlers.rs

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ impl fmt::Display for HandlerError {
2727
mod assign;
2828
mod autolabel;
2929
mod bot_pull_requests;
30+
mod check_commits;
3031
mod close;
3132
pub mod docs_update;
3233
mod github_releases;
@@ -71,6 +72,16 @@ pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
7172
handle_command(ctx, event, &config, body, &mut errors).await;
7273
}
7374

75+
if let Ok(config) = &config {
76+
if let Err(e) = check_commits::handle(ctx, event, &config).await {
77+
log::error!(
78+
"failed to process event {:?} with `check_commits` handler: {:?}",
79+
event,
80+
e
81+
);
82+
}
83+
}
84+
7485
if let Err(e) = project_goals::handle(ctx, event).await {
7586
log::error!(
7687
"failed to process event {:?} with `project_goals` handler: {:?}",

src/handlers/assign.rs

+1-61
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
//! the PR modifies.
2222
2323
use crate::{
24-
config::{AssignConfig, WarnNonDefaultBranchException},
24+
config::AssignConfig,
2525
github::{self, Event, FileDiff, Issue, IssuesAction, Selection},
2626
handlers::{Context, GithubClient, IssuesEvent},
2727
interactions::EditIssueBody,
@@ -74,18 +74,6 @@ const ON_VACATION_WARNING: &str = "{username} is on vacation.
7474
7575
Please choose another assignee.";
7676

77-
const NON_DEFAULT_BRANCH: &str =
78-
"Pull requests are usually filed against the {default} branch for this repo, \
79-
but this one is against {target}. \
80-
Please double check that you specified the right target!";
81-
82-
const NON_DEFAULT_BRANCH_EXCEPTION: &str =
83-
"Pull requests targetting the {default} branch are usually filed against the {default} \
84-
branch, but this one is against {target}. \
85-
Please double check that you specified the right target!";
86-
87-
const SUBMODULE_WARNING_MSG: &str = "These commits modify **submodules**.";
88-
8977
pub const SELF_ASSIGN_HAS_NO_CAPACITY: &str = "
9078
You have insufficient capacity to be assigned the pull request at this time. PR assignment has been reverted.
9179
@@ -218,20 +206,6 @@ pub(super) async fn handle_input(
218206
}
219207
}
220208

221-
// Compute some warning messages to post to new PRs.
222-
let mut warnings = Vec::new();
223-
if let Some(exceptions) = config.warn_non_default_branch.enabled_and_exceptions() {
224-
warnings.extend(non_default_branch(exceptions, event));
225-
}
226-
warnings.extend(modifies_submodule(diff));
227-
if !warnings.is_empty() {
228-
let warnings: Vec<_> = warnings
229-
.iter()
230-
.map(|warning| format!("* {warning}"))
231-
.collect();
232-
let warning = format!(":warning: **Warning** :warning:\n\n{}", warnings.join("\n"));
233-
event.issue.post_comment(&ctx.github, &warning).await?;
234-
};
235209
Ok(())
236210
}
237211

@@ -250,40 +224,6 @@ fn is_self_assign(assignee: &str, pr_author: &str) -> bool {
250224
assignee.to_lowercase() == pr_author.to_lowercase()
251225
}
252226

253-
/// Returns a message if the PR is opened against the non-default branch (or the exception branch
254-
/// if it's an exception).
255-
fn non_default_branch(
256-
exceptions: &[WarnNonDefaultBranchException],
257-
event: &IssuesEvent,
258-
) -> Option<String> {
259-
let target_branch = &event.issue.base.as_ref().unwrap().git_ref;
260-
let (default_branch, warn_msg) = exceptions
261-
.iter()
262-
.find(|e| event.issue.title.contains(&e.title))
263-
.map_or_else(
264-
|| (&event.repository.default_branch, NON_DEFAULT_BRANCH),
265-
|e| (&e.branch, NON_DEFAULT_BRANCH_EXCEPTION),
266-
);
267-
if target_branch == default_branch {
268-
return None;
269-
}
270-
Some(
271-
warn_msg
272-
.replace("{default}", default_branch)
273-
.replace("{target}", target_branch),
274-
)
275-
}
276-
277-
/// Returns a message if the PR modifies a git submodule.
278-
fn modifies_submodule(diff: &[FileDiff]) -> Option<String> {
279-
let re = regex::Regex::new(r"\+Subproject\scommit\s").unwrap();
280-
if diff.iter().any(|fd| re.is_match(&fd.diff)) {
281-
Some(SUBMODULE_WARNING_MSG.to_string())
282-
} else {
283-
None
284-
}
285-
}
286-
287227
/// Sets the assignee of a PR, alerting any errors.
288228
async fn set_assignee(issue: &Issue, github: &GithubClient, username: &str) {
289229
// Don't re-assign if already assigned, e.g. on comment edit

src/handlers/check_commits.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use anyhow::bail;
2+
3+
use super::Context;
4+
use crate::{
5+
config::Config,
6+
github::{Event, IssuesAction},
7+
};
8+
9+
mod modified_submodule;
10+
mod non_default_branch;
11+
12+
pub(super) async fn handle(ctx: &Context, event: &Event, config: &Config) -> anyhow::Result<()> {
13+
let Event::Issue(event) = event else {
14+
return Ok(());
15+
};
16+
17+
if !matches!(event.action, IssuesAction::Opened) || !event.issue.is_pr() {
18+
return Ok(());
19+
}
20+
21+
let Some(diff) = event.issue.diff(&ctx.github).await? else {
22+
bail!(
23+
"expected issue {} to be a PR, but the diff could not be determined",
24+
event.issue.number
25+
)
26+
};
27+
28+
let mut warnings = Vec::new();
29+
30+
if let Some(assign_config) = &config.assign {
31+
// For legacy reasons the non-default-branch and modifies-submodule warnings
32+
// are behind the `[assign]` config.
33+
34+
if let Some(exceptions) = assign_config
35+
.warn_non_default_branch
36+
.enabled_and_exceptions()
37+
{
38+
warnings.extend(non_default_branch::non_default_branch(exceptions, event));
39+
}
40+
warnings.extend(modified_submodule::modifies_submodule(diff));
41+
}
42+
43+
if !warnings.is_empty() {
44+
let warnings: Vec<_> = warnings
45+
.iter()
46+
.map(|warning| format!("* {warning}"))
47+
.collect();
48+
let warning = format!(":warning: **Warning** :warning:\n\n{}", warnings.join("\n"));
49+
event.issue.post_comment(&ctx.github, &warning).await?;
50+
};
51+
52+
Ok(())
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use crate::github::FileDiff;
2+
3+
const SUBMODULE_WARNING_MSG: &str = "These commits modify **submodules**.";
4+
5+
/// Returns a message if the PR modifies a git submodule.
6+
pub(super) fn modifies_submodule(diff: &[FileDiff]) -> Option<String> {
7+
let re = regex::Regex::new(r"\+Subproject\scommit\s").unwrap();
8+
if diff.iter().any(|fd| re.is_match(&fd.diff)) {
9+
Some(SUBMODULE_WARNING_MSG.to_string())
10+
} else {
11+
None
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use crate::{config::WarnNonDefaultBranchException, github::IssuesEvent};
2+
3+
/// Returns a message if the PR is opened against the non-default branch (or the
4+
/// exception branch if it's an exception).
5+
pub(super) fn non_default_branch(
6+
exceptions: &[WarnNonDefaultBranchException],
7+
event: &IssuesEvent,
8+
) -> Option<String> {
9+
let target_branch = &event.issue.base.as_ref().unwrap().git_ref;
10+
11+
if let Some(exception) = exceptions
12+
.iter()
13+
.find(|e| event.issue.title.contains(&e.title))
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+
));
26+
}
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!"
43+
)
44+
}

0 commit comments

Comments
 (0)