108 lines
3.9 KiB
Go
108 lines
3.9 KiB
Go
package workflow
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// Review step types: reviewing a pull request, and answering a review of
|
|
// archie's own.
|
|
const (
|
|
ReviewPRStepName = "review.pr"
|
|
ReviewRoundStepName = "review.start-round"
|
|
ReviewReplyStepName = "review.reply"
|
|
)
|
|
|
|
// ReviewStepTypes contributes the review step types.
|
|
func ReviewStepTypes() []StepType {
|
|
return []StepType{
|
|
{Name: ReviewPRStepName, Factory: newReviewPRStage, Settings: reviewPRSettings{}},
|
|
{Name: ReviewRoundStepName, Factory: newReviewRoundStage},
|
|
{Name: ReviewReplyStepName, Factory: newReviewReplyStage, Settings: reviewReplySettings{}},
|
|
}
|
|
}
|
|
|
|
// reviewPRSettings are off by default: the pipeline is recall-first and
|
|
// posts automatically unless the workflow opts into either.
|
|
type reviewPRSettings struct {
|
|
PrecisionGate bool `yaml:"precision_gate" doc:"Drop findings not worth posting before verification."`
|
|
ApproveBeforePost bool `yaml:"approve_before_post" doc:"Wait for operator approval before posting findings."`
|
|
}
|
|
|
|
// newReviewPRStage runs the whole review pipeline on the task's pull request:
|
|
// intake, anatomy, lenses, review, precision gate, verification, coverage,
|
|
// synthesis, operator approval, merge gate and output. Its phases share
|
|
// private state, so they run as one step.
|
|
func newReviewPRStage(settings yaml.Node) (Stage, error) {
|
|
s, err := decodeSettings[reviewPRSettings](ReviewPRStepName, settings)
|
|
if err != nil {
|
|
return Stage{}, err
|
|
}
|
|
phases := prReviewStandaloneStages(s)
|
|
return Stage{Name: ReviewPRStepName, Run: func(ctx context.Context, tc *TaskContext) error {
|
|
defer removePRSnapshot(tc)
|
|
for _, phase := range phases {
|
|
if err := phase.Run(ctx, tc); err != nil {
|
|
return fmt.Errorf("%s (%s): %w", ReviewPRStepName, phase.Name, err)
|
|
}
|
|
if tc.Outcome.Status != "" {
|
|
return nil
|
|
}
|
|
}
|
|
return nil
|
|
}}, nil
|
|
}
|
|
|
|
// newReviewRoundStage opens one remediation round: it refuses a review
|
|
// payload that does not decode, parks with a PR comment once the repository's
|
|
// round cap is reached, and otherwise counts the round.
|
|
func newReviewRoundStage(settings yaml.Node) (Stage, error) {
|
|
if err := noSettings(ReviewRoundStepName, settings); err != nil {
|
|
return Stage{}, err
|
|
}
|
|
check, roundCap := StageCheckReviewPayload(), StageRemediationRoundCap()
|
|
return Stage{Name: ReviewRoundStepName, Run: func(ctx context.Context, tc *TaskContext) error {
|
|
if err := check.Run(ctx, tc); err != nil {
|
|
return err
|
|
}
|
|
if err := roundCap.Run(ctx, tc); err != nil {
|
|
return err
|
|
}
|
|
tc.reviewUnit, _ = DecodeReviewUnit(tc.Task.ReviewPayload)
|
|
return nil
|
|
}}, nil
|
|
}
|
|
|
|
type reviewReplySettings struct {
|
|
Body string `yaml:"body" doc:"The reply to the review."`
|
|
}
|
|
|
|
// newReviewReplyStage answers the review this round addressed, in its own
|
|
// thread when it has one, clears the consumed payload so a resumed run cannot
|
|
// address it twice, and returns the task to pr_open.
|
|
func newReviewReplyStage(settings yaml.Node) (Stage, error) {
|
|
s, err := decodeSettings[reviewReplySettings](ReviewReplyStepName, settings)
|
|
if err != nil {
|
|
return Stage{}, err
|
|
}
|
|
if strings.TrimSpace(s.Body) == "" {
|
|
return Stage{}, fmt.Errorf("%s: settings.body is required", ReviewReplyStepName)
|
|
}
|
|
return Stage{Name: ReviewReplyStepName, Run: func(ctx context.Context, tc *TaskContext) error {
|
|
reply := fmt.Sprintf("%s\n\n---\n*archie remediation, round %d*", s.Body, tc.Task.RemediationRounds)
|
|
if id, ok := tc.reviewUnit.replyTarget(); ok {
|
|
if err := tc.Forge.ReplyToReview(ctx, tc.Task.Owner, tc.Task.Repo, tc.Task.PRNumber, id, reply); err != nil {
|
|
tc.Log.WarnContext(ctx, "remediation reply not posted", "pr", tc.Task.PRNumber, "comment", id, "err", err)
|
|
}
|
|
} else {
|
|
postRemediationComment(ctx, tc, reply)
|
|
}
|
|
tc.Task.ReviewPayload = ""
|
|
tc.Outcome = Outcome{Status: StatusPROpen, Detail: fmt.Sprintf("remediated review round %d", tc.Task.RemediationRounds)}
|
|
return nil
|
|
}}, nil
|
|
}
|