From 8b5f2093afd9e78aa82c16150e8b3c2704296513 Mon Sep 17 00:00:00 2001 From: Daniel Moran Date: Wed, 14 May 2025 13:45:45 -0700 Subject: [PATCH 1/2] Check if existing comment contains "Squawk Report" header. Closes #476 When searching for an existing comment to overwrite, check the body of each comment to see if it includes the expected header. This should prevent Squawk from accidentally overwriting comments generated by other GHAs. --- crates/squawk/src/github.rs | 5 ++++- crates/squawk_github/src/lib.rs | 10 ++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/crates/squawk/src/github.rs b/crates/squawk/src/github.rs index 8683bfd2..2a9bf699 100644 --- a/crates/squawk/src/github.rs +++ b/crates/squawk/src/github.rs @@ -66,6 +66,8 @@ fn create_gh_app( ) } +const COMMENT_HEADER: &str = "# Squawk Report"; + pub fn check_and_comment_on_pr( cmd: Command, cfg: &Config, @@ -130,6 +132,7 @@ pub fn check_and_comment_on_pr( &github_repo_name, github_pr_number, &comment_body, + COMMENT_HEADER, )?; let violations: usize = file_results.iter().map(|f| f.violations.len()).sum(); @@ -149,7 +152,7 @@ fn get_comment_body(files: &[CheckReport], version: &str) -> String { format!( r" -# Squawk Report +{COMMENT_HEADER} ### **{violations_emoji} {violation_count}** violations across **{file_count}** file(s) diff --git a/crates/squawk_github/src/lib.rs b/crates/squawk_github/src/lib.rs index d2cfec8b..9a7ad87b 100644 --- a/crates/squawk_github/src/lib.rs +++ b/crates/squawk_github/src/lib.rs @@ -69,16 +69,18 @@ pub fn comment_on_pr( repo: &str, issue: i64, body: &str, + existing_comment_text_includes: &str, ) -> Result<(), GithubError> { let comments = gh.list_issue_comments(owner, repo, issue)?; let bot_name = gh.app_slug(); info!("checking for existing comment"); - match comments - .iter() - .find(|x| x.user.r#type == "Bot" && x.user.login == bot_name) - { + match comments.iter().find(|x| { + x.user.r#type == "Bot" + && x.user.login == bot_name + && x.body.contains(existing_comment_text_includes) + }) { Some(prev_comment) => { info!("updating comment"); gh.update_issue_comment(owner, repo, prev_comment.id, body) From 8ac464205dd9a6780ee293da279f8bdaa7768764 Mon Sep 17 00:00:00 2001 From: Daniel Moran Date: Wed, 14 May 2025 16:28:29 -0700 Subject: [PATCH 2/2] Add comment --- crates/squawk_github/src/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/squawk_github/src/lib.rs b/crates/squawk_github/src/lib.rs index 9a7ad87b..e5520f98 100644 --- a/crates/squawk_github/src/lib.rs +++ b/crates/squawk_github/src/lib.rs @@ -79,6 +79,12 @@ pub fn comment_on_pr( match comments.iter().find(|x| { x.user.r#type == "Bot" && x.user.login == bot_name + // NOTE: We filter comments by their contents so we don't accidentally + // overwrite a comment made by some other tool. This happens often in + // GitHub repos that reuse the default GHA bot for all linters. + // + // This only works if `existing_comment_text_includes` is a "stable" + // piece of text included in all comments made by squawk! && x.body.contains(existing_comment_text_includes) }) { Some(prev_comment) => {