| 
 | 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 | +}  | 
0 commit comments