Go · 1899 bytes Raw Blame History
1 // SPDX-License-Identifier: AGPL-3.0-or-later
2
3 package sigverify
4
5 import (
6 "context"
7
8 "github.com/jackc/pgx/v5/pgtype"
9
10 reposdb "github.com/tenseleyFlow/shithub/internal/repos/sqlc"
11 )
12
13 // CacheWriter is the minimal surface WriteResult needs from the
14 // reposdb queries layer. Sub-PR 3 (backfill) and Sub-PR 4 (settings
15 // add handler) both consume WriteResult; this interface lets them
16 // pass any concrete DBTX-bound queries set without taking the full
17 // reposdb.Queries struct as a dependency.
18 type CacheWriter interface {
19 UpsertCommitVerification(ctx context.Context, db reposdb.DBTX, arg reposdb.UpsertCommitVerificationParams) error
20 }
21
22 // WriteResult persists a verification Result into the cache table.
23 // Wraps the sqlc-generated UpsertCommitVerification so the Result
24 // → params translation lives in one place.
25 //
26 // Concurrency: UpsertCommitVerification's ON CONFLICT clause makes
27 // this safe to call from the orchestrator and the backfill worker
28 // against the same (repo_id, commit_oid) without losing data — the
29 // most-recent invocation wins.
30 func WriteResult(
31 ctx context.Context,
32 q CacheWriter,
33 db reposdb.DBTX,
34 repoID int64,
35 commitOID string,
36 kind Kind,
37 r Result,
38 ) error {
39 return q.UpsertCommitVerification(ctx, db, reposdb.UpsertCommitVerificationParams{
40 RepoID: repoID,
41 CommitOid: commitOID,
42 Reason: string(r.Reason),
43 Verified: r.Verified,
44 SignerUserID: nullableInt64(r.SignerUserID),
45 SignerSubkeyID: nullableInt64(r.SignerSubkeyID),
46 Kind: string(kind),
47 SignatureArmored: nullableText(r.Signature),
48 Payload: r.Payload,
49 })
50 }
51
52 func nullableInt64(v int64) pgtype.Int8 {
53 if v == 0 {
54 return pgtype.Int8{}
55 }
56 return pgtype.Int8{Int64: v, Valid: true}
57 }
58
59 func nullableText(s string) pgtype.Text {
60 if s == "" {
61 return pgtype.Text{}
62 }
63 return pgtype.Text{String: s, Valid: true}
64 }
65