BlueGit

timfa bluegit

Files

bluegit / internal / server / api_collab.go

api_collab.go

api_collab.go 23.0 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, "ci_paused": repo.CIPaused,
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 DefaultBranch *string `json:"default_branch"`
259 CIPaused *bool `json:"ci_paused"`
260 }
261 if err := readJSON(r, &body); err != nil {
262 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
263 return
264 }
265 // Velden die ontbreken blijven zoals ze zijn.
266 desc, vis := repo.Description, repo.Visibility
267 if body.Description != nil {
268 desc = *body.Description
269 }
270 if body.Visibility != nil {
271 vis = *body.Visibility
272 }
273 if body.ReleaseVisibility != nil && !store.ValidReleaseVisibility(*body.ReleaseVisibility) {
274 s.writeAPIErr(w, r, fail(400, i18n.E("err.releaseVisibility")))
275 return
276 }
277 if body.StaleManualDays != nil && (*body.StaleManualDays < -1 || *body.StaleManualDays > 3650) {
278 s.writeAPIErr(w, r, fail(400, i18n.E("stale.err.days")))
279 return
280 }
281 if !s.apiOK(w, r, s.opUpdateRepo(r.Context(), apiUser(r), repo, desc, vis)) {
282 return
283 }
284 if body.ReleaseVisibility != nil {
285 if !s.apiOK(w, r, s.opSetReleaseVisibility(r.Context(), apiUser(r), repo, *body.ReleaseVisibility)) {
286 return
287 }
288 }
289 if body.StaleManualDays != nil {
290 if !s.apiOK(w, r, s.Store.SetRepoStaleManualDays(r.Context(), repo.ID, *body.StaleManualDays)) {
291 return
292 }
293 }
294 if body.DefaultBranch != nil {
295 if !s.apiOK(w, r, s.opSetDefaultBranch(r.Context(), apiUser(r), repo, *body.DefaultBranch)) {
296 return
297 }
298 }
299 if body.CIPaused != nil {
300 if !s.apiOK(w, r, s.opSetRepoCIPaused(r.Context(), apiUser(r), repo, *body.CIPaused)) {
301 return
302 }
303 }
304 repo, _ = s.Store.RepoByPath(r.Context(), repo.OwnerPath, repo.Name)
305 writeJSON(w, http.StatusOK, s.projectJSONFull(r, repo))
306}
307
308func (s *Server) apiDeleteProject(w http.ResponseWriter, r *http.Request) {
309 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
310 if !ok {
311 return
312 }
313 var body struct {
314 Confirm string `json:"confirm"`
315 }
316 if err := readJSON(r, &body); err != nil {
317 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
318 return
319 }
320 if !s.apiOK(w, r, s.opDeleteRepo(r.Context(), apiUser(r), repo, body.Confirm, forceParam(r))) {
321 return
322 }
323 writeJSON(w, http.StatusNoContent, nil)
324}
325
326func (s *Server) apiStar(w http.ResponseWriter, r *http.Request) {
327 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
328 if !ok {
329 return
330 }
331 on, err := s.opToggleStar(r.Context(), apiUser(r), repo)
332 if !s.apiOK(w, r, err) {
333 return
334 }
335 writeJSON(w, http.StatusOK, map[string]any{"starred": on})
336}
337
338func (s *Server) apiMembers(w http.ResponseWriter, r *http.Request, kind string, id int64) {
339 if r.Method == http.MethodGet {
340 list, err := s.Store.ListMembers(r.Context(), kind, id)
341 if !s.apiOK(w, r, err) {
342 return
343 }
344 page, per := pageQuery(r)
345 rows := make([]map[string]any, 0, len(list))
346 for _, m := range list {
347 rows = append(rows, memberJSON(m))
348 }
349 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
350 return
351 }
352 var body struct {
353 Username string `json:"username"`
354 Role string `json:"role"`
355 }
356 if err := readJSON(r, &body); err != nil {
357 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
358 return
359 }
360 if !s.apiOK(w, r, s.opSetMember(r.Context(), apiUser(r), kind, id, body.Username, body.Role)) {
361 return
362 }
363 writeJSON(w, http.StatusOK, map[string]any{"username": body.Username, "role": body.Role})
364}
365
366func (s *Server) apiGroupMembers(w http.ResponseWriter, r *http.Request) {
367 min := access.RoleReadonly
368 if r.Method != http.MethodGet {
369 min = access.RoleMaintainer
370 }
371 g, ok := s.apiGroup(w, r, min)
372 if !ok {
373 return
374 }
375 s.apiMembers(w, r, "group", g.ID)
376}
377
378func (s *Server) apiProjectMembers(w http.ResponseWriter, r *http.Request) {
379 min := access.RoleReadonly
380 if r.Method != http.MethodGet {
381 min = access.RoleMaintainer
382 }
383 repo, _, ok := s.apiRepo(w, r, min)
384 if !ok {
385 return
386 }
387 s.apiMembers(w, r, "repo", repo.ID)
388}
389
390func (s *Server) apiDeleteMember(w http.ResponseWriter, r *http.Request, kind string, id int64) {
391 if !s.apiOK(w, r, s.opRemoveMember(r.Context(), apiUser(r), kind, id, r.PathValue("username"))) {
392 return
393 }
394 writeJSON(w, http.StatusNoContent, nil)
395}
396
397func (s *Server) apiDeleteGroupMember(w http.ResponseWriter, r *http.Request) {
398 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
399 if !ok {
400 return
401 }
402 s.apiDeleteMember(w, r, "group", g.ID)
403}
404
405func (s *Server) apiDeleteProjectMember(w http.ResponseWriter, r *http.Request) {
406 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
407 if !ok {
408 return
409 }
410 s.apiDeleteMember(w, r, "repo", repo.ID)
411}
412
413func secretJSON(m store.SecretMeta) map[string]any {
414 return map[string]any{"name": m.Name, "protected": m.Protected, "expand": m.Expand, "created_at": rfcTime(m.CreatedAt)}
415}
416
417func (s *Server) apiSecrets(w http.ResponseWriter, r *http.Request, kind string, id int64) {
418 if r.Method == http.MethodGet {
419 list, err := s.Store.ListSecrets(r.Context(), kind, id)
420 if !s.apiOK(w, r, err) {
421 return
422 }
423 rows := make([]map[string]any, 0, len(list))
424 for _, m := range list {
425 rows = append(rows, secretJSON(m))
426 }
427 page, per := pageQuery(r)
428 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
429 return
430 }
431 var body struct {
432 Name string `json:"name"`
433 Value string `json:"value"`
434 Protected bool `json:"protected"`
435 Expand *bool `json:"expand"`
436 }
437 if err := readJSON(r, &body); err != nil {
438 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
439 return
440 }
441 if !s.apiOK(w, r, s.opPutSecret(r.Context(), apiUser(r), kind, id, body.Name, body.Value, body.Protected)) {
442 return
443 }
444 if body.Expand != nil {
445 _ = s.Store.SetSecretExpand(r.Context(), kind, id, body.Name, *body.Expand)
446 }
447 writeJSON(w, http.StatusOK, map[string]any{"name": body.Name})
448}
449
450func (s *Server) apiGroupSecrets(w http.ResponseWriter, r *http.Request) {
451 min := access.RoleReadonly
452 if r.Method != http.MethodGet {
453 min = access.RoleMaintainer
454 }
455 g, ok := s.apiGroup(w, r, min)
456 if !ok {
457 return
458 }
459 if r.Method == http.MethodGet {
460 if err := s.manageScope(r.Context(), apiUser(r), "group", g.ID); err != nil {
461 s.writeAPIErr(w, r, err)
462 return
463 }
464 }
465 s.apiSecrets(w, r, "group", g.ID)
466}
467
468func (s *Server) apiProjectSecrets(w http.ResponseWriter, r *http.Request) {
469 min := access.RoleReadonly
470 if r.Method != http.MethodGet {
471 min = access.RoleMaintainer
472 }
473 repo, _, ok := s.apiRepo(w, r, min)
474 if !ok {
475 return
476 }
477 if r.Method == http.MethodGet {
478 if err := s.manageScope(r.Context(), apiUser(r), "repo", repo.ID); err != nil {
479 s.writeAPIErr(w, r, err)
480 return
481 }
482 }
483 s.apiSecrets(w, r, "repo", repo.ID)
484}
485
486func (s *Server) apiDeleteSecret(w http.ResponseWriter, r *http.Request, kind string, id int64) {
487 if !s.apiOK(w, r, s.opDeleteSecret(r.Context(), apiUser(r), kind, id, r.PathValue("name"))) {
488 return
489 }
490 writeJSON(w, http.StatusNoContent, nil)
491}
492
493func (s *Server) apiDeleteGroupSecret(w http.ResponseWriter, r *http.Request) {
494 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
495 if !ok {
496 return
497 }
498 s.apiDeleteSecret(w, r, "group", g.ID)
499}
500
501func (s *Server) apiDeleteProjectSecret(w http.ResponseWriter, r *http.Request) {
502 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
503 if !ok {
504 return
505 }
506 s.apiDeleteSecret(w, r, "repo", repo.ID)
507}
508
509func hookJSON(h store.Webhook) map[string]any {
510 return map[string]any{
511 "id": h.ID, "name": h.Name, "preset": h.Preset, "url": h.URLTemplate,
512 "method": h.Method, "events": h.Events, "branch_filter": h.BranchFilter, "active": h.Active,
513 }
514}
515
516func (s *Server) apiHooks(w http.ResponseWriter, r *http.Request, kind string, id int64) {
517 if r.Method == http.MethodGet {
518 list, err := s.Store.ListWebhooks(r.Context(), kind, id)
519 if !s.apiOK(w, r, err) {
520 return
521 }
522 rows := make([]map[string]any, 0, len(list))
523 for _, h := range list {
524 rows = append(rows, hookJSON(h))
525 }
526 page, per := pageQuery(r)
527 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
528 return
529 }
530 var body struct {
531 Name string `json:"name"`
532 Preset string `json:"preset"`
533 BranchFilter string `json:"branch_filter"`
534 Events []string `json:"events"`
535 URL string `json:"url"`
536 Username string `json:"username"`
537 Avatar string `json:"avatar"`
538 Icon string `json:"icon"`
539 Alias string `json:"alias"`
540 Token string `json:"token"`
541 Chat string `json:"chat"`
542 Server string `json:"server"`
543 Topic string `json:"topic"`
544 Homeserver string `json:"homeserver"`
545 Room string `json:"room"`
546 Method string `json:"method"`
547 ContentType string `json:"content_type"`
548 Headers string `json:"headers"`
549 Body string `json:"body"`
550 }
551 if err := readJSON(r, &body); err != nil {
552 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
553 return
554 }
555 wid, err := s.opCreateWebhook(r.Context(), apiUser(r), kind, id, hookIn{
556 Name: body.Name, Preset: body.Preset, Filter: body.BranchFilter, Events: body.Events,
557 Fields: webhook.Input{
558 URL: body.URL, Username: body.Username, Avatar: body.Avatar, Icon: body.Icon, Alias: body.Alias,
559 Token: body.Token, Chat: body.Chat, Server: body.Server, Topic: body.Topic,
560 Homeserver: body.Homeserver, Room: body.Room, Method: body.Method, ContentType: body.ContentType,
561 Headers: body.Headers, Body: body.Body,
562 },
563 })
564 if !s.apiOK(w, r, err) {
565 return
566 }
567 hook, err := s.Store.Webhook(r.Context(), kind, id, wid)
568 if err != nil {
569 writeJSON(w, http.StatusCreated, map[string]any{"id": wid})
570 return
571 }
572 writeJSON(w, http.StatusCreated, hookJSON(hook))
573}
574
575func (s *Server) apiGroupHooks(w http.ResponseWriter, r *http.Request) {
576 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
577 if !ok {
578 return
579 }
580 s.apiHooks(w, r, "group", g.ID)
581}
582
583func (s *Server) apiProjectHooks(w http.ResponseWriter, r *http.Request) {
584 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
585 if !ok {
586 return
587 }
588 s.apiHooks(w, r, "repo", repo.ID)
589}
590
591func (s *Server) apiDeleteHook(w http.ResponseWriter, r *http.Request, kind string, id int64) {
592 hid, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
593 if err != nil {
594 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
595 return
596 }
597 if !s.apiOK(w, r, s.opDeleteWebhook(r.Context(), apiUser(r), kind, id, hid)) {
598 return
599 }
600 writeJSON(w, http.StatusNoContent, nil)
601}
602
603func (s *Server) apiDeleteGroupHook(w http.ResponseWriter, r *http.Request) {
604 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
605 if !ok {
606 return
607 }
608 s.apiDeleteHook(w, r, "group", g.ID)
609}
610
611func (s *Server) apiDeleteProjectHook(w http.ResponseWriter, r *http.Request) {
612 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
613 if !ok {
614 return
615 }
616 s.apiDeleteHook(w, r, "repo", repo.ID)
617}
618
619func approvalJSON(p store.ApprovalPolicy, locked string) map[string]any {
620 return map[string]any{
621 "configured": p.Configured, "maintainer_required": p.MaintainerRequired,
622 "required_count": p.RequiredCount, "developers_approve": p.DevelopersApprove,
623 "locked_by": locked,
624 }
625}
626
627func approvalBody(r *http.Request) (store.ApprovalPolicy, error) {
628 var body struct {
629 MaintainerRequired bool `json:"maintainer_required"`
630 RequiredCount int `json:"required_count"`
631 DevelopersApprove bool `json:"developers_approve"`
632 }
633 if err := readJSON(r, &body); err != nil {
634 return store.ApprovalPolicy{}, fail(400, i18n.E("err.json"))
635 }
636 return store.ApprovalPolicy{
637 MaintainerRequired: body.MaintainerRequired,
638 RequiredCount: body.RequiredCount,
639 DevelopersApprove: body.DevelopersApprove,
640 }, nil
641}
642
643func (s *Server) apiGroupApproval(w http.ResponseWriter, r *http.Request) {
644 min := access.RoleReadonly
645 if r.Method != http.MethodGet {
646 min = access.RoleMaintainer
647 }
648 g, ok := s.apiGroup(w, r, min)
649 if !ok {
650 return
651 }
652 if r.Method == http.MethodGet {
653 p, err := s.Store.GetApproval(r.Context(), "group", g.ID)
654 if !s.apiOK(w, r, err) {
655 return
656 }
657 writeJSON(w, http.StatusOK, approvalJSON(p, ""))
658 return
659 }
660 pol, err := approvalBody(r)
661 if !s.apiOK(w, r, err) {
662 return
663 }
664 if !s.apiOK(w, r, s.opSaveGroupApproval(r.Context(), apiUser(r), g, pol)) {
665 return
666 }
667 writeJSON(w, http.StatusOK, approvalJSON(pol, ""))
668}
669
670func (s *Server) apiProjectApproval(w http.ResponseWriter, r *http.Request) {
671 min := access.RoleReadonly
672 if r.Method != http.MethodGet {
673 min = access.RoleMaintainer
674 }
675 repo, _, ok := s.apiRepo(w, r, min)
676 if !ok {
677 return
678 }
679 pol, locked, err := s.Store.EffectiveApproval(r.Context(), repo)
680 if !s.apiOK(w, r, err) {
681 return
682 }
683 if r.Method == http.MethodGet {
684 writeJSON(w, http.StatusOK, approvalJSON(pol, locked))
685 return
686 }
687 next, err := approvalBody(r)
688 if !s.apiOK(w, r, err) {
689 return
690 }
691 if !s.apiOK(w, r, s.opSaveRepoApproval(r.Context(), apiUser(r), repo, next)) {
692 return
693 }
694 writeJSON(w, http.StatusOK, approvalJSON(next, ""))
695}
696
697func (s *Server) apiLogo(w http.ResponseWriter, r *http.Request, kind string, id int64, mime string) {
698 if r.Method == http.MethodGet {
699 s.serveLogo(w, r, kind, id, mime)
700 return
701 }
702 if err := r.ParseMultipartForm(2 << 20); err != nil {
703 s.writeAPIErr(w, r, fail(400, i18n.E("err.badForm")))
704 return
705 }
706 if err := s.storeLogo(r, kind, id); err != nil {
707 s.writeAPIErr(w, r, fail(400, err))
708 return
709 }
710 writeJSON(w, http.StatusOK, map[string]any{"logo": true})
711}
712
713func (s *Server) apiGroupLogo(w http.ResponseWriter, r *http.Request) {
714 min := access.RoleReadonly
715 if r.Method != http.MethodGet {
716 min = access.RoleMaintainer
717 }
718 g, ok := s.apiGroup(w, r, min)
719 if !ok {
720 return
721 }
722 s.apiLogo(w, r, "group", g.ID, g.LogoMIME)
723}
724
725func (s *Server) apiProjectLogo(w http.ResponseWriter, r *http.Request) {
726 min := access.RoleReadonly
727 if r.Method != http.MethodGet {
728 min = access.RoleMaintainer
729 }
730 repo, _, ok := s.apiRepo(w, r, min)
731 if !ok {
732 return
733 }
734 s.apiLogo(w, r, "repo", repo.ID, repo.LogoMIME)
735}
736
737func (s *Server) apiPatterns(w http.ResponseWriter, r *http.Request, tag bool) {
738 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
739 if !ok {
740 return
741 }
742 if r.Method == http.MethodGet {
743 var list []string
744 var err error
745 if tag {
746 list, err = s.Store.ProtectedTagPatterns(r.Context(), repo.ID)
747 } else {
748 list, err = s.Store.ProtectedPatterns(r.Context(), repo.ID)
749 }
750 if !s.apiOK(w, r, err) {
751 return
752 }
753 if list == nil {
754 list = []string{}
755 }
756 writeJSON(w, http.StatusOK, list)
757 return
758 }
759 if _, _, ok := s.apiRepo(w, r, access.RoleMaintainer); !ok {
760 return
761 }
762 var body struct {
763 Pattern string `json:"pattern"`
764 }
765 if err := readJSON(r, &body); err != nil {
766 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
767 return
768 }
769 if !s.apiOK(w, r, s.opProtect(r.Context(), apiUser(r), repo, body.Pattern, tag)) {
770 return
771 }
772 writeJSON(w, http.StatusCreated, map[string]any{"pattern": body.Pattern})
773}
774
775func (s *Server) apiProtectBranches(w http.ResponseWriter, r *http.Request) {
776 s.apiPatterns(w, r, false)
777}
778
779func (s *Server) apiProtectTags(w http.ResponseWriter, r *http.Request) {
780 s.apiPatterns(w, r, true)
781}
782
783func (s *Server) apiUnprotect(w http.ResponseWriter, r *http.Request, tag bool) {
784 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
785 if !ok {
786 return
787 }
788 name := "pattern"
789 if tag {
790 name = "pattern"
791 }
792 pattern := r.PathValue(name)
793 if !s.apiOK(w, r, s.opUnprotect(r.Context(), apiUser(r), repo, pattern, tag)) {
794 return
795 }
796 writeJSON(w, http.StatusNoContent, nil)
797}
798
799func (s *Server) apiUnprotectBranch(w http.ResponseWriter, r *http.Request) {
800 s.apiUnprotect(w, r, false)
801}
802
803func (s *Server) apiUnprotectTag(w http.ResponseWriter, r *http.Request) {
804 s.apiUnprotect(w, r, true)
805}
806
807// projectJSONFull is projectJSON plus stale_manual_days (-1 = instance-instelling).
808func (s *Server) projectJSONFull(r *http.Request, repo store.Repo) map[string]any {
809 out := projectJSON(repo)
810 n, err := s.Store.RepoStaleManualDays(r.Context(), repo.ID)
811 if err == nil {
812 out["stale_manual_days"] = n
813 }
814 return out
815}