Skip to content

Commit 92c3830

Browse files
committed
WIP: move comment functionality to module
1 parent 78c37d6 commit 92c3830

File tree

2 files changed

+199
-80
lines changed

2 files changed

+199
-80
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//! Commenter module for clippy-annotation-reporter
2+
//!
3+
//! This module handles interactions with GitHub for commenting on PRs,
4+
//! including finding existing comments and updating or creating comments.
5+
6+
use anyhow::{Context as _, Result};
7+
use octocrab::models::issues::Comment;
8+
use octocrab::Octocrab;
9+
10+
/// Handles GitHub comment operations
11+
pub struct Commenter<'a> {
12+
octocrab: &'a Octocrab,
13+
owner: String,
14+
repo: String,
15+
pr_number: u64,
16+
signature: String,
17+
}
18+
19+
impl<'a> Commenter<'a> {
20+
/// Create a new commenter instance
21+
pub fn new(octocrab: &'a Octocrab, owner: &str, repo: &str, pr_number: u64) -> Self {
22+
Self {
23+
octocrab,
24+
owner: owner.to_string(),
25+
repo: repo.to_string(),
26+
pr_number,
27+
signature: "<!-- clippy-annotation-reporter-comment -->".to_string(),
28+
}
29+
}
30+
31+
/// Set a custom signature for identifying the bot's comments
32+
pub fn with_signature(mut self, signature: &str) -> Self {
33+
self.signature = signature.to_string();
34+
self
35+
}
36+
37+
/// Post or update a comment on the PR with the given report
38+
pub async fn run(&self, report: String) -> Result<()> {
39+
// Add the signature to the report
40+
let report_with_signature = format!("{}\n\n{}", report, self.signature);
41+
42+
// Search for existing comment by the bot
43+
println!("Checking for existing comment on PR #{}", self.pr_number);
44+
let existing_comment = self.find_existing_comment().await?;
45+
46+
// Update existing comment or create a new one
47+
if let Some(comment_id) = existing_comment {
48+
println!("Updating existing comment #{}", comment_id);
49+
self.octocrab
50+
.issues(&self.owner, &self.repo)
51+
.update_comment(comment_id.into(), report_with_signature)
52+
.await
53+
.context("Failed to update existing comment")?;
54+
println!("Comment updated successfully!");
55+
} else {
56+
println!("Creating new comment on PR #{}", self.pr_number);
57+
self.octocrab
58+
.issues(&self.owner, &self.repo)
59+
.create_comment(self.pr_number, report_with_signature)
60+
.await
61+
.context("Failed to post comment to PR")?;
62+
println!("Comment created successfully!");
63+
}
64+
65+
Ok(())
66+
}
67+
68+
/// Find existing comment by the bot on a PR
69+
async fn find_existing_comment(&self) -> Result<Option<u64>> {
70+
// Get all comments on the PR
71+
let mut page = self
72+
.octocrab
73+
.issues(&self.owner, &self.repo)
74+
.list_comments(self.pr_number)
75+
.per_page(100)
76+
.send()
77+
.await
78+
.context("Failed to list PR comments")?;
79+
80+
// Process current and subsequent pages
81+
loop {
82+
for comment in &page {
83+
if comment
84+
.body
85+
.as_ref()
86+
.map_or(false, |body| body.contains(&self.signature))
87+
{
88+
return Ok(Some(*comment.id));
89+
}
90+
}
91+
92+
// Try to get the next page if it exists
93+
match self.octocrab.get_page(&page.next).await {
94+
Ok(Some(next_page)) => {
95+
page = next_page;
96+
}
97+
Ok(None) => {
98+
// No more pages
99+
break;
100+
}
101+
Err(e) => {
102+
println!("Warning: Failed to fetch next page of comments: {}", e);
103+
break;
104+
}
105+
}
106+
}
107+
108+
// No matching comment found
109+
Ok(None)
110+
}
111+
}

.github/actions/clippy-annotation-reporter/src/main.rs

Lines changed: 88 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use anyhow::{Context as _, Result};
22
use octocrab::Octocrab;
33

44
mod analyzer;
5+
mod commenter;
56
mod config;
67
mod report_generator;
78

@@ -56,90 +57,97 @@ async fn main() -> Result<()> {
5657
&config.head_branch,
5758
);
5859

59-
// 4. Post the report as a comment or update existing comment
60+
// Post or update the report as a PR comment
61+
let commenter =
62+
commenter::Commenter::new(&octocrab, &config.owner, &config.repo, config.pr_number);
6063

61-
// Create a unique signature for the bot's comments
62-
let bot_signature = "<!-- clippy-annotation-reporter-comment -->";
63-
let report_with_signature = format!("{}\n\n{}", report, bot_signature);
64+
commenter.run(report).await?;
6465

65-
// Search for existing comment by the bot
66-
println!("Checking for existing comment on PR #{}", config.pr_number);
67-
let existing_comment = find_existing_comment(
68-
&octocrab,
69-
&config.owner,
70-
&config.repo,
71-
config.pr_number,
72-
bot_signature,
73-
)
74-
.await?;
66+
println!("Process completed successfully!");
7567

76-
// Update existing comment or create a new one
77-
if let Some(comment_id) = existing_comment {
78-
println!("Updating existing comment #{}", comment_id);
79-
octocrab
80-
.issues(&config.owner, &config.repo)
81-
.update_comment(comment_id.into(), report_with_signature)
82-
.await
83-
.context("Failed to update existing comment")?;
84-
println!("Comment updated successfully!");
85-
} else {
86-
println!("Creating new comment on PR #{}", config.pr_number);
87-
octocrab
88-
.issues(&config.owner, &config.repo)
89-
.create_comment(config.pr_number, report_with_signature)
90-
.await
91-
.context("Failed to post comment to PR")?;
92-
println!("Comment created successfully!");
93-
}
68+
// 4. Post the report as a comment or update existing comment
69+
70+
// Create a unique signature for the bot's comments
71+
// let bot_signature = "<!-- clippy-annotation-reporter-comment -->";
72+
// let report_with_signature = format!("{}\n\n{}", report, bot_signature);
73+
//
74+
// // Search for existing comment by the bot
75+
// println!("Checking for existing comment on PR #{}", config.pr_number);
76+
// let existing_comment = find_existing_comment(
77+
// &octocrab,
78+
// &config.owner,
79+
// &config.repo,
80+
// config.pr_number,
81+
// bot_signature,
82+
// )
83+
// .await?;
84+
//
85+
// // Update existing comment or create a new one
86+
// if let Some(comment_id) = existing_comment {
87+
// println!("Updating existing comment #{}", comment_id);
88+
// octocrab
89+
// .issues(&config.owner, &config.repo)
90+
// .update_comment(comment_id.into(), report_with_signature)
91+
// .await
92+
// .context("Failed to update existing comment")?;
93+
// println!("Comment updated successfully!");
94+
// } else {
95+
// println!("Creating new comment on PR #{}", config.pr_number);
96+
// octocrab
97+
// .issues(&config.owner, &config.repo)
98+
// .create_comment(config.pr_number, report_with_signature)
99+
// .await
100+
// .context("Failed to post comment to PR")?;
101+
// println!("Comment created successfully!");
102+
// }
94103

95104
Ok(())
96105
}
97106

98-
/// Find existing comment by the bot on a PR
99-
async fn find_existing_comment(
100-
octocrab: &Octocrab,
101-
owner: &str,
102-
repo: &str,
103-
pr_number: u64,
104-
signature: &str,
105-
) -> Result<Option<u64>> {
106-
// Get all comments on the PR
107-
let mut page = octocrab
108-
.issues(owner, repo)
109-
.list_comments(pr_number)
110-
.per_page(100)
111-
.send()
112-
.await
113-
.context("Failed to list PR comments")?;
114-
115-
// Process current and subsequent pages
116-
loop {
117-
for comment in &page {
118-
if comment
119-
.body
120-
.as_ref()
121-
.map_or(false, |body| body.contains(signature))
122-
{
123-
return Ok(Some(*comment.id));
124-
}
125-
}
126-
127-
// Try to get the next page if it exists
128-
match octocrab.get_page(&page.next).await {
129-
Ok(Some(next_page)) => {
130-
page = next_page;
131-
}
132-
Ok(None) => {
133-
// No more pages
134-
break;
135-
}
136-
Err(e) => {
137-
println!("Warning: Failed to fetch next page of comments: {}", e);
138-
break;
139-
}
140-
}
141-
}
142-
143-
// No matching comment found
144-
Ok(None)
145-
}
107+
// async fn find_existing_comment(
108+
// octocrab: &Octocrab,
109+
// owner: &str,
110+
// repo: &str,
111+
// pr_number: u64,
112+
// signature: &str,
113+
// ) -> Result<Option<u64>> {
114+
// // Get all comments on the PR
115+
// let mut page = octocrab
116+
// .issues(owner, repo)
117+
// .list_comments(pr_number)
118+
// .per_page(100)
119+
// .send()
120+
// .await
121+
// .context("Failed to list PR comments")?;
122+
//
123+
// // Process current and subsequent pages
124+
// loop {
125+
// for comment in &page {
126+
// if comment
127+
// .body
128+
// .as_ref()
129+
// .map_or(false, |body| body.contains(signature))
130+
// {
131+
// return Ok(Some(*comment.id));
132+
// }
133+
// }
134+
//
135+
// // Try to get the next page if it exists
136+
// match octocrab.get_page(&page.next).await {
137+
// Ok(Some(next_page)) => {
138+
// page = next_page;
139+
// }
140+
// Ok(None) => {
141+
// // No more pages
142+
// break;
143+
// }
144+
// Err(e) => {
145+
// println!("Warning: Failed to fetch next page of comments: {}", e);
146+
// break;
147+
// }
148+
// }
149+
// }
150+
//
151+
// // No matching comment found
152+
// Ok(None)
153+
// }

0 commit comments

Comments
 (0)