+62
-1
appview/db/db.go
+62
-1
appview/db/db.go
···
331
331
unique(instance)
332
332
);
333
333
334
+
create table if not exists pipelines (
335
+
-- identifiers
336
+
id integer primary key autoincrement,
337
+
knot text not null,
338
+
rkey text not null,
339
+
340
+
repo_owner text not null,
341
+
repo_name text not null,
342
+
343
+
-- every pipeline must be associated with exactly one commit
344
+
sha text not null check (length(sha) = 40),
345
+
346
+
-- trigger data
347
+
trigger_id integer not null,
348
+
349
+
unique(knot, rkey),
350
+
foreign key (trigger_id) references triggers(id) on delete cascade
351
+
);
352
+
353
+
create table if not exists triggers (
354
+
-- primary key
355
+
id integer primary key autoincrement,
356
+
357
+
-- top-level fields
358
+
kind text not null,
359
+
360
+
-- pushTriggerData fields
361
+
push_ref text,
362
+
push_new_sha text check (length(push_new_sha) = 40),
363
+
push_old_sha text check (length(push_old_sha) = 40),
364
+
365
+
-- pullRequestTriggerData fields
366
+
pr_source_branch text,
367
+
pr_target_branch text,
368
+
pr_source_sha text check (length(pr_source_sha) = 40),
369
+
pr_action text
370
+
);
371
+
372
+
create table if not exists pipeline_statuses (
373
+
-- identifiers
374
+
id integer primary key autoincrement,
375
+
spindle text not null,
376
+
rkey text not null,
377
+
378
+
-- referenced pipeline. these form the (did, rkey) pair
379
+
pipeline_knot text not null,
380
+
pipeline_rkey text not null,
381
+
382
+
-- content
383
+
created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
384
+
workflow text not null,
385
+
status text not null,
386
+
error text,
387
+
exit_code integer not null default 0,
388
+
389
+
unique (spindle, rkey),
390
+
foreign key (pipeline_knot, pipeline_rkey)
391
+
references pipelines (knot, rkey)
392
+
on delete cascade
393
+
);
394
+
334
395
create table if not exists migrations (
335
396
id integer primary key autoincrement,
336
397
name text unique
337
-
)
398
+
);
338
399
`)
339
400
if err != nil {
340
401
return nil, err
+200
appview/db/pipeline.go
+200
appview/db/pipeline.go
···
1
+
package db
2
+
3
+
import (
4
+
"fmt"
5
+
"strings"
6
+
"time"
7
+
8
+
"github.com/bluesky-social/indigo/atproto/syntax"
9
+
)
10
+
11
+
type Pipeline struct {
12
+
Id int
13
+
Rkey string
14
+
Knot string
15
+
RepoOwner syntax.DID
16
+
RepoName string
17
+
TriggerId int
18
+
Sha string
19
+
20
+
// populate when querying for revers mappings
21
+
Trigger *Trigger
22
+
}
23
+
24
+
type Trigger struct {
25
+
Id int
26
+
Kind string
27
+
28
+
// push trigger fields
29
+
PushRef *string
30
+
PushNewSha *string
31
+
PushOldSha *string
32
+
33
+
// pull request trigger fields
34
+
PRSourceBranch *string
35
+
PRTargetBranch *string
36
+
PRSourceSha *string
37
+
PRAction *string
38
+
}
39
+
40
+
type PipelineStatus struct {
41
+
ID int
42
+
Spindle string
43
+
Rkey string
44
+
PipelineKnot string
45
+
PipelineRkey string
46
+
Created time.Time
47
+
Workflow string
48
+
Status string
49
+
Error *string
50
+
ExitCode int
51
+
}
52
+
53
+
func GetPipelines(e Execer, filters ...filter) ([]Pipeline, error) {
54
+
var pipelines []Pipeline
55
+
56
+
var conditions []string
57
+
var args []any
58
+
for _, filter := range filters {
59
+
conditions = append(conditions, filter.Condition())
60
+
args = append(args, filter.arg)
61
+
}
62
+
63
+
whereClause := ""
64
+
if conditions != nil {
65
+
whereClause = " where " + strings.Join(conditions, " and ")
66
+
}
67
+
68
+
query := fmt.Sprintf(`select id, rkey, knot, repo_owner, repo_name, sha from pipelines %s`, whereClause)
69
+
70
+
rows, err := e.Query(query, args...)
71
+
72
+
if err != nil {
73
+
return nil, err
74
+
}
75
+
defer rows.Close()
76
+
77
+
for rows.Next() {
78
+
var pipeline Pipeline
79
+
err = rows.Scan(
80
+
&pipeline.Id,
81
+
&pipeline.Rkey,
82
+
&pipeline.Knot,
83
+
&pipeline.RepoOwner,
84
+
&pipeline.RepoName,
85
+
&pipeline.Sha,
86
+
)
87
+
if err != nil {
88
+
return nil, err
89
+
}
90
+
91
+
pipelines = append(pipelines, pipeline)
92
+
}
93
+
94
+
if err = rows.Err(); err != nil {
95
+
return nil, err
96
+
}
97
+
98
+
return pipelines, nil
99
+
}
100
+
101
+
func AddPipeline(e Execer, pipeline Pipeline) error {
102
+
args := []any{
103
+
pipeline.Rkey,
104
+
pipeline.Knot,
105
+
pipeline.RepoOwner,
106
+
pipeline.RepoName,
107
+
pipeline.TriggerId,
108
+
pipeline.Sha,
109
+
}
110
+
111
+
placeholders := make([]string, len(args))
112
+
for i := range placeholders {
113
+
placeholders[i] = "?"
114
+
}
115
+
116
+
query := fmt.Sprintf(`
117
+
insert or ignore into pipelines (
118
+
rkey,
119
+
knot,
120
+
repo_owner,
121
+
repo_name,
122
+
trigger_id,
123
+
sha
124
+
) values (%s)
125
+
`, strings.Join(placeholders, ","))
126
+
127
+
_, err := e.Exec(query, args...)
128
+
129
+
return err
130
+
}
131
+
132
+
func AddTrigger(e Execer, trigger Trigger) (int64, error) {
133
+
args := []any{
134
+
trigger.Kind,
135
+
trigger.PushRef,
136
+
trigger.PushNewSha,
137
+
trigger.PushOldSha,
138
+
trigger.PRSourceBranch,
139
+
trigger.PRTargetBranch,
140
+
trigger.PRSourceSha,
141
+
trigger.PRAction,
142
+
}
143
+
144
+
placeholders := make([]string, len(args))
145
+
for i := range placeholders {
146
+
placeholders[i] = "?"
147
+
}
148
+
149
+
query := fmt.Sprintf(`insert or ignore into triggers (
150
+
kind,
151
+
push_ref,
152
+
push_new_sha,
153
+
push_old_sha,
154
+
pr_source_branch,
155
+
pr_target_branch,
156
+
pr_source_sha,
157
+
pr_action
158
+
) values (%s)`, strings.Join(placeholders, ","))
159
+
160
+
res, err := e.Exec(query, args...)
161
+
if err != nil {
162
+
return 0, err
163
+
}
164
+
165
+
return res.LastInsertId()
166
+
}
167
+
168
+
func AddPipelineStatus(e Execer, status PipelineStatus) error {
169
+
args := []any{
170
+
status.Spindle,
171
+
status.Rkey,
172
+
status.PipelineKnot,
173
+
status.PipelineRkey,
174
+
status.Workflow,
175
+
status.Status,
176
+
status.Error,
177
+
status.ExitCode,
178
+
}
179
+
180
+
placeholders := make([]string, len(args))
181
+
for i := range placeholders {
182
+
placeholders[i] = "?"
183
+
}
184
+
185
+
query := fmt.Sprintf(`
186
+
insert or ignore into pipeline_statuses (
187
+
spindle,
188
+
rkey,
189
+
pipeline_knot,
190
+
pipeline_rkey,
191
+
workflow,
192
+
status,
193
+
error,
194
+
exit_code
195
+
) values (%s)
196
+
`, strings.Join(placeholders, ","))
197
+
198
+
_, err := e.Exec(query, args...)
199
+
return err
200
+
}