BlueGit

timfa bluegit

Files

bluegit / internal / server / api_collab.go

api_collab.go

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