Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333package db
import ( "context" "database/sql" "encoding/json" "errors" "strconv" "strings"
"tangled.org/core/orm" "tangled.org/core/spindle/models")
func (d *DB) QueryPipelines(ctx context.Context, repoDID string, commits []string, cursor string, kinds []string, limit int, includePrivate bool) ([]*models.PipelineRecord, string, int64, error) { return d.queryPipelines(ctx, repoDID, commits, cursor, kinds, limit, includePrivate, false)}
func (d *DB) QueryRepoPipelines(ctx context.Context, repoDID string, commits []string, cursor string, kinds []string, limit int, includePrivate, includeUnproven bool) ([]*models.PipelineRecord, string, int64, error) { return d.queryPipelines(ctx, repoDID, commits, cursor, kinds, limit, includePrivate, !includeUnproven)}
func (d *DB) queryPipelines(ctx context.Context, repoDID string, commits []string, cursor string, kinds []string, limit int, includePrivate, sourceProof bool) ([]*models.PipelineRecord, string, int64, error) { if limit <= 0 { limit = 30 }
filters := []orm.Filter{orm.FilterEq("repo_did", repoDID)} if !includePrivate { filters = append(filters, orm.FilterEq("private_run", false)) } if len(commits) > 0 { filters = append(filters, orm.FilterIn("commit_sha", commits)) } if len(kinds) > 0 { filters = append(filters, orm.FilterIn("kind", kinds)) }
var conditions []string var args []any for _, filter := range filters { conditions = append(conditions, filter.Condition()) args = append(args, filter.Arg()...) } if sourceProof { conditions = append(conditions, provedPipelineSources) } whereClause := " where " + strings.Join(conditions, " and ")
var total int64 counted := false cursorID, hasCursor := int64(0), false if cursor != "" { idText, totalText, _ := strings.Cut(cursor, ":") if value, err := strconv.ParseInt(idText, 10, 64); err == nil { cursorID, hasCursor = value, true totalText, proven := strings.CutSuffix(totalText, ":proven") totalText, public := strings.CutSuffix(totalText, ":public") if carried, err := strconv.ParseInt(totalText, 10, 64); err == nil && public != includePrivate && proven == sourceProof { total, counted = carried, true } } } if !counted { if err := d.QueryRowContext(ctx, `select count(*) from pipelines`+whereClause, args...).Scan(&total); err != nil { return nil, "", 0, err } } if hasCursor { filter := orm.FilterLt("id", cursorID) conditions = append(conditions, filter.Condition()) args = append(args, filter.Arg()...) whereClause = " where " + strings.Join(conditions, " and ") } args = append(args, limit)
rows, err := d.QueryContext(ctx, `select id, payload, private_run from pipelines`+whereClause+` order by id desc limit ?`, args...) if err != nil { return nil, "", 0, err } defer rows.Close()
records := make([]*models.PipelineRecord, 0, limit) var lastID int64 var scanned int for rows.Next() { var id int64 var payload string var record models.PipelineRecord if err := rows.Scan(&id, &payload, &record.PrivateRun); err != nil { return nil, "", 0, err } lastID = id scanned++ if json.Unmarshal([]byte(payload), &record) != nil { continue } records = append(records, &record) } if err := rows.Err(); err != nil { return nil, "", 0, err } if err := d.applyStatuses(ctx, records); err != nil { return nil, "", 0, err }
nextCursor := "" if scanned == limit { nextCursor = strconv.FormatInt(lastID, 10) + ":" + strconv.FormatInt(total, 10) if !includePrivate { nextCursor += ":public" } if sourceProof { nextCursor += ":proven" } } return records, nextCursor, total, nil}
func (d *DB) GetPipeline(ctx context.Context, id models.PipelineId) (*models.PipelineRecord, error) { return d.GetVisiblePipeline(ctx, id, true)}
func (d *DB) GetVisiblePipeline(ctx context.Context, id models.PipelineId, includePrivate bool) (*models.PipelineRecord, error) { return d.visiblePipeline(ctx, d.QueryRowContext(ctx, `select payload, private_run from pipelines where pipeline_id = ? and (? or private_run = 0)`, id, includePrivate))}
func (d *DB) GetRepoPipeline(ctx context.Context, repo string, id models.PipelineId, includePrivate, includeUnproven bool) (*models.PipelineRecord, error) { query := `select payload, private_run from pipelines where repo_did = ? and pipeline_id = ? and (? or private_run = 0)` if !includeUnproven { query += ` and ` + provedPipelineSources } return d.visiblePipeline(ctx, d.QueryRowContext(ctx, query, repo, id, includePrivate))}
const pipelineSourceDIDs = ` select json_extract(case when json_valid(payload) then payload end, '$.sourceRepo') as did union all select json_extract(case when json_valid(payload) then payload end, '$.trigger.manual.sourceRepo') union all select json_extract(case when json_valid(payload) then payload end, '$.trigger.pullRequest.sourceRepo') union all select json_extract(case when json_valid(source_repo) then source_repo end, '$.repoDid')`
const distinctPipelineSources = `from (` + pipelineSourceDIDs + `) as sources where sources.did <> '' and sources.did <> pipelines.repo_did`
const provedPipelineSources = `(not unproven_source or ( json_valid(payload) and (source_repo is null or json_valid(source_repo)) and not exists ( select 1 ` + distinctPipelineSources + ` and (exists(select 1 from ever_private_repos where repo_did = sources.did) or not exists(select 1 from repos where repo_did = sources.did and confirmed_public = 1)) )))`
func (d *DB) visiblePipeline(ctx context.Context, row *sql.Row) (*models.PipelineRecord, error) { var record models.PipelineRecord var payload string if err := row.Scan(&payload, &record.PrivateRun); err != nil { return nil, err } if err := json.Unmarshal([]byte(payload), &record); err != nil { return nil, err } if err := d.applyStatuses(ctx, []*models.PipelineRecord{&record}); err != nil { return nil, err } return &record, nil}
func (d *DB) PipelineRunPrivate(ctx context.Context, id models.PipelineId) (bool, error) { var private bool err := d.QueryRowContext(ctx, `select private_run from pipelines where pipeline_id = ?`, id).Scan(&private) return private, err}
func (d *DB) CreatePipeline(record *models.PipelineRecord) error { return insertPipeline(d, record, nil, nil, nil)}
func insertPipeline(q DBTX, record *models.PipelineRecord, spec, sourceRepo, patchURI any) error { payload, err := json.Marshal(record) if err != nil { return err } var ref, oldSha *string if push := record.Trigger.Push; push != nil { ref, oldSha = &push.Ref, &push.OldCommit } unprovenSource, err := pipelineHasUnprovenSource(q, record, sourceRepo) if err != nil { return err } _, err = q.Exec( `insert into pipelines (pipeline_id, repo_did, commit_sha, kind, payload, ref, old_sha, spec, source_repo, patch_uri, private_run, unproven_source) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? or exists (select 1 from private_repos where repo_did = ?), ?)`, record.ID, record.RepoDID, record.Commit, record.Trigger.Kind, string(payload), ref, oldSha, spec, sourceRepo, patchURI, record.PrivateRun, record.RepoDID, unprovenSource, ) return err}
func pipelineHasUnprovenSource(q DBTX, record *models.PipelineRecord, sourceRepo any) (bool, error) { sources := []*string{record.SourceRepo} if t := record.Trigger.Manual; t != nil { sources = append(sources, t.SourceRepo) } if t := record.Trigger.PullRequest; t != nil { sources = append(sources, t.SourceRepo) } if value, ok := sourceRepo.(string); ok { var source struct { RepoDid *string `json:"repoDid"` } if err := json.Unmarshal([]byte(value), &source); err != nil { return false, err } sources = append(sources, source.RepoDid) } for _, did := range sources { if did == nil || *did == "" || *did == record.RepoDID { continue } var public bool err := q.QueryRow(`select exists(select 1 from repos where repo_did = ?1 and confirmed_public = 1) and not exists(select 1 from ever_private_repos where repo_did = ?1)`, *did).Scan(&public) if err != nil || !public { return true, err } } return false, nil}
// a replayed notify lands here so the pipeline doesn't run twicefunc (d *DB) PushPipeline(repoDid, ref, oldSha, newSha string) (id models.PipelineId, found bool, err error) { err = d.QueryRow( `select pipeline_id from pipelines where kind = 'push' and repo_did = ? and commit_sha = ? and ref = ? and old_sha = ? limit 1`, repoDid, newSha, ref, oldSha, ).Scan(&id) if errors.Is(err, sql.ErrNoRows) { return "", false, nil } return id, err == nil, err}
func (d *DB) applyStatuses(ctx context.Context, pipelines []*models.PipelineRecord) error { if len(pipelines) == 0 { return nil }
pipelineIDs := make([]string, 0, len(pipelines)) for _, pipeline := range pipelines { pipelineIDs = append(pipelineIDs, string(pipeline.ID)) }
statuses, err := d.workflowStatuses(ctx, pipelineIDs) if err != nil { return err }
for _, pipeline := range pipelines { for _, workflow := range pipeline.Workflows { if workflow == nil { continue } status, ok := statuses[wfKey{string(pipeline.ID), workflow.Name}] if !ok { continue } workflow.Status = status.Status workflow.Error = status.Error workflow.StartedAt = status.StartedAt workflow.FinishedAt = status.FinishedAt } } return nil}
type wfKey struct { PipelineID string Workflow string}
type wfStatus struct { Status string Error *string StartedAt *string FinishedAt *string}
func (d *DB) workflowStatuses(ctx context.Context, pipelineIDs []string) (map[wfKey]wfStatus, error) { filter := orm.FilterIn("pipeline_id", pipelineIDs) rows, err := d.QueryContext(ctx, `select pipeline_id, workflow, status, error, created_at from workflow_statuses where `+filter.Condition()+` order by id asc`, filter.Arg()...) if err != nil { return nil, err } defer rows.Close()
out := make(map[wfKey]wfStatus) for rows.Next() { var pipelineID, wfName, status, createdAt string var wfError *string if err := rows.Scan(&pipelineID, &wfName, &status, &wfError, &createdAt); err != nil { return nil, err }
k := wfKey{pipelineID, wfName} st := out[k]
// rows arrive in insertion order, so the last one wins for the current status st.Status = status st.Error = wfError
switch kind := models.StatusKind(status); { case kind == models.StatusKindRunning: // the first running row is when the workflow actually started if st.StartedAt == nil { at := createdAt st.StartedAt = &at } case kind.IsFinish(): at := createdAt st.FinishedAt = &at }
out[k] = st } return out, rows.Err()}