fix(workflow): remove the pr-review snapshot when any phase ends the run
This commit is contained in:
4 files changed
+42
-5
No files matched your search
@@ -17,11 +17,7 @@ type PRReviewDecision struct {
|
|||||||
func ReviewPullRequest(ctx context.Context, tc *TaskContext) (PRReviewDecision, error) {
|
func ReviewPullRequest(ctx context.Context, tc *TaskContext) (PRReviewDecision, error) {
|
||||||
ctx, cancel := context.WithTimeout(ctx, prReviewTotalBudget.WallClock)
|
ctx, cancel := context.WithTimeout(ctx, prReviewTotalBudget.WallClock)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
defer func() {
|
defer removePRSnapshot(tc)
|
||||||
if tc.prReview != nil && tc.prReview.snapshotDir != "" {
|
|
||||||
_ = os.RemoveAll(tc.prReview.snapshotDir)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
for _, stage := range prReviewDecisionStages() {
|
for _, stage := range prReviewDecisionStages() {
|
||||||
if err := ctx.Err(); err != nil {
|
if err := ctx.Err(); err != nil {
|
||||||
return PRReviewDecision{}, err
|
return PRReviewDecision{}, err
|
||||||
@@ -44,3 +40,11 @@ func ReviewPullRequest(ctx context.Context, tc *TaskContext) (PRReviewDecision,
|
|||||||
SkippedPhases: append([]string(nil), tc.prReview.skippedPhases...),
|
SkippedPhases: append([]string(nil), tc.prReview.skippedPhases...),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// removePRSnapshot deletes the snapshot anatomy materialized. Every runner of
|
||||||
|
// the pr-review phases defers it, since any phase may end the run early.
|
||||||
|
func removePRSnapshot(tc *TaskContext) {
|
||||||
|
if tc.prReview != nil && tc.prReview.snapshotDir != "" {
|
||||||
|
_ = os.RemoveAll(tc.prReview.snapshotDir)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -92,6 +92,7 @@ func stagePRReviewAndOpenPR(decisionStages []Stage, body func(*TaskContext) stri
|
|||||||
if tc.PRSource == nil {
|
if tc.PRSource == nil {
|
||||||
tc.PRSource = &localPRSource{tc: tc}
|
tc.PRSource = &localPRSource{tc: tc}
|
||||||
}
|
}
|
||||||
|
defer removePRSnapshot(tc)
|
||||||
for _, stage := range decisionStages {
|
for _, stage := range decisionStages {
|
||||||
if err := stage.Run(ctx, tc); err != nil {
|
if err := stage.Run(ctx, tc); err != nil {
|
||||||
return fmt.Errorf("pr-review (%s): %w", stage.Name, err)
|
return fmt.Errorf("pr-review (%s): %w", stage.Name, err)
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package workflow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||||
|
)
|
||||||
|
|
||||||
|
type failingSnapshotSource struct{ PRSource }
|
||||||
|
|
||||||
|
func (failingSnapshotSource) Snapshot(context.Context, string, string, int, string) (string, error) {
|
||||||
|
return "", errors.New("forge unreachable")
|
||||||
|
}
|
||||||
|
|
||||||
|
// A pr-review run that stops after anatomy must not leave its snapshot behind.
|
||||||
|
func TestPRReviewRemovesSnapshotOnEarlyStop(t *testing.T) {
|
||||||
|
tc := &TaskContext{Task: &task.Task{ID: 7}, PRSource: failingSnapshotSource{}, prReview: &prReviewState{}}
|
||||||
|
stage := stagePRReviewAndOpenPR([]Stage{stagePRAnatomy()}, func(*TaskContext) string { return "" })
|
||||||
|
if err := stage.Run(context.Background(), tc); err == nil {
|
||||||
|
t.Fatal("expected the snapshot failure to stop the run")
|
||||||
|
}
|
||||||
|
if tc.prReview.snapshotDir == "" {
|
||||||
|
t.Fatal("anatomy did not create a snapshot directory")
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(tc.prReview.snapshotDir); !errors.Is(err, os.ErrNotExist) {
|
||||||
|
t.Fatalf("snapshot directory %s still exists", tc.prReview.snapshotDir)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -43,6 +43,7 @@ func newReviewPRStage(settings yaml.Node) (Stage, error) {
|
|||||||
}
|
}
|
||||||
phases := prReviewStandaloneStages(s)
|
phases := prReviewStandaloneStages(s)
|
||||||
return Stage{Name: ReviewPRStepName, Run: func(ctx context.Context, tc *TaskContext) error {
|
return Stage{Name: ReviewPRStepName, Run: func(ctx context.Context, tc *TaskContext) error {
|
||||||
|
defer removePRSnapshot(tc)
|
||||||
for _, phase := range phases {
|
for _, phase := range phases {
|
||||||
if err := phase.Run(ctx, tc); err != nil {
|
if err := phase.Run(ctx, tc); err != nil {
|
||||||
return fmt.Errorf("%s (%s): %w", ReviewPRStepName, phase.Name, err)
|
return fmt.Errorf("%s (%s): %w", ReviewPRStepName, phase.Name, err)
|
||||||
|
|||||||
Reference in new issue
Block a user