BlueGit

timfa bluegit

Files

bluegit / internal / server / api_collab.go

api_collab.go

api_collab.go 21.2 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, forceParam(r))) {
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, "expand": m.Expand, "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 Expand *bool `json:"expand"`
388 }
389 if err := readJSON(r, &body); err != nil {
390 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
391 return
392 }
393 if !s.apiOK(w, r, s.opPutSecret(r.Context(), apiUser(r), kind, id, body.Name, body.Value, body.Protected)) {
394 return
395 }
396 if body.Expand != nil {
397 _ = s.Store.SetSecretExpand(r.Context(), kind, id, body.Name, *body.Expand)
398 }
399 writeJSON(w, http.StatusOK, map[string]any{"name": body.Name})
400}
401
402func (s *Server) apiGroupSecrets(w http.ResponseWriter, r *http.Request) {
403 min := access.RoleReadonly
404 if r.Method != http.MethodGet {
405 min = access.RoleMaintainer
406 }
407 g, ok := s.apiGroup(w, r, min)
408 if !ok {
409 return
410 }
411 if r.Method == http.MethodGet {
412 if err := s.manageScope(r.Context(), apiUser(r), "group", g.ID); err != nil {
413 s.writeAPIErr(w, r, err)
414 return
415 }
416 }
417 s.apiSecrets(w, r, "group", g.ID)
418}
419
420func (s *Server) apiProjectSecrets(w http.ResponseWriter, r *http.Request) {
421 min := access.RoleReadonly
422 if r.Method != http.MethodGet {
423 min = access.RoleMaintainer
424 }
425 repo, _, ok := s.apiRepo(w, r, min)
426 if !ok {
427 return
428 }
429 if r.Method == http.MethodGet {
430 if err := s.manageScope(r.Context(), apiUser(r), "repo", repo.ID); err != nil {
431 s.writeAPIErr(w, r, err)
432 return
433 }
434 }
435 s.apiSecrets(w, r, "repo", repo.ID)
436}
437
438func (s *Server) apiDeleteSecret(w http.ResponseWriter, r *http.Request, kind string, id int64) {
439 if !s.apiOK(w, r, s.opDeleteSecret(r.Context(), apiUser(r), kind, id, r.PathValue("name"))) {
440 return
441 }
442 writeJSON(w, http.StatusNoContent, nil)
443}
444
445func (s *Server) apiDeleteGroupSecret(w http.ResponseWriter, r *http.Request) {
446 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
447 if !ok {
448 return
449 }
450 s.apiDeleteSecret(w, r, "group", g.ID)
451}
452
453func (s *Server) apiDeleteProjectSecret(w http.ResponseWriter, r *http.Request) {
454 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
455 if !ok {
456 return
457 }
458 s.apiDeleteSecret(w, r, "repo", repo.ID)
459}
460
461func hookJSON(h store.Webhook) map[string]any {
462 return map[string]any{
463 "id": h.ID, "name": h.Name, "preset": h.Preset, "url": h.URLTemplate,
464 "method": h.Method, "events": h.Events, "branch_filter": h.BranchFilter, "active": h.Active,
465 }
466}
467
468func (s *Server) apiHooks(w http.ResponseWriter, r *http.Request, kind string, id int64) {
469 if r.Method == http.MethodGet {
470 list, err := s.Store.ListWebhooks(r.Context(), kind, id)
471 if !s.apiOK(w, r, err) {
472 return
473 }
474 rows := make([]map[string]any, 0, len(list))
475 for _, h := range list {
476 rows = append(rows, hookJSON(h))
477 }
478 page, per := pageQuery(r)
479 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
480 return
481 }
482 var body struct {
483 Name string `json:"name"`
484 Preset string `json:"preset"`
485 BranchFilter string `json:"branch_filter"`
486 Events []string `json:"events"`
487 URL string `json:"url"`
488 Username string `json:"username"`
489 Avatar string `json:"avatar"`
490 Icon string `json:"icon"`
491 Alias string `json:"alias"`
492 Token string `json:"token"`
493 Chat string `json:"chat"`
494 Server string `json:"server"`
495 Topic string `json:"topic"`
496 Homeserver string `json:"homeserver"`
497 Room string `json:"room"`
498 Method string `json:"method"`
499 ContentType string `json:"content_type"`
500 Headers string `json:"headers"`
501 Body string `json:"body"`
502 }
503 if err := readJSON(r, &body); err != nil {
504 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
505 return
506 }
507 wid, err := s.opCreateWebhook(r.Context(), apiUser(r), kind, id, hookIn{
508 Name: body.Name, Preset: body.Preset, Filter: body.BranchFilter, Events: body.Events,
509 Fields: webhook.Input{
510 URL: body.URL, Username: body.Username, Avatar: body.Avatar, Icon: body.Icon, Alias: body.Alias,
511 Token: body.Token, Chat: body.Chat, Server: body.Server, Topic: body.Topic,
512 Homeserver: body.Homeserver, Room: body.Room, Method: body.Method, ContentType: body.ContentType,
513 Headers: body.Headers, Body: body.Body,
514 },
515 })
516 if !s.apiOK(w, r, err) {
517 return
518 }
519 hook, err := s.Store.Webhook(r.Context(), kind, id, wid)
520 if err != nil {
521 writeJSON(w, http.StatusCreated, map[string]any{"id": wid})
522 return
523 }
524 writeJSON(w, http.StatusCreated, hookJSON(hook))
525}
526
527func (s *Server) apiGroupHooks(w http.ResponseWriter, r *http.Request) {
528 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
529 if !ok {
530 return
531 }
532 s.apiHooks(w, r, "group", g.ID)
533}
534
535func (s *Server) apiProjectHooks(w http.ResponseWriter, r *http.Request) {
536 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
537 if !ok {
538 return
539 }
540 s.apiHooks(w, r, "repo", repo.ID)
541}
542
543func (s *Server) apiDeleteHook(w http.ResponseWriter, r *http.Request, kind string, id int64) {
544 hid, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
545 if err != nil {
546 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
547 return
548 }
549 if !s.apiOK(w, r, s.opDeleteWebhook(r.Context(), apiUser(r), kind, id, hid)) {
550 return
551 }
552 writeJSON(w, http.StatusNoContent, nil)
553}
554
555func (s *Server) apiDeleteGroupHook(w http.ResponseWriter, r *http.Request) {
556 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
557 if !ok {
558 return
559 }
560 s.apiDeleteHook(w, r, "group", g.ID)
561}
562
563func (s *Server) apiDeleteProjectHook(w http.ResponseWriter, r *http.Request) {
564 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
565 if !ok {
566 return
567 }
568 s.apiDeleteHook(w, r, "repo", repo.ID)
569}
570
571func approvalJSON(p store.ApprovalPolicy, locked string) map[string]any {
572 return map[string]any{
573 "configured": p.Configured, "maintainer_required": p.MaintainerRequired,
574 "required_count": p.RequiredCount, "developers_approve": p.DevelopersApprove,
575 "locked_by": locked,
576 }
577}
578
579func approvalBody(r *http.Request) (store.ApprovalPolicy, error) {
580 var body struct {
581 MaintainerRequired bool `json:"maintainer_required"`
582 RequiredCount int `json:"required_count"`
583 DevelopersApprove bool `json:"developers_approve"`
584 }
585 if err := readJSON(r, &body); err != nil {
586 return store.ApprovalPolicy{}, fail(400, i18n.E("err.json"))
587 }
588 return store.ApprovalPolicy{
589 MaintainerRequired: body.MaintainerRequired,
590 RequiredCount: body.RequiredCount,
591 DevelopersApprove: body.DevelopersApprove,
592 }, nil
593}
594
595func (s *Server) apiGroupApproval(w http.ResponseWriter, r *http.Request) {
596 min := access.RoleReadonly
597 if r.Method != http.MethodGet {
598 min = access.RoleMaintainer
599 }
600 g, ok := s.apiGroup(w, r, min)
601 if !ok {
602 return
603 }
604 if r.Method == http.MethodGet {
605 p, err := s.Store.GetApproval(r.Context(), "group", g.ID)
606 if !s.apiOK(w, r, err) {
607 return
608 }
609 writeJSON(w, http.StatusOK, approvalJSON(p, ""))
610 return
611 }
612 pol, err := approvalBody(r)
613 if !s.apiOK(w, r, err) {
614 return
615 }
616 if !s.apiOK(w, r, s.opSaveGroupApproval(r.Context(), apiUser(r), g, pol)) {
617 return
618 }
619 writeJSON(w, http.StatusOK, approvalJSON(pol, ""))
620}
621
622func (s *Server) apiProjectApproval(w http.ResponseWriter, r *http.Request) {
623 min := access.RoleReadonly
624 if r.Method != http.MethodGet {
625 min = access.RoleMaintainer
626 }
627 repo, _, ok := s.apiRepo(w, r, min)
628 if !ok {
629 return
630 }
631 pol, locked, err := s.Store.EffectiveApproval(r.Context(), repo)
632 if !s.apiOK(w, r, err) {
633 return
634 }
635 if r.Method == http.MethodGet {
636 writeJSON(w, http.StatusOK, approvalJSON(pol, locked))
637 return
638 }
639 next, err := approvalBody(r)
640 if !s.apiOK(w, r, err) {
641 return
642 }
643 if !s.apiOK(w, r, s.opSaveRepoApproval(r.Context(), apiUser(r), repo, next)) {
644 return
645 }
646 writeJSON(w, http.StatusOK, approvalJSON(next, ""))
647}
648
649func (s *Server) apiLogo(w http.ResponseWriter, r *http.Request, kind string, id int64, mime string) {
650 if r.Method == http.MethodGet {
651 s.serveLogo(w, r, kind, id, mime)
652 return
653 }
654 if err := r.ParseMultipartForm(2 << 20); err != nil {
655 s.writeAPIErr(w, r, fail(400, i18n.E("err.badForm")))
656 return
657 }
658 if err := s.storeLogo(r, kind, id); err != nil {
659 s.writeAPIErr(w, r, fail(400, err))
660 return
661 }
662 writeJSON(w, http.StatusOK, map[string]any{"logo": true})
663}
664
665func (s *Server) apiGroupLogo(w http.ResponseWriter, r *http.Request) {
666 min := access.RoleReadonly
667 if r.Method != http.MethodGet {
668 min = access.RoleMaintainer
669 }
670 g, ok := s.apiGroup(w, r, min)
671 if !ok {
672 return
673 }
674 s.apiLogo(w, r, "group", g.ID, g.LogoMIME)
675}
676
677func (s *Server) apiProjectLogo(w http.ResponseWriter, r *http.Request) {
678 min := access.RoleReadonly
679 if r.Method != http.MethodGet {
680 min = access.RoleMaintainer
681 }
682 repo, _, ok := s.apiRepo(w, r, min)
683 if !ok {
684 return
685 }
686 s.apiLogo(w, r, "repo", repo.ID, repo.LogoMIME)
687}
688
689func (s *Server) apiPatterns(w http.ResponseWriter, r *http.Request, tag bool) {
690 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
691 if !ok {
692 return
693 }
694 if r.Method == http.MethodGet {
695 var list []string
696 var err error
697 if tag {
698 list, err = s.Store.ProtectedTagPatterns(r.Context(), repo.ID)
699 } else {
700 list, err = s.Store.ProtectedPatterns(r.Context(), repo.ID)
701 }
702 if !s.apiOK(w, r, err) {
703 return
704 }
705 if list == nil {
706 list = []string{}
707 }
708 writeJSON(w, http.StatusOK, list)
709 return
710 }
711 if _, _, ok := s.apiRepo(w, r, access.RoleMaintainer); !ok {
712 return
713 }
714 var body struct {
715 Pattern string `json:"pattern"`
716 }
717 if err := readJSON(r, &body); err != nil {
718 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
719 return
720 }
721 if !s.apiOK(w, r, s.opProtect(r.Context(), apiUser(r), repo, body.Pattern, tag)) {
722 return
723 }
724 writeJSON(w, http.StatusCreated, map[string]any{"pattern": body.Pattern})
725}
726
727func (s *Server) apiProtectBranches(w http.ResponseWriter, r *http.Request) {
728 s.apiPatterns(w, r, false)
729}
730
731func (s *Server) apiProtectTags(w http.ResponseWriter, r *http.Request) {
732 s.apiPatterns(w, r, true)
733}
734
735func (s *Server) apiUnprotect(w http.ResponseWriter, r *http.Request, tag bool) {
736 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
737 if !ok {
738 return
739 }
740 name := "pattern"
741 if tag {
742 name = "pattern"
743 }
744 pattern := r.PathValue(name)
745 if !s.apiOK(w, r, s.opUnprotect(r.Context(), apiUser(r), repo, pattern, tag)) {
746 return
747 }
748 writeJSON(w, http.StatusNoContent, nil)
749}
750
751func (s *Server) apiUnprotectBranch(w http.ResponseWriter, r *http.Request) {
752 s.apiUnprotect(w, r, false)
753}
754
755func (s *Server) apiUnprotectTag(w http.ResponseWriter, r *http.Request) {
756 s.apiUnprotect(w, r, true)
757}