| 1 | //! What each action does once its input has cleared the guards in |
| 2 | //! [`super`]: read context for an issue or PR, post a comment, close a |
| 3 | //! thread. |
| 4 | |
| 5 | use serde_json::Value; |
| 6 | use serde_json::json; |
| 7 | |
| 8 | use crate::tools::spec::{ |
| 9 | ToolContext, ToolError, ToolResult, optional_bool, optional_str, required_str, required_u64, |
| 10 | }; |
| 11 | |
| 12 | use super::cli::{ensure_github_repo, git_status_porcelain, run_gh_json, run_gh_text}; |
| 13 | use super::shape::{ |
| 14 | BODY_ARTIFACT_THRESHOLD, DIFF_ARTIFACT_THRESHOLD, artifact_refs_from_context, |
| 15 | github_event_metadata, shape_large_text, summarize, write_artifact_if_needed, |
| 16 | }; |
| 17 | use super::{GithubTool, validate_evidence}; |
| 18 | |
| 19 | impl GithubTool { |
| 20 | pub(super) async fn execute_issue_context( |
| 21 | &self, |
| 22 | input: &Value, |
| 23 | context: &ToolContext, |
| 24 | ) -> Result<ToolResult, ToolError> { |
| 25 | ensure_github_repo(context)?; |
| 26 | let number = required_u64(input, "number")?; |
| 27 | let include_comments = optional_bool(input, "include_comments", true)?; |
| 28 | let fields = if include_comments { |
| 29 | "number,title,state,author,labels,assignees,milestone,body,comments,url,createdAt,updatedAt" |
| 30 | } else { |
| 31 | "number,title,state,author,labels,assignees,milestone,body,url,createdAt,updatedAt" |
| 32 | }; |
| 33 | let number_s = number.to_string(); |
| 34 | let raw = run_gh_json(context, &["issue", "view", &number_s, "--json", fields])?; |
| 35 | let shaped = shape_large_text(context, raw, "issue_body", BODY_ARTIFACT_THRESHOLD)?; |
| 36 | let mut result = ToolResult::json(&json!({ |
| 37 | "summary": format!("Issue #{number}: {}", shaped["title"].as_str().unwrap_or("")), |
| 38 | "issue": shaped, |
| 39 | })) |
| 40 | .map_err(|e| ToolError::execution_failed(e.to_string()))?; |
| 41 | let artifacts = artifact_refs_from_context(&result.content, "github_issue_body"); |
| 42 | if !artifacts.is_empty() { |
| 43 | result = result.with_metadata(json!({ "task_updates": { "artifacts": artifacts } })); |
| 44 | } |
| 45 | Ok(result) |
| 46 | } |
| 47 | |
| 48 | pub(super) async fn execute_pr_context( |
| 49 | &self, |
| 50 | input: &Value, |
| 51 | context: &ToolContext, |
| 52 | ) -> Result<ToolResult, ToolError> { |
| 53 | ensure_github_repo(context)?; |
| 54 | let number = required_u64(input, "number")?; |
| 55 | let number_s = number.to_string(); |
| 56 | let raw = run_gh_json( |
| 57 | context, |
| 58 | &[ |
| 59 | "pr", |
| 60 | "view", |
| 61 | &number_s, |
| 62 | "--json", |
| 63 | "number,title,state,author,body,comments,reviews,reviewDecision,statusCheckRollup,baseRefName,headRefName,headRefOid,baseRefOid,files,url,createdAt,updatedAt", |
| 64 | ], |
| 65 | )?; |
| 66 | let mut shaped = shape_large_text(context, raw, "pr_body", BODY_ARTIFACT_THRESHOLD)?; |
| 67 | if optional_bool(input, "include_diff", false)? { |
| 68 | let diff = run_gh_text(context, &["pr", "diff", &number_s, "--patch"])?; |
| 69 | let diff_ref = |
| 70 | write_artifact_if_needed(context, "pr_diff", &diff, DIFF_ARTIFACT_THRESHOLD)?; |
| 71 | shaped["diff_summary"] = json!(summarize(&diff, 900)); |
| 72 | shaped["diff_artifact"] = json!(diff_ref); |
| 73 | } |
| 74 | let mut result = ToolResult::json(&json!({ |
| 75 | "summary": format!("PR #{number}: {}", shaped["title"].as_str().unwrap_or("")), |
| 76 | "pr": shaped, |
| 77 | })) |
| 78 | .map_err(|e| ToolError::execution_failed(e.to_string()))?; |
| 79 | let mut artifacts = artifact_refs_from_context(&result.content, "github_pr_body"); |
| 80 | artifacts.extend(artifact_refs_from_context( |
| 81 | &result.content, |
| 82 | "github_pr_diff", |
| 83 | )); |
| 84 | if !artifacts.is_empty() { |
| 85 | result = result.with_metadata(json!({ "task_updates": { "artifacts": artifacts } })); |
| 86 | } |
| 87 | Ok(result) |
| 88 | } |
| 89 | |
| 90 | pub(super) async fn execute_comment( |
| 91 | &self, |
| 92 | input: &Value, |
| 93 | context: &ToolContext, |
| 94 | ) -> Result<ToolResult, ToolError> { |
| 95 | validate_evidence(input, false)?; |
| 96 | let target = required_str(input, "target")?; |
| 97 | let number = required_u64(input, "number")?; |
| 98 | let body = required_str(input, "body")?; |
| 99 | if optional_bool(input, "dry_run", false)? { |
| 100 | return Ok(ToolResult::success(format!( |
| 101 | "Dry run: would comment on {target} #{number}." |
| 102 | ))); |
| 103 | } |
| 104 | let subcmd = if target == "pr" { "pr" } else { "issue" }; |
| 105 | let number_s = number.to_string(); |
| 106 | run_gh_text(context, &[subcmd, "comment", &number_s, "--body", body])?; |
| 107 | let metadata = github_event_metadata( |
| 108 | "comment", |
| 109 | target, |
| 110 | number, |
| 111 | summarize(body, 240), |
| 112 | None, |
| 113 | write_artifact_if_needed(context, "github_comment", body, BODY_ARTIFACT_THRESHOLD)?, |
| 114 | ); |
| 115 | Ok( |
| 116 | ToolResult::success(format!("Commented on {target} #{number}.")) |
| 117 | .with_metadata(metadata), |
| 118 | ) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 123 | pub(super) enum GithubCloseTarget { |
| 124 | Issue, |
| 125 | Pr, |
| 126 | } |
| 127 | |
| 128 | impl GithubCloseTarget { |
| 129 | fn cli_subcommand(self) -> &'static str { |
| 130 | match self { |
| 131 | Self::Issue => "issue", |
| 132 | Self::Pr => "pr", |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | fn metadata_target(self) -> &'static str { |
| 137 | match self { |
| 138 | Self::Issue => "issue", |
| 139 | Self::Pr => "pr", |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | pub(super) fn display(self) -> &'static str { |
| 144 | match self { |
| 145 | Self::Issue => "issue", |
| 146 | Self::Pr => "PR", |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | fn summary_subject(self) -> &'static str { |
| 151 | match self { |
| 152 | Self::Issue => "Issue", |
| 153 | Self::Pr => "PR", |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | pub(super) fn close_github_thread( |
| 159 | input: Value, |
| 160 | context: &ToolContext, |
| 161 | target: GithubCloseTarget, |
| 162 | ) -> Result<ToolResult, ToolError> { |
| 163 | validate_evidence(&input, true)?; |
| 164 | if !optional_bool(&input, "allow_dirty", false)? { |
| 165 | let status = git_status_porcelain(context)?; |
| 166 | if !status.trim().is_empty() { |
| 167 | return Ok(ToolResult::error(format!( |
| 168 | "Refusing to close {}: worktree is dirty and allow_dirty was false.", |
| 169 | target.display() |
| 170 | )) |
| 171 | .with_metadata(json!({ "dirty_status": status }))); |
| 172 | } |
| 173 | } |
| 174 | let number = required_u64(&input, "number")?; |
| 175 | if optional_bool(&input, "dry_run", false)? { |
| 176 | return Ok(ToolResult::success(format!( |
| 177 | "Dry run: would close {} #{number}.", |
| 178 | target.display() |
| 179 | ))); |
| 180 | } |
| 181 | let subcmd = target.cli_subcommand(); |
| 182 | let number_s = number.to_string(); |
| 183 | if let Some(comment) = optional_str(&input, "comment")? { |
| 184 | run_gh_text(context, &[subcmd, "comment", &number_s, "--body", comment])?; |
| 185 | } |
| 186 | let close_args: Vec<&str> = match target { |
| 187 | GithubCloseTarget::Issue => vec!["issue", "close", &number_s, "--reason", "completed"], |
| 188 | GithubCloseTarget::Pr => vec!["pr", "close", &number_s], |
| 189 | }; |
| 190 | run_gh_text(context, &close_args)?; |
| 191 | let metadata = github_event_metadata( |
| 192 | "close", |
| 193 | target.metadata_target(), |
| 194 | number, |
| 195 | format!( |
| 196 | "{} closed as completed with structured evidence", |
| 197 | target.summary_subject() |
| 198 | ), |
| 199 | None, |
| 200 | optional_str(&input, "comment")? |
| 201 | .and_then(|comment| { |
| 202 | write_artifact_if_needed( |
| 203 | context, |
| 204 | "github_close_comment", |
| 205 | comment, |
| 206 | BODY_ARTIFACT_THRESHOLD, |
| 207 | ) |
| 208 | .ok() |
| 209 | }) |
| 210 | .flatten(), |
| 211 | ); |
| 212 | Ok( |
| 213 | ToolResult::success(format!("Closed {} #{number}.", target.display())) |
| 214 | .with_metadata(metadata), |
| 215 | ) |
| 216 | } |
| 217 |