BlueGit

timfa bluegit

Files

bluegit / internal / server / api_collab.go

api_collab.go

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