BlueGit

timfa bluegit

Files

bluegit / internal / server / api_collab.go

api_collab.go

feat: bied dezelfde acties als de UI aan via /api/v1
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
d89195e9 History
api_collab.go 20.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 }
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 writeJSON(w, http.StatusCreated, map[string]any{"id": wid})
494}
495
496func (s *Server) apiGroupHooks(w http.ResponseWriter, r *http.Request) {
497 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
498 if !ok {
499 return
500 }
501 s.apiHooks(w, r, "group", g.ID)
502}
503
504func (s *Server) apiProjectHooks(w http.ResponseWriter, r *http.Request) {
505 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
506 if !ok {
507 return
508 }
509 s.apiHooks(w, r, "repo", repo.ID)
510}
511
512func (s *Server) apiDeleteHook(w http.ResponseWriter, r *http.Request, kind string, id int64) {
513 hid, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
514 if err != nil {
515 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
516 return
517 }
518 if !s.apiOK(w, r, s.opDeleteWebhook(r.Context(), apiUser(r), kind, id, hid)) {
519 return
520 }
521 writeJSON(w, http.StatusNoContent, nil)
522}
523
524func (s *Server) apiDeleteGroupHook(w http.ResponseWriter, r *http.Request) {
525 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
526 if !ok {
527 return
528 }
529 s.apiDeleteHook(w, r, "group", g.ID)
530}
531
532func (s *Server) apiDeleteProjectHook(w http.ResponseWriter, r *http.Request) {
533 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
534 if !ok {
535 return
536 }
537 s.apiDeleteHook(w, r, "repo", repo.ID)
538}
539
540func approvalJSON(p store.ApprovalPolicy, locked string) map[string]any {
541 return map[string]any{
542 "configured": p.Configured, "maintainer_required": p.MaintainerRequired,
543 "required_count": p.RequiredCount, "developers_approve": p.DevelopersApprove,
544 "locked_by": locked,
545 }
546}
547
548func approvalBody(r *http.Request) (store.ApprovalPolicy, error) {
549 var body struct {
550 MaintainerRequired bool `json:"maintainer_required"`
551 RequiredCount int `json:"required_count"`
552 DevelopersApprove bool `json:"developers_approve"`
553 }
554 if err := readJSON(r, &body); err != nil {
555 return store.ApprovalPolicy{}, fail(400, i18n.E("err.json"))
556 }
557 return store.ApprovalPolicy{
558 MaintainerRequired: body.MaintainerRequired,
559 RequiredCount: body.RequiredCount,
560 DevelopersApprove: body.DevelopersApprove,
561 }, nil
562}
563
564func (s *Server) apiGroupApproval(w http.ResponseWriter, r *http.Request) {
565 min := access.RoleReadonly
566 if r.Method != http.MethodGet {
567 min = access.RoleMaintainer
568 }
569 g, ok := s.apiGroup(w, r, min)
570 if !ok {
571 return
572 }
573 if r.Method == http.MethodGet {
574 p, err := s.Store.GetApproval(r.Context(), "group", g.ID)
575 if !s.apiOK(w, r, err) {
576 return
577 }
578 writeJSON(w, http.StatusOK, approvalJSON(p, ""))
579 return
580 }
581 pol, err := approvalBody(r)
582 if !s.apiOK(w, r, err) {
583 return
584 }
585 if !s.apiOK(w, r, s.opSaveGroupApproval(r.Context(), apiUser(r), g, pol)) {
586 return
587 }
588 writeJSON(w, http.StatusOK, approvalJSON(pol, ""))
589}
590
591func (s *Server) apiProjectApproval(w http.ResponseWriter, r *http.Request) {
592 min := access.RoleReadonly
593 if r.Method != http.MethodGet {
594 min = access.RoleMaintainer
595 }
596 repo, _, ok := s.apiRepo(w, r, min)
597 if !ok {
598 return
599 }
600 pol, locked, err := s.Store.EffectiveApproval(r.Context(), repo)
601 if !s.apiOK(w, r, err) {
602 return
603 }
604 if r.Method == http.MethodGet {
605 writeJSON(w, http.StatusOK, approvalJSON(pol, locked))
606 return
607 }
608 next, err := approvalBody(r)
609 if !s.apiOK(w, r, err) {
610 return
611 }
612 if !s.apiOK(w, r, s.opSaveRepoApproval(r.Context(), apiUser(r), repo, next)) {
613 return
614 }
615 writeJSON(w, http.StatusOK, approvalJSON(next, ""))
616}
617
618func (s *Server) apiLogo(w http.ResponseWriter, r *http.Request, kind string, id int64, mime string) {
619 if r.Method == http.MethodGet {
620 s.serveLogo(w, r, kind, id, mime)
621 return
622 }
623 if err := r.ParseMultipartForm(2 << 20); err != nil {
624 s.writeAPIErr(w, r, fail(400, i18n.E("err.badForm")))
625 return
626 }
627 if err := s.storeLogo(r, kind, id); err != nil {
628 s.writeAPIErr(w, r, fail(400, err))
629 return
630 }
631 writeJSON(w, http.StatusOK, map[string]any{"logo": true})
632}
633
634func (s *Server) apiGroupLogo(w http.ResponseWriter, r *http.Request) {
635 min := access.RoleReadonly
636 if r.Method != http.MethodGet {
637 min = access.RoleMaintainer
638 }
639 g, ok := s.apiGroup(w, r, min)
640 if !ok {
641 return
642 }
643 s.apiLogo(w, r, "group", g.ID, g.LogoMIME)
644}
645
646func (s *Server) apiProjectLogo(w http.ResponseWriter, r *http.Request) {
647 min := access.RoleReadonly
648 if r.Method != http.MethodGet {
649 min = access.RoleMaintainer
650 }
651 repo, _, ok := s.apiRepo(w, r, min)
652 if !ok {
653 return
654 }
655 s.apiLogo(w, r, "repo", repo.ID, repo.LogoMIME)
656}
657
658func (s *Server) apiPatterns(w http.ResponseWriter, r *http.Request, tag bool) {
659 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
660 if !ok {
661 return
662 }
663 if r.Method == http.MethodGet {
664 var list []string
665 var err error
666 if tag {
667 list, err = s.Store.ProtectedTagPatterns(r.Context(), repo.ID)
668 } else {
669 list, err = s.Store.ProtectedPatterns(r.Context(), repo.ID)
670 }
671 if !s.apiOK(w, r, err) {
672 return
673 }
674 if list == nil {
675 list = []string{}
676 }
677 writeJSON(w, http.StatusOK, list)
678 return
679 }
680 if _, _, ok := s.apiRepo(w, r, access.RoleMaintainer); !ok {
681 return
682 }
683 var body struct {
684 Pattern string `json:"pattern"`
685 }
686 if err := readJSON(r, &body); err != nil {
687 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
688 return
689 }
690 if !s.apiOK(w, r, s.opProtect(r.Context(), apiUser(r), repo, body.Pattern, tag)) {
691 return
692 }
693 writeJSON(w, http.StatusCreated, map[string]any{"pattern": body.Pattern})
694}
695
696func (s *Server) apiProtectBranches(w http.ResponseWriter, r *http.Request) {
697 s.apiPatterns(w, r, false)
698}
699
700func (s *Server) apiProtectTags(w http.ResponseWriter, r *http.Request) {
701 s.apiPatterns(w, r, true)
702}
703
704func (s *Server) apiUnprotect(w http.ResponseWriter, r *http.Request, tag bool) {
705 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
706 if !ok {
707 return
708 }
709 name := "pattern"
710 if tag {
711 name = "pattern"
712 }
713 pattern := r.PathValue(name)
714 if !s.apiOK(w, r, s.opUnprotect(r.Context(), apiUser(r), repo, pattern, tag)) {
715 return
716 }
717 writeJSON(w, http.StatusNoContent, nil)
718}
719
720func (s *Server) apiUnprotectBranch(w http.ResponseWriter, r *http.Request) {
721 s.apiUnprotect(w, r, false)
722}
723
724func (s *Server) apiUnprotectTag(w http.ResponseWriter, r *http.Request) {
725 s.apiUnprotect(w, r, true)
726}