|
| 1 | +use anyhow::{anyhow, bail, Result}; |
| 2 | +use reqwest::blocking::{Client, RequestBuilder}; |
| 3 | +use serde_json::json; |
| 4 | + |
| 5 | +use crate::args::EvalArgs; |
| 6 | + |
| 7 | +/// Posts the report to the configured GitHub PR. |
| 8 | +/// |
| 9 | +/// Updates an existing previous comment by `github-actions[bot]` (matching the |
| 10 | +/// prover output marker) if present, otherwise posts a new one. |
| 11 | +pub(crate) fn post_to_github_pr(args: &EvalArgs, message: &str) -> Result<()> { |
| 12 | + if args.github_token.trim().is_empty() { |
| 13 | + bail!("--github-token is required when --post-to-gh is set"); |
| 14 | + } |
| 15 | + if args.pr_number.trim().is_empty() { |
| 16 | + bail!("--pr-number is required when --post-to-gh is set"); |
| 17 | + } |
| 18 | + |
| 19 | + let client = Client::new(); |
| 20 | + let comments_url = format!( |
| 21 | + "https://api.github.com/repos/{}/issues/{}/comments", |
| 22 | + args.github_repo, args.pr_number |
| 23 | + ); |
| 24 | + |
| 25 | + let comments_response = |
| 26 | + set_github_headers(client.get(&comments_url), &args.github_token).send()?; |
| 27 | + if !comments_response.status().is_success() { |
| 28 | + let status = comments_response.status(); |
| 29 | + let body = comments_response.text().unwrap_or_default(); |
| 30 | + bail!("failed to fetch PR comments ({status}): {body}"); |
| 31 | + } |
| 32 | + |
| 33 | + let comments: Vec<serde_json::Value> = comments_response |
| 34 | + .json() |
| 35 | + .map_err(|e| anyhow!("failed to decode PR comments response: {e}"))?; |
| 36 | + |
| 37 | + let bot_comment = comments.iter().find(|comment| { |
| 38 | + let is_actions_bot = comment["user"]["login"].as_str() == Some("github-actions[bot]"); |
| 39 | + let has_perf_marker = comment["body"] |
| 40 | + .as_str() |
| 41 | + .map(|body| body.contains("SP1 Execution Results")) |
| 42 | + .unwrap_or(false); |
| 43 | + is_actions_bot && has_perf_marker |
| 44 | + }); |
| 45 | + |
| 46 | + let request = if let Some(existing_comment) = bot_comment { |
| 47 | + let comment_url = existing_comment["url"] |
| 48 | + .as_str() |
| 49 | + .ok_or_else(|| anyhow!("existing bot comment did not include url field"))?; |
| 50 | + client.patch(comment_url) |
| 51 | + } else { |
| 52 | + client.post(&comments_url) |
| 53 | + }; |
| 54 | + |
| 55 | + let response = set_github_headers(request, &args.github_token) |
| 56 | + .json(&json!({ "body": message })) |
| 57 | + .send()?; |
| 58 | + |
| 59 | + if !response.status().is_success() { |
| 60 | + let status = response.status(); |
| 61 | + let body = response.text().unwrap_or_default(); |
| 62 | + bail!("failed to post/update PR comment ({status}): {body}"); |
| 63 | + } |
| 64 | + |
| 65 | + Ok(()) |
| 66 | +} |
| 67 | + |
| 68 | +fn set_github_headers(builder: RequestBuilder, token: &str) -> RequestBuilder { |
| 69 | + builder |
| 70 | + .header("Authorization", format!("Bearer {token}")) |
| 71 | + .header("X-GitHub-Api-Version", "2022-11-28") |
| 72 | + .header("User-Agent", "strata-asm-prover-perf") |
| 73 | +} |
| 74 | + |
| 75 | +pub(crate) fn format_github_message(results_text: &[String]) -> String { |
| 76 | + let mut message = String::new(); |
| 77 | + for line in results_text { |
| 78 | + message.push_str(&line.replace('*', "**")); |
| 79 | + message.push('\n'); |
| 80 | + } |
| 81 | + message |
| 82 | +} |
0 commit comments