Skip to main content

elizaos_plugin_github/actions/
create_comment.rs

1#![allow(missing_docs)]
2
3use async_trait::async_trait;
4use serde_json::json;
5
6use super::{ActionContext, ActionResult, GitHubAction};
7use crate::error::Result;
8use crate::types::CreateCommentParams;
9use crate::GitHubService;
10
11pub struct CreateCommentAction;
12
13#[async_trait]
14impl GitHubAction for CreateCommentAction {
15    fn name(&self) -> &str {
16        "CREATE_GITHUB_COMMENT"
17    }
18
19    fn description(&self) -> &str {
20        "Creates a comment on a GitHub issue or pull request."
21    }
22
23    fn similes(&self) -> Vec<&str> {
24        vec![
25            "COMMENT_ON_ISSUE",
26            "COMMENT_ON_PR",
27            "ADD_COMMENT",
28            "REPLY_TO_ISSUE",
29            "POST_COMMENT",
30        ]
31    }
32
33    async fn validate(&self, context: &ActionContext) -> Result<bool> {
34        let text = context
35            .message
36            .get("content")
37            .and_then(|c| c.get("text"))
38            .and_then(|t| t.as_str())
39            .unwrap_or("")
40            .to_lowercase();
41
42        Ok(text.contains("comment") || text.contains("reply") || text.contains("respond"))
43    }
44
45    async fn handler(
46        &self,
47        context: &ActionContext,
48        service: &GitHubService,
49    ) -> Result<ActionResult> {
50        let text = context
51            .message
52            .get("content")
53            .and_then(|c| c.get("text"))
54            .and_then(|t| t.as_str())
55            .unwrap_or("");
56
57        let issue_number = context
58            .state
59            .get("issue_number")
60            .and_then(|n| n.as_u64())
61            .unwrap_or(0);
62
63        let params = CreateCommentParams {
64            owner: context.owner.clone(),
65            repo: context.repo.clone(),
66            issue_number,
67            body: text.to_string(),
68        };
69
70        let comment = service.create_comment(params).await?;
71
72        Ok(ActionResult::success(
73            format!("Added comment to #{}", issue_number),
74            json!({
75                "comment_id": comment.id,
76                "html_url": comment.html_url,
77            }),
78        ))
79    }
80}