orch-cli 0.105.8

Orch, the agent orchestration engine: runs coding agents on tasks and cron jobs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
//! Status check, ruleset, check run and workflow calls

use super::{GITHUB_API, GhHttp, has_status};
use crate::github::app;
use crate::github::types;
use crate::github::types::is_own_check;
use crate::github::types::{GitHubAnnotation, GitHubCheckRun, RequiredStatusCheck};
use crate::github::types::{merge_missing_checks, required_contexts_cover};
use reqwest::header;
use std::collections::HashSet;
use std::fmt::Debug;
use std::path::Path;
use urlencoding;

impl GhHttp {
    /// Create orch's required-check ruleset. When a ruleset already has the name, add the checks
    /// it lacks to that one instead
    pub(crate) async fn apply_required_check_ruleset(
        &self,
        repo: &str,
        checks: &[RequiredStatusCheck],
    ) -> anyhow::Result<RulesetApply> {
        let err = match self.create_required_check_ruleset(repo, checks).await {
            Ok(()) => return Ok(RulesetApply::Applied),
            Err(e) => e.to_string(),
        };
        let kind = classify_ruleset_create_error(&err);
        if !matches!(kind, RulesetCreateKind::NameTaken) {
            return Ok(RulesetApply::Failed { err, kind });
        }
        let Some((id, existing)) = self
            .get_named_ruleset_required_checks(repo, REQUIRED_CHECK_RULESET_NAME)
            .await?
        else {
            return Ok(RulesetApply::NameTakenNotFound);
        };
        if required_contexts_cover(&existing, checks) {
            return Ok(RulesetApply::Applied);
        }
        let merged = merge_missing_checks(existing, checks);
        match self.update_required_check_ruleset(repo, id, &merged).await {
            Ok(()) => Ok(RulesetApply::Applied),
            Err(e) => {
                let err = e.to_string();
                let kind = classify_ruleset_create_error(&err);
                Ok(RulesetApply::Failed { err, kind })
            }
        }
    }

    /// Post a commit status. `state` is one of pending, success, failure, error.
    pub async fn post_commit_status(
        &self,
        repo: &str,
        sha: &str,
        state: &str,
        context: &str,
        description: &str,
    ) -> anyhow::Result<()> {
        let url = format!("{GITHUB_API}/repos/{repo}/statuses/{sha}");
        let payload = serde_json::json!({
            "state": state,
            "context": context,
            "description": description,
        });
        self.post_json_raw(&url, &payload).await?;
        Ok(())
    }

    /// Required status checks for a branch from classic protection and from rulesets
    pub async fn get_required_status_check_contexts(
        &self,
        repo: &str,
        branch: &str,
    ) -> anyhow::Result<Vec<RequiredStatusCheck>> {
        let branch = urlencoding::encode(branch);
        let classic_url = format!(
            "{}/repos/{repo}/branches/{branch}/protection",
            self.api_root()
        );
        let rules_url = format!("{}/repos/{repo}/rules/branches/{branch}", self.api_root());

        let classic_fut = self.get_json::<serde_json::Value>(&classic_url);
        let rules_fut = self.get_all_pages::<serde_json::Value>(&rules_url, &[("per_page", "100")]);
        let (classic_resp, rules_resp) = tokio::join!(classic_fut, rules_fut);

        let classic = match classic_resp {
            Ok(value) => parse_classic_required_status_checks(&value),
            Err(e) => {
                if is_no_branch_protection_error(&e.to_string()) {
                    Vec::new()
                } else {
                    return Err(e);
                }
            }
        };

        let ruleset = match rules_resp {
            Ok(rules) => parse_ruleset_required_status_checks(&rules),
            Err(e) => {
                if has_status(&e.to_string(), 404) {
                    Vec::new()
                } else {
                    return Err(e);
                }
            }
        };

        Ok(merge_required_status_checks(classic, ruleset))
    }

    /// Get commit status contexts for a SHA.
    pub async fn get_commit_status_contexts(
        &self,
        repo: &str,
        sha: &str,
    ) -> anyhow::Result<Vec<(String, String)>> {
        let url = format!(
            "{}/repos/{repo}/commits/{sha}/status?per_page=100",
            self.api_root()
        );
        // The /commits/{sha}/status endpoint returns a wrapper object:
        // { "state": "pending", "total_count": N, "statuses": [...] }
        // Use get_all_pages_from_wrapper to follow pagination and extract the
        // `statuses` array across pages so we don't silently truncate at 100.
        let statuses: Vec<serde_json::Value> =
            self.get_all_pages_from_wrapper(&url, "statuses").await?;

        Ok(statuses
            .into_iter()
            .filter_map(|status| {
                let context = status.get("context").and_then(|v| v.as_str())?;
                let state = status.get("state").and_then(|v| v.as_str())?;
                Some((context.to_string(), state.to_string()))
            })
            .collect())
    }

    pub(super) fn combined_status_state(
        total: u64,
        failing: u64,
        pending: u64,
        has_workflows: bool,
        mergeable_state: Option<&str>,
    ) -> String {
        if failing > 0 {
            "failure".to_string()
        } else if pending > 0 {
            "pending".to_string()
        } else if total == 0 && has_workflows {
            // No check runs yet. If GitHub already reports mergeable_state="clean"
            // the PR is ready (e.g. all workflows use paths-ignore and skipped this
            // commit). Treating it as pending would stall auto-merge forever.
            if mergeable_state == Some("clean") {
                "success".to_string()
            } else {
                "pending".to_string()
            }
        } else {
            "success".to_string()
        }
    }

    /// Return `true` when the repository has at least one GitHub Actions workflow.
    ///
    /// This is used to distinguish repos that genuinely have no CI configured from
    /// repos where CI exists but GitHub has not surfaced any runs yet.
    pub async fn has_workflows(&self, repo: &str) -> anyhow::Result<bool> {
        let url = format!("{GITHUB_API}/repos/{repo}/actions/workflows?per_page=1");
        let resp = self.get_json::<serde_json::Value>(&url).await;
        match resp {
            Ok(value) => Ok(value
                .get("workflows")
                .and_then(|v| v.as_array())
                .is_some_and(|workflows| !workflows.is_empty())),
            Err(e) => {
                let err = e.to_string();
                if has_status(&err, 404) {
                    Ok(false)
                } else {
                    Err(e)
                }
            }
        }
    }

    /// True when `.github/workflows` on `branch` has at least one YAML file
    pub async fn has_workflow_on_branch(&self, repo: &str, branch: &str) -> anyhow::Result<bool> {
        let branch = urlencoding::encode(branch);
        let url = format!(
            "{}/repos/{repo}/contents/.github/workflows?ref={branch}",
            self.api_root()
        );
        match self.get_json::<serde_json::Value>(&url).await {
            Ok(value) => Ok(value.as_array().is_some_and(|entries| {
                entries.iter().any(|entry| {
                    entry
                        .get("name")
                        .and_then(|name| name.as_str())
                        .is_some_and(|name| {
                            Path::new(name)
                                .extension()
                                .and_then(|ext| ext.to_str())
                                .is_some_and(|ext| {
                                    ext.eq_ignore_ascii_case("yml")
                                        || ext.eq_ignore_ascii_case("yaml")
                                })
                        })
                })
            })),
            Err(e) => {
                let err = e.to_string();
                if has_status(&err, 404) {
                    Ok(false)
                } else {
                    Err(e)
                }
            }
        }
    }

    /// True when `path` exists on `branch`
    pub async fn has_file_on_branch(
        &self,
        repo: &str,
        path: &str,
        branch: &str,
    ) -> anyhow::Result<bool> {
        let path = path
            .split('/')
            .map(|segment| urlencoding::encode(segment).into_owned())
            .collect::<Vec<_>>()
            .join("/");
        let branch = urlencoding::encode(branch);
        let url = format!(
            "{}/repos/{repo}/contents/{path}?ref={branch}",
            self.api_root()
        );
        match self.get_json::<serde_json::Value>(&url).await {
            Ok(_) => Ok(true),
            Err(e) => {
                let err = e.to_string();
                if has_status(&err, 404) {
                    Ok(false)
                } else {
                    Err(e)
                }
            }
        }
    }

    /// Create a repository ruleset that requires `checks` on the default branch
    pub async fn create_required_check_ruleset(
        &self,
        repo: &str,
        checks: &[RequiredStatusCheck],
    ) -> anyhow::Result<()> {
        let url = format!("{}/repos/{repo}/rulesets", self.api_root());
        let payload = required_check_ruleset_payload(checks);
        self.post_json_raw(&url, &payload).await?;
        Ok(())
    }

    /// Id and required checks of the repository ruleset named `name`, if it exists
    pub async fn get_named_ruleset_required_checks(
        &self,
        repo: &str,
        name: &str,
    ) -> anyhow::Result<Option<(i64, Vec<RequiredStatusCheck>)>> {
        let url = format!("{}/repos/{repo}/rulesets", self.api_root());
        let rulesets: Vec<serde_json::Value> =
            self.get_all_pages(&url, &[("per_page", "100")]).await?;
        let Some(id) = rulesets.iter().find_map(|ruleset| {
            if ruleset.get("name").and_then(|n| n.as_str()) != Some(name) {
                return None;
            }
            ruleset.get("id").and_then(serde_json::Value::as_i64)
        }) else {
            return Ok(None);
        };
        let url = format!("{}/repos/{repo}/rulesets/{id}", self.api_root());
        let value: serde_json::Value = self.get_json(&url).await?;
        let rules = value
            .get("rules")
            .and_then(|r| r.as_array())
            .cloned()
            .unwrap_or_default();
        Ok(Some((id, parse_ruleset_required_status_checks(&rules))))
    }

    /// Replace the required checks on an existing repository ruleset
    pub async fn update_required_check_ruleset(
        &self,
        repo: &str,
        ruleset_id: i64,
        checks: &[RequiredStatusCheck],
    ) -> anyhow::Result<()> {
        let url = format!("{}/repos/{repo}/rulesets/{ruleset_id}", self.api_root());
        let payload = required_check_ruleset_payload(checks);
        self.put_json_raw(&url, &payload).await?;
        Ok(())
    }

    /// Get check runs for a PR head SHA.
    pub async fn get_check_runs(
        &self,
        repo: &str,
        sha: &str,
    ) -> anyhow::Result<Vec<GitHubCheckRun>> {
        // Use `filter=latest` so GitHub returns only the most recent check run
        // per check name. Without this, the API returns all runs (oldest
        // first) and callers that pick the first match may observe a stale
        // failed run when a newer run with the same name has succeeded.
        //
        // Use `per_page=100` and manual pagination via `get_all_pages_from_wrapper`
        // because this endpoint returns a *wrapper object* — `{ "total_count": N,
        // "check_runs": [...] }` — rather than a raw JSON array.  `get_all_pages`
        // cannot be used here because it calls `serde_json::from_str::<Vec<T>>`
        // which would fail on an object body.
        let url = format!(
            "{}/repos/{repo}/commits/{sha}/check-runs?filter=latest&per_page=100",
            self.api_root()
        );
        self.get_all_pages_from_wrapper(&url, "check_runs").await
    }

    /// Get annotations for a specific check run.
    ///
    /// Returns the list of annotations attached to the check run, including
    /// billing failure messages ("The job was not started because recent account
    /// payments have failed …") that GitHub surfaces as annotations rather than
    /// as a distinct CI conclusion value.
    pub async fn get_check_run_annotations(
        &self,
        repo: &str,
        check_run_id: u64,
    ) -> anyhow::Result<Vec<GitHubAnnotation>> {
        let url = format!("{GITHUB_API}/repos/{repo}/check-runs/{check_run_id}/annotations");
        self.get_all_pages(&url, &[("per_page", "100")]).await
    }

    /// Last `lines` lines of the log of an Actions job. A check run id is the job id
    pub async fn get_job_log_tail(
        &self,
        repo: &str,
        job_id: u64,
        lines: usize,
    ) -> anyhow::Result<String> {
        let url = format!("{GITHUB_API}/repos/{repo}/actions/jobs/{job_id}/logs");
        let bytes = self.get_bytes(&url).await?;
        let text = String::from_utf8_lossy(&bytes);
        let all: Vec<&str> = text.lines().collect();
        let start = all.len().saturating_sub(lines);
        Ok(all[start..].join("\n"))
    }

    /// Get combined CI check status for a git ref
    ///
    /// Counts come from [`Self::get_check_runs`], which requests `filter=latest`
    /// and follows pagination, so a failure or in-progress run past the first page still counts
    pub async fn get_combined_status(
        &self,
        repo: &str,
        git_ref: &str,
        has_workflows: bool,
        mergeable_state: Option<&str>,
    ) -> anyhow::Result<(String, u64, u64, u64, u64)> {
        let runs = self.get_check_runs(repo, git_ref).await?;
        let (total, passing, failing, pending) = Self::count_ci_runs(&runs);
        let state =
            Self::combined_status_state(total, failing, pending, has_workflows, mergeable_state);

        Ok((state, total, passing, failing, pending))
    }

    /// Count CI runs as (total, passing, failing, pending), skipping the review's own checks
    pub(super) fn count_ci_runs(runs: &[types::GitHubCheckRun]) -> (u64, u64, u64, u64) {
        let mut total = 0u64;
        let mut passing = 0u64;
        let mut failing = 0u64;
        let mut pending = 0u64;

        for run in runs.iter().filter(|r| !is_own_check(&r.name)) {
            total += 1;
            if !run.is_finished() {
                pending += 1;
            } else if run.is_passed() {
                passing += 1;
            } else {
                failing += 1;
            }
        }

        (total, passing, failing, pending)
    }

    /// Rerun the latest failed workflow run for a given workflow name and branch.
    ///
    /// Finds the most recent failed (or cancelled) run of `workflow_name` on
    /// `branch` triggered by a `push` event, then calls the GitHub Actions
    /// rerun endpoint so the **same check run** on the commit flips green.
    pub async fn rerun_failed_workflow(
        &self,
        repo: &str,
        workflow_name: &str,
        branch: &str,
    ) -> anyhow::Result<()> {
        // List recent runs for this branch, filtered to push events
        let url = format!(
            "{GITHUB_API}/repos/{repo}/actions/runs?branch={branch}&event=push&per_page=10"
        );
        let resp: serde_json::Value = self.get_json(&url).await?;
        let runs = resp["workflow_runs"]
            .as_array()
            .ok_or_else(|| anyhow::anyhow!("no workflow_runs array in response"))?;

        // Find the latest failed/cancelled run matching the workflow name
        let run_id = runs
            .iter()
            .find(|r| {
                r["name"].as_str() == Some(workflow_name)
                    && matches!(r["conclusion"].as_str(), Some("failure" | "cancelled"))
            })
            .and_then(|r| r["id"].as_u64())
            .ok_or_else(|| {
                anyhow::anyhow!(
                    "no failed run found for workflow '{workflow_name}' on branch '{branch}'"
                )
            })?;

        // POST rerun — no body needed
        let rerun_url = format!("{GITHUB_API}/repos/{repo}/actions/runs/{run_id}/rerun");
        let auth = self.auth_header(&rerun_url).await?;
        let make_req = || {
            Ok(self
                .client
                .post(&rerun_url)
                .header(header::AUTHORIZATION, auth.clone())
                .header(header::ACCEPT, "application/vnd.github+json")
                .header("X-GitHub-Api-Version", "2022-11-28"))
        };
        let resp = self.send_with_retries(make_req, false).await?;
        let status = resp.status();
        if !status.is_success() {
            let body = resp.text().await.unwrap_or_default();
            Self::maybe_record_rate_limit_from_body(status, &body);
            anyhow::bail!("GitHub API POST rerun {rerun_url} failed ({status}): {body}");
        }

        Ok(())
    }

    /// Check the latest automated review comment on a PR.
    pub async fn get_automated_review_status(
        &self,
        repo: &str,
        pr_number: u64,
    ) -> anyhow::Result<Option<String>> {
        let comments = self.list_comments(repo, &pr_number.to_string()).await?;

        // Find the most recent automated review comment authored by a collaborator or the App
        let app_login = app::bot_login().await;
        let mut newest: Option<&types::GitHubComment> = None;
        for c in &comments {
            if !c.body.starts_with("## Automated Review") {
                continue;
            }
            let trusted = match app_login.as_deref() {
                Some(app) if app::same_login(&c.user.login, app) => Ok(true),
                _ => self.is_collaborator(repo, &c.user.login).await,
            };
            match trusted {
                Ok(true) => match newest {
                    None => newest = Some(c),
                    Some(prev) => {
                        if c.created_at > prev.created_at {
                            newest = Some(c);
                        }
                    }
                },
                Ok(false) => {
                    tracing::warn!(
                        user = %c.user.login,
                        pr_number,
                        "ignoring automated review comment from non-collaborator"
                    );
                }
                Err(e) => {
                    tracing::debug!(
                        user = %c.user.login,
                        error = %e,
                        "failed to check collaborator status, skipping comment"
                    );
                }
            }
        }

        match newest {
            Some(c) => {
                let first_line = c.body.lines().next().unwrap_or("");
                if first_line.contains("Automated Review \u{2014} Approve") {
                    Ok(Some("approve".to_string()))
                } else if first_line.contains("Automated Review \u{2014} Changes Requested") {
                    Ok(Some("changes_requested".to_string()))
                } else {
                    Ok(None)
                }
            }
            None => Ok(None),
        }
    }
}

/// GitHub returns for private repos on the Free plan
pub(super) fn is_no_branch_protection_error(err: &str) -> bool {
    has_status(err, 404)
        || (err.contains("403")
            && err.contains("Upgrade to GitHub Pro or make this repository public"))
}

pub(crate) const REQUIRED_CHECK_RULESET_NAME: &str = "orch required checks";

/// What applying orch's required-check ruleset came to
#[derive(Debug)]
pub(crate) enum RulesetApply {
    /// The ruleset requires every needed check
    Applied,
    /// A ruleset has the name but none was found by it
    NameTakenNotFound,
    /// GitHub refused the write, `kind` says why
    Failed {
        err: String,
        kind: RulesetCreateKind,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RulesetCreateKind {
    CheckNeverRun,
    NameTaken,
    Refused,
    Retryable,
    Other,
}

pub(crate) fn required_check_ruleset_payload(checks: &[RequiredStatusCheck]) -> serde_json::Value {
    let required: Vec<serde_json::Value> = checks
        .iter()
        .map(|check| {
            if let Some(id) = check.integration_id {
                serde_json::json!({ "context": check.context, "integration_id": id })
            } else {
                serde_json::json!({ "context": check.context })
            }
        })
        .collect();
    serde_json::json!({
        "name": REQUIRED_CHECK_RULESET_NAME,
        "target": "branch",
        "enforcement": "active",
        "conditions": {
            "ref_name": {
                "include": ["~DEFAULT_BRANCH"],
                "exclude": []
            }
        },
        "rules": [{
            "type": "required_status_checks",
            "parameters": {
                "strict_required_status_checks_policy": true,
                "do_not_enforce_on_create": false,
                "required_status_checks": required
            }
        }]
    })
}

pub(crate) fn classify_ruleset_create_error(err: &str) -> RulesetCreateKind {
    let lower = err.to_ascii_lowercase();
    if lower.contains("already been taken") || lower.contains("name already exists") {
        return RulesetCreateKind::NameTaken;
    }
    if lower.contains("not a valid check")
        || lower.contains("required to be present is not found")
        || (lower.contains("422") && lower.contains("status check") && lower.contains("not found"))
    {
        return RulesetCreateKind::CheckNeverRun;
    }
    if lower.contains("(500")
        || lower.contains("(502")
        || lower.contains("(503")
        || lower.contains("(504")
        || lower.contains("(429")
    {
        return RulesetCreateKind::Retryable;
    }
    // "(401" / "(403" is the status token, a 422 body can contain those digits
    if lower.contains("(403")
        || lower.contains("(401")
        || lower.contains("upgrade to github pro")
        || lower.contains("not accessible")
    {
        return RulesetCreateKind::Refused;
    }
    // timeout and transport errors never include an HTTP status
    if !has_http_status_token(&lower) {
        return RulesetCreateKind::Retryable;
    }
    RulesetCreateKind::Other
}

fn has_http_status_token(err: &str) -> bool {
    err.match_indices('(').any(|(i, _)| {
        err.as_bytes()
            .get(i + 1..i + 4)
            .is_some_and(|digits| digits.iter().all(u8::is_ascii_digit))
    })
}

/// Classic protection prefers `checks` (includes `app_id`) and falls back to `contexts`
pub(super) fn parse_classic_required_status_checks(
    value: &serde_json::Value,
) -> Vec<RequiredStatusCheck> {
    if let Some(checks) = value
        .pointer("/required_status_checks/checks")
        .and_then(|v| v.as_array())
    {
        let parsed: Vec<RequiredStatusCheck> =
            checks.iter().filter_map(parse_classic_check).collect();
        if !parsed.is_empty() {
            return parsed;
        }
    }

    value
        .pointer("/required_status_checks/contexts")
        .and_then(|v| v.as_array())
        .map(|contexts| {
            contexts
                .iter()
                .filter_map(|context| {
                    context.as_str().map(|s| RequiredStatusCheck {
                        context: s.to_string(),
                        integration_id: None,
                    })
                })
                .collect()
        })
        .unwrap_or_default()
}

fn parse_classic_check(check: &serde_json::Value) -> Option<RequiredStatusCheck> {
    let context = check.get("context")?.as_str()?.to_string();
    let integration_id = check
        .get("app_id")
        .and_then(serde_json::Value::as_i64)
        .filter(|&id| id > 0);
    Some(RequiredStatusCheck {
        context,
        integration_id,
    })
}

/// Collect `required_status_checks` entries from every active `required_status_checks` rule
pub(super) fn parse_ruleset_required_status_checks(
    rules: &[serde_json::Value],
) -> Vec<RequiredStatusCheck> {
    let mut out = Vec::new();
    for rule in rules {
        if rule.get("type").and_then(|t| t.as_str()) != Some("required_status_checks") {
            continue;
        }
        let Some(checks) = rule
            .pointer("/parameters/required_status_checks")
            .or_else(|| rule.get("required_status_checks"))
            .and_then(|v| v.as_array())
        else {
            continue;
        };
        for check in checks {
            let Some(context) = check.get("context").and_then(|v| v.as_str()) else {
                continue;
            };
            let integration_id = check
                .get("integration_id")
                .and_then(serde_json::Value::as_i64)
                .filter(|&id| id > 0);
            out.push(RequiredStatusCheck {
                context: context.to_string(),
                integration_id,
            });
        }
    }
    out
}

pub(super) fn merge_required_status_checks(
    mut checks: Vec<RequiredStatusCheck>,
    extra: Vec<RequiredStatusCheck>,
) -> Vec<RequiredStatusCheck> {
    checks.extend(extra);
    let mut seen = HashSet::new();
    checks.retain(|c| seen.insert((c.context.clone(), c.integration_id)));
    checks
}