diff --git a/internal/domain/workflow/baseline_ledger.go b/internal/domain/workflow/baseline_ledger.go index f678d0dc7..002b172d6 100644 --- a/internal/domain/workflow/baseline_ledger.go +++ b/internal/domain/workflow/baseline_ledger.go @@ -16,7 +16,7 @@ type BaselineClaim struct { // Task is the task that owns the repair when Lead is false. Task int64 // Done is true once that task has finished; Failure is its failure, empty - // when it repaired the baseline on its own branch. + // when it repaired the baseline in a pull request. Done bool Failure string } diff --git a/internal/domain/workflow/baseline_ledger_test.go b/internal/domain/workflow/baseline_ledger_test.go index 954a9e170..0f3af4c5a 100644 --- a/internal/domain/workflow/baseline_ledger_test.go +++ b/internal/domain/workflow/baseline_ledger_test.go @@ -62,7 +62,7 @@ func TestRedBaselineIsRepairedByOneTask(t *testing.T) { { name: "a task reuses a repair made on another branch", claims: []BaselineClaim{{Task: 7, Done: true}}, - wantInErr: "task 7 repaired it on its own branch", + wantInErr: "task 7 repaired it in a pull request", }, } for _, tt := range tests { diff --git a/internal/domain/workflow/baseline_publish_test.go b/internal/domain/workflow/baseline_publish_test.go new file mode 100644 index 000000000..c30fcfa2e --- /dev/null +++ b/internal/domain/workflow/baseline_publish_test.go @@ -0,0 +1,45 @@ +package workflow + +import ( + "context" + "testing" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/workflow/task" +) + +type prRecorder struct { + Forger + created int +} + +func (f *prRecorder) CreatePR(context.Context, string, string, string, string, string, string) (int, error) { + f.created++ + return 42, nil +} + +// A baseline repair is published as a PR before the task continues, and the +// task's own PR joins it instead of opening a second. +func TestBaselineRepairPRIsReusedByTheTaskPR(t *testing.T) { + forge := &prRecorder{} + trees := &noChangesTrees{} + tc := &TaskContext{ + Task: &task.Task{ID: 1, Owner: "acme", Repo: "widgets", Source: task.SourceTest}, + Repo: config.Repo{Owner: "acme", Name: "widgets"}, + Forge: forge, + Trees: trees, + Branch: "archie/1", + } + if err := publishBaselineRepair(t.Context(), tc, []string{"task", "check"}); err != nil { + t.Fatal(err) + } + if !trees.pushed || tc.BaselinePR != 42 || forge.created != 1 { + t.Fatalf("pushed=%v pr=%d created=%d, want pushed, 42, 1", trees.pushed, tc.BaselinePR, forge.created) + } + if err := OpenPR(t.Context(), tc, "body"); err != nil { + t.Fatal(err) + } + if forge.created != 1 { + t.Fatalf("OpenPR created a second PR (%d)", forge.created) + } +} diff --git a/internal/domain/workflow/gate_environment.go b/internal/domain/workflow/gate_environment.go index 012b5b381..d50182452 100644 --- a/internal/domain/workflow/gate_environment.go +++ b/internal/domain/workflow/gate_environment.go @@ -64,7 +64,7 @@ func baselineEnvironmentError(argv []string, out, cause string) error { // baselineSharedError is the park reason for a task that did not repair the // baseline because another task owns that repair. func baselineSharedError(argv []string, out string, claim BaselineClaim) error { - verdict := fmt.Sprintf("task %d repaired it on its own branch; retry once that change is merged", claim.Task) + verdict := fmt.Sprintf("task %d repaired it in a pull request; retry once the baseline gate passes on the base branch", claim.Task) if claim.Failure != "" { verdict = fmt.Sprintf("task %d could not repair it: %s", claim.Task, claim.Failure) } diff --git a/internal/domain/workflow/implement.go b/internal/domain/workflow/implement.go index 0370299a7..f5c264fb4 100644 --- a/internal/domain/workflow/implement.go +++ b/internal/domain/workflow/implement.go @@ -126,6 +126,28 @@ func runBaselineFix(ctx context.Context, tc *TaskContext, argv []string, out []b tc.BuildSummary = res.Summary tc.BaselineFixed = true tc.captureChanges(ctx, capturedAfterBaselineFix) + if err := publishBaselineRepair(ctx, tc, argv); err != nil { + return baselineFixFailedError(argv, string(out), fmt.Sprintf("the repair could not be published: %v", err)) + } } return nil } + +// publishBaselineRepair pushes the repair and opens its PR before the task +// goes on, so the repair survives this task parking or being deleted. The +// task's own PR later reuses this one. +func publishBaselineRepair(ctx context.Context, tc *TaskContext, argv []string) error { + if err := tc.Trees.Push(ctx, tc.Dir, tc.Branch); err != nil { + return err + } + cmd := strings.Join(argv, " ") + body := fmt.Sprintf("The baseline gate `%s` failed on %s before any feature work; this repairs it.", + cmd, tc.Repo.BaseBranch()) + num, err := tc.Forge.CreatePR(ctx, tc.Task.Owner, tc.Task.Repo, + fmt.Sprintf("fix: baseline gate repair (%s)", cmd), tc.Branch, tc.Repo.BaseBranch(), body) + if err != nil { + return err + } + tc.BaselinePR = num + return nil +} diff --git a/internal/domain/workflow/steps.go b/internal/domain/workflow/steps.go index 7f9256a9a..7dd376c4e 100644 --- a/internal/domain/workflow/steps.go +++ b/internal/domain/workflow/steps.go @@ -154,9 +154,12 @@ func OpenPR(ctx context.Context, tc *TaskContext, body string) error { } } } - num, err := tc.Forge.CreatePR(ctx, t.Owner, t.Repo, title, tc.Branch, tc.Repo.BaseBranch(), body) - if err != nil { - return err + num := tc.BaselinePR + if num == 0 { + var err error + if num, err = tc.Forge.CreatePR(ctx, t.Owner, t.Repo, title, tc.Branch, tc.Repo.BaseBranch(), body); err != nil { + return err + } } if t.IsTestRun() { // No PR exists to watch, so the run ends here rather than as pr_open. diff --git a/internal/domain/workflow/workflow.go b/internal/domain/workflow/workflow.go index 261bcbab9..17c0a3372 100644 --- a/internal/domain/workflow/workflow.go +++ b/internal/domain/workflow/workflow.go @@ -168,6 +168,9 @@ type TaskContext struct { // BaselineFixed is set when StageBaselineGate committed a fix, so // StageCommitPush pushes it even when the build changed nothing. BaselineFixed bool + // BaselinePR is the PR StageBaselineGate opened for its fix; the task's + // own change joins it rather than opening a second. + BaselinePR int // ReproProof is the captured failing-test output from a TDD repro // stage, posted on the PR as evidence the bug was reproduced. ReproProof string