package workflow import ( "context" "fmt" "github.com/samcharles93/archie-core/internal/events" ) // Deliveries is the one door a run's outward effects pass through: a forge // write, a push, an HTTP post. There is one implementation, recordDeliveries, // because a live run needs none -- a nil TaskContext.Deliveries performs. type Deliveries interface { // Record records one effect the run must not perform: what it would have // done, phrased without the leading "would". The caller returns success as // though it had been delivered, so the rest of the step runs as it would // have. Record(ctx context.Context, detail string) } // RecordDeliveries makes tc record every outward delivery its steps ask for // and perform none of them, so an operator can run a draft workflow and see // what it would do without it reaching anyone. The forge and push writes are // wrapped here; a Kit run's network traffic and central tool calls are // recorded by the egress proxy (egress.RecordOptions), which is also why a // recording command.run still executes: only its network egress is recorded. A // native container's own network has no proxy in front of it, so nothing it // sends is recorded. func RecordDeliveries(tc *TaskContext) { tc.Deliveries = recordDeliveries{tc: tc} tc.Forge = recordedForge{Forger: tc.Forge, deliveries: tc.Deliveries} tc.Trees = recordedTrees{Trees: tc.Trees, deliveries: tc.Deliveries} } type recordDeliveries struct { tc *TaskContext } func (r recordDeliveries) Record(ctx context.Context, detail string) { r.tc.Emit(ctx, events.KindDeliveryRecorded, r.tc.Stage, "would "+detail, nil) } type recordedForge struct { Forger deliveries Deliveries } func (f recordedForge) CloseIssue(ctx context.Context, owner, repo string, number int, _ string) error { f.deliveries.Record(ctx, fmt.Sprintf("close issue %s/%s#%d", owner, repo, number)) return nil } func (f recordedForge) CreatePR(ctx context.Context, owner, repo, title, head, base, _ string) (int, error) { f.deliveries.Record(ctx, fmt.Sprintf("open PR %q in %s/%s from %s into %s", title, owner, repo, head, base)) return 0, nil } func (f recordedForge) LinkBranch(ctx context.Context, owner, repo string, number int, branch string) error { f.deliveries.Record(ctx, fmt.Sprintf("link %s to %s/%s#%d", branch, owner, repo, number)) return nil } func (f recordedForge) CreateReviewComments(ctx context.Context, owner, repo string, number int, _ string, review Review) error { f.deliveries.Record(ctx, fmt.Sprintf("post a %s review with %d comments on %s/%s#%d", review.Event, len(review.Comments), owner, repo, number)) return nil } func (f recordedForge) Comment(ctx context.Context, owner, repo string, number int, body string) (int64, error) { f.deliveries.Record(ctx, fmt.Sprintf("comment on %s/%s#%d: %s", owner, repo, number, body)) return 0, nil } func (f recordedForge) ReplyToReview(ctx context.Context, owner, repo string, number int, _ int64, body string) error { f.deliveries.Record(ctx, fmt.Sprintf("reply to a review on %s/%s#%d: %s", owner, repo, number, body)) return nil } type recordedTrees struct { Trees deliveries Deliveries } func (t recordedTrees) Push(ctx context.Context, _, branch string) error { t.deliveries.Record(ctx, fmt.Sprintf("push branch %s", branch)) return nil }