BlueGit

timfa bluegit

Files

bluegit / internal / server / api_collab.go

api_collab.go

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