@@ -0,0 +1,180 @@ |
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package lifecycle |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "io" |
| 9 | + "log/slog" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/jackc/pgx/v5/pgxpool" |
| 15 | + |
| 16 | + actionsdb "github.com/tenseleyFlow/shithub/internal/actions/sqlc" |
| 17 | + "github.com/tenseleyFlow/shithub/internal/actions/trigger" |
| 18 | + "github.com/tenseleyFlow/shithub/internal/actions/workflow" |
| 19 | + "github.com/tenseleyFlow/shithub/internal/infra/storage" |
| 20 | + repogit "github.com/tenseleyFlow/shithub/internal/repos/git" |
| 21 | + "github.com/tenseleyFlow/shithub/internal/testing/dbtest" |
| 22 | +) |
| 23 | + |
| 24 | +const oldWorkflow = `name: CI |
| 25 | +on: push |
| 26 | +jobs: |
| 27 | + old_job: |
| 28 | + name: Old job |
| 29 | + runs-on: ubuntu-latest |
| 30 | + steps: |
| 31 | + - run: echo old |
| 32 | +` |
| 33 | + |
| 34 | +const newWorkflow = `name: CI |
| 35 | +on: push |
| 36 | +jobs: |
| 37 | + new_job: |
| 38 | + name: New job |
| 39 | + runs-on: ubuntu-latest |
| 40 | + steps: |
| 41 | + - run: echo new |
| 42 | +` |
| 43 | + |
| 44 | +func TestRerunRunUsesOriginalCommitWorkflowAndParentsNewRun(t *testing.T) { |
| 45 | + ctx := context.Background() |
| 46 | + pool := dbtestPool(t) |
| 47 | + repoID, userID := setupLifecycleRepo(t, pool) |
| 48 | + rfs := lifecycleRepoFS(t) |
| 49 | + gitDir := lifecycleGitDir(t, rfs) |
| 50 | + oldSHA := lifecycleCommitWorkflow(t, gitDir, oldWorkflow, time.Date(2026, 5, 11, 12, 0, 0, 0, time.UTC)) |
| 51 | + wf := parseLifecycleWorkflow(t, oldWorkflow) |
| 52 | + |
| 53 | + logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 54 | + original, err := trigger.Enqueue(ctx, trigger.Deps{Pool: pool, Logger: logger}, trigger.EnqueueParams{ |
| 55 | + RepoID: repoID, |
| 56 | + WorkflowFile: ".shithub/workflows/ci.yml", |
| 57 | + HeadSHA: oldSHA, |
| 58 | + HeadRef: "refs/heads/trunk", |
| 59 | + EventKind: trigger.EventPush, |
| 60 | + EventPayload: map[string]any{"ref": "refs/heads/trunk"}, |
| 61 | + ActorUserID: userID, |
| 62 | + TriggerEventID: "push:original", |
| 63 | + Workflow: wf, |
| 64 | + }) |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("trigger.Enqueue original: %v", err) |
| 67 | + } |
| 68 | + if _, err := actionsdb.New().CompleteWorkflowRun(ctx, pool, actionsdb.CompleteWorkflowRunParams{ |
| 69 | + ID: original.RunID, |
| 70 | + Conclusion: actionsdb.CheckConclusionFailure, |
| 71 | + }); err != nil { |
| 72 | + t.Fatalf("CompleteWorkflowRun: %v", err) |
| 73 | + } |
| 74 | + _ = lifecycleCommitWorkflow(t, gitDir, newWorkflow, time.Date(2026, 5, 11, 12, 5, 0, 0, time.UTC)) |
| 75 | + |
| 76 | + result, err := RerunRun(ctx, Deps{Pool: pool, RepoFS: rfs, Logger: logger}, original.RunID, userID) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("RerunRun: %v", err) |
| 79 | + } |
| 80 | + if result.ParentRunID != original.RunID || result.RunID == 0 || result.RunID == original.RunID { |
| 81 | + t.Fatalf("unexpected result: %+v", result) |
| 82 | + } |
| 83 | + |
| 84 | + q := actionsdb.New() |
| 85 | + rerun, err := q.GetWorkflowRunByID(ctx, pool, result.RunID) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("GetWorkflowRunByID rerun: %v", err) |
| 88 | + } |
| 89 | + if !rerun.ParentRunID.Valid || rerun.ParentRunID.Int64 != original.RunID { |
| 90 | + t.Fatalf("parent_run_id: %+v want %d", rerun.ParentRunID, original.RunID) |
| 91 | + } |
| 92 | + if rerun.HeadSha != oldSHA { |
| 93 | + t.Fatalf("rerun head_sha = %q want original %q", rerun.HeadSha, oldSHA) |
| 94 | + } |
| 95 | + if !strings.HasPrefix(rerun.TriggerEventID, "rerun:") { |
| 96 | + t.Fatalf("rerun trigger_event_id = %q", rerun.TriggerEventID) |
| 97 | + } |
| 98 | + if !rerun.ActorUserID.Valid || rerun.ActorUserID.Int64 != userID { |
| 99 | + t.Fatalf("rerun actor_user_id: %+v want %d", rerun.ActorUserID, userID) |
| 100 | + } |
| 101 | + jobs, err := q.ListJobsForRun(ctx, pool, result.RunID) |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("ListJobsForRun: %v", err) |
| 104 | + } |
| 105 | + if len(jobs) != 1 || jobs[0].JobKey != "old_job" || jobs[0].JobName != "Old job" { |
| 106 | + t.Fatalf("rerun used wrong workflow jobs: %+v", jobs) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestRerunRunRejectsNonTerminalRun(t *testing.T) { |
| 111 | + ctx := context.Background() |
| 112 | + pool := dbtestPool(t) |
| 113 | + repoID, userID := setupLifecycleRepo(t, pool) |
| 114 | + rfs := lifecycleRepoFS(t) |
| 115 | + run := insertLifecycleRun(t, pool, repoID, userID, 1) |
| 116 | + |
| 117 | + _, err := RerunRun(ctx, Deps{Pool: pool, RepoFS: rfs}, run.ID, userID) |
| 118 | + if !errors.Is(err, ErrRunNotRerunnable) { |
| 119 | + t.Fatalf("RerunRun error = %v, want ErrRunNotRerunnable", err) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func dbtestPool(t *testing.T) *pgxpool.Pool { |
| 124 | + t.Helper() |
| 125 | + return dbtest.NewTestDB(t) |
| 126 | +} |
| 127 | + |
| 128 | +func lifecycleRepoFS(t *testing.T) *storage.RepoFS { |
| 129 | + t.Helper() |
| 130 | + rfs, err := storage.NewRepoFS(t.TempDir()) |
| 131 | + if err != nil { |
| 132 | + t.Fatalf("NewRepoFS: %v", err) |
| 133 | + } |
| 134 | + return rfs |
| 135 | +} |
| 136 | + |
| 137 | +func lifecycleGitDir(t *testing.T, rfs *storage.RepoFS) string { |
| 138 | + t.Helper() |
| 139 | + gitDir, err := rfs.RepoPath("alice", "demo") |
| 140 | + if err != nil { |
| 141 | + t.Fatalf("RepoPath: %v", err) |
| 142 | + } |
| 143 | + if err := rfs.InitBare(context.Background(), gitDir); err != nil { |
| 144 | + t.Fatalf("InitBare: %v", err) |
| 145 | + } |
| 146 | + return gitDir |
| 147 | +} |
| 148 | + |
| 149 | +func lifecycleCommitWorkflow(t *testing.T, gitDir, body string, when time.Time) string { |
| 150 | + t.Helper() |
| 151 | + commit, err := (repogit.InitialCommit{ |
| 152 | + GitDir: gitDir, |
| 153 | + AuthorName: "Alice", |
| 154 | + AuthorEmail: "alice@example.test", |
| 155 | + Branch: "trunk", |
| 156 | + Message: "Update workflow", |
| 157 | + When: when, |
| 158 | + Files: []repogit.FileEntry{ |
| 159 | + {Path: ".shithub/workflows/ci.yml", Body: []byte(body)}, |
| 160 | + }, |
| 161 | + }).Build(context.Background()) |
| 162 | + if err != nil { |
| 163 | + t.Fatalf("InitialCommit.Build: %v", err) |
| 164 | + } |
| 165 | + return commit |
| 166 | +} |
| 167 | + |
| 168 | +func parseLifecycleWorkflow(t *testing.T, body string) *workflow.Workflow { |
| 169 | + t.Helper() |
| 170 | + wf, diags, err := workflow.Parse([]byte(body)) |
| 171 | + if err != nil { |
| 172 | + t.Fatalf("workflow.Parse: %v", err) |
| 173 | + } |
| 174 | + for _, d := range diags { |
| 175 | + if d.Severity == workflow.Error { |
| 176 | + t.Fatalf("workflow diagnostic: %v", d) |
| 177 | + } |
| 178 | + } |
| 179 | + return wf |
| 180 | +} |