BlueGit

timfa bluegit

Files

bluegit / internal / server / api_collab.go

api_collab.go

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