A source is keyed by a uuid, owned by the organisation or one person, and labelled within that scope. The webhook path is a separate unique column, so renaming either leaves stored events, event types, triggers, groups, mappings and drafts alone. Delivery policies, sealed secrets and group keys use the id.
This commit is contained in:
54 files changed
+1444
-682
No files matched your search
@@ -122,17 +122,23 @@ type View struct {
|
||||
|
||||
// Trigger is the trigger a draft proposes.
|
||||
type Trigger struct {
|
||||
Source string `json:"source"`
|
||||
SourceID uuid.UUID `json:"source_id"`
|
||||
// Source is the source's label, empty when the source no longer exists.
|
||||
Source string `json:"source,omitzero"`
|
||||
EventType string `json:"event_type"`
|
||||
Filter jsontext.Value `json:"filter"`
|
||||
}
|
||||
|
||||
func (m *Module) view(ctx context.Context, d store.Draft) (View, error) {
|
||||
label, err := m.sourceLabel(ctx, d.GroupID)
|
||||
if err != nil {
|
||||
return View{}, err
|
||||
}
|
||||
v := View{
|
||||
ID: d.ID, GroupID: d.GroupID, Name: d.Name,
|
||||
Trigger: Trigger{Source: d.Source, EventType: d.EventType, Filter: d.Filter},
|
||||
Trigger: Trigger{SourceID: d.SourceID, Source: label, EventType: d.EventType, Filter: d.Filter},
|
||||
Workflow: d.Document, PolicyKind: d.PolicyKind, Policy: d.Policy,
|
||||
Summary: summarize(d),
|
||||
Summary: summarize(d, label),
|
||||
}
|
||||
if d.PublishedAt.Valid {
|
||||
v.PublishedAt = &d.PublishedAt.Time
|
||||
@@ -159,11 +165,24 @@ var (
|
||||
policyKinds = map[string]string{judgement.KindHardRules: "hard rule", judgement.KindChangeRequest: "change request rule"}
|
||||
)
|
||||
|
||||
func summarize(d store.Draft) string {
|
||||
// sourceLabel returns the label of the source of a draft's group, or "" when
|
||||
// the group went with its source.
|
||||
func (m *Module) sourceLabel(ctx context.Context, groupID uuid.UUID) (string, error) {
|
||||
g, found, err := m.groups.Group(ctx, groupID)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read group: %w", err)
|
||||
}
|
||||
if !found {
|
||||
return "", nil
|
||||
}
|
||||
return g.Source, nil
|
||||
}
|
||||
|
||||
func summarize(d store.Draft, source string) string {
|
||||
var filter map[string]string
|
||||
_ = json.Unmarshal(d.Filter, &filter)
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "When %s sends %s", d.Source, d.EventType)
|
||||
fmt.Fprintf(&b, "When %s sends %s", source, d.EventType)
|
||||
if len(filter) > 0 {
|
||||
pairs := make([]string, 0, len(filter))
|
||||
for k, v := range filter {
|
||||
@@ -240,7 +259,7 @@ func (m *Module) create(ctx context.Context, in *idInput) (*httpapi.Reply[View],
|
||||
return nil, m.fail(ctx, "encode filter", err)
|
||||
}
|
||||
d, err := m.store.Create(ctx, store.Draft{
|
||||
ID: uuid.New(), OrgID: m.orgID, GroupID: g.ID, Name: defaultName(g), Source: g.Source, EventType: g.EventType,
|
||||
ID: uuid.New(), OrgID: m.orgID, GroupID: g.ID, Name: defaultName(g), SourceID: g.SourceID, EventType: g.EventType,
|
||||
Filter: filter, Document: runs.InvestigateDocument(), PolicyKind: judgement.KindHardRules,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -417,7 +436,7 @@ func (m *Module) publish(ctx context.Context, in *idInput) (*httpapi.Reply[View]
|
||||
return nil, m.fail(ctx, "read the trigger filter", err)
|
||||
}
|
||||
if err := m.groups.SaveTrigger(ctx, events.TriggerSpec{
|
||||
Name: d.Name, Source: d.Source, EventType: d.EventType, Filter: filter, Workflow: d.Name,
|
||||
Name: d.Name, SourceID: d.SourceID, EventType: d.EventType, Filter: filter, Workflow: d.Name,
|
||||
}); err != nil {
|
||||
return nil, m.fail(ctx, "save the trigger", err)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ type Draft struct {
|
||||
OrgID string `db:"org_id"`
|
||||
GroupID uuid.UUID `db:"group_id"`
|
||||
Name string `db:"name"`
|
||||
Source string `db:"source"`
|
||||
SourceID uuid.UUID `db:"source_id"`
|
||||
EventType string `db:"event_type"`
|
||||
Filter jsontext.Value `db:"filter"`
|
||||
Document string `db:"document"`
|
||||
@@ -42,7 +42,7 @@ type Draft struct {
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
const columns = `id, org_id, group_id, name, source, event_type, filter, document, policy_kind, policy,
|
||||
const columns = `id, org_id, group_id, name, source_id, event_type, filter, document, policy_kind, policy,
|
||||
dry_run_id, dry_event_id, installed_version, published_at, created_at, updated_at`
|
||||
|
||||
// Store is the drafts module's Postgres store.
|
||||
@@ -56,10 +56,10 @@ func New(db *sqlx.DB) *Store { return &Store{db: db} }
|
||||
// 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)
|
||||
INSERT INTO drafts.drafts (id, org_id, group_id, name, source_id, event_type, filter, document, policy_kind, policy)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
ON CONFLICT (org_id, group_id) DO NOTHING`,
|
||||
d.ID, d.OrgID, d.GroupID, d.Name, d.Source, d.EventType, d.Filter, d.Document, d.PolicyKind, d.Policy)
|
||||
d.ID, d.OrgID, d.GroupID, d.Name, d.SourceID, d.EventType, d.Filter, d.Document, d.PolicyKind, d.Policy)
|
||||
if err != nil {
|
||||
return Draft{}, fmt.Errorf("create draft: %w", err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user