|
| 1 | +package ci_backends |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "net/url" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/diggerhq/digger/backend/config" |
| 13 | + "github.com/diggerhq/digger/libs/spec" |
| 14 | + "github.com/google/go-github/v61/github" |
| 15 | +) |
| 16 | + |
| 17 | +// Helper to create a github.Client that talks to an httptest server |
| 18 | +func newTestGithubClient(ts *httptest.Server) *github.Client { |
| 19 | + client := github.NewClient(ts.Client()) |
| 20 | + base, _ := url.Parse(ts.URL + "/") |
| 21 | + client.BaseURL = base |
| 22 | + client.UploadURL, _ = url.Parse(ts.URL + "/") |
| 23 | + return client |
| 24 | +} |
| 25 | + |
| 26 | +// setupTestClientAndSpec centralizes common test setup: set the feature flag, |
| 27 | +// create a client and GithubActionCi configured to point to the test server, |
| 28 | +// and build a basic Spec with the given branch. Tests should use this helper |
| 29 | +// to keep setups consistent and small. |
| 30 | +func setupTestClientAndSpec(ts *httptest.Server, forceDefault bool, branch string) (GithubActionCi, spec.Spec) { |
| 31 | + config.DiggerConfig.Set("force_trigger_from_default_branch", forceDefault) |
| 32 | + client := newTestGithubClient(ts) |
| 33 | + ga := GithubActionCi{Client: client} |
| 34 | + |
| 35 | + s := spec.Spec{} |
| 36 | + s.VCS.RepoOwner = "owner" |
| 37 | + s.VCS.RepoName = "repo" |
| 38 | + s.VCS.WorkflowFile = "workflow.yml" |
| 39 | + s.Job.Branch = branch |
| 40 | + |
| 41 | + return ga, s |
| 42 | +} |
| 43 | + |
| 44 | +func TestTriggerWorkflow_UsesJobBranchWhenNotForced(t *testing.T) { |
| 45 | + // server returns error if repo default branch is requested (shouldn't be called) |
| 46 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 47 | + if r.Method == http.MethodPost { |
| 48 | + // Expect the ref to be the job branch |
| 49 | + bodyBytes, _ := io.ReadAll(r.Body) |
| 50 | + defer r.Body.Close() |
| 51 | + var payload map[string]interface{} |
| 52 | + _ = json.Unmarshal(bodyBytes, &payload) |
| 53 | + if payload["ref"] != "feature/abc" { |
| 54 | + t.Fatalf("expected ref 'feature/abc', got %v", payload["ref"]) |
| 55 | + } |
| 56 | + w.WriteHeader(http.StatusCreated) |
| 57 | + return |
| 58 | + } |
| 59 | + t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path) |
| 60 | + })) |
| 61 | + defer ts.Close() |
| 62 | + |
| 63 | + // Ensure flag is false |
| 64 | + config.DiggerConfig.Set("force_trigger_from_default_branch", false) |
| 65 | + |
| 66 | + ga, s := setupTestClientAndSpec(ts, false, "feature/abc") |
| 67 | + |
| 68 | + if err := ga.TriggerWorkflow(s, "run", "token"); err != nil { |
| 69 | + t.Fatalf("TriggerWorkflow failed: %v", err) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestTriggerWorkflow_UsesRepoDefaultBranchWhenForced(t *testing.T) { |
| 74 | + // Server returns repo info and accept the dispatch |
| 75 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 76 | + switch r.Method { |
| 77 | + case http.MethodGet: |
| 78 | + // repos/{owner}/{repo} |
| 79 | + resp := map[string]string{"default_branch": "main"} |
| 80 | + _ = json.NewEncoder(w).Encode(resp) |
| 81 | + return |
| 82 | + case http.MethodPost: |
| 83 | + // Check dispatched ref == main |
| 84 | + bodyBytes, _ := io.ReadAll(r.Body) |
| 85 | + defer r.Body.Close() |
| 86 | + var payload map[string]interface{} |
| 87 | + _ = json.Unmarshal(bodyBytes, &payload) |
| 88 | + if payload["ref"] != "main" { |
| 89 | + t.Fatalf("expected ref 'main' when forced, got %v", payload["ref"]) |
| 90 | + } |
| 91 | + |
| 92 | + // Accept the dispatch — assertion for the spec contents is handled |
| 93 | + // in a separate dedicated test below. |
| 94 | + w.WriteHeader(http.StatusCreated) |
| 95 | + return |
| 96 | + default: |
| 97 | + t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path) |
| 98 | + } |
| 99 | + })) |
| 100 | + defer ts.Close() |
| 101 | + |
| 102 | + // Enable the flag and prepare client + spec |
| 103 | + ga, s := setupTestClientAndSpec(ts, true, "feature/abc") |
| 104 | + |
| 105 | + if err := ga.TriggerWorkflow(s, "run", "token"); err != nil { |
| 106 | + t.Fatalf("TriggerWorkflow failed: %v", err) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestTriggerWorkflow_SpecStillContainsJobBranchWhenForced(t *testing.T) { |
| 111 | + // Server returns repo info and accept the dispatch |
| 112 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 113 | + switch r.Method { |
| 114 | + case http.MethodGet: |
| 115 | + // repos/{owner}/{repo} |
| 116 | + resp := map[string]string{"default_branch": "main"} |
| 117 | + _ = json.NewEncoder(w).Encode(resp) |
| 118 | + return |
| 119 | + case http.MethodPost: |
| 120 | + // Check inputs.spec still contains the original PR branch |
| 121 | + bodyBytes, _ := io.ReadAll(r.Body) |
| 122 | + defer r.Body.Close() |
| 123 | + var payload map[string]interface{} |
| 124 | + _ = json.Unmarshal(bodyBytes, &payload) |
| 125 | + |
| 126 | + inputs, ok := payload["inputs"].(map[string]interface{}) |
| 127 | + if !ok { |
| 128 | + t.Fatalf("expected inputs to be map, got %T", payload["inputs"]) |
| 129 | + } |
| 130 | + specStr, ok := inputs["spec"].(string) |
| 131 | + if !ok { |
| 132 | + t.Fatalf("expected inputs.spec to be string, got %T", inputs["spec"]) |
| 133 | + } |
| 134 | + |
| 135 | + var decoded spec.Spec |
| 136 | + if err := json.Unmarshal([]byte(specStr), &decoded); err != nil { |
| 137 | + t.Fatalf("failed to unmarshal spec from inputs: %v", err) |
| 138 | + } |
| 139 | + if decoded.Job.Branch != "feature/abc" { |
| 140 | + t.Fatalf("expected spec.job.branch to still be feature/abc, got %v", decoded.Job.Branch) |
| 141 | + } |
| 142 | + |
| 143 | + w.WriteHeader(http.StatusCreated) |
| 144 | + return |
| 145 | + default: |
| 146 | + t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path) |
| 147 | + } |
| 148 | + })) |
| 149 | + defer ts.Close() |
| 150 | + |
| 151 | + // Enable flag and create test client/spec |
| 152 | + ga, s := setupTestClientAndSpec(ts, true, "feature/abc") |
| 153 | + |
| 154 | + if err := ga.TriggerWorkflow(s, "run", "token"); err != nil { |
| 155 | + t.Fatalf("TriggerWorkflow failed: %v", err) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func TestResolveWorkflowRef_NotForcedReturnsJobBranch(t *testing.T) { |
| 160 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 161 | + t.Fatalf("no requests expected when flag is disabled, got %s %s", r.Method, r.URL.Path) |
| 162 | + })) |
| 163 | + defer ts.Close() |
| 164 | + |
| 165 | + config.DiggerConfig.Set("force_trigger_from_default_branch", false) |
| 166 | + |
| 167 | + client := newTestGithubClient(ts) |
| 168 | + ga := GithubActionCi{Client: client} |
| 169 | + |
| 170 | + s := spec.Spec{} |
| 171 | + s.Job.Branch = "feature/xyz" |
| 172 | + |
| 173 | + ref, err := ga.resolveWorkflowRef(context.Background(), s) |
| 174 | + if err != nil { |
| 175 | + t.Fatalf("unexpected error: %v", err) |
| 176 | + } |
| 177 | + if ref != "feature/xyz" { |
| 178 | + t.Fatalf("expected feature/xyz branch, got %v", ref) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +func TestResolveWorkflowRef_ForcedWithNoDefaultBranchFallsBackToMain(t *testing.T) { |
| 183 | + // server returns repo info without default_branch |
| 184 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 185 | + if r.Method == http.MethodGet { |
| 186 | + // repos/{owner}/{repo} -> respond with empty object |
| 187 | + w.WriteHeader(http.StatusOK) |
| 188 | + _, _ = io.WriteString(w, `{}`) |
| 189 | + return |
| 190 | + } |
| 191 | + t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path) |
| 192 | + })) |
| 193 | + defer ts.Close() |
| 194 | + |
| 195 | + config.DiggerConfig.Set("force_trigger_from_default_branch", true) |
| 196 | + |
| 197 | + client := newTestGithubClient(ts) |
| 198 | + ga := GithubActionCi{Client: client} |
| 199 | + |
| 200 | + s := spec.Spec{} |
| 201 | + s.VCS.RepoOwner = "owner" |
| 202 | + s.VCS.RepoName = "repo" |
| 203 | + s.Job.Branch = "feature/xyz" |
| 204 | + |
| 205 | + ref, err := ga.resolveWorkflowRef(context.Background(), s) |
| 206 | + if err != nil { |
| 207 | + t.Fatalf("unexpected error: %v", err) |
| 208 | + } |
| 209 | + if ref != "main" { |
| 210 | + t.Fatalf("expected fallback main, got %v", ref) |
| 211 | + } |
| 212 | +} |
0 commit comments