2a1f7c2edf
* feat: sync workflows from a configured github repo Per-workspace setting pointing at a GitHub repo directory of portable workflow export files. A background poller fetches it on an interval and creates, updates, or deletes the matching workflows; a force-sync endpoint bypasses the content-hash check. Synced workflows carry source/source_path provenance and coexist with manual ones; changes that cannot be applied safely (steps or workflows that still hold tasks, ambiguous step names, unparseable files) surface as warnings in the new settings section. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): fill workflow sync form from a pasted github link A new Repository link field parses plain repo URLs, SSH remotes, and tree/blob links (which also carry branch and directory) into the structured owner/repo/branch/path fields. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): simplify workflow sync form to link, branch, and interval The repository link is now the only repo input: owner, repo, and directory resolve from the pasted URL (echoed as a summary under the field), and a stored config renders back into a canonical tree link via buildGitHubRepoUrl. Branch and poll interval stay individually editable. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): collapse workflow sync into a button, dialog, and status card The Workflows settings page now shows only a GitHub Sync button by default; the link/branch/interval form lives in a dialog that closes on successful save or removal. Once configured, a compact status card (mirroring the GitHub connection-status card) shows the synced repo, branch, directory, last result, warnings, and a Sync now action. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): move github sync button into the workflows page header The GitHub Sync trigger now stacks under the Workspace settings button on the top right, and the extra separator around the sync area is gone — unconfigured workspaces show no sync chrome at all, configured ones show just the status card above the workflow list. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): move github sync button next to the workflow actions GitHub Sync now sits in the Workflows section toolbar alongside Export All, Import, and Add Workflow; the status card renders inside the section above the workflow list. The toolbar moved to its own component to keep the page under the file-size limit. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): separate sync status card from the workflow list Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(web): align sync status card with the workflow list gutter Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: repair local edits to synced workflows on periodic sync Periodic syncs previously skipped applying when the repo content hash was unchanged, so local UI edits to a synced workflow were only reverted by a manual Sync now. Every sync now reconciles against the repo; the applier compares each step against its definition and only writes (and broadcasts) actual differences, so a no-drift sync stays silent. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: make github-synced workflows read-only Workflows with source=github reject UI mutations: workflow rename, description, agent-profile, and delete return 409 from the task-domain handlers; step create/update/delete/reorder and template application are guarded in the workflow controller. The sync applier writes through the service/repo layer and stays exempt. The frontend disables the card's inputs, save/delete, and the step pipeline editor for synced workflows, with a tooltip pointing at the synced repository. Drift repair on sync remains as the backstop. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: optional workflow sync polling and repo-derived branch picker A per-workspace auto-sync toggle (poll_enabled, default on) controls the background polling loop; when off the workspace only syncs via Sync now. The dialog's branch field becomes a select fed by the repository's actual branches (same loader the task-create dialog uses), falling back to free text until branches load. Existing dev databases gain the new column via an idempotent ALTER. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat: release synced workflows when the sync config is removed Deleting a workspace's workflow sync configuration converts its GitHub-sourced workflows back to manual ownership (source=manual), so they become editable instead of being stranded read-only. Release runs before the config row is deleted so a failure keeps the config for retry. The settings page reloads after removal (and after syncs that changed workflows) so cards unlock without a manual refresh. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(web): drop branch selector and compact the auto-sync row The pasted link already carries the branch (main by default), so the dialog no longer needs a branch field — the resolved summary echoes it. The auto-sync toggle is now one compact row with the interval inline and a single short helper line. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: address PR review feedback Backend: fail-open read-only guard on workflow lookup errors (fixes MCP step handler CI failures); poller Stop holds its mutex through drain; per-workspace lock serializes syncs against config delete/replace; rollback workflow creation when provenance stamping fails; idiomatic duplicate-column migration via db.IsDuplicateColumnError; branch-name validation and a 30-day interval cap; generic 500s with logged detail; WS read-only rejections use CONFLICT with the real reason; e2e reset aborts when sync-config cleanup fails; test hygiene (t.Cleanup, SetMaxOpenConns). Frontend: malformed percent escapes parse as invalid instead of throwing; save disabled during initial load and for invalid intervals; Remove disabled while saving; stale async config responses discarded; delete-dialog confirms re-check read-only; focusable Synced badge; explicit button types; failed polls labeled 'last attempt'; unique warning list keys; test cleanup via afterEach. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: guard workflow sync test fake against concurrent poller access The poller invokes the fake applier from its own goroutine while the synctest test asserts on call counts; -race caught the unguarded reads. Also documents that the per-workspace lock is deliberately held across the GitHub fetch. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Kandev Agent <agent@kandev.dev> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
443 lines
14 KiB
Go
443 lines
14 KiB
Go
package github
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRepoContentsPath(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
in string
|
|
want string
|
|
}{
|
|
{name: "empty is root", in: "", want: ""},
|
|
{name: "dot is root", in: ".", want: ""},
|
|
{name: "simple path", in: "workflows", want: "workflows"},
|
|
{name: "nested path preserves slashes", in: "a/b/c.yaml", want: "a/b/c.yaml"},
|
|
{name: "segment with space is escaped", in: "my dir/file.yaml", want: "my%20dir/file.yaml"},
|
|
{name: "leading and trailing slashes trimmed", in: "/workflows/", want: "workflows"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := repoContentsPath(tc.in); got != tc.want {
|
|
t.Fatalf("repoContentsPath(%q) = %q, want %q", tc.in, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPATClient_ListRepoDirectory_DecodesEntries(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
wantPath := "/repos/o/r/contents/workflows"
|
|
if r.URL.Path != wantPath {
|
|
t.Errorf("path = %q, want %q", r.URL.Path, wantPath)
|
|
}
|
|
if got := r.URL.Query().Get("ref"); got != "main" {
|
|
t.Errorf("ref = %q, want main", got)
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write([]byte(`[
|
|
{"name":"deploy.yaml","path":"workflows/deploy.yaml","type":"file","sha":"abc123","size":42},
|
|
{"name":"nested","path":"workflows/nested","type":"dir","sha":"def456","size":0}
|
|
]`))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
c := newPATClientPointingAt(t, srv.URL)
|
|
|
|
entries, err := c.ListRepoDirectory(context.Background(), "o", "r", "workflows", "main")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if len(entries) != 2 {
|
|
t.Fatalf("expected 2 entries, got %d", len(entries))
|
|
}
|
|
if entries[0].Name != "deploy.yaml" || entries[0].Type != "file" || entries[0].SHA != "abc123" || entries[0].Size != 42 {
|
|
t.Fatalf("unexpected first entry: %+v", entries[0])
|
|
}
|
|
if entries[1].Name != "nested" || entries[1].Type != "dir" {
|
|
t.Fatalf("unexpected second entry: %+v", entries[1])
|
|
}
|
|
}
|
|
|
|
func TestPATClient_ListRepoDirectory_RootOmitsPathSegment(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
wantPath := "/repos/o/r/contents"
|
|
if r.URL.Path != wantPath {
|
|
t.Errorf("path = %q, want %q", r.URL.Path, wantPath)
|
|
}
|
|
if got := r.URL.Query().Get("ref"); got != "" {
|
|
t.Errorf("ref = %q, want empty (omitted)", got)
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write([]byte(`[]`))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
c := newPATClientPointingAt(t, srv.URL)
|
|
|
|
entries, err := c.ListRepoDirectory(context.Background(), "o", "r", "", "")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if len(entries) != 0 {
|
|
t.Fatalf("expected 0 entries, got %d", len(entries))
|
|
}
|
|
}
|
|
|
|
func TestPATClient_ListRepoDirectory_404(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
_, _ = w.Write([]byte(`{"message":"Not Found"}`))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
c := newPATClientPointingAt(t, srv.URL)
|
|
|
|
_, err := c.ListRepoDirectory(context.Background(), "o", "r", "missing", "main")
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
var apiErr *GitHubAPIError
|
|
if !errors.As(err, &apiErr) {
|
|
t.Fatalf("expected *GitHubAPIError, got %T: %v", err, err)
|
|
}
|
|
if apiErr.StatusCode != http.StatusNotFound {
|
|
t.Fatalf("StatusCode = %d, want 404", apiErr.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestPATClient_GetRepoFileContent_DecodesBase64(t *testing.T) {
|
|
want := []byte("name: deploy\nsteps:\n - run: echo hi\n")
|
|
// GitHub wraps base64 content with embedded newlines every 60 chars;
|
|
// simulate that here to prove the decoder strips them.
|
|
encoded := base64.StdEncoding.EncodeToString(want)
|
|
wrapped := encoded[:len(encoded)/2] + "\n" + encoded[len(encoded)/2:] + "\n"
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
wantPath := "/repos/o/r/contents/workflows/deploy.yaml"
|
|
if r.URL.Path != wantPath {
|
|
t.Errorf("path = %q, want %q", r.URL.Path, wantPath)
|
|
}
|
|
respBody, err := json.Marshal(map[string]string{
|
|
"type": "file",
|
|
"encoding": "base64",
|
|
"content": wrapped,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("marshal response body: %v", err)
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write(respBody)
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
c := newPATClientPointingAt(t, srv.URL)
|
|
|
|
got, err := c.GetRepoFileContent(context.Background(), "o", "r", "workflows/deploy.yaml", "main")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if string(got) != string(want) {
|
|
t.Fatalf("content = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestPATClient_GetRepoFileContent_NonBase64EncodingErrors(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
_, _ = w.Write([]byte(`{"type":"file","encoding":"none","content":"raw text"}`))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
c := newPATClientPointingAt(t, srv.URL)
|
|
|
|
_, err := c.GetRepoFileContent(context.Background(), "o", "r", "workflows/deploy.yaml", "main")
|
|
if err == nil {
|
|
t.Fatal("expected error for non-base64 encoding, got nil")
|
|
}
|
|
}
|
|
|
|
func TestPATClient_GetRepoFileContent_404(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
_, _ = w.Write([]byte(`{"message":"Not Found"}`))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
c := newPATClientPointingAt(t, srv.URL)
|
|
|
|
_, err := c.GetRepoFileContent(context.Background(), "o", "r", "missing.yaml", "main")
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
var apiErr *GitHubAPIError
|
|
if !errors.As(err, &apiErr) {
|
|
t.Fatalf("expected *GitHubAPIError, got %T: %v", err, err)
|
|
}
|
|
if apiErr.StatusCode != http.StatusNotFound {
|
|
t.Fatalf("StatusCode = %d, want 404", apiErr.StatusCode)
|
|
}
|
|
}
|
|
|
|
// --- MockClient repo-file seeding ---
|
|
|
|
func TestMockClient_SeedRepoFile_GetRepoFileContent_ExactRef(t *testing.T) {
|
|
m := NewMockClient()
|
|
m.SeedRepoFile("o", "r", "main", "workflows/deploy.yaml", []byte("on-main"))
|
|
m.SeedRepoFile("o", "r", "dev", "workflows/deploy.yaml", []byte("on-dev"))
|
|
|
|
got, err := m.GetRepoFileContent(context.Background(), "o", "r", "workflows/deploy.yaml", "main")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if string(got) != "on-main" {
|
|
t.Fatalf("content = %q, want on-main", got)
|
|
}
|
|
|
|
got, err = m.GetRepoFileContent(context.Background(), "o", "r", "workflows/deploy.yaml", "dev")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if string(got) != "on-dev" {
|
|
t.Fatalf("content = %q, want on-dev", got)
|
|
}
|
|
}
|
|
|
|
func TestMockClient_SeedRepoFile_WildcardRefMatchesAnyRequestedRef(t *testing.T) {
|
|
m := NewMockClient()
|
|
m.SeedRepoFile("o", "r", "", "workflows/deploy.yaml", []byte("wildcard"))
|
|
|
|
for _, ref := range []string{"", "main", "feature/x"} {
|
|
got, err := m.GetRepoFileContent(context.Background(), "o", "r", "workflows/deploy.yaml", ref)
|
|
if err != nil {
|
|
t.Fatalf("ref %q: unexpected error: %v", ref, err)
|
|
}
|
|
if string(got) != "wildcard" {
|
|
t.Fatalf("ref %q: content = %q, want wildcard", ref, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMockClient_SeedRepoFile_ExactRefPreferredOverWildcard(t *testing.T) {
|
|
m := NewMockClient()
|
|
m.SeedRepoFile("o", "r", "", "workflows/deploy.yaml", []byte("wildcard"))
|
|
m.SeedRepoFile("o", "r", "main", "workflows/deploy.yaml", []byte("exact-main"))
|
|
|
|
got, err := m.GetRepoFileContent(context.Background(), "o", "r", "workflows/deploy.yaml", "main")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if string(got) != "exact-main" {
|
|
t.Fatalf("content = %q, want exact-main (exact ref should win over wildcard)", got)
|
|
}
|
|
|
|
// A specific-ref seed must NOT satisfy a different ref, or the default
|
|
// (empty) ref, since only the wildcard seed matches those.
|
|
got, err = m.GetRepoFileContent(context.Background(), "o", "r", "workflows/deploy.yaml", "dev")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error for dev ref: %v", err)
|
|
}
|
|
if string(got) != "wildcard" {
|
|
t.Fatalf("dev ref content = %q, want wildcard", got)
|
|
}
|
|
}
|
|
|
|
func TestMockClient_GetRepoFileContent_404(t *testing.T) {
|
|
m := NewMockClient()
|
|
_, err := m.GetRepoFileContent(context.Background(), "o", "r", "missing.yaml", "main")
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
var apiErr *GitHubAPIError
|
|
if !errors.As(err, &apiErr) {
|
|
t.Fatalf("expected *GitHubAPIError, got %T: %v", err, err)
|
|
}
|
|
if apiErr.StatusCode != 404 {
|
|
t.Fatalf("StatusCode = %d, want 404", apiErr.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestMockClient_ListRepoDirectory_InfersFilesAndSubdirs(t *testing.T) {
|
|
m := NewMockClient()
|
|
m.SeedRepoFile("o", "r", "main", "workflows/deploy.yaml", []byte("a"))
|
|
m.SeedRepoFile("o", "r", "main", "workflows/nested/inner.yaml", []byte("b"))
|
|
m.SeedRepoFile("o", "r", "main", "README.md", []byte("c"))
|
|
|
|
entries, err := m.ListRepoDirectory(context.Background(), "o", "r", "", "main")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
byName := make(map[string]RepoContentEntry, len(entries))
|
|
for _, e := range entries {
|
|
byName[e.Name] = e
|
|
}
|
|
if len(entries) != 2 {
|
|
t.Fatalf("expected 2 root entries, got %d: %+v", len(entries), entries)
|
|
}
|
|
if got := byName["README.md"]; got.Type != "file" || got.Path != "README.md" {
|
|
t.Fatalf("README.md entry = %+v, want file at README.md", got)
|
|
}
|
|
if got := byName["workflows"]; got.Type != "dir" || got.Path != "workflows" {
|
|
t.Fatalf("workflows entry = %+v, want dir at workflows", got)
|
|
}
|
|
|
|
nested, err := m.ListRepoDirectory(context.Background(), "o", "r", "workflows", "main")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
nestedByName := make(map[string]RepoContentEntry, len(nested))
|
|
for _, e := range nested {
|
|
nestedByName[e.Name] = e
|
|
}
|
|
if len(nested) != 2 {
|
|
t.Fatalf("expected 2 entries under workflows, got %d: %+v", len(nested), nested)
|
|
}
|
|
if got := nestedByName["deploy.yaml"]; got.Type != "file" || got.Path != "workflows/deploy.yaml" {
|
|
t.Fatalf("deploy.yaml entry = %+v", got)
|
|
}
|
|
if got := nestedByName["nested"]; got.Type != "dir" || got.Path != "workflows/nested" {
|
|
t.Fatalf("nested entry = %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestMockClient_ListRepoDirectory_404OnMissingDir(t *testing.T) {
|
|
m := NewMockClient()
|
|
m.SeedRepoFile("o", "r", "main", "workflows/deploy.yaml", []byte("a"))
|
|
|
|
_, err := m.ListRepoDirectory(context.Background(), "o", "r", "does-not-exist", "main")
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
var apiErr *GitHubAPIError
|
|
if !errors.As(err, &apiErr) {
|
|
t.Fatalf("expected *GitHubAPIError, got %T: %v", err, err)
|
|
}
|
|
if apiErr.StatusCode != 404 {
|
|
t.Fatalf("StatusCode = %d, want 404", apiErr.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestMockClient_ListRepoDirectory_RefScoping(t *testing.T) {
|
|
m := NewMockClient()
|
|
m.SeedRepoFile("o", "r", "main", "only-on-main.yaml", []byte("a"))
|
|
|
|
_, err := m.ListRepoDirectory(context.Background(), "o", "r", "", "dev")
|
|
if err == nil {
|
|
t.Fatal("expected 404 for a ref with no matching seeded files, got nil")
|
|
}
|
|
|
|
entries, err := m.ListRepoDirectory(context.Background(), "o", "r", "", "main")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if len(entries) != 1 || entries[0].Name != "only-on-main.yaml" {
|
|
t.Fatalf("unexpected entries: %+v", entries)
|
|
}
|
|
}
|
|
|
|
// --- GHClient (gh CLI) ---
|
|
|
|
// installFakeGH writes a fake `gh` shell script onto PATH for the duration of
|
|
// the test, logging every invocation's argv to a file the test can inspect.
|
|
// script receives the raw argv (via "$*") and stdout/stderr as it sees fit.
|
|
func installFakeGH(t *testing.T, script string) (argsLogPath string) {
|
|
t.Helper()
|
|
binDir := t.TempDir()
|
|
logPath := filepath.Join(t.TempDir(), "gh-args.log")
|
|
ghPath := filepath.Join(binDir, "gh")
|
|
if err := os.WriteFile(ghPath, []byte(script), 0o755); err != nil {
|
|
t.Fatalf("write fake gh: %v", err)
|
|
}
|
|
t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
|
|
t.Setenv("GH_ARGS_LOG", logPath)
|
|
return logPath
|
|
}
|
|
|
|
func TestGHClient_ListRepoDirectory_DecodesEntries(t *testing.T) {
|
|
logPath := installFakeGH(t, `#!/bin/sh
|
|
printf '%s\n' "$*" >> "$GH_ARGS_LOG"
|
|
case "$*" in
|
|
*contents/workflows*)
|
|
printf '%s\n' '[{"name":"deploy.yaml","path":"workflows/deploy.yaml","type":"file","sha":"abc123","size":42}]'
|
|
;;
|
|
*) printf '%s\n' 'gh: HTTP 404: Not Found' >&2; exit 1 ;;
|
|
esac
|
|
`)
|
|
|
|
entries, err := NewGHClient().ListRepoDirectory(context.Background(), "o", "r", "workflows", "main")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if len(entries) != 1 || entries[0].Name != "deploy.yaml" || entries[0].Type != "file" {
|
|
t.Fatalf("unexpected entries: %+v", entries)
|
|
}
|
|
logged, err := os.ReadFile(logPath)
|
|
if err != nil {
|
|
t.Fatalf("read gh args log: %v", err)
|
|
}
|
|
if !strings.Contains(string(logged), "repos/o/r/contents/workflows") {
|
|
t.Fatalf("expected contents endpoint in gh args, got:\n%s", logged)
|
|
}
|
|
if !strings.Contains(string(logged), "-f ref=main") {
|
|
t.Fatalf("expected ref field in gh args, got:\n%s", logged)
|
|
}
|
|
}
|
|
|
|
func TestGHClient_ListRepoDirectory_404(t *testing.T) {
|
|
installFakeGH(t, `#!/bin/sh
|
|
printf '%s\n' 'gh: HTTP 404: Not Found' >&2
|
|
exit 1
|
|
`)
|
|
|
|
_, err := NewGHClient().ListRepoDirectory(context.Background(), "o", "r", "missing", "main")
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
var apiErr *GitHubAPIError
|
|
if !errors.As(err, &apiErr) {
|
|
t.Fatalf("expected *GitHubAPIError, got %T: %v", err, err)
|
|
}
|
|
if apiErr.StatusCode != http.StatusNotFound {
|
|
t.Fatalf("StatusCode = %d, want 404", apiErr.StatusCode)
|
|
}
|
|
}
|
|
|
|
func TestGHClient_GetRepoFileContent_DecodesBase64(t *testing.T) {
|
|
want := []byte("name: deploy\n")
|
|
encoded := base64.StdEncoding.EncodeToString(want)
|
|
installFakeGH(t, `#!/bin/sh
|
|
printf '{"type":"file","encoding":"base64","content":"`+encoded+`"}\n'
|
|
`)
|
|
|
|
got, err := NewGHClient().GetRepoFileContent(context.Background(), "o", "r", "workflows/deploy.yaml", "main")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if string(got) != string(want) {
|
|
t.Fatalf("content = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestGHClient_GetRepoFileContent_404(t *testing.T) {
|
|
installFakeGH(t, `#!/bin/sh
|
|
printf '%s\n' 'gh: HTTP 404: Not Found' >&2
|
|
exit 1
|
|
`)
|
|
|
|
_, err := NewGHClient().GetRepoFileContent(context.Background(), "o", "r", "missing.yaml", "main")
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
var apiErr *GitHubAPIError
|
|
if !errors.As(err, &apiErr) {
|
|
t.Fatalf("expected *GitHubAPIError, got %T: %v", err, err)
|
|
}
|
|
if apiErr.StatusCode != http.StatusNotFound {
|
|
t.Fatalf("StatusCode = %d, want 404", apiErr.StatusCode)
|
|
}
|
|
}
|