| 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]).await?; |
| 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 | .await?; |
| 67 | let mut shaped = shape_large_text(context, raw, "pr_body", BODY_ARTIFACT_THRESHOLD)?; |
| 68 | if optional_bool(input, "include_diff", false)? { |
| 69 | let diff = run_gh_text(context, &["pr", "diff", &number_s, "--patch"]).await?; |
| 70 | let diff_ref = |
| 71 | write_artifact_if_needed(context, "pr_diff", &diff, DIFF_ARTIFACT_THRESHOLD)?; |
| 72 | shaped["diff_summary"] = json!(summarize(&diff, 900)); |
| 73 | shaped["diff_artifact"] = json!(diff_ref); |
| 74 | } |
| 75 | let mut result = ToolResult::json(&json!({ |
| 76 | "summary": format!("PR #{number}: {}", shaped["title"].as_str().unwrap_or("")), |
| 77 | "pr": shaped, |
| 78 | })) |
| 79 | .map_err(|e| ToolError::execution_failed(e.to_string()))?; |
| 80 | let mut artifacts = artifact_refs_from_context(&result.content, "github_pr_body"); |
| 81 | artifacts.extend(artifact_refs_from_context( |
| 82 | &result.content, |
| 83 | "github_pr_diff", |
| 84 | )); |
| 85 | if !artifacts.is_empty() { |
| 86 | result = result.with_metadata(json!({ "task_updates": { "artifacts": artifacts } })); |
| 87 | } |
| 88 | Ok(result) |
| 89 | } |
| 90 | |
| 91 | pub(super) async fn execute_comment( |
| 92 | &self, |
| 93 | input: &Value, |
| 94 | context: &ToolContext, |
| 95 | ) -> Result<ToolResult, ToolError> { |
| 96 | validate_evidence(input, false)?; |
| 97 | let target = required_str(input, "target")?; |
| 98 | // Exactly the schema's enum. Any other spelling ("PR", |
| 99 | // "pull_request") used to fall through to an issue comment. |
| 100 | let subcmd = match target { |
| 101 | "issue" => "issue", |
| 102 | "pr" => "pr", |
| 103 | other => { |
| 104 | return Err(ToolError::invalid_input(format!( |
| 105 | "github comment: target must be \"issue\" or \"pr\", got {other:?}; nothing was posted" |
| 106 | ))); |
| 107 | } |
| 108 | }; |
| 109 | let number = required_u64(input, "number")?; |
| 110 | let body = required_str(input, "body")?; |
| 111 | if optional_bool(input, "dry_run", false)? { |
| 112 | return Ok(ToolResult::success(format!( |
| 113 | "Dry run: would comment on {target} #{number}." |
| 114 | ))); |
| 115 | } |
| 116 | let number_s = number.to_string(); |
| 117 | run_gh_text(context, &[subcmd, "comment", &number_s, "--body", body]).await?; |
| 118 | let metadata = github_event_metadata( |
| 119 | "comment", |
| 120 | target, |
| 121 | number, |
| 122 | summarize(body, 240), |
| 123 | None, |
| 124 | write_artifact_if_needed(context, "github_comment", body, BODY_ARTIFACT_THRESHOLD)?, |
| 125 | ); |
| 126 | Ok( |
| 127 | ToolResult::success(format!("Commented on {target} #{number}.")) |
| 128 | .with_metadata(metadata), |
| 129 | ) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 134 | pub(super) enum GithubCloseTarget { |
| 135 | Issue, |
| 136 | Pr, |
| 137 | } |
| 138 | |
| 139 | impl GithubCloseTarget { |
| 140 | fn cli_subcommand(self) -> &'static str { |
| 141 | match self { |
| 142 | Self::Issue => "issue", |
| 143 | Self::Pr => "pr", |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | fn metadata_target(self) -> &'static str { |
| 148 | match self { |
| 149 | Self::Issue => "issue", |
| 150 | Self::Pr => "pr", |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | pub(super) fn display(self) -> &'static str { |
| 155 | match self { |
| 156 | Self::Issue => "issue", |
| 157 | Self::Pr => "PR", |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | fn summary_subject(self) -> &'static str { |
| 162 | match self { |
| 163 | Self::Issue => "Issue", |
| 164 | Self::Pr => "PR", |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | pub(super) async fn close_github_thread( |
| 170 | input: Value, |
| 171 | context: &ToolContext, |
| 172 | target: GithubCloseTarget, |
| 173 | ) -> Result<ToolResult, ToolError> { |
| 174 | validate_evidence(&input, true)?; |
| 175 | if !optional_bool(&input, "allow_dirty", false)? { |
| 176 | let worker_context = context.clone(); |
| 177 | let status = tokio::task::spawn_blocking(move || git_status_porcelain(&worker_context)) |
| 178 | .await |
| 179 | .map_err(|e| ToolError::execution_failed(format!("git status worker failed: {e}")))??; |
| 180 | if !status.trim().is_empty() { |
| 181 | return Ok(ToolResult::error(format!( |
| 182 | "Refusing to close {}: worktree is dirty and allow_dirty was false.", |
| 183 | target.display() |
| 184 | )) |
| 185 | .with_metadata(json!({ "dirty_status": status }))); |
| 186 | } |
| 187 | } |
| 188 | let number = required_u64(&input, "number")?; |
| 189 | if optional_bool(&input, "dry_run", false)? { |
| 190 | return Ok(ToolResult::success(format!( |
| 191 | "Dry run: would close {} #{number}.", |
| 192 | target.display() |
| 193 | ))); |
| 194 | } |
| 195 | let subcmd = target.cli_subcommand(); |
| 196 | let number_s = number.to_string(); |
| 197 | if let Some(comment) = optional_str(&input, "comment")? { |
| 198 | run_gh_text(context, &[subcmd, "comment", &number_s, "--body", comment]).await?; |
| 199 | } |
| 200 | let close_args: Vec<&str> = match target { |
| 201 | GithubCloseTarget::Issue => vec!["issue", "close", &number_s, "--reason", "completed"], |
| 202 | GithubCloseTarget::Pr => vec!["pr", "close", &number_s], |
| 203 | }; |
| 204 | run_gh_text(context, &close_args).await?; |
| 205 | let metadata = github_event_metadata( |
| 206 | "close", |
| 207 | target.metadata_target(), |
| 208 | number, |
| 209 | format!( |
| 210 | "{} closed as completed with structured evidence", |
| 211 | target.summary_subject() |
| 212 | ), |
| 213 | None, |
| 214 | optional_str(&input, "comment")? |
| 215 | .and_then(|comment| { |
| 216 | write_artifact_if_needed( |
| 217 | context, |
| 218 | "github_close_comment", |
| 219 | comment, |
| 220 | BODY_ARTIFACT_THRESHOLD, |
| 221 | ) |
| 222 | .ok() |
| 223 | }) |
| 224 | .flatten(), |
| 225 | ); |
| 226 | Ok( |
| 227 | ToolResult::success(format!("Closed {} #{number}.", target.display())) |
| 228 | .with_metadata(metadata), |
| 229 | ) |
| 230 | } |
| 231 |