BlueGit

timfa bluegit

Files

bluegit / internal / server / api_collab.go

api_collab.go

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