|
| 1 | +//! Purpose: When opening a PR, or pushing new changes, check for merge commits |
| 2 | +//! and notify the user of our no-merge policy. |
| 3 | +
|
| 4 | +use crate::{ |
| 5 | + config::NoMergesConfig, |
| 6 | + db::issue_data::IssueData, |
| 7 | + github::{IssuesAction, IssuesEvent}, |
| 8 | + handlers::Context, |
| 9 | +}; |
| 10 | +use anyhow::Context as _; |
| 11 | +use serde::{Deserialize, Serialize}; |
| 12 | +use std::collections::HashSet; |
| 13 | +use std::fmt::Write; |
| 14 | +use tracing as log; |
| 15 | + |
| 16 | +const NO_MERGES_KEY: &str = "no_merges"; |
| 17 | + |
| 18 | +pub(super) struct NoMergesInput { |
| 19 | + /// Hashes of merge commits in the pull request. |
| 20 | + merge_commits: HashSet<String>, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Debug, Default, Deserialize, Serialize)] |
| 24 | +struct NoMergesState { |
| 25 | + /// Hashes of merge commits that have already been mentioned by triagebot in a comment. |
| 26 | + mentioned_merge_commits: HashSet<String>, |
| 27 | +} |
| 28 | + |
| 29 | +pub(super) async fn parse_input( |
| 30 | + ctx: &Context, |
| 31 | + event: &IssuesEvent, |
| 32 | + config: Option<&NoMergesConfig>, |
| 33 | +) -> Result<Option<NoMergesInput>, String> { |
| 34 | + if !matches!( |
| 35 | + event.action, |
| 36 | + IssuesAction::Opened | IssuesAction::Synchronize | IssuesAction::ReadyForReview |
| 37 | + ) { |
| 38 | + return Ok(None); |
| 39 | + } |
| 40 | + |
| 41 | + // Require an empty configuration block to enable no-merges notifications. |
| 42 | + if config.is_none() { |
| 43 | + return Ok(None); |
| 44 | + } |
| 45 | + |
| 46 | + // Don't ping on rollups or draft PRs. |
| 47 | + if event.issue.title.starts_with("Rollup of") || event.issue.draft { |
| 48 | + return Ok(None); |
| 49 | + } |
| 50 | + |
| 51 | + let mut merge_commits = HashSet::new(); |
| 52 | + let commits = event |
| 53 | + .issue |
| 54 | + .commits(&ctx.github) |
| 55 | + .await |
| 56 | + .map_err(|e| { |
| 57 | + log::error!("failed to fetch commits: {:?}", e); |
| 58 | + }) |
| 59 | + .unwrap_or_default(); |
| 60 | + for commit in commits { |
| 61 | + if commit.parents.len() > 1 { |
| 62 | + merge_commits.insert(commit.sha.clone()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + let input = NoMergesInput { merge_commits }; |
| 67 | + Ok(if input.merge_commits.is_empty() { |
| 68 | + None |
| 69 | + } else { |
| 70 | + Some(input) |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +pub(super) async fn handle_input( |
| 75 | + ctx: &Context, |
| 76 | + _config: &NoMergesConfig, |
| 77 | + event: &IssuesEvent, |
| 78 | + input: NoMergesInput, |
| 79 | +) -> anyhow::Result<()> { |
| 80 | + let mut client = ctx.db.get().await; |
| 81 | + let mut state: IssueData<'_, NoMergesState> = |
| 82 | + IssueData::load(&mut client, &event.issue, NO_MERGES_KEY).await?; |
| 83 | + |
| 84 | + let since_last_posted = if state.data.mentioned_merge_commits.is_empty() { |
| 85 | + "" |
| 86 | + } else { |
| 87 | + " (since this message was last posted)" |
| 88 | + }; |
| 89 | + |
| 90 | + let mut should_send = false; |
| 91 | + let mut message = format!( |
| 92 | + " |
| 93 | + There are merge commits (commits with multiple parents) in your changes. We have a |
| 94 | + [no merge policy](https://rustc-dev-guide.rust-lang.org/git.html#no-merge-policy) so |
| 95 | + these commits will need to be removed for this pull request to be merged. |
| 96 | +
|
| 97 | + You can start a rebase with the following commands: |
| 98 | +
|
| 99 | + ```shell-session |
| 100 | + $ # rebase |
| 101 | + $ git rebase -i master |
| 102 | + $ # delete any merge commits in the editor that appears |
| 103 | + $ git push --force-with-lease |
| 104 | + ``` |
| 105 | +
|
| 106 | + The following commits are merge commits{since_last_posted}: |
| 107 | +
|
| 108 | + " |
| 109 | + ); |
| 110 | + for commit in &input.merge_commits { |
| 111 | + if state.data.mentioned_merge_commits.contains(commit) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + should_send = true; |
| 116 | + state.data.mentioned_merge_commits.insert((*commit).clone()); |
| 117 | + write!(message, "- {commit}").unwrap(); |
| 118 | + } |
| 119 | + |
| 120 | + if should_send { |
| 121 | + event |
| 122 | + .issue |
| 123 | + .post_comment(&ctx.github, &message) |
| 124 | + .await |
| 125 | + .context("failed to post no_merges comment")?; |
| 126 | + state.save().await?; |
| 127 | + } |
| 128 | + Ok(()) |
| 129 | +} |
0 commit comments