tenseleyflow/shithub / 069a4d0

Browse files

List repo check suites

Authored by espadonne
SHA
069a4d0ff55e4d1b2b79f2a5f4505274e905550d
Parents
e2c3943
Tree
186b291

3 changed files

StatusFile+-
M internal/checks/queries/checks.sql 54 0
M internal/checks/sqlc/checks.sql.go 154 0
M internal/checks/sqlc/querier.go 2 0
internal/checks/queries/checks.sqlmodified
@@ -17,6 +17,60 @@ RETURNING *;
1717
 -- name: GetCheckSuite :one
1818
 SELECT * FROM check_suites WHERE id = $1;
1919
 
20
+-- name: GetCheckSuiteForRepo :one
21
+SELECT
22
+    cs.*,
23
+    COALESCE(pr_meta.number, 0)::bigint AS pull_number,
24
+    COALESCE(pr_meta.title, '')::text AS pull_title,
25
+    COALESCE(pr_meta.author_username, '')::text AS pull_author_username,
26
+    COALESCE(pr_meta.head_ref, '')::text AS head_ref,
27
+    COALESCE(pr_meta.base_ref, '')::text AS base_ref
28
+FROM check_suites cs
29
+LEFT JOIN LATERAL (
30
+    SELECT
31
+        i.number,
32
+        i.title,
33
+        COALESCE(u.username, '') AS author_username,
34
+        pr.head_ref,
35
+        pr.base_ref
36
+    FROM pull_requests pr
37
+    JOIN issues i ON i.id = pr.issue_id AND i.kind = 'pr'
38
+    LEFT JOIN users u ON u.id = i.author_user_id
39
+    WHERE pr.head_repo_id = cs.repo_id
40
+      AND pr.head_oid = cs.head_sha
41
+    ORDER BY i.updated_at DESC, i.number DESC
42
+    LIMIT 1
43
+) pr_meta ON true
44
+WHERE cs.repo_id = $1 AND cs.id = $2;
45
+
46
+-- name: ListCheckSuitesForRepo :many
47
+SELECT
48
+    cs.*,
49
+    COALESCE(pr_meta.number, 0)::bigint AS pull_number,
50
+    COALESCE(pr_meta.title, '')::text AS pull_title,
51
+    COALESCE(pr_meta.author_username, '')::text AS pull_author_username,
52
+    COALESCE(pr_meta.head_ref, '')::text AS head_ref,
53
+    COALESCE(pr_meta.base_ref, '')::text AS base_ref
54
+FROM check_suites cs
55
+LEFT JOIN LATERAL (
56
+    SELECT
57
+        i.number,
58
+        i.title,
59
+        COALESCE(u.username, '') AS author_username,
60
+        pr.head_ref,
61
+        pr.base_ref
62
+    FROM pull_requests pr
63
+    JOIN issues i ON i.id = pr.issue_id AND i.kind = 'pr'
64
+    LEFT JOIN users u ON u.id = i.author_user_id
65
+    WHERE pr.head_repo_id = cs.repo_id
66
+      AND pr.head_oid = cs.head_sha
67
+    ORDER BY i.updated_at DESC, i.number DESC
68
+    LIMIT 1
69
+) pr_meta ON true
70
+WHERE cs.repo_id = $1
71
+ORDER BY cs.updated_at DESC, cs.id DESC
72
+LIMIT $2 OFFSET $3;
73
+
2074
 -- name: ListCheckSuitesForCommit :many
2175
 SELECT * FROM check_suites
2276
 WHERE repo_id = $1 AND head_sha = $2
internal/checks/sqlc/checks.sql.gomodified
@@ -163,6 +163,75 @@ func (q *Queries) GetCheckSuite(ctx context.Context, db DBTX, id int64) (CheckSu
163163
 	return i, err
164164
 }
165165
 
166
+const getCheckSuiteForRepo = `-- name: GetCheckSuiteForRepo :one
167
+SELECT
168
+    cs.id, cs.repo_id, cs.head_sha, cs.app_slug, cs.status, cs.conclusion, cs.created_at, cs.updated_at,
169
+    COALESCE(pr_meta.number, 0)::bigint AS pull_number,
170
+    COALESCE(pr_meta.title, '')::text AS pull_title,
171
+    COALESCE(pr_meta.author_username, '')::text AS pull_author_username,
172
+    COALESCE(pr_meta.head_ref, '')::text AS head_ref,
173
+    COALESCE(pr_meta.base_ref, '')::text AS base_ref
174
+FROM check_suites cs
175
+LEFT JOIN LATERAL (
176
+    SELECT
177
+        i.number,
178
+        i.title,
179
+        COALESCE(u.username, '') AS author_username,
180
+        pr.head_ref,
181
+        pr.base_ref
182
+    FROM pull_requests pr
183
+    JOIN issues i ON i.id = pr.issue_id AND i.kind = 'pr'
184
+    LEFT JOIN users u ON u.id = i.author_user_id
185
+    WHERE pr.head_repo_id = cs.repo_id
186
+      AND pr.head_oid = cs.head_sha
187
+    ORDER BY i.updated_at DESC, i.number DESC
188
+    LIMIT 1
189
+) pr_meta ON true
190
+WHERE cs.repo_id = $1 AND cs.id = $2
191
+`
192
+
193
+type GetCheckSuiteForRepoParams struct {
194
+	RepoID int64
195
+	ID     int64
196
+}
197
+
198
+type GetCheckSuiteForRepoRow struct {
199
+	ID                 int64
200
+	RepoID             int64
201
+	HeadSha            string
202
+	AppSlug            string
203
+	Status             CheckStatus
204
+	Conclusion         NullCheckConclusion
205
+	CreatedAt          pgtype.Timestamptz
206
+	UpdatedAt          pgtype.Timestamptz
207
+	PullNumber         int64
208
+	PullTitle          string
209
+	PullAuthorUsername string
210
+	HeadRef            string
211
+	BaseRef            string
212
+}
213
+
214
+func (q *Queries) GetCheckSuiteForRepo(ctx context.Context, db DBTX, arg GetCheckSuiteForRepoParams) (GetCheckSuiteForRepoRow, error) {
215
+	row := db.QueryRow(ctx, getCheckSuiteForRepo, arg.RepoID, arg.ID)
216
+	var i GetCheckSuiteForRepoRow
217
+	err := row.Scan(
218
+		&i.ID,
219
+		&i.RepoID,
220
+		&i.HeadSha,
221
+		&i.AppSlug,
222
+		&i.Status,
223
+		&i.Conclusion,
224
+		&i.CreatedAt,
225
+		&i.UpdatedAt,
226
+		&i.PullNumber,
227
+		&i.PullTitle,
228
+		&i.PullAuthorUsername,
229
+		&i.HeadRef,
230
+		&i.BaseRef,
231
+	)
232
+	return i, err
233
+}
234
+
166235
 const getLatestCheckRunByName = `-- name: GetLatestCheckRunByName :one
167236
 SELECT id, suite_id, repo_id, head_sha, name, status, conclusion, started_at, completed_at, details_url, output, external_id, created_at, updated_at FROM check_runs
168237
 WHERE repo_id = $1 AND head_sha = $2 AND name = $3
@@ -398,6 +467,91 @@ func (q *Queries) ListCheckSuitesForCommit(ctx context.Context, db DBTX, arg Lis
398467
 	return items, nil
399468
 }
400469
 
470
+const listCheckSuitesForRepo = `-- name: ListCheckSuitesForRepo :many
471
+SELECT
472
+    cs.id, cs.repo_id, cs.head_sha, cs.app_slug, cs.status, cs.conclusion, cs.created_at, cs.updated_at,
473
+    COALESCE(pr_meta.number, 0)::bigint AS pull_number,
474
+    COALESCE(pr_meta.title, '')::text AS pull_title,
475
+    COALESCE(pr_meta.author_username, '')::text AS pull_author_username,
476
+    COALESCE(pr_meta.head_ref, '')::text AS head_ref,
477
+    COALESCE(pr_meta.base_ref, '')::text AS base_ref
478
+FROM check_suites cs
479
+LEFT JOIN LATERAL (
480
+    SELECT
481
+        i.number,
482
+        i.title,
483
+        COALESCE(u.username, '') AS author_username,
484
+        pr.head_ref,
485
+        pr.base_ref
486
+    FROM pull_requests pr
487
+    JOIN issues i ON i.id = pr.issue_id AND i.kind = 'pr'
488
+    LEFT JOIN users u ON u.id = i.author_user_id
489
+    WHERE pr.head_repo_id = cs.repo_id
490
+      AND pr.head_oid = cs.head_sha
491
+    ORDER BY i.updated_at DESC, i.number DESC
492
+    LIMIT 1
493
+) pr_meta ON true
494
+WHERE cs.repo_id = $1
495
+ORDER BY cs.updated_at DESC, cs.id DESC
496
+LIMIT $2 OFFSET $3
497
+`
498
+
499
+type ListCheckSuitesForRepoParams struct {
500
+	RepoID int64
501
+	Limit  int32
502
+	Offset int32
503
+}
504
+
505
+type ListCheckSuitesForRepoRow struct {
506
+	ID                 int64
507
+	RepoID             int64
508
+	HeadSha            string
509
+	AppSlug            string
510
+	Status             CheckStatus
511
+	Conclusion         NullCheckConclusion
512
+	CreatedAt          pgtype.Timestamptz
513
+	UpdatedAt          pgtype.Timestamptz
514
+	PullNumber         int64
515
+	PullTitle          string
516
+	PullAuthorUsername string
517
+	HeadRef            string
518
+	BaseRef            string
519
+}
520
+
521
+func (q *Queries) ListCheckSuitesForRepo(ctx context.Context, db DBTX, arg ListCheckSuitesForRepoParams) ([]ListCheckSuitesForRepoRow, error) {
522
+	rows, err := db.Query(ctx, listCheckSuitesForRepo, arg.RepoID, arg.Limit, arg.Offset)
523
+	if err != nil {
524
+		return nil, err
525
+	}
526
+	defer rows.Close()
527
+	items := []ListCheckSuitesForRepoRow{}
528
+	for rows.Next() {
529
+		var i ListCheckSuitesForRepoRow
530
+		if err := rows.Scan(
531
+			&i.ID,
532
+			&i.RepoID,
533
+			&i.HeadSha,
534
+			&i.AppSlug,
535
+			&i.Status,
536
+			&i.Conclusion,
537
+			&i.CreatedAt,
538
+			&i.UpdatedAt,
539
+			&i.PullNumber,
540
+			&i.PullTitle,
541
+			&i.PullAuthorUsername,
542
+			&i.HeadRef,
543
+			&i.BaseRef,
544
+		); err != nil {
545
+			return nil, err
546
+		}
547
+		items = append(items, i)
548
+	}
549
+	if err := rows.Err(); err != nil {
550
+		return nil, err
551
+	}
552
+	return items, nil
553
+}
554
+
401555
 const markCheckSuiteStale = `-- name: MarkCheckSuiteStale :exec
402556
 UPDATE check_suites
403557
 SET status = 'completed', conclusion = 'stale'
internal/checks/sqlc/querier.gomodified
@@ -16,6 +16,7 @@ type Querier interface {
1616
 	// external_id). NULL external_id never matches via this query.
1717
 	GetCheckRunByExternalID(ctx context.Context, db DBTX, arg GetCheckRunByExternalIDParams) (CheckRun, error)
1818
 	GetCheckSuite(ctx context.Context, db DBTX, id int64) (CheckSuite, error)
19
+	GetCheckSuiteForRepo(ctx context.Context, db DBTX, arg GetCheckSuiteForRepoParams) (GetCheckSuiteForRepoRow, error)
1920
 	// Required-check evaluator: most recent run with the given name on the
2021
 	// specified head_sha.
2122
 	GetLatestCheckRunByName(ctx context.Context, db DBTX, arg GetLatestCheckRunByNameParams) (CheckRun, error)
@@ -33,6 +34,7 @@ type Querier interface {
3334
 	// the previous head to conclusion='stale'.
3435
 	ListCheckSuiteIDsForHead(ctx context.Context, db DBTX, arg ListCheckSuiteIDsForHeadParams) ([]int64, error)
3536
 	ListCheckSuitesForCommit(ctx context.Context, db DBTX, arg ListCheckSuitesForCommitParams) ([]CheckSuite, error)
37
+	ListCheckSuitesForRepo(ctx context.Context, db DBTX, arg ListCheckSuitesForRepoParams) ([]ListCheckSuitesForRepoRow, error)
3638
 	MarkCheckSuiteStale(ctx context.Context, db DBTX, id int64) error
3739
 	UpdateCheckRun(ctx context.Context, db DBTX, arg UpdateCheckRunParams) error
3840
 	// Persists the rollup result computed in Go (suite_rollup.go).