Skip to main content

elizaos_plugin_github/actions/
create_branch.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::CreateBranchParams;
9use crate::GitHubService;
10
11pub struct CreateBranchAction;
12
13#[async_trait]
14impl GitHubAction for CreateBranchAction {
15    fn name(&self) -> &str {
16        "CREATE_GITHUB_BRANCH"
17    }
18
19    fn description(&self) -> &str {
20        "Creates a new branch in a GitHub repository from an existing branch or commit."
21    }
22
23    fn similes(&self) -> Vec<&str> {
24        vec![
25            "NEW_BRANCH",
26            "BRANCH_FROM",
27            "FORK_BRANCH",
28            "CREATE_FEATURE_BRANCH",
29        ]
30    }
31
32    async fn validate(&self, context: &ActionContext) -> Result<bool> {
33        let text = context
34            .message
35            .get("content")
36            .and_then(|c| c.get("text"))
37            .and_then(|t| t.as_str())
38            .unwrap_or("")
39            .to_lowercase();
40
41        Ok(text.contains("branch") || text.contains("fork") || text.contains("checkout"))
42    }
43
44    async fn handler(
45        &self,
46        context: &ActionContext,
47        service: &GitHubService,
48    ) -> Result<ActionResult> {
49        let branch_name = context
50            .state
51            .get("branch_name")
52            .and_then(|n| n.as_str())
53            .unwrap_or("new-branch");
54
55        let from_ref = context
56            .state
57            .get("from_ref")
58            .and_then(|r| r.as_str())
59            .unwrap_or(&service.config().branch);
60
61        let params = CreateBranchParams {
62            owner: context.owner.clone(),
63            repo: context.repo.clone(),
64            branch_name: branch_name.to_string(),
65            from_ref: from_ref.to_string(),
66        };
67
68        let branch = service.create_branch(params).await?;
69
70        Ok(ActionResult::success(
71            format!("Created branch '{}' from {}", branch.name, from_ref),
72            json!({
73                "branch_name": branch.name,
74                "sha": branch.sha,
75            }),
76        ))
77    }
78}