-
Notifications
You must be signed in to change notification settings - Fork 90
Extract non-default-branch and modifies-submodules warnings out of the assign
handler
#1922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use anyhow::bail; | ||
Kobzol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
use super::Context; | ||
use crate::{ | ||
config::Config, | ||
github::{Event, IssuesAction}, | ||
}; | ||
|
||
mod modified_submodule; | ||
mod non_default_branch; | ||
|
||
pub(super) async fn handle(ctx: &Context, event: &Event, config: &Config) -> anyhow::Result<()> { | ||
let Event::Issue(event) = event else { | ||
return Ok(()); | ||
}; | ||
|
||
if !matches!(event.action, IssuesAction::Opened) || !event.issue.is_pr() { | ||
return Ok(()); | ||
} | ||
|
||
let Some(diff) = event.issue.diff(&ctx.github).await? else { | ||
bail!( | ||
"expected issue {} to be a PR, but the diff could not be determined", | ||
event.issue.number | ||
) | ||
}; | ||
|
||
let mut warnings = Vec::new(); | ||
|
||
if let Some(assign_config) = &config.assign { | ||
// For legacy reasons the non-default-branch and modifies-submodule warnings | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might want to eventually introduce a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, I think it make sense to have a |
||
// are behind the `[assign]` config. | ||
|
||
if let Some(exceptions) = assign_config | ||
.warn_non_default_branch | ||
.enabled_and_exceptions() | ||
{ | ||
warnings.extend(non_default_branch::non_default_branch(exceptions, event)); | ||
} | ||
warnings.extend(modified_submodule::modifies_submodule(diff)); | ||
} | ||
|
||
if !warnings.is_empty() { | ||
let warnings: Vec<_> = warnings | ||
.iter() | ||
.map(|warning| format!("* {warning}")) | ||
.collect(); | ||
let warning = format!(":warning: **Warning** :warning:\n\n{}", warnings.join("\n")); | ||
event.issue.post_comment(&ctx.github, &warning).await?; | ||
}; | ||
|
||
Ok(()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use crate::github::FileDiff; | ||
|
||
const SUBMODULE_WARNING_MSG: &str = "These commits modify **submodules**."; | ||
|
||
/// Returns a message if the PR modifies a git submodule. | ||
pub(super) fn modifies_submodule(diff: &[FileDiff]) -> Option<String> { | ||
let re = regex::Regex::new(r"\+Subproject\scommit\s").unwrap(); | ||
if diff.iter().any(|fd| re.is_match(&fd.diff)) { | ||
Some(SUBMODULE_WARNING_MSG.to_string()) | ||
} else { | ||
None | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use crate::{config::WarnNonDefaultBranchException, github::IssuesEvent}; | ||
|
||
/// Returns a message if the PR is opened against the non-default branch (or the | ||
/// exception branch if it's an exception). | ||
pub(super) fn non_default_branch( | ||
exceptions: &[WarnNonDefaultBranchException], | ||
event: &IssuesEvent, | ||
) -> Option<String> { | ||
let target_branch = &event.issue.base.as_ref().unwrap().git_ref; | ||
|
||
if let Some(exception) = exceptions | ||
.iter() | ||
.find(|e| event.issue.title.contains(&e.title)) | ||
{ | ||
if &exception.branch != target_branch { | ||
return Some(not_default_exception_branch_warn( | ||
&exception.branch, | ||
target_branch, | ||
)); | ||
} | ||
} else if &event.repository.default_branch != target_branch { | ||
return Some(not_default_branch_warn( | ||
&event.repository.default_branch, | ||
target_branch, | ||
)); | ||
} | ||
None | ||
} | ||
|
||
fn not_default_branch_warn(default: &str, target: &str) -> String { | ||
format!( | ||
"Pull requests are usually filed against the {default} branch for this repo, \ | ||
but this one is against {target}. \ | ||
Please double check that you specified the right target!" | ||
) | ||
} | ||
|
||
fn not_default_exception_branch_warn(default: &str, target: &str) -> String { | ||
format!( | ||
"Pull requests targetting the {default} branch are usually filed against the {default} \ | ||
branch, but this one is against {target}. \ | ||
Please double check that you specified the right target!" | ||
) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.