BlueGit

timfa bluegit

Files

bluegit / internal / server / api_collab.go

api_collab.go

api_collab.go 21.7 KiB Download Raw
1package server
2
3import (
4 "net/http"
5 "strconv"
6
7 "gitlab.com/Timfa/bluegit/internal/access"
8 "gitlab.com/Timfa/bluegit/internal/i18n"
9 "gitlab.com/Timfa/bluegit/internal/store"
10 "gitlab.com/Timfa/bluegit/internal/webhook"
11)
12
13func groupJSON(g store.Group) map[string]any {
14 return map[string]any{
15 "id": g.ID, "name": g.Name, "path": g.Path, "description": g.Description,
16 "visibility": g.Visibility, "default_role": g.DefaultRole,
17 }
18}
19
20func projectJSON(repo store.Repo) map[string]any {
21 return map[string]any{
22 "id": repo.ID, "path": repo.FullPath(), "name": repo.Name, "owner": repo.OwnerPath,
23 "description": repo.Description, "visibility": repo.Visibility, "default_branch": repo.DefaultBranch,
24 "release_visibility": repo.ReleaseVisibility,
25 }
26}
27
28func memberJSON(m store.Member) map[string]any {
29 return map[string]any{"user_id": m.UserID, "username": m.Username, "role": m.Role, "pending": m.Pending}
30}
31
32func (s *Server) apiListGroups(w http.ResponseWriter, r *http.Request) {
33 list, err := s.Store.ListGroups(r.Context(), apiUser(r).ID)
34 if !s.apiOK(w, r, err) {
35 return
36 }
37 page, per := pageQuery(r)
38 rows := make([]map[string]any, 0, len(list))
39 for _, g := range list {
40 rows = append(rows, groupJSON(g))
41 }
42 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
43}
44
45func (s *Server) apiCreateGroup(w http.ResponseWriter, r *http.Request) {
46 var body struct {
47 Name string `json:"name"`
48 Description string `json:"description"`
49 DefaultRole string `json:"default_role"`
50 }
51 if err := readJSON(r, &body); err != nil {
52 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
53 return
54 }
55 g, err := s.opCreateGroup(r.Context(), apiUser(r), body.Name, body.Description, body.DefaultRole)
56 if !s.apiOK(w, r, err) {
57 return
58 }
59 writeJSON(w, http.StatusCreated, groupJSON(g))
60}
61
62func (s *Server) apiGroup(w http.ResponseWriter, r *http.Request, min access.Role) (store.Group, bool) {
63 g, err := s.Store.GroupByPath(r.Context(), r.PathValue("path"))
64 if err != nil {
65 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
66 return store.Group{}, false
67 }
68 role, err := s.Store.GroupRole(r.Context(), g.ID, apiUser(r).ID)
69 if err != nil {
70 s.writeAPIErr(w, r, fail(500, err))
71 return store.Group{}, false
72 }
73 if !role.CanRead() && g.Visibility != "public" {
74 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
75 return store.Group{}, false
76 }
77 if min != access.RoleNone && min != access.RoleReadonly && !role.AtLeast(min) {
78 s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden")))
79 return store.Group{}, false
80 }
81 if min == access.RoleReadonly && !role.CanRead() && g.Visibility != "public" {
82 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
83 return store.Group{}, false
84 }
85 return g, true
86}
87
88func (s *Server) apiGetGroup(w http.ResponseWriter, r *http.Request) {
89 g, ok := s.apiGroup(w, r, access.RoleReadonly)
90 if !ok {
91 return
92 }
93 writeJSON(w, http.StatusOK, groupJSON(g))
94}
95
96func (s *Server) apiPutGroup(w http.ResponseWriter, r *http.Request) {
97 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
98 if !ok {
99 return
100 }
101 var body struct {
102 Description string `json:"description"`
103 Visibility string `json:"visibility"`
104 DefaultRole string `json:"default_role"`
105 }
106 if err := readJSON(r, &body); err != nil {
107 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
108 return
109 }
110 if !s.apiOK(w, r, s.opUpdateGroup(r.Context(), apiUser(r), g, body.Description, body.Visibility, body.DefaultRole)) {
111 return
112 }
113 g, _ = s.Store.GroupByPath(r.Context(), g.Path)
114 writeJSON(w, http.StatusOK, groupJSON(g))
115}
116
117func (s *Server) apiDeleteGroup(w http.ResponseWriter, r *http.Request) {
118 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
119 if !ok {
120 return
121 }
122 var body struct {
123 Confirm string `json:"confirm"`
124 }
125 if err := readJSON(r, &body); err != nil {
126 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
127 return
128 }
129 if !s.apiOK(w, r, s.opDeleteGroup(r.Context(), apiUser(r), g, body.Confirm)) {
130 return
131 }
132 writeJSON(w, http.StatusNoContent, nil)
133}
134
135func (s *Server) apiTransferGroup(w http.ResponseWriter, r *http.Request) {
136 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
137 if !ok {
138 return
139 }
140 var body struct {
141 Username string `json:"username"`
142 Confirm string `json:"confirm"`
143 }
144 if err := readJSON(r, &body); err != nil {
145 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
146 return
147 }
148 if !s.apiOK(w, r, s.opTransferGroup(r.Context(), apiUser(r), g, body.Username, body.Confirm)) {
149 return
150 }
151 writeJSON(w, http.StatusOK, map[string]any{"path": g.Path, "owner": body.Username})
152}
153
154func (s *Server) apiGroupProjects(w http.ResponseWriter, r *http.Request) {
155 g, ok := s.apiGroup(w, r, access.RoleReadonly)
156 if !ok {
157 return
158 }
159 list, err := s.Store.ReposByGroup(r.Context(), g.ID)
160 if !s.apiOK(w, r, err) {
161 return
162 }
163 s.writeProjects(w, r, list)
164}
165
166func (s *Server) apiListProjects(w http.ResponseWriter, r *http.Request) {
167 ids, err := s.Store.ListRepoIDs(r.Context(), apiUser(r).ID)
168 if !s.apiOK(w, r, err) {
169 return
170 }
171 list, err := s.Store.ReposByIDs(r.Context(), ids)
172 if !s.apiOK(w, r, err) {
173 return
174 }
175 s.writeProjects(w, r, list)
176}
177
178func (s *Server) writeProjects(w http.ResponseWriter, r *http.Request, list []store.Repo) {
179 page, per := pageQuery(r)
180 rows := make([]map[string]any, 0, len(list))
181 for _, repo := range list {
182 rows = append(rows, projectJSON(repo))
183 }
184 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
185}
186
187func (s *Server) apiCreateProject(w http.ResponseWriter, r *http.Request) {
188 var body struct {
189 Name string `json:"name"`
190 Description string `json:"description"`
191 Group string `json:"group"`
192 Visibility string `json:"visibility"`
193 Readme bool `json:"initialize_with_readme"`
194 }
195 if err := readJSON(r, &body); err != nil {
196 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
197 return
198 }
199 repo, err := s.opCreateRepo(r.Context(), apiUser(r), body.Name, body.Description, body.Group, body.Visibility, body.Readme)
200 if !s.apiOK(w, r, err) {
201 return
202 }
203 writeJSON(w, http.StatusCreated, projectJSON(repo))
204}
205
206func (s *Server) apiRepo(w http.ResponseWriter, r *http.Request, min access.Role) (store.Repo, access.Role, bool) {
207 repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo"))
208 if err != nil {
209 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
210 return store.Repo{}, "", false
211 }
212 role, err := s.Store.EffectiveRole(r.Context(), apiUser(r).ID, repo.ID)
213 if err != nil {
214 s.writeAPIErr(w, r, fail(500, err))
215 return store.Repo{}, "", false
216 }
217 if jr := jobRole(r, repo.ID); jr != access.RoleNone && !role.CanRead() {
218 role = jr
219 }
220 readable := role.CanRead() || repo.Visibility == "public"
221 if !readable {
222 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
223 return store.Repo{}, "", false
224 }
225 if min.Rank() > access.RoleReadonly.Rank() && !role.AtLeast(min) {
226 s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden")))
227 return store.Repo{}, "", false
228 }
229 return repo, role, true
230}
231
232func (s *Server) apiGetProject(w http.ResponseWriter, r *http.Request) {
233 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
234 if !ok {
235 return
236 }
237 writeJSON(w, http.StatusOK, projectJSON(repo))
238}
239
240func (s *Server) apiPutProject(w http.ResponseWriter, r *http.Request) {
241 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
242 if !ok {
243 return
244 }
245 var body struct {
246 Description *string `json:"description"`
247 Visibility *string `json:"visibility"`
248 ReleaseVisibility *string `json:"release_visibility"`
249 }
250 if err := readJSON(r, &body); err != nil {
251 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
252 return
253 }
254 // Velden die ontbreken blijven zoals ze zijn.
255 desc, vis := repo.Description, repo.Visibility
256 if body.Description != nil {
257 desc = *body.Description
258 }
259 if body.Visibility != nil {
260 vis = *body.Visibility
261 }
262 if body.ReleaseVisibility != nil && !store.ValidReleaseVisibility(*body.ReleaseVisibility) {
263 s.writeAPIErr(w, r, fail(400, i18n.E("err.releaseVisibility")))
264 return
265 }
266 if !s.apiOK(w, r, s.opUpdateRepo(r.Context(), apiUser(r), repo, desc, vis)) {
267 return
268 }
269 if body.ReleaseVisibility != nil {
270 if !s.apiOK(w, r, s.opSetReleaseVisibility(r.Context(), apiUser(r), repo, *body.ReleaseVisibility)) {
271 return
272 }
273 }
274 repo, _ = s.Store.RepoByPath(r.Context(), repo.OwnerPath, repo.Name)
275 writeJSON(w, http.StatusOK, projectJSON(repo))
276}
277
278func (s *Server) apiDeleteProject(w http.ResponseWriter, r *http.Request) {
279 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
280 if !ok {
281 return
282 }
283 var body struct {
284 Confirm string `json:"confirm"`
285 }
286 if err := readJSON(r, &body); err != nil {
287 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
288 return
289 }
290 if !s.apiOK(w, r, s.opDeleteRepo(r.Context(), apiUser(r), repo, body.Confirm, forceParam(r))) {
291 return
292 }
293 writeJSON(w, http.StatusNoContent, nil)
294}
295
296func (s *Server) apiStar(w http.ResponseWriter, r *http.Request) {
297 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
298 if !ok {
299 return
300 }
301 on, err := s.opToggleStar(r.Context(), apiUser(r), repo)
302 if !s.apiOK(w, r, err) {
303 return
304 }
305 writeJSON(w, http.StatusOK, map[string]any{"starred": on})
306}
307
308func (s *Server) apiMembers(w http.ResponseWriter, r *http.Request, kind string, id int64) {
309 if r.Method == http.MethodGet {
310 list, err := s.Store.ListMembers(r.Context(), kind, id)
311 if !s.apiOK(w, r, err) {
312 return
313 }
314 page, per := pageQuery(r)
315 rows := make([]map[string]any, 0, len(list))
316 for _, m := range list {
317 rows = append(rows, memberJSON(m))
318 }
319 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
320 return
321 }
322 var body struct {
323 Username string `json:"username"`
324 Role string `json:"role"`
325 }
326 if err := readJSON(r, &body); err != nil {
327 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
328 return
329 }
330 if !s.apiOK(w, r, s.opSetMember(r.Context(), apiUser(r), kind, id, body.Username, body.Role)) {
331 return
332 }
333 writeJSON(w, http.StatusOK, map[string]any{"username": body.Username, "role": body.Role})
334}
335
336func (s *Server) apiGroupMembers(w http.ResponseWriter, r *http.Request) {
337 min := access.RoleReadonly
338 if r.Method != http.MethodGet {
339 min = access.RoleMaintainer
340 }
341 g, ok := s.apiGroup(w, r, min)
342 if !ok {
343 return
344 }
345 s.apiMembers(w, r, "group", g.ID)
346}
347
348func (s *Server) apiProjectMembers(w http.ResponseWriter, r *http.Request) {
349 min := access.RoleReadonly
350 if r.Method != http.MethodGet {
351 min = access.RoleMaintainer
352 }
353 repo, _, ok := s.apiRepo(w, r, min)
354 if !ok {
355 return
356 }
357 s.apiMembers(w, r, "repo", repo.ID)
358}
359
360func (s *Server) apiDeleteMember(w http.ResponseWriter, r *http.Request, kind string, id int64) {
361 if !s.apiOK(w, r, s.opRemoveMember(r.Context(), apiUser(r), kind, id, r.PathValue("username"))) {
362 return
363 }
364 writeJSON(w, http.StatusNoContent, nil)
365}
366
367func (s *Server) apiDeleteGroupMember(w http.ResponseWriter, r *http.Request) {
368 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
369 if !ok {
370 return
371 }
372 s.apiDeleteMember(w, r, "group", g.ID)
373}
374
375func (s *Server) apiDeleteProjectMember(w http.ResponseWriter, r *http.Request) {
376 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
377 if !ok {
378 return
379 }
380 s.apiDeleteMember(w, r, "repo", repo.ID)
381}
382
383func secretJSON(m store.SecretMeta) map[string]any {
384 return map[string]any{"name": m.Name, "protected": m.Protected, "expand": m.Expand, "created_at": rfcTime(m.CreatedAt)}
385}
386
387func (s *Server) apiSecrets(w http.ResponseWriter, r *http.Request, kind string, id int64) {
388 if r.Method == http.MethodGet {
389 list, err := s.Store.ListSecrets(r.Context(), kind, id)
390 if !s.apiOK(w, r, err) {
391 return
392 }
393 rows := make([]map[string]any, 0, len(list))
394 for _, m := range list {
395 rows = append(rows, secretJSON(m))
396 }
397 page, per := pageQuery(r)
398 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
399 return
400 }
401 var body struct {
402 Name string `json:"name"`
403 Value string `json:"value"`
404 Protected bool `json:"protected"`
405 Expand *bool `json:"expand"`
406 }
407 if err := readJSON(r, &body); err != nil {
408 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
409 return
410 }
411 if !s.apiOK(w, r, s.opPutSecret(r.Context(), apiUser(r), kind, id, body.Name, body.Value, body.Protected)) {
412 return
413 }
414 if body.Expand != nil {
415 _ = s.Store.SetSecretExpand(r.Context(), kind, id, body.Name, *body.Expand)
416 }
417 writeJSON(w, http.StatusOK, map[string]any{"name": body.Name})
418}
419
420func (s *Server) apiGroupSecrets(w http.ResponseWriter, r *http.Request) {
421 min := access.RoleReadonly
422 if r.Method != http.MethodGet {
423 min = access.RoleMaintainer
424 }
425 g, ok := s.apiGroup(w, r, min)
426 if !ok {
427 return
428 }
429 if r.Method == http.MethodGet {
430 if err := s.manageScope(r.Context(), apiUser(r), "group", g.ID); err != nil {
431 s.writeAPIErr(w, r, err)
432 return
433 }
434 }
435 s.apiSecrets(w, r, "group", g.ID)
436}
437
438func (s *Server) apiProjectSecrets(w http.ResponseWriter, r *http.Request) {
439 min := access.RoleReadonly
440 if r.Method != http.MethodGet {
441 min = access.RoleMaintainer
442 }
443 repo, _, ok := s.apiRepo(w, r, min)
444 if !ok {
445 return
446 }
447 if r.Method == http.MethodGet {
448 if err := s.manageScope(r.Context(), apiUser(r), "repo", repo.ID); err != nil {
449 s.writeAPIErr(w, r, err)
450 return
451 }
452 }
453 s.apiSecrets(w, r, "repo", repo.ID)
454}
455
456func (s *Server) apiDeleteSecret(w http.ResponseWriter, r *http.Request, kind string, id int64) {
457 if !s.apiOK(w, r, s.opDeleteSecret(r.Context(), apiUser(r), kind, id, r.PathValue("name"))) {
458 return
459 }
460 writeJSON(w, http.StatusNoContent, nil)
461}
462
463func (s *Server) apiDeleteGroupSecret(w http.ResponseWriter, r *http.Request) {
464 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
465 if !ok {
466 return
467 }
468 s.apiDeleteSecret(w, r, "group", g.ID)
469}
470
471func (s *Server) apiDeleteProjectSecret(w http.ResponseWriter, r *http.Request) {
472 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
473 if !ok {
474 return
475 }
476 s.apiDeleteSecret(w, r, "repo", repo.ID)
477}
478
479func hookJSON(h store.Webhook) map[string]any {
480 return map[string]any{
481 "id": h.ID, "name": h.Name, "preset": h.Preset, "url": h.URLTemplate,
482 "method": h.Method, "events": h.Events, "branch_filter": h.BranchFilter, "active": h.Active,
483 }
484}
485
486func (s *Server) apiHooks(w http.ResponseWriter, r *http.Request, kind string, id int64) {
487 if r.Method == http.MethodGet {
488 list, err := s.Store.ListWebhooks(r.Context(), kind, id)
489 if !s.apiOK(w, r, err) {
490 return
491 }
492 rows := make([]map[string]any, 0, len(list))
493 for _, h := range list {
494 rows = append(rows, hookJSON(h))
495 }
496 page, per := pageQuery(r)
497 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
498 return
499 }
500 var body struct {
501 Name string `json:"name"`
502 Preset string `json:"preset"`
503 BranchFilter string `json:"branch_filter"`
504 Events []string `json:"events"`
505 URL string `json:"url"`
506 Username string `json:"username"`
507 Avatar string `json:"avatar"`
508 Icon string `json:"icon"`
509 Alias string `json:"alias"`
510 Token string `json:"token"`
511 Chat string `json:"chat"`
512 Server string `json:"server"`
513 Topic string `json:"topic"`
514 Homeserver string `json:"homeserver"`
515 Room string `json:"room"`
516 Method string `json:"method"`
517 ContentType string `json:"content_type"`
518 Headers string `json:"headers"`
519 Body string `json:"body"`
520 }
521 if err := readJSON(r, &body); err != nil {
522 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
523 return
524 }
525 wid, err := s.opCreateWebhook(r.Context(), apiUser(r), kind, id, hookIn{
526 Name: body.Name, Preset: body.Preset, Filter: body.BranchFilter, Events: body.Events,
527 Fields: webhook.Input{
528 URL: body.URL, Username: body.Username, Avatar: body.Avatar, Icon: body.Icon, Alias: body.Alias,
529 Token: body.Token, Chat: body.Chat, Server: body.Server, Topic: body.Topic,
530 Homeserver: body.Homeserver, Room: body.Room, Method: body.Method, ContentType: body.ContentType,
531 Headers: body.Headers, Body: body.Body,
532 },
533 })
534 if !s.apiOK(w, r, err) {
535 return
536 }
537 hook, err := s.Store.Webhook(r.Context(), kind, id, wid)
538 if err != nil {
539 writeJSON(w, http.StatusCreated, map[string]any{"id": wid})
540 return
541 }
542 writeJSON(w, http.StatusCreated, hookJSON(hook))
543}
544
545func (s *Server) apiGroupHooks(w http.ResponseWriter, r *http.Request) {
546 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
547 if !ok {
548 return
549 }
550 s.apiHooks(w, r, "group", g.ID)
551}
552
553func (s *Server) apiProjectHooks(w http.ResponseWriter, r *http.Request) {
554 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
555 if !ok {
556 return
557 }
558 s.apiHooks(w, r, "repo", repo.ID)
559}
560
561func (s *Server) apiDeleteHook(w http.ResponseWriter, r *http.Request, kind string, id int64) {
562 hid, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
563 if err != nil {
564 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
565 return
566 }
567 if !s.apiOK(w, r, s.opDeleteWebhook(r.Context(), apiUser(r), kind, id, hid)) {
568 return
569 }
570 writeJSON(w, http.StatusNoContent, nil)
571}
572
573func (s *Server) apiDeleteGroupHook(w http.ResponseWriter, r *http.Request) {
574 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
575 if !ok {
576 return
577 }
578 s.apiDeleteHook(w, r, "group", g.ID)
579}
580
581func (s *Server) apiDeleteProjectHook(w http.ResponseWriter, r *http.Request) {
582 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
583 if !ok {
584 return
585 }
586 s.apiDeleteHook(w, r, "repo", repo.ID)
587}
588
589func approvalJSON(p store.ApprovalPolicy, locked string) map[string]any {
590 return map[string]any{
591 "configured": p.Configured, "maintainer_required": p.MaintainerRequired,
592 "required_count": p.RequiredCount, "developers_approve": p.DevelopersApprove,
593 "locked_by": locked,
594 }
595}
596
597func approvalBody(r *http.Request) (store.ApprovalPolicy, error) {
598 var body struct {
599 MaintainerRequired bool `json:"maintainer_required"`
600 RequiredCount int `json:"required_count"`
601 DevelopersApprove bool `json:"developers_approve"`
602 }
603 if err := readJSON(r, &body); err != nil {
604 return store.ApprovalPolicy{}, fail(400, i18n.E("err.json"))
605 }
606 return store.ApprovalPolicy{
607 MaintainerRequired: body.MaintainerRequired,
608 RequiredCount: body.RequiredCount,
609 DevelopersApprove: body.DevelopersApprove,
610 }, nil
611}
612
613func (s *Server) apiGroupApproval(w http.ResponseWriter, r *http.Request) {
614 min := access.RoleReadonly
615 if r.Method != http.MethodGet {
616 min = access.RoleMaintainer
617 }
618 g, ok := s.apiGroup(w, r, min)
619 if !ok {
620 return
621 }
622 if r.Method == http.MethodGet {
623 p, err := s.Store.GetApproval(r.Context(), "group", g.ID)
624 if !s.apiOK(w, r, err) {
625 return
626 }
627 writeJSON(w, http.StatusOK, approvalJSON(p, ""))
628 return
629 }
630 pol, err := approvalBody(r)
631 if !s.apiOK(w, r, err) {
632 return
633 }
634 if !s.apiOK(w, r, s.opSaveGroupApproval(r.Context(), apiUser(r), g, pol)) {
635 return
636 }
637 writeJSON(w, http.StatusOK, approvalJSON(pol, ""))
638}
639
640func (s *Server) apiProjectApproval(w http.ResponseWriter, r *http.Request) {
641 min := access.RoleReadonly
642 if r.Method != http.MethodGet {
643 min = access.RoleMaintainer
644 }
645 repo, _, ok := s.apiRepo(w, r, min)
646 if !ok {
647 return
648 }
649 pol, locked, err := s.Store.EffectiveApproval(r.Context(), repo)
650 if !s.apiOK(w, r, err) {
651 return
652 }
653 if r.Method == http.MethodGet {
654 writeJSON(w, http.StatusOK, approvalJSON(pol, locked))
655 return
656 }
657 next, err := approvalBody(r)
658 if !s.apiOK(w, r, err) {
659 return
660 }
661 if !s.apiOK(w, r, s.opSaveRepoApproval(r.Context(), apiUser(r), repo, next)) {
662 return
663 }
664 writeJSON(w, http.StatusOK, approvalJSON(next, ""))
665}
666
667func (s *Server) apiLogo(w http.ResponseWriter, r *http.Request, kind string, id int64, mime string) {
668 if r.Method == http.MethodGet {
669 s.serveLogo(w, r, kind, id, mime)
670 return
671 }
672 if err := r.ParseMultipartForm(2 << 20); err != nil {
673 s.writeAPIErr(w, r, fail(400, i18n.E("err.badForm")))
674 return
675 }
676 if err := s.storeLogo(r, kind, id); err != nil {
677 s.writeAPIErr(w, r, fail(400, err))
678 return
679 }
680 writeJSON(w, http.StatusOK, map[string]any{"logo": true})
681}
682
683func (s *Server) apiGroupLogo(w http.ResponseWriter, r *http.Request) {
684 min := access.RoleReadonly
685 if r.Method != http.MethodGet {
686 min = access.RoleMaintainer
687 }
688 g, ok := s.apiGroup(w, r, min)
689 if !ok {
690 return
691 }
692 s.apiLogo(w, r, "group", g.ID, g.LogoMIME)
693}
694
695func (s *Server) apiProjectLogo(w http.ResponseWriter, r *http.Request) {
696 min := access.RoleReadonly
697 if r.Method != http.MethodGet {
698 min = access.RoleMaintainer
699 }
700 repo, _, ok := s.apiRepo(w, r, min)
701 if !ok {
702 return
703 }
704 s.apiLogo(w, r, "repo", repo.ID, repo.LogoMIME)
705}
706
707func (s *Server) apiPatterns(w http.ResponseWriter, r *http.Request, tag bool) {
708 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
709 if !ok {
710 return
711 }
712 if r.Method == http.MethodGet {
713 var list []string
714 var err error
715 if tag {
716 list, err = s.Store.ProtectedTagPatterns(r.Context(), repo.ID)
717 } else {
718 list, err = s.Store.ProtectedPatterns(r.Context(), repo.ID)
719 }
720 if !s.apiOK(w, r, err) {
721 return
722 }
723 if list == nil {
724 list = []string{}
725 }
726 writeJSON(w, http.StatusOK, list)
727 return
728 }
729 if _, _, ok := s.apiRepo(w, r, access.RoleMaintainer); !ok {
730 return
731 }
732 var body struct {
733 Pattern string `json:"pattern"`
734 }
735 if err := readJSON(r, &body); err != nil {
736 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
737 return
738 }
739 if !s.apiOK(w, r, s.opProtect(r.Context(), apiUser(r), repo, body.Pattern, tag)) {
740 return
741 }
742 writeJSON(w, http.StatusCreated, map[string]any{"pattern": body.Pattern})
743}
744
745func (s *Server) apiProtectBranches(w http.ResponseWriter, r *http.Request) {
746 s.apiPatterns(w, r, false)
747}
748
749func (s *Server) apiProtectTags(w http.ResponseWriter, r *http.Request) {
750 s.apiPatterns(w, r, true)
751}
752
753func (s *Server) apiUnprotect(w http.ResponseWriter, r *http.Request, tag bool) {
754 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
755 if !ok {
756 return
757 }
758 name := "pattern"
759 if tag {
760 name = "pattern"
761 }
762 pattern := r.PathValue(name)
763 if !s.apiOK(w, r, s.opUnprotect(r.Context(), apiUser(r), repo, pattern, tag)) {
764 return
765 }
766 writeJSON(w, http.StatusNoContent, nil)
767}
768
769func (s *Server) apiUnprotectBranch(w http.ResponseWriter, r *http.Request) {
770 s.apiUnprotect(w, r, false)
771}
772
773func (s *Server) apiUnprotectTag(w http.ResponseWriter, r *http.Request) {
774 s.apiUnprotect(w, r, true)
775}