v0.1.0 3f267b5

repo.go RawHistoryFolder

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package rpc
4
5import (
6	"context"
7	"fmt"
8
9	"connectrpc.com/connect"
10
11	pb "bnema.dev/ermite-api/gen/ermite"
12	"bnema.dev/ermite/internal/app"
13	"bnema.dev/ermite/internal/domain"
14)
15
16// repoService implements ermiteconnect.RepoServiceHandler.
17type repoService struct{ repos *app.Repos }
18
19func (s *repoService) ListRepos(ctx context.Context, req *connect.Request[pb.ListReposRequest]) (*connect.Response[pb.ListReposResponse], error) {
20	page, err := s.repos.ListPage(ctx, caller(ctx), int(req.Msg.GetPageSize()), req.Msg.GetPageToken())
21	if err != nil {
22		return nil, connectError(ctx, err)
23	}
24	out := &pb.ListReposResponse{NextPageToken: page.NextPageToken, Repos: make([]*pb.Repo, 0, len(page.Repos))}
25	for _, repo := range page.Repos {
26		view, err := s.repos.View(ctx, repo)
27		if err != nil {
28			return nil, connectError(ctx, err)
29		}
30		out.Repos = append(out.Repos, repoToPB(view))
31	}
32	return connect.NewResponse(out), nil
33}
34
35func (s *repoService) GetRepo(ctx context.Context, req *connect.Request[pb.GetRepoRequest]) (*connect.Response[pb.GetRepoResponse], error) {
36	name, err := domain.ParseRepoName(req.Msg.GetRepo())
37	if err != nil {
38		return nil, connectError(ctx, err)
39	}
40	repo, err := s.repos.Get(ctx, caller(ctx), name)
41	if err != nil {
42		return nil, connectError(ctx, err)
43	}
44	return respondRepo(ctx, s.repos, repo, func(r *pb.Repo) *pb.GetRepoResponse { return &pb.GetRepoResponse{Repo: r} })
45}
46
47func (s *repoService) CreateRepo(ctx context.Context, req *connect.Request[pb.CreateRepoRequest]) (*connect.Response[pb.CreateRepoResponse], error) {
48	name, err := domain.ParseRepoName(req.Msg.GetName())
49	if err != nil {
50		return nil, connectError(ctx, err)
51	}
52	vis, err := visibilityFromPB(req.Msg.GetVisibility())
53	if err != nil {
54		return nil, connectError(ctx, err)
55	}
56	repo, err := s.repos.Create(ctx, caller(ctx), app.CreateParams{
57		Name: name, Description: req.Msg.GetDescription(), Visibility: vis,
58	})
59	if err != nil {
60		return nil, connectError(ctx, err)
61	}
62	return respondRepo(ctx, s.repos, repo, func(r *pb.Repo) *pb.CreateRepoResponse { return &pb.CreateRepoResponse{Repo: r} })
63}
64
65func (s *repoService) UpdateRepo(ctx context.Context, req *connect.Request[pb.UpdateRepoRequest]) (*connect.Response[pb.UpdateRepoResponse], error) {
66	name, err := domain.ParseRepoName(req.Msg.GetRepo())
67	if err != nil {
68		return nil, connectError(ctx, err)
69	}
70	params, err := updateParams(req.Msg)
71	if err != nil {
72		return nil, connectError(ctx, err)
73	}
74	repo, err := s.repos.Update(ctx, caller(ctx), name, params)
75	if err != nil {
76		return nil, connectError(ctx, err)
77	}
78	return respondRepo(ctx, s.repos, repo, func(r *pb.Repo) *pb.UpdateRepoResponse { return &pb.UpdateRepoResponse{Repo: r} })
79}
80
81// updateParams maps the field mask of an update. The mask must name at least
82// one of "description", "visibility" and "default_branch" and nothing else.
83//
84//hexcheck:ignore suspicious-business-logic-in-adapter maps the field mask of the contract to the optional fields of the use case; no rule is decided here
85func updateParams(msg *pb.UpdateRepoRequest) (app.UpdateParams, error) {
86	var params app.UpdateParams
87	paths := msg.GetUpdateMask().GetPaths()
88	if len(paths) == 0 {
89		return params, fmt.Errorf("%w: update_mask is empty", domain.ErrInvalid)
90	}
91	for _, path := range paths {
92		switch path {
93		case "description":
94			params.Description = new(msg.GetDescription())
95		case "visibility":
96			vis, err := visibilityFromPB(msg.GetVisibility())
97			if err != nil {
98				return params, err
99			}
100			if vis == "" {
101				return params, fmt.Errorf("%w: visibility is unspecified", domain.ErrInvalid)
102			}
103			params.Visibility = &vis
104		case "default_branch":
105			params.DefaultBranch = new(msg.GetDefaultBranch())
106		default:
107			return params, fmt.Errorf("%w: unknown path %q in update_mask", domain.ErrInvalid, path)
108		}
109	}
110	return params, nil
111}
112
113func (s *repoService) DeleteRepo(ctx context.Context, req *connect.Request[pb.DeleteRepoRequest]) (*connect.Response[pb.DeleteRepoResponse], error) {
114	name, err := domain.ParseRepoName(req.Msg.GetRepo())
115	if err != nil {
116		return nil, connectError(ctx, err)
117	}
118	if err := s.repos.Delete(ctx, caller(ctx), name); err != nil {
119		return nil, connectError(ctx, err)
120	}
121	return connect.NewResponse(&pb.DeleteRepoResponse{}), nil
122}
123
124// respondRepo adds the default branch to repo and wraps it in a response.
125func respondRepo[T any](ctx context.Context, repos *app.Repos, repo domain.Repo, wrap func(*pb.Repo) *T) (*connect.Response[T], error) {
126	view, err := repos.View(ctx, repo)
127	if err != nil {
128		return nil, connectError(ctx, err)
129	}
130	return connect.NewResponse(wrap(repoToPB(view))), nil
131}

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 2

Tags 45

↑↓ moveenter openesc close