Files
archie/internal/domain/workflow/finish_step.go
T

48 lines
1.4 KiB
Go

package workflow
import (
"context"
"fmt"
"gopkg.in/yaml.v3"
)
// FinishStepName is the step type that ends the workflow with a chosen outcome.
const FinishStepName = "workflow.finish"
// finishStatuses are the outcomes a workflow.finish step may choose.
var finishStatuses = map[string]string{
"completed": StatusCompleted,
"parked": StatusParked,
"waiting_approval": StatusWaitingApproval,
"declined": StatusClosedWontDo,
}
type finishSettings struct {
Status string `yaml:"status" enum:"completed,parked,waiting_approval,declined" doc:"How the run ends. Empty means completed."`
Detail string `yaml:"detail" doc:"What the operator reads about the outcome."`
}
// FinishStepType contributes the workflow.finish step type.
func FinishStepType() StepType {
return StepType{Name: FinishStepName, Factory: newFinishStage, Settings: finishSettings{}}
}
func newFinishStage(settings yaml.Node) (Stage, error) {
s, err := decodeSettings[finishSettings](FinishStepName, settings)
if err != nil {
return Stage{}, err
}
if s.Status == "" {
s.Status = "completed"
}
status, ok := finishStatuses[s.Status]
if !ok {
return Stage{}, fmt.Errorf("%s: settings.status is completed, parked, waiting_approval or declined, not %q", FinishStepName, s.Status)
}
return Stage{Name: FinishStepName, Run: func(_ context.Context, tc *TaskContext) error {
tc.Outcome = Outcome{Status: status, Detail: s.Detail}
return nil
}}, nil
}