Skip to content

Commit e49a5a1

Browse files
committed
move behind_master into check-commits
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
1 parent 118bc9a commit e49a5a1

5 files changed

Lines changed: 86 additions & 164 deletions

File tree

src/config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub(crate) struct Config {
4949
#[serde(alias = "canonicalize-issue-links")]
5050
pub(crate) issue_links: Option<IssueLinksConfig>,
5151
pub(crate) no_mentions: Option<NoMentionsConfig>,
52-
pub(crate) pr_behind_commits: Option<PRBehindCommitsConfig>,
52+
pub(crate) commits_behind_master: Option<CommitsBehindMasterConfig>,
5353
}
5454

5555
#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
@@ -431,7 +431,7 @@ pub(crate) struct NoMentionsConfig {}
431431
/// Configuration for PR behind commits checks
432432
#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
433433
#[serde(deny_unknown_fields)]
434-
pub(crate) struct PRBehindCommitsConfig {
434+
pub(crate) struct CommitsBehindMasterConfig {
435435
/// The threshold of commits behind master to trigger a warning.
436436
/// Default is 100 if not specified.
437437
pub(crate) threshold: Option<u32>,
@@ -628,7 +628,7 @@ mod tests {
628628
}),
629629
issue_links: Some(IssueLinksConfig {}),
630630
no_mentions: Some(NoMentionsConfig {}),
631-
pr_behind_commits: None,
631+
commits_behind_master: None,
632632
}
633633
);
634634
}
@@ -696,7 +696,7 @@ mod tests {
696696
rendered_link: None,
697697
issue_links: Some(IssueLinksConfig {}),
698698
no_mentions: None,
699-
pr_behind_commits: None,
699+
commits_behind_master: None,
700700
}
701701
);
702702
}

src/handlers.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ mod note;
4343
mod notification;
4444
mod notify_zulip;
4545
mod ping;
46-
mod pr_behind_commits;
4746
pub mod pr_tracking;
4847
mod prioritize;
4948
pub mod project_goals;
@@ -186,21 +185,6 @@ pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
186185
);
187186
}
188187
}
189-
190-
if let Some(pr_behind_commits_config) = config
191-
.as_ref()
192-
.ok()
193-
.and_then(|c| c.pr_behind_commits.as_ref())
194-
{
195-
if let Err(e) = pr_behind_commits::handle(ctx, event, pr_behind_commits_config).await {
196-
log::error!(
197-
"failed to process event {:?} with pr_behind_commits handler: {:?}",
198-
event,
199-
e
200-
);
201-
}
202-
}
203-
204188
errors
205189
}
206190

src/handlers/check_commits.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
#[cfg(test)]
1111
use crate::github::GithubCommit;
1212

13+
mod behind_master;
1314
mod issue_links;
1415
mod modified_submodule;
1516
mod no_mentions;
@@ -72,6 +73,14 @@ pub(super) async fn handle(ctx: &Context, event: &Event, config: &Config) -> any
7273
warnings.extend(issue_links::issue_links_in_commits(issue_links, &commits));
7374
}
7475

76+
// Check if PR is behind master branch by a significant number of commits
77+
if let Some(commits_behind_master) = &config.commits_behind_master {
78+
let threshold = commits_behind_master.threshold.unwrap_or(behind_master::DEFAULT_BEHIND_THRESHOLD);
79+
if let Some(warning) = behind_master::behind_master(threshold, event, &ctx.github).await {
80+
warnings.push(warning);
81+
}
82+
}
83+
7584
handle_warnings(ctx, event, warnings).await
7685
}
7786

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use crate::github::{GithubClient, IssuesEvent};
2+
use tracing as log;
3+
4+
/// Default threshold for the number of commits behind master to trigger a warning
5+
pub const DEFAULT_BEHIND_THRESHOLD: u32 = 100;
6+
7+
/// Check if the PR is behind the main branch by a significant number of commits
8+
pub async fn behind_master(
9+
threshold: u32,
10+
event: &IssuesEvent,
11+
client: &GithubClient,
12+
) -> Option<String> {
13+
if !event.issue.is_pr() {
14+
return None;
15+
}
16+
17+
log::debug!("Checking if PR #{} is behind master", event.issue.number);
18+
19+
// Check how many commits the PR is behind master
20+
let behind_by = match event.issue.commits_behind_base(client).await {
21+
Ok(Some(count)) => count,
22+
Ok(None) => {
23+
log::warn!(
24+
"Unable to determine commits behind base for PR #{}",
25+
event.issue.number
26+
);
27+
return None;
28+
}
29+
Err(e) => {
30+
log::error!(
31+
"Error checking commits behind master for PR #{}: {}",
32+
event.issue.number,
33+
e
34+
);
35+
return None;
36+
}
37+
};
38+
39+
40+
// If PR is behind by at least the threshold, generate a warning
41+
if behind_by >= threshold {
42+
// Get repository information for the message
43+
let repo_info = match client.repository(&event.issue.repository().full_repo_name()).await {
44+
Ok(repo) => repo,
45+
Err(e) => {
46+
log::error!(
47+
"Error getting repository info for PR #{}: {}",
48+
event.issue.number,
49+
e
50+
);
51+
return None;
52+
}
53+
};
54+
55+
log::info!(
56+
"PR #{} is {} commits behind {} (threshold: {})",
57+
event.issue.number,
58+
behind_by,
59+
repo_info.default_branch,
60+
threshold
61+
);
62+
63+
return Some(format!(
64+
"This PR is {} commits behind the `{}` branch. \
65+
It's recommended to update your branch according to the \
66+
[Rustc Dev Guide](https://rustc-dev-guide.rust-lang.org/contributing.html#keeping-your-branch-up-to-date).",
67+
behind_by,
68+
repo_info.default_branch
69+
));
70+
}
71+
72+
None
73+
}

src/handlers/pr_behind_commits.rs

Lines changed: 0 additions & 144 deletions
This file was deleted.

0 commit comments

Comments
 (0)