v0.31.0 bf0756b

permission_repo.go RawHistoryFolder

1package sqlite
2
3import (
4	"context"
5	"database/sql"
6	"errors"
7
8	"github.com/bnema/dumber/internal/application/port"
9	"github.com/bnema/dumber/internal/domain/entity"
10	"github.com/bnema/dumber/internal/infrastructure/persistence/sqlite/sqlc"
11	"github.com/bnema/dumber/internal/logging"
12)
13
14type permissionRepo struct {
15	queries *sqlc.Queries
16}
17
18// NewPermissionRepository creates a new SQLite-backed permission repository.
19func NewPermissionRepository(db *sql.DB) port.PermissionRepository {
20	return &permissionRepo{queries: sqlc.New(db)}
21}
22
23func (r *permissionRepo) Get(ctx context.Context, origin string, permType entity.PermissionType) (*entity.PermissionRecord, error) {
24	log := logging.FromContext(ctx)
25	log.Debug().Str("origin", origin).Str("type", string(permType)).Msg("getting permission")
26
27	row, err := r.queries.GetPermission(ctx, sqlc.GetPermissionParams{
28		Origin:         origin,
29		PermissionType: string(permType),
30	})
31	if err != nil {
32		if errors.Is(err, sql.ErrNoRows) {
33			return nil, nil
34		}
35		return nil, err
36	}
37
38	return permissionFromRow(row), nil
39}
40
41func (r *permissionRepo) Set(ctx context.Context, record *entity.PermissionRecord) error {
42	log := logging.FromContext(ctx)
43
44	if record == nil {
45		log.Error().Msg("cannot set nil permission record")
46		return errors.New("cannot set nil permission record")
47	}
48
49	log.Debug().
50		Str("origin", record.Origin).
51		Str("type", string(record.Type)).
52		Str("decision", string(record.Decision)).
53		Msg("setting permission")
54
55	return r.queries.SetPermission(ctx, sqlc.SetPermissionParams{
56		Origin:         record.Origin,
57		PermissionType: string(record.Type),
58		Decision:       string(record.Decision),
59	})
60}
61
62func (r *permissionRepo) Delete(ctx context.Context, origin string, permType entity.PermissionType) error {
63	log := logging.FromContext(ctx)
64	log.Debug().Str("origin", origin).Str("type", string(permType)).Msg("deleting permission")
65
66	return r.queries.DeletePermission(ctx, sqlc.DeletePermissionParams{
67		Origin:         origin,
68		PermissionType: string(permType),
69	})
70}
71
72func (r *permissionRepo) GetAll(ctx context.Context, origin string) ([]*entity.PermissionRecord, error) {
73	log := logging.FromContext(ctx)
74	log.Debug().Str("origin", origin).Msg("getting all permissions for origin")
75
76	// sqlc returns an empty slice (not sql.ErrNoRows) when nothing matches.
77	rows, err := r.queries.ListPermissionsByOrigin(ctx, origin)
78	if err != nil {
79		return nil, err
80	}
81	if len(rows) == 0 {
82		return []*entity.PermissionRecord{}, nil
83	}
84
85	records := make([]*entity.PermissionRecord, len(rows))
86	for i, row := range rows {
87		records[i] = permissionFromRow(row)
88	}
89	return records, nil
90}
91
92func permissionFromRow(row sqlc.Permission) *entity.PermissionRecord {
93	record := &entity.PermissionRecord{
94		Origin:   row.Origin,
95		Type:     entity.PermissionType(row.PermissionType),
96		Decision: entity.PermissionDecision(row.Decision),
97	}
98	if row.UpdatedAt.Valid {
99		record.UpdatedAt = row.UpdatedAt.Time.Unix()
100	}
101	return record
102}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 8

Tags 42

↑↓ moveenter openesc close