readRemoteExperimentState fetches state.json from an experiments/* branch via the GitHub API. Returns an empty state on any error (branch missing, file absent, parse failure).
(repoOverride, branchName string)
| 618 | // readRemoteExperimentState fetches state.json from an experiments/* branch via the GitHub API. |
| 619 | // Returns an empty state on any error (branch missing, file absent, parse failure). |
| 620 | func readRemoteExperimentState(repoOverride, branchName string) *ExperimentState { |
| 621 | args := []string{"api", |
| 622 | "repos/{owner}/{repo}/contents/state.json", |
| 623 | "--field", "ref=" + branchName, |
| 624 | "--jq", ".content", |
| 625 | "--repo", repoOverride, |
| 626 | } |
| 627 | cmd := workflow.ExecGH(args...) |
| 628 | out, err := cmd.Output() |
| 629 | if err != nil { |
| 630 | return emptyExperimentState() |
| 631 | } |
| 632 | |
| 633 | // GitHub API returns base64-encoded content with embedded newlines for line-wrapping. |
| 634 | // Strip all whitespace before decoding. |
| 635 | b64 := strings.Join(strings.Fields(strings.TrimSpace(string(out))), "") |
| 636 | decoded, err := base64.StdEncoding.DecodeString(b64) |
| 637 | if err != nil { |
| 638 | experimentsLog.Printf("Failed to base64-decode state.json from %s: %v", branchName, err) |
| 639 | return emptyExperimentState() |
| 640 | } |
| 641 | return parseExperimentState(decoded) |
| 642 | } |
| 643 | |
| 644 | // parseExperimentState unmarshals raw JSON into an ExperimentState. |
| 645 | // Returns an empty state when parsing fails or the data is invalid. |
no test coverage detected