Skip to content

Commit eff1f73

Browse files
authored
Check if existing comment contains "Squawk Report" header. (#477)
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.
1 parent c8d4163 commit eff1f73

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

crates/squawk/src/github.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ fn create_gh_app(
6666
)
6767
}
6868

69+
const COMMENT_HEADER: &str = "# Squawk Report";
70+
6971
pub fn check_and_comment_on_pr(
7072
cmd: Command,
7173
cfg: &Config,
@@ -130,6 +132,7 @@ pub fn check_and_comment_on_pr(
130132
&github_repo_name,
131133
github_pr_number,
132134
&comment_body,
135+
COMMENT_HEADER,
133136
)?;
134137

135138
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 {
149152

150153
format!(
151154
r"
152-
# Squawk Report
155+
{COMMENT_HEADER}
153156
154157
### **{violations_emoji} {violation_count}** violations across **{file_count}** file(s)
155158

crates/squawk_github/src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,24 @@ pub fn comment_on_pr(
6969
repo: &str,
7070
issue: i64,
7171
body: &str,
72+
existing_comment_text_includes: &str,
7273
) -> Result<(), GithubError> {
7374
let comments = gh.list_issue_comments(owner, repo, issue)?;
7475

7576
let bot_name = gh.app_slug();
7677

7778
info!("checking for existing comment");
78-
match comments
79-
.iter()
80-
.find(|x| x.user.r#type == "Bot" && x.user.login == bot_name)
81-
{
79+
match comments.iter().find(|x| {
80+
x.user.r#type == "Bot"
81+
&& x.user.login == bot_name
82+
// NOTE: We filter comments by their contents so we don't accidentally
83+
// overwrite a comment made by some other tool. This happens often in
84+
// GitHub repos that reuse the default GHA bot for all linters.
85+
//
86+
// This only works if `existing_comment_text_includes` is a "stable"
87+
// piece of text included in all comments made by squawk!
88+
&& x.body.contains(existing_comment_text_includes)
89+
}) {
8290
Some(prev_comment) => {
8391
info!("updating comment");
8492
gh.update_issue_comment(owner, repo, prev_comment.id, body)

0 commit comments

Comments
 (0)