@@ -0,0 +1,123 @@ |
| 1 | +// Code generated by sqlc. DO NOT EDIT. |
| 2 | +// versions: |
| 3 | +// sqlc v1.31.1 |
| 4 | +// source: repo_collaborators.sql |
| 5 | + |
| 6 | +package policydb |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + |
| 11 | + "github.com/jackc/pgx/v5/pgtype" |
| 12 | +) |
| 13 | + |
| 14 | +const getCollabRole = `-- name: GetCollabRole :one |
| 15 | + |
| 16 | +SELECT role FROM repo_collaborators |
| 17 | +WHERE repo_id = $1 AND user_id = $2 |
| 18 | +` |
| 19 | + |
| 20 | +type GetCollabRoleParams struct { |
| 21 | + RepoID int64 |
| 22 | + UserID int64 |
| 23 | +} |
| 24 | + |
| 25 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 26 | +// |
| 27 | +// Query surface for the policy package. The policy code reads roles to |
| 28 | +// decide; admin/settings UIs (S32) write through these as well. |
| 29 | +// Returns the collaborator role for (repo_id, user_id), or pgx.ErrNoRows |
| 30 | +// when the user is not a collaborator on this repo. |
| 31 | +func (q *Queries) GetCollabRole(ctx context.Context, db DBTX, arg GetCollabRoleParams) (CollabRole, error) { |
| 32 | + row := db.QueryRow(ctx, getCollabRole, arg.RepoID, arg.UserID) |
| 33 | + var role CollabRole |
| 34 | + err := row.Scan(&role) |
| 35 | + return role, err |
| 36 | +} |
| 37 | + |
| 38 | +const listCollabs = `-- name: ListCollabs :many |
| 39 | +SELECT rc.repo_id, rc.user_id, rc.role, rc.added_at, |
| 40 | + u.username AS username, u.display_name AS display_name |
| 41 | +FROM repo_collaborators rc |
| 42 | +JOIN users u ON u.id = rc.user_id |
| 43 | +WHERE rc.repo_id = $1 |
| 44 | +ORDER BY rc.added_at DESC |
| 45 | +` |
| 46 | + |
| 47 | +type ListCollabsRow struct { |
| 48 | + RepoID int64 |
| 49 | + UserID int64 |
| 50 | + Role CollabRole |
| 51 | + AddedAt pgtype.Timestamptz |
| 52 | + Username string |
| 53 | + DisplayName string |
| 54 | +} |
| 55 | + |
| 56 | +// Used by the repo settings page (S32) and tests. |
| 57 | +func (q *Queries) ListCollabs(ctx context.Context, db DBTX, repoID int64) ([]ListCollabsRow, error) { |
| 58 | + rows, err := db.Query(ctx, listCollabs, repoID) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + defer rows.Close() |
| 63 | + items := []ListCollabsRow{} |
| 64 | + for rows.Next() { |
| 65 | + var i ListCollabsRow |
| 66 | + if err := rows.Scan( |
| 67 | + &i.RepoID, |
| 68 | + &i.UserID, |
| 69 | + &i.Role, |
| 70 | + &i.AddedAt, |
| 71 | + &i.Username, |
| 72 | + &i.DisplayName, |
| 73 | + ); err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + items = append(items, i) |
| 77 | + } |
| 78 | + if err := rows.Err(); err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + return items, nil |
| 82 | +} |
| 83 | + |
| 84 | +const removeCollab = `-- name: RemoveCollab :exec |
| 85 | +DELETE FROM repo_collaborators WHERE repo_id = $1 AND user_id = $2 |
| 86 | +` |
| 87 | + |
| 88 | +type RemoveCollabParams struct { |
| 89 | + RepoID int64 |
| 90 | + UserID int64 |
| 91 | +} |
| 92 | + |
| 93 | +func (q *Queries) RemoveCollab(ctx context.Context, db DBTX, arg RemoveCollabParams) error { |
| 94 | + _, err := db.Exec(ctx, removeCollab, arg.RepoID, arg.UserID) |
| 95 | + return err |
| 96 | +} |
| 97 | + |
| 98 | +const upsertCollabRole = `-- name: UpsertCollabRole :exec |
| 99 | +INSERT INTO repo_collaborators (repo_id, user_id, role, added_by_user_id) |
| 100 | +VALUES ($1, $2, $3, $4::bigint) |
| 101 | +ON CONFLICT (repo_id, user_id) |
| 102 | +DO UPDATE SET role = EXCLUDED.role, |
| 103 | + added_by_user_id = EXCLUDED.added_by_user_id |
| 104 | +` |
| 105 | + |
| 106 | +type UpsertCollabRoleParams struct { |
| 107 | + RepoID int64 |
| 108 | + UserID int64 |
| 109 | + Role CollabRole |
| 110 | + AddedByUserID pgtype.Int8 |
| 111 | +} |
| 112 | + |
| 113 | +// Insert or upgrade a collaborator's role. added_by_user_id records who |
| 114 | +// granted the role for the audit trail. |
| 115 | +func (q *Queries) UpsertCollabRole(ctx context.Context, db DBTX, arg UpsertCollabRoleParams) error { |
| 116 | + _, err := db.Exec(ctx, upsertCollabRole, |
| 117 | + arg.RepoID, |
| 118 | + arg.UserID, |
| 119 | + arg.Role, |
| 120 | + arg.AddedByUserID, |
| 121 | + ) |
| 122 | + return err |
| 123 | +} |