tenseleyflow/shithub / 2f1478a

Browse files

actions/sqlc: add workflow run list filters

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
2f1478a65a1d6ca4260d6b5dcbef634030883ba6
Parents
12d548f
Tree
ff9a121

3 changed files

StatusFile+-
M internal/actions/queries/workflow_runs.sql 43 7
M internal/actions/sqlc/querier.go 2 0
M internal/actions/sqlc/workflow_runs.sql.go 134 25
internal/actions/queries/workflow_runs.sqlmodified
@@ -96,10 +96,46 @@ FROM workflow_runs
9696
 WHERE repo_id = $1;
9797
 
9898
 -- name: ListWorkflowRunsForRepo :many
99
-SELECT id, repo_id, run_index, workflow_file, workflow_name,
100
-       head_sha, head_ref, event, status, conclusion,
101
-       actor_user_id, started_at, completed_at, created_at
102
-FROM workflow_runs
103
-WHERE repo_id = $1
104
-ORDER BY created_at DESC
105
-LIMIT $2 OFFSET $3;
99
+SELECT r.id, r.repo_id, r.run_index, r.workflow_file, r.workflow_name,
100
+       r.head_sha, r.head_ref, r.event, r.status, r.conclusion,
101
+       r.actor_user_id, COALESCE(u.username::text, '')::text AS actor_username,
102
+       r.started_at, r.completed_at, r.created_at, r.updated_at
103
+FROM workflow_runs r
104
+LEFT JOIN users u ON u.id = r.actor_user_id
105
+WHERE r.repo_id = sqlc.arg(repo_id)::bigint
106
+  AND (sqlc.narg(workflow_file)::text IS NULL OR r.workflow_file = sqlc.narg(workflow_file)::text)
107
+  AND (sqlc.narg(head_ref)::text IS NULL OR r.head_ref = sqlc.narg(head_ref)::text)
108
+  AND (sqlc.narg(event)::workflow_run_event IS NULL OR r.event = sqlc.narg(event)::workflow_run_event)
109
+  AND (sqlc.narg(status)::workflow_run_status IS NULL OR r.status = sqlc.narg(status)::workflow_run_status)
110
+  AND (sqlc.narg(conclusion)::check_conclusion IS NULL OR r.conclusion = sqlc.narg(conclusion)::check_conclusion)
111
+  AND (sqlc.narg(actor_username)::text IS NULL OR u.username = sqlc.narg(actor_username)::citext)
112
+ORDER BY r.created_at DESC, r.id DESC
113
+LIMIT sqlc.arg(page_limit) OFFSET sqlc.arg(page_offset);
114
+
115
+-- name: CountWorkflowRunsForRepo :one
116
+SELECT COUNT(*)::bigint
117
+FROM workflow_runs r
118
+LEFT JOIN users u ON u.id = r.actor_user_id
119
+WHERE r.repo_id = sqlc.arg(repo_id)::bigint
120
+  AND (sqlc.narg(workflow_file)::text IS NULL OR r.workflow_file = sqlc.narg(workflow_file)::text)
121
+  AND (sqlc.narg(head_ref)::text IS NULL OR r.head_ref = sqlc.narg(head_ref)::text)
122
+  AND (sqlc.narg(event)::workflow_run_event IS NULL OR r.event = sqlc.narg(event)::workflow_run_event)
123
+  AND (sqlc.narg(status)::workflow_run_status IS NULL OR r.status = sqlc.narg(status)::workflow_run_status)
124
+  AND (sqlc.narg(conclusion)::check_conclusion IS NULL OR r.conclusion = sqlc.narg(conclusion)::check_conclusion)
125
+  AND (sqlc.narg(actor_username)::text IS NULL OR u.username = sqlc.narg(actor_username)::citext);
126
+
127
+-- name: ListWorkflowRunWorkflowsForRepo :many
128
+WITH ranked AS (
129
+    SELECT workflow_file,
130
+           workflow_name,
131
+           (COUNT(*) OVER (PARTITION BY workflow_file))::bigint AS run_count,
132
+           ROW_NUMBER() OVER (PARTITION BY workflow_file ORDER BY created_at DESC, id DESC) AS rn
133
+    FROM workflow_runs
134
+    WHERE repo_id = $1
135
+)
136
+SELECT workflow_file,
137
+       COALESCE(NULLIF(workflow_name, ''), workflow_file) AS workflow_name,
138
+       run_count
139
+FROM ranked
140
+WHERE rn = 1
141
+ORDER BY lower(COALESCE(NULLIF(workflow_name, ''), workflow_file)), workflow_file;
internal/actions/sqlc/querier.gomodified
@@ -16,6 +16,7 @@ type Querier interface {
1616
 	ClaimQueuedWorkflowJob(ctx context.Context, db DBTX, arg ClaimQueuedWorkflowJobParams) (ClaimQueuedWorkflowJobRow, error)
1717
 	CompleteWorkflowRun(ctx context.Context, db DBTX, arg CompleteWorkflowRunParams) (WorkflowRun, error)
1818
 	CountRunningJobsForRunner(ctx context.Context, db DBTX, runnerID int64) (int32, error)
19
+	CountWorkflowRunsForRepo(ctx context.Context, db DBTX, arg CountWorkflowRunsForRepoParams) (int64, error)
1920
 	DeleteExpiredArtifacts(ctx context.Context, db DBTX) ([]DeleteExpiredArtifactsRow, error)
2021
 	DeleteExpiredRunnerJWTUses(ctx context.Context, db DBTX) error
2122
 	DeleteOrgSecret(ctx context.Context, db DBTX, arg DeleteOrgSecretParams) error
@@ -70,6 +71,7 @@ type Querier interface {
7071
 	ListRunners(ctx context.Context, db DBTX) ([]ListRunnersRow, error)
7172
 	ListStepLogChunks(ctx context.Context, db DBTX, arg ListStepLogChunksParams) ([]WorkflowStepLogChunk, error)
7273
 	ListStepsForJob(ctx context.Context, db DBTX, jobID int64) ([]ListStepsForJobRow, error)
74
+	ListWorkflowRunWorkflowsForRepo(ctx context.Context, db DBTX, repoID int64) ([]ListWorkflowRunWorkflowsForRepoRow, error)
7375
 	ListWorkflowRunsForRepo(ctx context.Context, db DBTX, arg ListWorkflowRunsForRepoParams) ([]ListWorkflowRunsForRepoRow, error)
7476
 	LockRunnerByID(ctx context.Context, db DBTX, id int64) (WorkflowRunner, error)
7577
 	// Companion to EnqueueWorkflowRun for the conflict path: when an
internal/actions/sqlc/workflow_runs.sql.gomodified
@@ -63,6 +63,44 @@ func (q *Queries) CompleteWorkflowRun(ctx context.Context, db DBTX, arg Complete
6363
 	return i, err
6464
 }
6565
 
66
+const countWorkflowRunsForRepo = `-- name: CountWorkflowRunsForRepo :one
67
+SELECT COUNT(*)::bigint
68
+FROM workflow_runs r
69
+LEFT JOIN users u ON u.id = r.actor_user_id
70
+WHERE r.repo_id = $1::bigint
71
+  AND ($2::text IS NULL OR r.workflow_file = $2::text)
72
+  AND ($3::text IS NULL OR r.head_ref = $3::text)
73
+  AND ($4::workflow_run_event IS NULL OR r.event = $4::workflow_run_event)
74
+  AND ($5::workflow_run_status IS NULL OR r.status = $5::workflow_run_status)
75
+  AND ($6::check_conclusion IS NULL OR r.conclusion = $6::check_conclusion)
76
+  AND ($7::text IS NULL OR u.username = $7::citext)
77
+`
78
+
79
+type CountWorkflowRunsForRepoParams struct {
80
+	RepoID        int64
81
+	WorkflowFile  pgtype.Text
82
+	HeadRef       pgtype.Text
83
+	Event         NullWorkflowRunEvent
84
+	Status        NullWorkflowRunStatus
85
+	Conclusion    NullCheckConclusion
86
+	ActorUsername pgtype.Text
87
+}
88
+
89
+func (q *Queries) CountWorkflowRunsForRepo(ctx context.Context, db DBTX, arg CountWorkflowRunsForRepoParams) (int64, error) {
90
+	row := db.QueryRow(ctx, countWorkflowRunsForRepo,
91
+		arg.RepoID,
92
+		arg.WorkflowFile,
93
+		arg.HeadRef,
94
+		arg.Event,
95
+		arg.Status,
96
+		arg.Conclusion,
97
+		arg.ActorUsername,
98
+	)
99
+	var column_1 int64
100
+	err := row.Scan(&column_1)
101
+	return column_1, err
102
+}
103
+
66104
 const enqueueWorkflowRun = `-- name: EnqueueWorkflowRun :one
67105
 INSERT INTO workflow_runs (
68106
     repo_id, run_index, workflow_file, workflow_name,
@@ -268,41 +306,110 @@ func (q *Queries) InsertWorkflowRun(ctx context.Context, db DBTX, arg InsertWork
268306
 	return i, err
269307
 }
270308
 
309
+const listWorkflowRunWorkflowsForRepo = `-- name: ListWorkflowRunWorkflowsForRepo :many
310
+WITH ranked AS (
311
+    SELECT workflow_file,
312
+           workflow_name,
313
+           (COUNT(*) OVER (PARTITION BY workflow_file))::bigint AS run_count,
314
+           ROW_NUMBER() OVER (PARTITION BY workflow_file ORDER BY created_at DESC, id DESC) AS rn
315
+    FROM workflow_runs
316
+    WHERE repo_id = $1
317
+)
318
+SELECT workflow_file,
319
+       COALESCE(NULLIF(workflow_name, ''), workflow_file) AS workflow_name,
320
+       run_count
321
+FROM ranked
322
+WHERE rn = 1
323
+ORDER BY lower(COALESCE(NULLIF(workflow_name, ''), workflow_file)), workflow_file
324
+`
325
+
326
+type ListWorkflowRunWorkflowsForRepoRow struct {
327
+	WorkflowFile string
328
+	WorkflowName string
329
+	RunCount     int64
330
+}
331
+
332
+func (q *Queries) ListWorkflowRunWorkflowsForRepo(ctx context.Context, db DBTX, repoID int64) ([]ListWorkflowRunWorkflowsForRepoRow, error) {
333
+	rows, err := db.Query(ctx, listWorkflowRunWorkflowsForRepo, repoID)
334
+	if err != nil {
335
+		return nil, err
336
+	}
337
+	defer rows.Close()
338
+	items := []ListWorkflowRunWorkflowsForRepoRow{}
339
+	for rows.Next() {
340
+		var i ListWorkflowRunWorkflowsForRepoRow
341
+		if err := rows.Scan(&i.WorkflowFile, &i.WorkflowName, &i.RunCount); err != nil {
342
+			return nil, err
343
+		}
344
+		items = append(items, i)
345
+	}
346
+	if err := rows.Err(); err != nil {
347
+		return nil, err
348
+	}
349
+	return items, nil
350
+}
351
+
271352
 const listWorkflowRunsForRepo = `-- name: ListWorkflowRunsForRepo :many
272
-SELECT id, repo_id, run_index, workflow_file, workflow_name,
273
-       head_sha, head_ref, event, status, conclusion,
274
-       actor_user_id, started_at, completed_at, created_at
275
-FROM workflow_runs
276
-WHERE repo_id = $1
277
-ORDER BY created_at DESC
278
-LIMIT $2 OFFSET $3
353
+SELECT r.id, r.repo_id, r.run_index, r.workflow_file, r.workflow_name,
354
+       r.head_sha, r.head_ref, r.event, r.status, r.conclusion,
355
+       r.actor_user_id, COALESCE(u.username::text, '')::text AS actor_username,
356
+       r.started_at, r.completed_at, r.created_at, r.updated_at
357
+FROM workflow_runs r
358
+LEFT JOIN users u ON u.id = r.actor_user_id
359
+WHERE r.repo_id = $1::bigint
360
+  AND ($2::text IS NULL OR r.workflow_file = $2::text)
361
+  AND ($3::text IS NULL OR r.head_ref = $3::text)
362
+  AND ($4::workflow_run_event IS NULL OR r.event = $4::workflow_run_event)
363
+  AND ($5::workflow_run_status IS NULL OR r.status = $5::workflow_run_status)
364
+  AND ($6::check_conclusion IS NULL OR r.conclusion = $6::check_conclusion)
365
+  AND ($7::text IS NULL OR u.username = $7::citext)
366
+ORDER BY r.created_at DESC, r.id DESC
367
+LIMIT $9 OFFSET $8
279368
 `
280369
 
281370
 type ListWorkflowRunsForRepoParams struct {
282
-	RepoID int64
283
-	Limit  int32
284
-	Offset int32
371
+	RepoID        int64
372
+	WorkflowFile  pgtype.Text
373
+	HeadRef       pgtype.Text
374
+	Event         NullWorkflowRunEvent
375
+	Status        NullWorkflowRunStatus
376
+	Conclusion    NullCheckConclusion
377
+	ActorUsername pgtype.Text
378
+	PageOffset    int32
379
+	PageLimit     int32
285380
 }
286381
 
287382
 type ListWorkflowRunsForRepoRow struct {
288
-	ID           int64
289
-	RepoID       int64
290
-	RunIndex     int64
291
-	WorkflowFile string
292
-	WorkflowName string
293
-	HeadSha      string
294
-	HeadRef      string
295
-	Event        WorkflowRunEvent
296
-	Status       WorkflowRunStatus
297
-	Conclusion   NullCheckConclusion
298
-	ActorUserID  pgtype.Int8
299
-	StartedAt    pgtype.Timestamptz
300
-	CompletedAt  pgtype.Timestamptz
301
-	CreatedAt    pgtype.Timestamptz
383
+	ID            int64
384
+	RepoID        int64
385
+	RunIndex      int64
386
+	WorkflowFile  string
387
+	WorkflowName  string
388
+	HeadSha       string
389
+	HeadRef       string
390
+	Event         WorkflowRunEvent
391
+	Status        WorkflowRunStatus
392
+	Conclusion    NullCheckConclusion
393
+	ActorUserID   pgtype.Int8
394
+	ActorUsername string
395
+	StartedAt     pgtype.Timestamptz
396
+	CompletedAt   pgtype.Timestamptz
397
+	CreatedAt     pgtype.Timestamptz
398
+	UpdatedAt     pgtype.Timestamptz
302399
 }
303400
 
304401
 func (q *Queries) ListWorkflowRunsForRepo(ctx context.Context, db DBTX, arg ListWorkflowRunsForRepoParams) ([]ListWorkflowRunsForRepoRow, error) {
305
-	rows, err := db.Query(ctx, listWorkflowRunsForRepo, arg.RepoID, arg.Limit, arg.Offset)
402
+	rows, err := db.Query(ctx, listWorkflowRunsForRepo,
403
+		arg.RepoID,
404
+		arg.WorkflowFile,
405
+		arg.HeadRef,
406
+		arg.Event,
407
+		arg.Status,
408
+		arg.Conclusion,
409
+		arg.ActorUsername,
410
+		arg.PageOffset,
411
+		arg.PageLimit,
412
+	)
306413
 	if err != nil {
307414
 		return nil, err
308415
 	}
@@ -322,9 +429,11 @@ func (q *Queries) ListWorkflowRunsForRepo(ctx context.Context, db DBTX, arg List
322429
 			&i.Status,
323430
 			&i.Conclusion,
324431
 			&i.ActorUserID,
432
+			&i.ActorUsername,
325433
 			&i.StartedAt,
326434
 			&i.CompletedAt,
327435
 			&i.CreatedAt,
436
+			&i.UpdatedAt,
328437
 		); err != nil {
329438
 			return nil, err
330439
 		}