147 lines
4.8 KiB
Go
147 lines
4.8 KiB
Go
package runs
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/samcharles93/archie-core/modules/judgement"
|
|
)
|
|
|
|
// ProposalWorkflow is the workflow that takes the actions of an approved
|
|
// proposal. Its input is the proposal's id.
|
|
const ProposalWorkflow = "run-proposal"
|
|
|
|
// proposalDocument runs an approved proposal's actions in order.
|
|
const proposalDocument = `inputs:
|
|
proposal_id:
|
|
type: string
|
|
required: true
|
|
repository: none
|
|
steps:
|
|
- id: actions
|
|
type: proposal.actions
|
|
`
|
|
|
|
// ProposalDocument defines the workflow that ships with the binary to run an
|
|
// approved proposal's actions.
|
|
func ProposalDocument() string { return proposalDocument }
|
|
|
|
// The states an action of a proposal settles as.
|
|
const (
|
|
ActionRan = "ran"
|
|
ActionFailed = "failed"
|
|
ActionUnknown = "unknown"
|
|
ActionRefused = "refused"
|
|
ActionSkipped = "skipped"
|
|
)
|
|
|
|
// ApprovedAction is one action of an approved proposal: the decision it was
|
|
// judged under when proposed, and the approval the proposal's answer gave it.
|
|
type ApprovedAction struct {
|
|
Seq int
|
|
Decision judgement.Decision
|
|
Approval judgement.Approval
|
|
}
|
|
|
|
// Proposals provides the actions of an approved proposal and records how each
|
|
// one settled.
|
|
type Proposals interface {
|
|
// ApprovedActions returns the proposal's actions in order, only while the
|
|
// proposal is approved and each approval covers exactly its action.
|
|
ApprovedActions(ctx context.Context, proposalID uuid.UUID) ([]ApprovedAction, error)
|
|
// SettleAction records how one action ended. Settling it again changes
|
|
// nothing.
|
|
SettleAction(ctx context.Context, proposalID uuid.UUID, seq int, state, outcome string) error
|
|
}
|
|
|
|
type proposalActions struct{}
|
|
|
|
func (proposalActions) valid() error { return nil }
|
|
|
|
func (proposalActions) describe() string {
|
|
return "Runs the actions of an approved proposal in order, each judged again first, and stops at the first that does not run"
|
|
}
|
|
|
|
// runProposalActions takes a proposal's actions one at a time. Each is judged
|
|
// again and runs only if that judgement still allows what the approval
|
|
// covered. An action that does not run stops the rest, which settle as
|
|
// skipped. Every settlement is its own step, so a resumed run repeats none.
|
|
func runProposalActions(d Deps, rc *Context, p *progress, _ string, _ proposalActions) (StepResult, error) {
|
|
if d.Proposals == nil {
|
|
return StepResult{}, errors.New("proposals are not available")
|
|
}
|
|
id, err := uuid.Parse(fmt.Sprint(p.inputs["proposal_id"]))
|
|
if err != nil {
|
|
return StepResult{}, fmt.Errorf("proposal_id is not a proposal id: %w", err)
|
|
}
|
|
actions, err := rc.Step("proposal-actions", func(ctx context.Context, _ string) ([]ApprovedAction, error) {
|
|
return d.Proposals.ApprovedActions(ctx, id)
|
|
})
|
|
if err != nil {
|
|
return StepResult{}, err
|
|
}
|
|
stopped, ran := false, 0
|
|
for _, a := range actions {
|
|
state, outcome := ActionSkipped, "an earlier action did not run"
|
|
if !stopped {
|
|
out, err := takeProposed(rc, d.Judge, a)
|
|
if err != nil {
|
|
return StepResult{}, err
|
|
}
|
|
p.outcomes = append(p.outcomes, out)
|
|
state, outcome = settlement(out)
|
|
stopped = state != ActionRan
|
|
}
|
|
if state == ActionRan {
|
|
ran++
|
|
}
|
|
if _, err := rc.Step(fmt.Sprintf("settle:%d", a.Seq), func(ctx context.Context, _ string) (bool, error) {
|
|
return true, d.Proposals.SettleAction(ctx, id, a.Seq, state, outcome)
|
|
}); err != nil {
|
|
return StepResult{}, err
|
|
}
|
|
}
|
|
return StepResult{Summary: fmt.Sprintf("%d of %d actions ran", ran, len(actions))}, nil
|
|
}
|
|
|
|
// takeProposed judges one approved action again and runs it if the approval
|
|
// still holds, as for an approval given during a run.
|
|
func takeProposed(rc *Context, judge Judge, a ApprovedAction) (approvalOutcome, error) {
|
|
d := a.Decision
|
|
out := approvalOutcome{
|
|
Action: d.Action.Connector + " " + d.Action.Name, Target: d.Action.Target.Kind + ":" + d.Action.Target.ID,
|
|
State: "approved", digest: d.Action.Digest, By: a.Approval.By,
|
|
}
|
|
fresh, err := rc.Step(fmt.Sprintf("judge-again:%d", a.Seq), func(ctx context.Context, key string) (judgement.Decision, error) {
|
|
return judge.Assess(ctx, rc.request(key, d.Action.Connector, d.Action.Name, d.Action.Input))
|
|
})
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
switch next, why := afterApproval(d, fresh); next {
|
|
case refuseAction, reaskPerson:
|
|
out.State, out.Error = ActionRefused, why
|
|
case runFresh:
|
|
out.Result, out.Error, out.Unknown = runJudged(rc, judge, fresh, nil)
|
|
default:
|
|
out.Result, out.Error, out.Unknown = runJudged(rc, judge, d, &a.Approval)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// settlement is the state and text an action's outcome settles as.
|
|
func settlement(o approvalOutcome) (state, text string) {
|
|
switch {
|
|
case o.State == ActionRefused:
|
|
return ActionRefused, o.Error
|
|
case o.Unknown:
|
|
return ActionUnknown, o.Error
|
|
case o.Error != "":
|
|
return ActionFailed, o.Error
|
|
}
|
|
return ActionRan, o.Result
|
|
}
|