* fix(postgres): scope OpenPRs to the caller's org A principal-carrying caller read every org's pr_open tasks. The query now honours the request scope; the daemon's unscoped review scan still sees all orgs. Issue: archie-core-kgxw.23 * fix(statestore): serve task logs only to the task's org ReadTaskLog and StreamTaskLogContent took a task ID and read the file with no org check. The State Store's task-log reader now confirms the task is visible under the caller's scope first; unscoped services read as before. Issue: archie-core-kgxw.24 * fix(postgres): scope ListPolicies to the caller's org Every org's policy text reached any principal-carrying caller. A scoped read now sees its own org's policies, plus instance policies when scoped to the system org. The engines and the lockout guard decide for every org, so they read the full chain unscoped: the dashboard engine loads without the request's principal and the guard lifts the caller's scope. Issue: archie-core-kgxw.25 * fix(postgres): scope identity reads to the caller's org ListIdentities and GetIdentity (and its alias fallback) returned every identity to any caller. A caller scoped to an org now sees only that org's members and assigned agents; the system org and unscoped services still see the whole instance. Issue: archie-core-kgxw.26 * fix(postgres): hide instance status from other orgs ListPresence, ListChannelStatus, ListApplyStatus and GetConfigSnapshot answered any org principal with instance-wide state. They now return nothing unless the request is unscoped or scoped to the system org. The dashboard reads the published config as itself, since its handlers project it for callers of every org. Issue: archie-core-kgxw.27 * feat(postgres): assign new agents an org and role at creation A bot or service account created through the API served no org until the next State Store boot granted it one. Create now assigns it to the org the request acts in with the developer role in the same transaction, so it is authorized immediately. Issue: archie-core-kgxw.14 * feat(edastore): create event records in a chosen workspace Sources are created in a workspace of the caller's org, the default one when none is named; event types and bindings take their source's workspace and mappings their event type's, the same rule the org backfill applied. The binding, mapping and event type contracts carry the workspace, the four list endpoints accept ?workspace=, and the Events page has a workspace selector that filters every step and picks where a new source goes. Issue: archie-core-kgxw.11 * feat(webui): authorize a record in its workspace The API built every access request with no workspace, so workspace policies and per-workspace roles never applied. A request naming a source, binding, mapping, event type or task now reads the record's workspace under the caller's scope and authorizes in it; tasks carry their workspace on the State Store contract for this. Issue: archie-core-kgxw.9 * feat(access): expose workspace environment to policies Policies can test resource.workspace.environment. The workspace attribute is the workspace entity, which carries its environment; the principal brings its org's workspaces from the State Store, so the dashboard and the daemon's run checks describe a resource's workspace without another lookup, and the daemon now authorizes runs in the task's or binding's workspace. Issue: archie-core-kgxw.10 * fix(webui): ask read_secret when a source secret is read Rotating a source's secret returns its value, but the route asked update, which every developer holds, so no policy could narrow who reads a secret. It now asks read_secret on the secret, which the shipped roles give admins and an object policy can narrow further. Issue: archie-core-kgxw.17 * refactor(org): delete the unused upgrade-incomplete sentinel Nothing returns org.ErrUpgradeIncomplete: the State Store finishes the org upgrade before it serves. Issue: archie-core-kgxw.18
1377 lines
35 KiB
Go
1377 lines
35 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: edastore.sql
|
|
|
|
package postgresdb
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const armedBindingsForSource = `-- name: ArmedBindingsForSource :many
|
|
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id, workspace_id
|
|
FROM bindings WHERE source = $1 AND status = 'armed' AND ($2::text IS NULL OR org_id = $2) ORDER BY created_at DESC
|
|
`
|
|
|
|
type ArmedBindingsForSourceParams struct {
|
|
Source string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
type ArmedBindingsForSourceRow struct {
|
|
ID string
|
|
Name string
|
|
Source string
|
|
Mapping string
|
|
Workflow string
|
|
Owner string
|
|
Repo string
|
|
Version int64
|
|
Status string
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
Filter string
|
|
Inputs string
|
|
RepoParam string
|
|
OrgID string
|
|
WorkspaceID string
|
|
}
|
|
|
|
func (q *Queries) ArmedBindingsForSource(ctx context.Context, arg ArmedBindingsForSourceParams) ([]ArmedBindingsForSourceRow, error) {
|
|
rows, err := q.db.Query(ctx, armedBindingsForSource, arg.Source, arg.ScopeOrg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ArmedBindingsForSourceRow
|
|
for rows.Next() {
|
|
var i ArmedBindingsForSourceRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Source,
|
|
&i.Mapping,
|
|
&i.Workflow,
|
|
&i.Owner,
|
|
&i.Repo,
|
|
&i.Version,
|
|
&i.Status,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Filter,
|
|
&i.Inputs,
|
|
&i.RepoParam,
|
|
&i.OrgID,
|
|
&i.WorkspaceID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const captureRefusalSummary = `-- name: CaptureRefusalSummary :many
|
|
WITH r AS (
|
|
SELECT source, count(*) AS refused, array_agg(DISTINCT addr ORDER BY addr)::text[] AS addrs
|
|
FROM capture_refusals cr WHERE refused_at >= $1
|
|
AND ($2::text IS NULL
|
|
OR EXISTS (SELECT 1 FROM sources s WHERE s.path = cr.source AND s.org_id = $2))
|
|
GROUP BY source
|
|
), a AS (
|
|
SELECT source, count(*) AS accepted FROM captures WHERE received_at >= $1 GROUP BY source
|
|
)
|
|
SELECT r.source, r.refused, COALESCE(a.accepted, 0)::bigint AS accepted, r.addrs
|
|
FROM r LEFT JOIN a ON a.source = r.source
|
|
ORDER BY r.source
|
|
`
|
|
|
|
type CaptureRefusalSummaryParams struct {
|
|
Since time.Time
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
type CaptureRefusalSummaryRow struct {
|
|
Source string
|
|
Refused int64
|
|
Accepted int64
|
|
Addrs []string
|
|
}
|
|
|
|
func (q *Queries) CaptureRefusalSummary(ctx context.Context, arg CaptureRefusalSummaryParams) ([]CaptureRefusalSummaryRow, error) {
|
|
rows, err := q.db.Query(ctx, captureRefusalSummary, arg.Since, arg.ScopeOrg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []CaptureRefusalSummaryRow
|
|
for rows.Next() {
|
|
var i CaptureRefusalSummaryRow
|
|
if err := rows.Scan(
|
|
&i.Source,
|
|
&i.Refused,
|
|
&i.Accepted,
|
|
&i.Addrs,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const deleteBinding = `-- name: DeleteBinding :execrows
|
|
DELETE FROM bindings WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
|
|
`
|
|
|
|
type DeleteBindingParams struct {
|
|
ID string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) DeleteBinding(ctx context.Context, arg DeleteBindingParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deleteBinding, arg.ID, arg.ScopeOrg)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const deleteCaptureRefusalsBefore = `-- name: DeleteCaptureRefusalsBefore :exec
|
|
DELETE FROM capture_refusals WHERE refused_at < $1
|
|
`
|
|
|
|
func (q *Queries) DeleteCaptureRefusalsBefore(ctx context.Context, refusedAt time.Time) error {
|
|
_, err := q.db.Exec(ctx, deleteCaptureRefusalsBefore, refusedAt)
|
|
return err
|
|
}
|
|
|
|
const deleteCapturesBeyondCount = `-- name: DeleteCapturesBeyondCount :exec
|
|
DELETE FROM captures AS c
|
|
WHERE c.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = $1)
|
|
AND c.id NOT IN (
|
|
SELECT k.id FROM captures AS k
|
|
WHERE k.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = $1)
|
|
ORDER BY k.received_at DESC
|
|
LIMIT $2
|
|
)
|
|
AND NOT (
|
|
c.event_type <> '' AND (c.authenticated OR c.unsigned)
|
|
AND EXISTS (
|
|
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
|
|
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
|
|
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
|
|
)
|
|
)
|
|
`
|
|
|
|
type DeleteCapturesBeyondCountParams struct {
|
|
CaptureID string
|
|
MaxEvents int32
|
|
}
|
|
|
|
// Count retention stays within the just-inserted capture's org: one org's
|
|
// volume must never evict another org's recent captures. It never evicts a
|
|
// capture ListUndispatchedCaptures would still offer: that is accepted work.
|
|
func (q *Queries) DeleteCapturesBeyondCount(ctx context.Context, arg DeleteCapturesBeyondCountParams) error {
|
|
_, err := q.db.Exec(ctx, deleteCapturesBeyondCount, arg.CaptureID, arg.MaxEvents)
|
|
return err
|
|
}
|
|
|
|
const deleteCapturesOlderThan = `-- name: DeleteCapturesOlderThan :exec
|
|
DELETE FROM captures WHERE received_at < $1
|
|
`
|
|
|
|
func (q *Queries) DeleteCapturesOlderThan(ctx context.Context, receivedAt time.Time) error {
|
|
_, err := q.db.Exec(ctx, deleteCapturesOlderThan, receivedAt)
|
|
return err
|
|
}
|
|
|
|
const deleteEventType = `-- name: DeleteEventType :execrows
|
|
DELETE FROM event_types WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
|
|
`
|
|
|
|
type DeleteEventTypeParams struct {
|
|
ID string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) DeleteEventType(ctx context.Context, arg DeleteEventTypeParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deleteEventType, arg.ID, arg.ScopeOrg)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const deleteMapping = `-- name: DeleteMapping :execrows
|
|
DELETE FROM mappings WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
|
|
`
|
|
|
|
type DeleteMappingParams struct {
|
|
ID string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) DeleteMapping(ctx context.Context, arg DeleteMappingParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deleteMapping, arg.ID, arg.ScopeOrg)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const deletePlaybookDispatches = `-- name: DeletePlaybookDispatches :exec
|
|
DELETE FROM playbook_dispatches WHERE playbook_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeletePlaybookDispatches(ctx context.Context, playbookID string) error {
|
|
_, err := q.db.Exec(ctx, deletePlaybookDispatches, playbookID)
|
|
return err
|
|
}
|
|
|
|
const deleteUnboundSource = `-- name: DeleteUnboundSource :execrows
|
|
DELETE FROM sources s WHERE s.path = $1 AND ($2::text IS NULL OR s.org_id = $2)
|
|
AND NOT EXISTS (SELECT 1 FROM bindings b WHERE b.source = s.path AND b.status = 'armed')
|
|
`
|
|
|
|
type DeleteUnboundSourceParams struct {
|
|
Path string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
// A source an armed binding fires on is not deleted: the binding would
|
|
// silently stop receiving events.
|
|
func (q *Queries) DeleteUnboundSource(ctx context.Context, arg DeleteUnboundSourceParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deleteUnboundSource, arg.Path, arg.ScopeOrg)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const deriveSources = `-- name: DeriveSources :exec
|
|
SELECT derive_sources()
|
|
`
|
|
|
|
func (q *Queries) DeriveSources(ctx context.Context) error {
|
|
_, err := q.db.Exec(ctx, deriveSources)
|
|
return err
|
|
}
|
|
|
|
const eventTypesForSource = `-- name: EventTypesForSource :many
|
|
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
|
|
FROM event_types WHERE source = $1 ORDER BY name
|
|
`
|
|
|
|
func (q *Queries) EventTypesForSource(ctx context.Context, source string) ([]EventType, error) {
|
|
rows, err := q.db.Query(ctx, eventTypesForSource, source)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []EventType
|
|
for rows.Next() {
|
|
var i EventType
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Source,
|
|
&i.Name,
|
|
&i.Rule,
|
|
&i.Schema,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.OrgID,
|
|
&i.WorkspaceID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const getBinding = `-- name: GetBinding :one
|
|
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id, workspace_id
|
|
FROM bindings WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
|
|
`
|
|
|
|
type GetBindingParams struct {
|
|
ID string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
type GetBindingRow struct {
|
|
ID string
|
|
Name string
|
|
Source string
|
|
Mapping string
|
|
Workflow string
|
|
Owner string
|
|
Repo string
|
|
Version int64
|
|
Status string
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
Filter string
|
|
Inputs string
|
|
RepoParam string
|
|
OrgID string
|
|
WorkspaceID string
|
|
}
|
|
|
|
func (q *Queries) GetBinding(ctx context.Context, arg GetBindingParams) (GetBindingRow, error) {
|
|
row := q.db.QueryRow(ctx, getBinding, arg.ID, arg.ScopeOrg)
|
|
var i GetBindingRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Source,
|
|
&i.Mapping,
|
|
&i.Workflow,
|
|
&i.Owner,
|
|
&i.Repo,
|
|
&i.Version,
|
|
&i.Status,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Filter,
|
|
&i.Inputs,
|
|
&i.RepoParam,
|
|
&i.OrgID,
|
|
&i.WorkspaceID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getEventType = `-- name: GetEventType :one
|
|
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
|
|
FROM event_types WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
|
|
`
|
|
|
|
type GetEventTypeParams struct {
|
|
ID string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) GetEventType(ctx context.Context, arg GetEventTypeParams) (EventType, error) {
|
|
row := q.db.QueryRow(ctx, getEventType, arg.ID, arg.ScopeOrg)
|
|
var i EventType
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Source,
|
|
&i.Name,
|
|
&i.Rule,
|
|
&i.Schema,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.OrgID,
|
|
&i.WorkspaceID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getMapping = `-- name: GetMapping :one
|
|
SELECT mappings.id, mappings.name, mappings.source_hint, mappings.fields, mappings.created_at, mappings.updated_at, mappings.event_type, mappings.org_id, mappings.workspace_id,
|
|
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
|
|
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
|
|
FROM mappings WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
|
|
`
|
|
|
|
type GetMappingParams struct {
|
|
ID string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
type GetMappingRow struct {
|
|
Mapping Mapping
|
|
MatchCount int64
|
|
LastMatchedAt time.Time
|
|
}
|
|
|
|
func (q *Queries) GetMapping(ctx context.Context, arg GetMappingParams) (GetMappingRow, error) {
|
|
row := q.db.QueryRow(ctx, getMapping, arg.ID, arg.ScopeOrg)
|
|
var i GetMappingRow
|
|
err := row.Scan(
|
|
&i.Mapping.ID,
|
|
&i.Mapping.Name,
|
|
&i.Mapping.SourceHint,
|
|
&i.Mapping.Fields,
|
|
&i.Mapping.CreatedAt,
|
|
&i.Mapping.UpdatedAt,
|
|
&i.Mapping.EventType,
|
|
&i.Mapping.OrgID,
|
|
&i.Mapping.WorkspaceID,
|
|
&i.MatchCount,
|
|
&i.LastMatchedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getSource = `-- name: GetSource :one
|
|
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE path = $1 AND ($2::text IS NULL OR org_id = $2)
|
|
`
|
|
|
|
type GetSourceParams struct {
|
|
Path string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) GetSource(ctx context.Context, arg GetSourceParams) (Source, error) {
|
|
row := q.db.QueryRow(ctx, getSource, arg.Path, arg.ScopeOrg)
|
|
var i Source
|
|
err := row.Scan(
|
|
&i.Path,
|
|
&i.Signing,
|
|
&i.Secret,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.OrgID,
|
|
&i.WorkspaceID,
|
|
&i.Name,
|
|
&i.DeliveryHeader,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const insertBinding = `-- name: InsertBinding :exec
|
|
INSERT INTO bindings (id, name, source, mapping, filter, workflow, owner, repo, version, status, inputs, repo_param, org_id, workspace_id)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 1,
|
|
$9, $10, $11, $12,
|
|
COALESCE((SELECT s.workspace_id FROM sources s WHERE s.path = $3 AND s.org_id = $12), 'default'))
|
|
`
|
|
|
|
type InsertBindingParams struct {
|
|
ID string
|
|
Name string
|
|
Source string
|
|
Mapping string
|
|
Filter string
|
|
Workflow string
|
|
Owner string
|
|
Repo string
|
|
Status string
|
|
Inputs string
|
|
RepoParam string
|
|
OrgID string
|
|
}
|
|
|
|
// A binding lives in its source's workspace.
|
|
func (q *Queries) InsertBinding(ctx context.Context, arg InsertBindingParams) error {
|
|
_, err := q.db.Exec(ctx, insertBinding,
|
|
arg.ID,
|
|
arg.Name,
|
|
arg.Source,
|
|
arg.Mapping,
|
|
arg.Filter,
|
|
arg.Workflow,
|
|
arg.Owner,
|
|
arg.Repo,
|
|
arg.Status,
|
|
arg.Inputs,
|
|
arg.RepoParam,
|
|
arg.OrgID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertBindingDispatch = `-- name: InsertBindingDispatch :execrows
|
|
INSERT INTO binding_dispatches (binding, binding_version, capture, task_id, reason, delivery)
|
|
SELECT $1, $2, c.id, $3, $4, c.delivery
|
|
FROM captures c WHERE c.id = $5
|
|
`
|
|
|
|
type InsertBindingDispatchParams struct {
|
|
Binding string
|
|
BindingVersion int64
|
|
TaskID int64
|
|
Reason string
|
|
Capture string
|
|
}
|
|
|
|
func (q *Queries) InsertBindingDispatch(ctx context.Context, arg InsertBindingDispatchParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, insertBindingDispatch,
|
|
arg.Binding,
|
|
arg.BindingVersion,
|
|
arg.TaskID,
|
|
arg.Reason,
|
|
arg.Capture,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const insertCapture = `-- name: InsertCapture :exec
|
|
|
|
INSERT INTO captures (id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type, delivery, org_id, workspace_id)
|
|
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11,
|
|
COALESCE(s.org_id, $12::text), COALESCE(s.workspace_id, $13::text)
|
|
FROM (SELECT 1) intake LEFT JOIN sources s ON s.path = $2
|
|
`
|
|
|
|
type InsertCaptureParams struct {
|
|
ID string
|
|
Source string
|
|
RemoteAddr string
|
|
ContentType string
|
|
Headers string
|
|
Body string
|
|
Authenticated bool
|
|
ReceivedAt time.Time
|
|
Unsigned bool
|
|
EventType string
|
|
Delivery string
|
|
DefaultOrg string
|
|
DefaultWorkspace string
|
|
}
|
|
|
|
// EDA queries: captures, mappings, bindings, dispatch ledgers, tool_calls.
|
|
func (q *Queries) InsertCapture(ctx context.Context, arg InsertCaptureParams) error {
|
|
_, err := q.db.Exec(ctx, insertCapture,
|
|
arg.ID,
|
|
arg.Source,
|
|
arg.RemoteAddr,
|
|
arg.ContentType,
|
|
arg.Headers,
|
|
arg.Body,
|
|
arg.Authenticated,
|
|
arg.ReceivedAt,
|
|
arg.Unsigned,
|
|
arg.EventType,
|
|
arg.Delivery,
|
|
arg.DefaultOrg,
|
|
arg.DefaultWorkspace,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertCaptureRefusal = `-- name: InsertCaptureRefusal :exec
|
|
INSERT INTO capture_refusals (source, addr, refused_at) VALUES ($1, $2, $3)
|
|
`
|
|
|
|
type InsertCaptureRefusalParams struct {
|
|
Source string
|
|
Addr string
|
|
RefusedAt time.Time
|
|
}
|
|
|
|
func (q *Queries) InsertCaptureRefusal(ctx context.Context, arg InsertCaptureRefusalParams) error {
|
|
_, err := q.db.Exec(ctx, insertCaptureRefusal, arg.Source, arg.Addr, arg.RefusedAt)
|
|
return err
|
|
}
|
|
|
|
const insertEventType = `-- name: InsertEventType :exec
|
|
INSERT INTO event_types (id, source, name, rule, schema, org_id, workspace_id)
|
|
VALUES ($1, $2, $3, $4, $5, $6,
|
|
COALESCE((SELECT s.workspace_id FROM sources s WHERE s.path = $2 AND s.org_id = $6), 'default'))
|
|
`
|
|
|
|
type InsertEventTypeParams struct {
|
|
ID string
|
|
Source string
|
|
Name string
|
|
Rule string
|
|
Schema string
|
|
OrgID string
|
|
}
|
|
|
|
// An event type lives in its source's workspace.
|
|
func (q *Queries) InsertEventType(ctx context.Context, arg InsertEventTypeParams) error {
|
|
_, err := q.db.Exec(ctx, insertEventType,
|
|
arg.ID,
|
|
arg.Source,
|
|
arg.Name,
|
|
arg.Rule,
|
|
arg.Schema,
|
|
arg.OrgID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertMapping = `-- name: InsertMapping :exec
|
|
INSERT INTO mappings (id, name, source_hint, event_type, fields, org_id, workspace_id)
|
|
VALUES ($1, $2, $3, $4, $5, $6,
|
|
COALESCE((SELECT y.workspace_id FROM event_types y WHERE y.id = $4 AND y.org_id = $6), 'default'))
|
|
`
|
|
|
|
type InsertMappingParams struct {
|
|
ID string
|
|
Name string
|
|
SourceHint string
|
|
EventType string
|
|
Fields string
|
|
OrgID string
|
|
}
|
|
|
|
// A mapping lives in its event type's workspace; one with no event type in
|
|
// the org's default workspace.
|
|
func (q *Queries) InsertMapping(ctx context.Context, arg InsertMappingParams) error {
|
|
_, err := q.db.Exec(ctx, insertMapping,
|
|
arg.ID,
|
|
arg.Name,
|
|
arg.SourceHint,
|
|
arg.EventType,
|
|
arg.Fields,
|
|
arg.OrgID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertMappingMatch = `-- name: InsertMappingMatch :exec
|
|
INSERT INTO mapping_matches (mapping, capture) VALUES ($1, $2)
|
|
ON CONFLICT DO NOTHING
|
|
`
|
|
|
|
type InsertMappingMatchParams struct {
|
|
Mapping string
|
|
Capture string
|
|
}
|
|
|
|
func (q *Queries) InsertMappingMatch(ctx context.Context, arg InsertMappingMatchParams) error {
|
|
_, err := q.db.Exec(ctx, insertMappingMatch, arg.Mapping, arg.Capture)
|
|
return err
|
|
}
|
|
|
|
const insertPlaybookDispatch = `-- name: InsertPlaybookDispatch :exec
|
|
INSERT INTO playbook_dispatches (playbook_id, playbook_version, event_id, action_id)
|
|
VALUES ($1, $2, $3, $4)
|
|
`
|
|
|
|
type InsertPlaybookDispatchParams struct {
|
|
PlaybookID string
|
|
PlaybookVersion string
|
|
EventID string
|
|
ActionID string
|
|
}
|
|
|
|
func (q *Queries) InsertPlaybookDispatch(ctx context.Context, arg InsertPlaybookDispatchParams) error {
|
|
_, err := q.db.Exec(ctx, insertPlaybookDispatch,
|
|
arg.PlaybookID,
|
|
arg.PlaybookVersion,
|
|
arg.EventID,
|
|
arg.ActionID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const insertSource = `-- name: InsertSource :execrows
|
|
INSERT INTO sources (path, signing, secret, org_id, workspace_id)
|
|
SELECT $1, $2, $3, w.org_id, w.id
|
|
FROM workspaces w WHERE w.org_id = $4 AND w.id = $5
|
|
`
|
|
|
|
type InsertSourceParams struct {
|
|
Path string
|
|
Signing string
|
|
Secret string
|
|
OrgID string
|
|
WorkspaceID string
|
|
}
|
|
|
|
// Inserts nothing when the workspace is not one of the org's.
|
|
func (q *Queries) InsertSource(ctx context.Context, arg InsertSourceParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, insertSource,
|
|
arg.Path,
|
|
arg.Signing,
|
|
arg.Secret,
|
|
arg.OrgID,
|
|
arg.WorkspaceID,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const insertToolCall = `-- name: InsertToolCall :exec
|
|
INSERT INTO tool_calls (id, task_id, attempt, tool, result, error, called_at, org_id, workspace_id)
|
|
SELECT $1, t.id, $2, $3, $4, $5, $6, t.org_id, t.workspace_id
|
|
FROM tasks t WHERE t.id = $7
|
|
`
|
|
|
|
type InsertToolCallParams struct {
|
|
ID string
|
|
Attempt int64
|
|
Tool string
|
|
Result string
|
|
Error string
|
|
CalledAt time.Time
|
|
TaskID int64
|
|
}
|
|
|
|
func (q *Queries) InsertToolCall(ctx context.Context, arg InsertToolCallParams) error {
|
|
_, err := q.db.Exec(ctx, insertToolCall,
|
|
arg.ID,
|
|
arg.Attempt,
|
|
arg.Tool,
|
|
arg.Result,
|
|
arg.Error,
|
|
arg.CalledAt,
|
|
arg.TaskID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const listBindingDispatches = `-- name: ListBindingDispatches :many
|
|
SELECT d.binding, d.binding_version, d.capture, d.task_id, d.reason, d.dispatched_at
|
|
FROM binding_dispatches d JOIN bindings b ON b.id = d.binding
|
|
WHERE ($1::text IS NULL OR b.org_id = $1)
|
|
AND ($2::text IS NULL OR d.binding = $2)
|
|
AND ($3::text IS NULL OR d.capture = $3)
|
|
AND ($4::bigint IS NULL OR d.task_id = $4)
|
|
ORDER BY d.dispatched_at DESC
|
|
LIMIT $5
|
|
`
|
|
|
|
type ListBindingDispatchesParams struct {
|
|
ScopeOrg pgtype.Text
|
|
Binding pgtype.Text
|
|
Capture pgtype.Text
|
|
TaskID pgtype.Int8
|
|
EntryLimit int32
|
|
}
|
|
|
|
type ListBindingDispatchesRow struct {
|
|
Binding string
|
|
BindingVersion int64
|
|
Capture string
|
|
TaskID int64
|
|
Reason string
|
|
DispatchedAt time.Time
|
|
}
|
|
|
|
func (q *Queries) ListBindingDispatches(ctx context.Context, arg ListBindingDispatchesParams) ([]ListBindingDispatchesRow, error) {
|
|
rows, err := q.db.Query(ctx, listBindingDispatches,
|
|
arg.ScopeOrg,
|
|
arg.Binding,
|
|
arg.Capture,
|
|
arg.TaskID,
|
|
arg.EntryLimit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListBindingDispatchesRow
|
|
for rows.Next() {
|
|
var i ListBindingDispatchesRow
|
|
if err := rows.Scan(
|
|
&i.Binding,
|
|
&i.BindingVersion,
|
|
&i.Capture,
|
|
&i.TaskID,
|
|
&i.Reason,
|
|
&i.DispatchedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listBindings = `-- name: ListBindings :many
|
|
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id, workspace_id
|
|
FROM bindings WHERE ($1::text IS NULL OR org_id = $1) ORDER BY created_at DESC
|
|
`
|
|
|
|
type ListBindingsRow struct {
|
|
ID string
|
|
Name string
|
|
Source string
|
|
Mapping string
|
|
Workflow string
|
|
Owner string
|
|
Repo string
|
|
Version int64
|
|
Status string
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
Filter string
|
|
Inputs string
|
|
RepoParam string
|
|
OrgID string
|
|
WorkspaceID string
|
|
}
|
|
|
|
func (q *Queries) ListBindings(ctx context.Context, scopeOrg pgtype.Text) ([]ListBindingsRow, error) {
|
|
rows, err := q.db.Query(ctx, listBindings, scopeOrg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListBindingsRow
|
|
for rows.Next() {
|
|
var i ListBindingsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.Source,
|
|
&i.Mapping,
|
|
&i.Workflow,
|
|
&i.Owner,
|
|
&i.Repo,
|
|
&i.Version,
|
|
&i.Status,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.Filter,
|
|
&i.Inputs,
|
|
&i.RepoParam,
|
|
&i.OrgID,
|
|
&i.WorkspaceID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listCaptures = `-- name: ListCaptures :many
|
|
SELECT id, source, remote_addr, content_type, headers, body, authenticated, received_at, unsigned, event_type, org_id, workspace_id, delivery
|
|
FROM captures
|
|
WHERE org_id = $2
|
|
ORDER BY received_at DESC
|
|
LIMIT $1
|
|
`
|
|
|
|
type ListCapturesParams struct {
|
|
Limit int32
|
|
OrgID string
|
|
}
|
|
|
|
func (q *Queries) ListCaptures(ctx context.Context, arg ListCapturesParams) ([]Capture, error) {
|
|
rows, err := q.db.Query(ctx, listCaptures, arg.Limit, arg.OrgID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Capture
|
|
for rows.Next() {
|
|
var i Capture
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Source,
|
|
&i.RemoteAddr,
|
|
&i.ContentType,
|
|
&i.Headers,
|
|
&i.Body,
|
|
&i.Authenticated,
|
|
&i.ReceivedAt,
|
|
&i.Unsigned,
|
|
&i.EventType,
|
|
&i.OrgID,
|
|
&i.WorkspaceID,
|
|
&i.Delivery,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listEventTypes = `-- name: ListEventTypes :many
|
|
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
|
|
FROM event_types WHERE ($1::text IS NULL OR org_id = $1) ORDER BY source, name
|
|
`
|
|
|
|
func (q *Queries) ListEventTypes(ctx context.Context, scopeOrg pgtype.Text) ([]EventType, error) {
|
|
rows, err := q.db.Query(ctx, listEventTypes, scopeOrg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []EventType
|
|
for rows.Next() {
|
|
var i EventType
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Source,
|
|
&i.Name,
|
|
&i.Rule,
|
|
&i.Schema,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.OrgID,
|
|
&i.WorkspaceID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listMappings = `-- name: ListMappings :many
|
|
SELECT mappings.id, mappings.name, mappings.source_hint, mappings.fields, mappings.created_at, mappings.updated_at, mappings.event_type, mappings.org_id, mappings.workspace_id,
|
|
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
|
|
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
|
|
FROM mappings WHERE ($1::text IS NULL OR org_id = $1) ORDER BY created_at DESC
|
|
`
|
|
|
|
type ListMappingsRow struct {
|
|
Mapping Mapping
|
|
MatchCount int64
|
|
LastMatchedAt time.Time
|
|
}
|
|
|
|
func (q *Queries) ListMappings(ctx context.Context, scopeOrg pgtype.Text) ([]ListMappingsRow, error) {
|
|
rows, err := q.db.Query(ctx, listMappings, scopeOrg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListMappingsRow
|
|
for rows.Next() {
|
|
var i ListMappingsRow
|
|
if err := rows.Scan(
|
|
&i.Mapping.ID,
|
|
&i.Mapping.Name,
|
|
&i.Mapping.SourceHint,
|
|
&i.Mapping.Fields,
|
|
&i.Mapping.CreatedAt,
|
|
&i.Mapping.UpdatedAt,
|
|
&i.Mapping.EventType,
|
|
&i.Mapping.OrgID,
|
|
&i.Mapping.WorkspaceID,
|
|
&i.MatchCount,
|
|
&i.LastMatchedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listSources = `-- name: ListSources :many
|
|
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE ($1::text IS NULL OR org_id = $1) ORDER BY created_at DESC, path
|
|
`
|
|
|
|
func (q *Queries) ListSources(ctx context.Context, scopeOrg pgtype.Text) ([]Source, error) {
|
|
rows, err := q.db.Query(ctx, listSources, scopeOrg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Source
|
|
for rows.Next() {
|
|
var i Source
|
|
if err := rows.Scan(
|
|
&i.Path,
|
|
&i.Signing,
|
|
&i.Secret,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.OrgID,
|
|
&i.WorkspaceID,
|
|
&i.Name,
|
|
&i.DeliveryHeader,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listUndispatchedCaptures = `-- name: ListUndispatchedCaptures :many
|
|
SELECT c.id, c.source, c.remote_addr, c.content_type, c.headers, c.body, c.authenticated, c.received_at, c.unsigned, c.event_type, c.org_id, c.workspace_id, c.delivery
|
|
FROM captures c
|
|
WHERE c.org_id = $1 AND c.source = ANY($2::text[])
|
|
AND c.event_type <> ''
|
|
AND (c.authenticated OR c.unsigned)
|
|
AND EXISTS (
|
|
SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping
|
|
WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type
|
|
AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery)
|
|
)
|
|
ORDER BY c.received_at, c.id
|
|
LIMIT $3
|
|
`
|
|
|
|
type ListUndispatchedCapturesParams struct {
|
|
OrgID string
|
|
Sources []string
|
|
EntryLimit int32
|
|
}
|
|
|
|
// Oldest first, so sustained intake cannot starve accepted work. Only
|
|
// dispatchable captures (signed, or from an approved unsigned source) are
|
|
// listed: no binding ever matches the rest, so they would hold the batch.
|
|
func (q *Queries) ListUndispatchedCaptures(ctx context.Context, arg ListUndispatchedCapturesParams) ([]Capture, error) {
|
|
rows, err := q.db.Query(ctx, listUndispatchedCaptures, arg.OrgID, arg.Sources, arg.EntryLimit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Capture
|
|
for rows.Next() {
|
|
var i Capture
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Source,
|
|
&i.RemoteAddr,
|
|
&i.ContentType,
|
|
&i.Headers,
|
|
&i.Body,
|
|
&i.Authenticated,
|
|
&i.ReceivedAt,
|
|
&i.Unsigned,
|
|
&i.EventType,
|
|
&i.OrgID,
|
|
&i.WorkspaceID,
|
|
&i.Delivery,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockEventTypeSource = `-- name: LockEventTypeSource :exec
|
|
SELECT pg_advisory_xact_lock(hashtext('event_types:' || $1::text))
|
|
`
|
|
|
|
// Serialises saves per source so two concurrent saves cannot each pass the
|
|
// overlap check against a set that excludes the other.
|
|
func (q *Queries) LockEventTypeSource(ctx context.Context, source string) error {
|
|
_, err := q.db.Exec(ctx, lockEventTypeSource, source)
|
|
return err
|
|
}
|
|
|
|
const setBindingArmed = `-- name: SetBindingArmed :execrows
|
|
UPDATE bindings SET status = 'armed', updated_at = now() WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
|
|
`
|
|
|
|
type SetBindingArmedParams struct {
|
|
ID string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) SetBindingArmed(ctx context.Context, arg SetBindingArmedParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, setBindingArmed, arg.ID, arg.ScopeOrg)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const setBindingDispatchTask = `-- name: SetBindingDispatchTask :exec
|
|
UPDATE binding_dispatches SET task_id = $3 WHERE binding = $1 AND capture = $2
|
|
`
|
|
|
|
type SetBindingDispatchTaskParams struct {
|
|
Binding string
|
|
Capture string
|
|
TaskID int64
|
|
}
|
|
|
|
func (q *Queries) SetBindingDispatchTask(ctx context.Context, arg SetBindingDispatchTaskParams) error {
|
|
_, err := q.db.Exec(ctx, setBindingDispatchTask, arg.Binding, arg.Capture, arg.TaskID)
|
|
return err
|
|
}
|
|
|
|
const setSourceDeliveryHeader = `-- name: SetSourceDeliveryHeader :execrows
|
|
UPDATE sources SET delivery_header = $1, updated_at = now() WHERE path = $2 AND ($3::text IS NULL OR org_id = $3)
|
|
`
|
|
|
|
type SetSourceDeliveryHeaderParams struct {
|
|
DeliveryHeader string
|
|
Path string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) SetSourceDeliveryHeader(ctx context.Context, arg SetSourceDeliveryHeaderParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, setSourceDeliveryHeader, arg.DeliveryHeader, arg.Path, arg.ScopeOrg)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const setSourceName = `-- name: SetSourceName :execrows
|
|
UPDATE sources SET name = $1, updated_at = now() WHERE path = $2 AND ($3::text IS NULL OR org_id = $3)
|
|
`
|
|
|
|
type SetSourceNameParams struct {
|
|
Name string
|
|
Path string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) SetSourceName(ctx context.Context, arg SetSourceNameParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, setSourceName, arg.Name, arg.Path, arg.ScopeOrg)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const setSourceSecret = `-- name: SetSourceSecret :execrows
|
|
UPDATE sources SET secret = $1, updated_at = now() WHERE path = $2 AND ($3::text IS NULL OR org_id = $3)
|
|
`
|
|
|
|
type SetSourceSecretParams struct {
|
|
Secret string
|
|
Path string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) SetSourceSecret(ctx context.Context, arg SetSourceSecretParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, setSourceSecret, arg.Secret, arg.Path, arg.ScopeOrg)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const setSourceSigning = `-- name: SetSourceSigning :execrows
|
|
UPDATE sources SET signing = $1, updated_at = now()
|
|
WHERE path = $2 AND signing = $3 AND ($4::text IS NULL OR org_id = $4)
|
|
`
|
|
|
|
type SetSourceSigningParams struct {
|
|
ToSigning string
|
|
Path string
|
|
FromSigning string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) SetSourceSigning(ctx context.Context, arg SetSourceSigningParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, setSourceSigning,
|
|
arg.ToSigning,
|
|
arg.Path,
|
|
arg.FromSigning,
|
|
arg.ScopeOrg,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const sourceExists = `-- name: SourceExists :one
|
|
SELECT EXISTS (SELECT 1 FROM sources WHERE path = $1)
|
|
`
|
|
|
|
func (q *Queries) SourceExists(ctx context.Context, path string) (bool, error) {
|
|
row := q.db.QueryRow(ctx, sourceExists, path)
|
|
var exists bool
|
|
err := row.Scan(&exists)
|
|
return exists, err
|
|
}
|
|
|
|
const taskToolCalls = `-- name: TaskToolCalls :many
|
|
SELECT id, task_id, attempt, tool, result, error, called_at
|
|
FROM tool_calls WHERE task_id = $1 AND ($2::text IS NULL OR org_id = $2) ORDER BY called_at ASC
|
|
`
|
|
|
|
type TaskToolCallsParams struct {
|
|
TaskID int64
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
type TaskToolCallsRow struct {
|
|
ID string
|
|
TaskID int64
|
|
Attempt int64
|
|
Tool string
|
|
Result string
|
|
Error string
|
|
CalledAt time.Time
|
|
}
|
|
|
|
func (q *Queries) TaskToolCalls(ctx context.Context, arg TaskToolCallsParams) ([]TaskToolCallsRow, error) {
|
|
rows, err := q.db.Query(ctx, taskToolCalls, arg.TaskID, arg.ScopeOrg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []TaskToolCallsRow
|
|
for rows.Next() {
|
|
var i TaskToolCallsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.TaskID,
|
|
&i.Attempt,
|
|
&i.Tool,
|
|
&i.Result,
|
|
&i.Error,
|
|
&i.CalledAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const transitionBindingStatus = `-- name: TransitionBindingStatus :execrows
|
|
UPDATE bindings SET status = $1, updated_at = now()
|
|
WHERE id = $2 AND status = $3 AND ($4::text IS NULL OR org_id = $4)
|
|
`
|
|
|
|
type TransitionBindingStatusParams struct {
|
|
ToStatus string
|
|
ID string
|
|
FromStatus string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) TransitionBindingStatus(ctx context.Context, arg TransitionBindingStatusParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, transitionBindingStatus,
|
|
arg.ToStatus,
|
|
arg.ID,
|
|
arg.FromStatus,
|
|
arg.ScopeOrg,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const updateBinding = `-- name: UpdateBinding :execrows
|
|
UPDATE bindings
|
|
SET name = $1, source = $2, mapping = $3, filter = $4,
|
|
workflow = $5, owner = $6, repo = $7,
|
|
version = version + 1, status = $8, inputs = $9, repo_param = $10, updated_at = now(),
|
|
workspace_id = COALESCE((SELECT s.workspace_id FROM sources s WHERE s.path = $2 AND s.org_id = bindings.org_id), 'default')
|
|
WHERE bindings.id = $11 AND ($12::text IS NULL OR bindings.org_id = $12)
|
|
`
|
|
|
|
type UpdateBindingParams struct {
|
|
Name string
|
|
Source string
|
|
Mapping string
|
|
Filter string
|
|
Workflow string
|
|
Owner string
|
|
Repo string
|
|
Status string
|
|
Inputs string
|
|
RepoParam string
|
|
ID string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) UpdateBinding(ctx context.Context, arg UpdateBindingParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, updateBinding,
|
|
arg.Name,
|
|
arg.Source,
|
|
arg.Mapping,
|
|
arg.Filter,
|
|
arg.Workflow,
|
|
arg.Owner,
|
|
arg.Repo,
|
|
arg.Status,
|
|
arg.Inputs,
|
|
arg.RepoParam,
|
|
arg.ID,
|
|
arg.ScopeOrg,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const updateEventType = `-- name: UpdateEventType :execrows
|
|
UPDATE event_types SET name = $1, rule = $2, updated_at = now()
|
|
WHERE id = $3 AND ($4::text IS NULL OR org_id = $4)
|
|
`
|
|
|
|
type UpdateEventTypeParams struct {
|
|
Name string
|
|
Rule string
|
|
ID string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) UpdateEventType(ctx context.Context, arg UpdateEventTypeParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, updateEventType,
|
|
arg.Name,
|
|
arg.Rule,
|
|
arg.ID,
|
|
arg.ScopeOrg,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const updateMapping = `-- name: UpdateMapping :execrows
|
|
UPDATE mappings
|
|
SET name = $1, source_hint = $2, event_type = $3, fields = $4, updated_at = now(),
|
|
workspace_id = COALESCE((SELECT y.workspace_id FROM event_types y WHERE y.id = $3 AND y.org_id = mappings.org_id), 'default')
|
|
WHERE mappings.id = $5 AND ($6::text IS NULL OR mappings.org_id = $6)
|
|
`
|
|
|
|
type UpdateMappingParams struct {
|
|
Name string
|
|
SourceHint string
|
|
EventType string
|
|
Fields string
|
|
ID string
|
|
ScopeOrg pgtype.Text
|
|
}
|
|
|
|
func (q *Queries) UpdateMapping(ctx context.Context, arg UpdateMappingParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, updateMapping,
|
|
arg.Name,
|
|
arg.SourceHint,
|
|
arg.EventType,
|
|
arg.Fields,
|
|
arg.ID,
|
|
arg.ScopeOrg,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|