feat(runs): add typed results and model aliases (#2043)
Also fix repository-free message.send validation (#2046) and add a single-binary development smoke path (#1977). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
55 files changed
+1162
-517
No files matched your search
+24
-29
@@ -1,7 +1,6 @@
|
||||
// Package drafts holds what an unmatched group of events could become: the
|
||||
// trigger it proposes, the workflow that trigger would start and any policy
|
||||
// that workflow needs, with a plain-language summary and a dry run. Nothing in
|
||||
// a draft is in force until it is published.
|
||||
// Package drafts manages proposed triggers, workflows and policies for groups
|
||||
// of unmatched events. Drafts can be tested by replaying a stored event in a
|
||||
// dry run before the trigger is enabled.
|
||||
package drafts
|
||||
|
||||
import (
|
||||
@@ -28,11 +27,10 @@ import (
|
||||
"github.com/samcharles93/archie-core/modules/runs"
|
||||
)
|
||||
|
||||
// replayable is how many of a group's newest events a dry run can choose from.
|
||||
// replayable limits dry runs to the group's most recently received events.
|
||||
const replayable = 50
|
||||
|
||||
// Groups is where a draft finds the unmatched events it is made from, and
|
||||
// where its trigger is saved.
|
||||
// Groups provides unmatched event groups, replay tracking and trigger storage.
|
||||
type Groups interface {
|
||||
Group(ctx context.Context, id uuid.UUID) (events.Group, bool, error)
|
||||
GroupEvents(ctx context.Context, id uuid.UUID, limit int) ([]events.Stored, error)
|
||||
@@ -40,7 +38,7 @@ type Groups interface {
|
||||
SaveTrigger(ctx context.Context, t events.TriggerSpec) error
|
||||
}
|
||||
|
||||
// Workflows stores workflow documents and runs them dry.
|
||||
// Workflows installs workflow documents and manages dry runs.
|
||||
type Workflows interface {
|
||||
Install(ctx context.Context, orgID, name, body, by string) (int, error)
|
||||
Has(ctx context.Context, orgID, name string) (bool, error)
|
||||
@@ -48,7 +46,8 @@ type Workflows interface {
|
||||
DryReport(ctx context.Context, orgID string, id uuid.UUID) (runs.DryReport, bool, error)
|
||||
}
|
||||
|
||||
// Policies checks a draft's policy against the set in force and adds it.
|
||||
// Policies validates a draft's policy against the current policy set and
|
||||
// adds it when the draft is published.
|
||||
type Policies interface {
|
||||
Check(ctx context.Context, kind, extra string) error
|
||||
Append(ctx context.Context, kind, extra, by string) (judgement.PolicySetView, error)
|
||||
@@ -90,7 +89,7 @@ func (m *Module) Register(api huma.API) {
|
||||
}, m.edit)
|
||||
huma.Register(api, huma.Operation{
|
||||
OperationID: "dry-run-draft", Method: http.MethodPost, Path: "/api/drafts/{id}/dry-run",
|
||||
Summary: "Replay one of the group's events through the draft, changing nothing", Tags: tags,
|
||||
Summary: "Replay a stored event against the draft workflow and policy in a dry run", Tags: tags,
|
||||
}, m.dryRun)
|
||||
huma.Register(api, huma.Operation{
|
||||
OperationID: "publish-draft", Method: http.MethodPost, Path: "/api/drafts/{id}/publish",
|
||||
@@ -102,7 +101,7 @@ type idInput struct {
|
||||
ID string `path:"id" format:"uuid"`
|
||||
}
|
||||
|
||||
// View is a draft as the API returns it.
|
||||
// View is the API representation of a draft.
|
||||
type View struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
GroupID uuid.UUID `json:"group_id"`
|
||||
@@ -114,7 +113,7 @@ type View struct {
|
||||
// Policy is Cedar policy text added to the set in force when published.
|
||||
Policy string `json:"policy"`
|
||||
Summary string `json:"summary"`
|
||||
// DryRun is the last dry run of the draft as it stands.
|
||||
// DryRun reports the latest dry run since the draft was last edited.
|
||||
DryRun *runs.DryReport `json:"dry_run,omitzero"`
|
||||
DryEventID *uuid.UUID `json:"dry_event_id,omitzero"`
|
||||
PublishedAt *time.Time `json:"published_at,omitzero"`
|
||||
@@ -159,8 +158,6 @@ var (
|
||||
policyKinds = map[string]string{judgement.KindHardRules: "hard rule", judgement.KindChangeRequest: "change request rule"}
|
||||
)
|
||||
|
||||
// summarize says in plain language when a draft fires and what it does. It is
|
||||
// built from the draft's own fields, so it can't disagree with them.
|
||||
func summarize(d store.Draft) string {
|
||||
var filter map[string]string
|
||||
_ = json.Unmarshal(d.Filter, &filter)
|
||||
@@ -197,7 +194,8 @@ func summarize(d store.Draft) string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// defaultName is a trigger name that is valid and tells groups apart.
|
||||
// defaultName uses part of the group ID to distinguish groups with the same
|
||||
// source and event type.
|
||||
func defaultName(g events.Group) string {
|
||||
slug := strings.Trim(nameChars.ReplaceAllString(strings.ToLower(g.Source+"-"+g.EventType), "-"), "-")
|
||||
if len(slug) > 50 {
|
||||
@@ -222,12 +220,12 @@ func (m *Module) list(ctx context.Context, _ *struct{}) (*httpapi.Reply[[]View],
|
||||
return httpapi.OK(out), nil
|
||||
}
|
||||
|
||||
// create makes a group's draft, or returns the one it has. A new draft
|
||||
// proposes the group's trigger and a copy of the investigate workflow.
|
||||
// create reuses an existing group draft rather than replacing any edits
|
||||
// with the default investigation workflow.
|
||||
func (m *Module) create(ctx context.Context, in *idInput) (*httpapi.Reply[View], error) {
|
||||
id, err := uuid.Parse(in.ID)
|
||||
if err != nil {
|
||||
return nil, huma.Error400BadRequest("an id is a UUID")
|
||||
return nil, huma.Error400BadRequest("ID must be a UUID")
|
||||
}
|
||||
g, found, err := m.groups.Group(ctx, id)
|
||||
switch {
|
||||
@@ -268,8 +266,6 @@ type editInput struct {
|
||||
}
|
||||
}
|
||||
|
||||
// edit replaces a draft's name, workflow and policy after checking them. What
|
||||
// doesn't validate changes nothing.
|
||||
func (m *Module) edit(ctx context.Context, in *editInput) (*httpapi.Reply[View], error) {
|
||||
d, err := m.draft(ctx, in.ID)
|
||||
if err != nil {
|
||||
@@ -295,7 +291,6 @@ func (m *Module) edit(ctx context.Context, in *editInput) (*httpapi.Reply[View],
|
||||
return m.respond(ctx, d)
|
||||
}
|
||||
|
||||
// check validates what a draft would put in force.
|
||||
func (m *Module) check(ctx context.Context, name, document, policyKind, policy string) error {
|
||||
if !validName.MatchString(name) {
|
||||
return &runs.InvalidError{Problems: []runs.Problem{{Path: "name", Message: "lowercase letters, digits and hyphens, starting with a letter"}}}
|
||||
@@ -331,8 +326,8 @@ type dryRunInput struct {
|
||||
}
|
||||
}
|
||||
|
||||
// dryRun replays one of the group's stored events through the draft's workflow
|
||||
// in dry mode: the newest by default.
|
||||
// dryRun tests the draft workflow and policy against a stored event. If the
|
||||
// caller omits the event ID, it replays the most recently received event.
|
||||
func (m *Module) dryRun(ctx context.Context, in *dryRunInput) (*httpapi.Reply[View], error) {
|
||||
d, err := m.draft(ctx, in.ID)
|
||||
if err != nil {
|
||||
@@ -372,7 +367,7 @@ func (m *Module) dryRun(ctx context.Context, in *dryRunInput) (*httpapi.Reply[Vi
|
||||
return m.respond(ctx, d)
|
||||
}
|
||||
|
||||
// pick returns the event asked for, or the newest when none is.
|
||||
// pick expects events in newest-first order so a missing ID selects the newest.
|
||||
func pick(stored []events.Stored, id uuid.UUID) (events.Stored, bool) {
|
||||
for _, e := range stored {
|
||||
if id == uuid.Nil || e.ID == id {
|
||||
@@ -382,10 +377,10 @@ func pick(stored []events.Stored, id uuid.UUID) (events.Stored, bool) {
|
||||
return events.Stored{}, false
|
||||
}
|
||||
|
||||
// publish puts a draft in force: its policy is added to the set, its workflow
|
||||
// is installed as the next version, and last its trigger is saved, so nothing
|
||||
// fires before the rules that govern it exist. A retry after a failure carries
|
||||
// on from where it stopped.
|
||||
// publish installs the policy and workflow before saving the trigger so the
|
||||
// trigger cannot fire before both are available. A recorded workflow version
|
||||
// lets a retry skip the name-conflict check. The retry still calls the policy
|
||||
// and workflow installers, which avoid duplicating unchanged content.
|
||||
func (m *Module) publish(ctx context.Context, in *idInput) (*httpapi.Reply[View], error) {
|
||||
d, err := m.draft(ctx, in.ID)
|
||||
if err != nil {
|
||||
@@ -438,7 +433,7 @@ func (m *Module) publish(ctx context.Context, in *idInput) (*httpapi.Reply[View]
|
||||
func (m *Module) draft(ctx context.Context, rawID string) (store.Draft, error) {
|
||||
id, err := uuid.Parse(rawID)
|
||||
if err != nil {
|
||||
return store.Draft{}, huma.Error400BadRequest("an id is a UUID")
|
||||
return store.Draft{}, huma.Error400BadRequest("ID must be a UUID")
|
||||
}
|
||||
d, err := m.store.ByID(ctx, m.orgID, id)
|
||||
switch {
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
// ErrNotFound means no such draft.
|
||||
var ErrNotFound = errors.New("no such draft")
|
||||
|
||||
// ErrTaken means another draft already has that name or that group.
|
||||
// ErrTaken indicates that another draft already uses the name.
|
||||
var ErrTaken = errors.New("name already used by another draft")
|
||||
|
||||
// Draft is one stored draft.
|
||||
@@ -34,7 +34,8 @@ type Draft struct {
|
||||
Policy string `db:"policy"`
|
||||
DryRunID uuid.NullUUID `db:"dry_run_id"`
|
||||
DryEventID uuid.NullUUID `db:"dry_event_id"`
|
||||
// InstalledVersion is the workflow version a publish installed, once one did.
|
||||
// InstalledVersion is recorded before the trigger is saved so a retry
|
||||
// can distinguish an earlier installation from a workflow name conflict.
|
||||
InstalledVersion sql.NullInt32 `db:"installed_version"`
|
||||
PublishedAt sql.NullTime `db:"published_at"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
@@ -52,8 +53,7 @@ type Store struct {
|
||||
// New returns a store on db.
|
||||
func New(db *sqlx.DB) *Store { return &Store{db: db} }
|
||||
|
||||
// Create stores d unless its group already has a draft, and returns the draft
|
||||
// the group has.
|
||||
// Create inserts a draft or returns the existing draft for the group.
|
||||
func (s *Store) Create(ctx context.Context, d Draft) (Draft, error) {
|
||||
_, err := s.db.ExecContext(ctx, `
|
||||
INSERT INTO drafts.drafts (id, org_id, group_id, name, source, event_type, filter, document, policy_kind, policy)
|
||||
@@ -98,8 +98,8 @@ func (s *Store) List(ctx context.Context, orgID string) ([]Draft, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Edit replaces what a person edits of an unpublished draft. A dry run made of
|
||||
// an earlier version no longer describes it, so it is cleared.
|
||||
// Edit updates an unpublished draft and clears its dry-run references because
|
||||
// the previous run no longer represents the current draft.
|
||||
func (s *Store) Edit(ctx context.Context, orgID string, id uuid.UUID, name, document, policyKind, policy string) error {
|
||||
res, err := s.db.ExecContext(ctx, `
|
||||
UPDATE drafts.drafts
|
||||
@@ -118,7 +118,7 @@ func (s *Store) Edit(ctx context.Context, orgID string, id uuid.UUID, name, docu
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetDryRun records the dry run last made of a draft and the event it replayed.
|
||||
// SetDryRun records the latest dry run and the event it replayed.
|
||||
func (s *Store) SetDryRun(ctx context.Context, orgID string, id, runID, eventID uuid.UUID) error {
|
||||
if _, err := s.db.ExecContext(ctx, `
|
||||
UPDATE drafts.drafts SET dry_run_id = $3, dry_event_id = $4, updated_at = now()
|
||||
@@ -128,7 +128,7 @@ func (s *Store) SetDryRun(ctx context.Context, orgID string, id, runID, eventID
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarkInstalled records the workflow version a publish installed.
|
||||
// MarkInstalled records the workflow version installed during publication.
|
||||
func (s *Store) MarkInstalled(ctx context.Context, orgID string, id uuid.UUID, version int) error {
|
||||
if _, err := s.db.ExecContext(ctx, `
|
||||
UPDATE drafts.drafts SET installed_version = $3, updated_at = now() WHERE org_id = $1 AND id = $2`,
|
||||
@@ -138,7 +138,7 @@ func (s *Store) MarkInstalled(ctx context.Context, orgID string, id uuid.UUID, v
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarkPublished records that a draft is in force.
|
||||
// MarkPublished records the publication time without overwriting it on retry.
|
||||
func (s *Store) MarkPublished(ctx context.Context, orgID string, id uuid.UUID) error {
|
||||
if _, err := s.db.ExecContext(ctx, `
|
||||
UPDATE drafts.drafts SET published_at = COALESCE(published_at, now()), updated_at = now()
|
||||
|
||||
Reference in new issue
Block a user