BlueGit

timfa bluegit

Files

bluegit / internal / server / api_flow.go

api_flow.go

api_flow.go 22.4 KiB Download Raw
1package server
2
3import (
4 "context"
5 "net/http"
6 "strconv"
7 "strings"
8
9 "gitlab.com/Timfa/bluegit/internal/access"
10 "gitlab.com/Timfa/bluegit/internal/i18n"
11 "gitlab.com/Timfa/bluegit/internal/images"
12 "gitlab.com/Timfa/bluegit/internal/store"
13)
14
15func (s *Server) pageURL(repo store.Repo, suffix string) string {
16 if !strings.HasPrefix(suffix, "/") {
17 suffix = "/" + suffix
18 }
19 return strings.TrimRight(s.Cfg.BaseURL, "/") + "/" + repo.FullPath() + suffix
20}
21
22func (s *Server) mrJSON(repo store.Repo, mr store.MergeRequest) map[string]any {
23 return map[string]any{
24 "id": mr.ID, "iid": mr.Number, "title": mr.Title, "description": mr.Description,
25 "source_branch": mr.SourceBranch, "target_branch": mr.TargetBranch,
26 "author": mr.Author, "state": mr.Status, "draft": mr.Draft, "labels": mr.Labels,
27 "remove_source_branch": mr.RemoveSourceBranch, "squash": mr.Squash,
28 "squash_commit_message": mr.SquashMessage,
29 "web_url": s.pageURL(repo, "/-/merge-requests/"+strconv.FormatInt(mr.Number, 10)),
30 "created_at": rfcTime(mr.CreatedAt), "updated_at": rfcTime(mr.UpdatedAt),
31 }
32}
33
34func (s *Server) apiMRs(w http.ResponseWriter, r *http.Request) {
35 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
36 if !ok {
37 return
38 }
39 if r.Method == http.MethodPost {
40 var body struct {
41 Title string `json:"title"`
42 Description string `json:"description"`
43 SourceBranch string `json:"source_branch"`
44 TargetBranch string `json:"target_branch"`
45 Draft bool `json:"draft"`
46 Remove *bool `json:"remove_source_branch"`
47 Squash *bool `json:"squash"`
48 Message *string `json:"squash_commit_message"`
49 }
50 if err := readJSON(r, &body); err != nil {
51 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
52 return
53 }
54 mr, err := s.opCreateMR(r.Context(), apiUser(r), repo, body.Title, body.Description, body.SourceBranch, body.TargetBranch, body.Draft)
55 if !s.apiOK(w, r, err) {
56 return
57 }
58 remove, squash, message := true, false, ""
59 if body.Remove != nil {
60 remove = *body.Remove
61 }
62 if body.Squash != nil {
63 squash = *body.Squash
64 }
65 if body.Message != nil {
66 message = *body.Message
67 }
68 if err := s.Store.SetMergeOptions(r.Context(), mr.ID, remove, squash, message); !s.apiOK(w, r, err) {
69 return
70 }
71 mr, err = s.Store.MergeRequestByNumber(r.Context(), repo.ID, mr.Number)
72 if !s.apiOK(w, r, err) {
73 return
74 }
75 writeJSON(w, http.StatusCreated, s.mrJSON(repo, mr))
76 return
77 }
78 list, err := s.Store.ListMergeRequests(r.Context(), repo.ID)
79 if !s.apiOK(w, r, err) {
80 return
81 }
82 // Optionele filters: state (open, merged, closed of all), source_branch, target_branch.
83 q := r.URL.Query()
84 wantState := strings.TrimSpace(q.Get("state"))
85 wantSource := strings.TrimSpace(q.Get("source_branch"))
86 wantTarget := strings.TrimSpace(q.Get("target_branch"))
87 page, per := pageQuery(r)
88 rows := make([]map[string]any, 0, len(list))
89 for _, mr := range list {
90 if wantState != "" && wantState != "all" && mr.Status != wantState {
91 continue
92 }
93 if wantSource != "" && mr.SourceBranch != wantSource {
94 continue
95 }
96 if wantTarget != "" && mr.TargetBranch != wantTarget {
97 continue
98 }
99 rows = append(rows, s.mrJSON(repo, mr))
100 }
101 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
102}
103
104func (s *Server) apiMR(w http.ResponseWriter, r *http.Request) {
105 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
106 if !ok {
107 return
108 }
109 n, err := strconv.ParseInt(r.PathValue("number"), 10, 64)
110 if err != nil {
111 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
112 return
113 }
114 if r.Method == http.MethodPut {
115 var body struct {
116 Title string `json:"title"`
117 Description string `json:"description"`
118 Labels string `json:"labels"`
119 Remove *bool `json:"remove_source_branch"`
120 Squash *bool `json:"squash"`
121 Message *string `json:"squash_commit_message"`
122 }
123 if err := readJSON(r, &body); err != nil {
124 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
125 return
126 }
127 mr, err := s.opUpdateMR(r.Context(), apiUser(r), repo, n, body.Title, body.Description, body.Labels)
128 if !s.apiOK(w, r, err) {
129 return
130 }
131 if body.Remove != nil || body.Squash != nil || body.Message != nil {
132 remove, squash, message := mr.RemoveSourceBranch, mr.Squash, mr.SquashMessage
133 if body.Remove != nil {
134 remove = *body.Remove
135 }
136 if body.Squash != nil {
137 squash = *body.Squash
138 }
139 if body.Message != nil {
140 message = *body.Message
141 }
142 if err := s.Store.SetMergeOptions(r.Context(), mr.ID, remove, squash, message); !s.apiOK(w, r, err) {
143 return
144 }
145 mr, err = s.Store.MergeRequestByNumber(r.Context(), repo.ID, mr.Number)
146 if !s.apiOK(w, r, err) {
147 return
148 }
149 }
150 writeJSON(w, http.StatusOK, s.mrJSON(repo, mr))
151 return
152 }
153 mr, err := s.Store.MergeRequestByNumber(r.Context(), repo.ID, n)
154 if err != nil {
155 s.writeAPIErr(w, r, notFound(err))
156 return
157 }
158 writeJSON(w, http.StatusOK, s.mrJSON(repo, mr))
159}
160
161func (s *Server) apiMRNotes(w http.ResponseWriter, r *http.Request) {
162 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
163 if !ok {
164 return
165 }
166 n, err := strconv.ParseInt(r.PathValue("number"), 10, 64)
167 if err != nil {
168 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
169 return
170 }
171 if r.Method == http.MethodGet {
172 mr, err := s.Store.MergeRequestByNumber(r.Context(), repo.ID, n)
173 if err != nil {
174 s.writeAPIErr(w, r, notFound(err))
175 return
176 }
177 notes, err := s.Store.ListNotes(r.Context(), mr.ID)
178 if !s.apiOK(w, r, err) {
179 return
180 }
181 rows := make([]map[string]any, 0, len(notes))
182 for _, note := range notes {
183 rows = append(rows, map[string]any{
184 "id": note.ID, "body": note.Body, "author": note.Author,
185 "file_path": note.FilePath, "line_start": note.LineStart, "line_end": note.LineEnd,
186 "suggestion": note.Suggestion, "parent_id": note.ParentID, "created_at": rfcTime(note.CreatedAt),
187 })
188 }
189 page, per := pageQuery(r)
190 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
191 return
192 }
193 var body struct {
194 Body string `json:"body"`
195 Path string `json:"path"`
196 LineStart int `json:"line_start"`
197 LineEnd int `json:"line_end"`
198 ParentID int64 `json:"parent_id"`
199 }
200 if err := readJSON(r, &body); err != nil {
201 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
202 return
203 }
204 note, err := s.opAddNote(r, apiUser(r), repo, n, body.Body, body.Path, body.LineStart, body.LineEnd, body.ParentID)
205 if !s.apiOK(w, r, err) {
206 return
207 }
208 writeJSON(w, http.StatusCreated, map[string]any{"id": note.ID})
209}
210
211func (s *Server) apiReview(w http.ResponseWriter, r *http.Request) {
212 repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)
213 if !ok {
214 return
215 }
216 n, err := strconv.ParseInt(r.PathValue("number"), 10, 64)
217 if err != nil {
218 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
219 return
220 }
221 var body struct {
222 Status string `json:"status"`
223 }
224 if err := readJSON(r, &body); err != nil {
225 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
226 return
227 }
228 if !s.apiOK(w, r, s.opReview(r.Context(), apiUser(r), repo, n, body.Status)) {
229 return
230 }
231 writeJSON(w, http.StatusOK, map[string]any{"status": body.Status})
232}
233
234func (s *Server) apiDismissReview(w http.ResponseWriter, r *http.Request) {
235 repo, _, ok := s.apiRepo(w, r, access.RoleMaintainer)
236 if !ok {
237 return
238 }
239 n, err := strconv.ParseInt(r.PathValue("number"), 10, 64)
240 if err != nil {
241 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
242 return
243 }
244 var body struct {
245 UserID int64 `json:"user_id"`
246 }
247 if err := readJSON(r, &body); err != nil {
248 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
249 return
250 }
251 if !s.apiOK(w, r, s.opDismissReview(r.Context(), apiUser(r), repo, n, body.UserID)) {
252 return
253 }
254 writeJSON(w, http.StatusOK, map[string]any{"status": "dismissed"})
255}
256
257func (s *Server) apiSuggestions(w http.ResponseWriter, r *http.Request) {
258 repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)
259 if !ok {
260 return
261 }
262 n, err := strconv.ParseInt(r.PathValue("number"), 10, 64)
263 if err != nil {
264 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
265 return
266 }
267 var body struct {
268 Title string `json:"title"`
269 Description string `json:"description"`
270 IDs []int64 `json:"ids"`
271 }
272 if err := readJSON(r, &body); err != nil {
273 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
274 return
275 }
276 if !s.apiOK(w, r, s.opApplySuggestions(r.Context(), apiUser(r), repo, n, body.Title, body.Description, body.IDs)) {
277 return
278 }
279 writeJSON(w, http.StatusOK, map[string]any{"applied": len(body.IDs)})
280}
281
282func (s *Server) apiMerge(w http.ResponseWriter, r *http.Request) {
283 repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)
284 if !ok {
285 return
286 }
287 n, err := strconv.ParseInt(r.PathValue("number"), 10, 64)
288 if err != nil {
289 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
290 return
291 }
292 var body struct {
293 Remove *bool `json:"remove_source_branch"`
294 Squash *bool `json:"squash"`
295 Message *string `json:"squash_commit_message"`
296 }
297 if err := readJSON(r, &body); err != nil {
298 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
299 return
300 }
301 out, err := s.opMergeMR(r, apiUser(r), repo, n, mergeChoice{Remove: body.Remove, Squash: body.Squash, Message: body.Message})
302 if !s.apiOK(w, r, err) {
303 return
304 }
305 writeJSON(w, http.StatusOK, map[string]any{
306 "state": "merged", "source_branch_removed": out.Removed, "source_branch_skip": out.Skip,
307 "web_url": s.pageURL(repo, "/-/merge-requests/"+strconv.FormatInt(n, 10)),
308 })
309}
310
311func (s *Server) apiCloseMR(w http.ResponseWriter, r *http.Request) {
312 s.apiSetMR(w, r, false)
313}
314
315func (s *Server) apiReopenMR(w http.ResponseWriter, r *http.Request) {
316 s.apiSetMR(w, r, true)
317}
318
319func (s *Server) apiSetMR(w http.ResponseWriter, r *http.Request, reopen bool) {
320 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
321 if !ok {
322 return
323 }
324 n, err := strconv.ParseInt(r.PathValue("number"), 10, 64)
325 if err != nil {
326 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
327 return
328 }
329 if !s.apiOK(w, r, s.opCloseMR(r.Context(), apiUser(r), repo, n, reopen)) {
330 return
331 }
332 state := "closed"
333 if reopen {
334 state = "open"
335 }
336 writeJSON(w, http.StatusOK, map[string]any{
337 "state": state, "web_url": s.pageURL(repo, "/-/merge-requests/"+strconv.FormatInt(n, 10)),
338 })
339}
340
341func (s *Server) pipeJSON(repo store.Repo, p store.Pipeline) map[string]any {
342 return map[string]any{
343 "id": p.ID, "sha": p.CommitSHA, "ref": p.Ref, "status": s.pipeStatusFor(p), "source": p.Source,
344 "actor": p.Actor, "created_at": rfcTime(p.CreatedAt), "started_at": rfcTime(p.StartedAt), "finished_at": rfcTime(p.FinishedAt),
345 "web_url": s.pageURL(repo, "/-/pipelines/"+strconv.FormatInt(p.ID, 10)),
346 }
347}
348
349func (s *Server) jobJSON(ctx context.Context, j store.Job) map[string]any {
350 out := map[string]any{
351 "id": j.ID, "name": j.Name, "stage": j.Stage, "status": j.Status, "when": j.When,
352 "allow_failure": j.AllowFailure, "started_at": rfcTime(j.StartedAt), "finished_at": rfcTime(j.FinishedAt),
353 "runner": nil,
354 }
355 if jr := s.Store.JobRun(ctx, j.ID); jr.RunnerName != "" {
356 kind := "builtin"
357 if jr.RunnerID > 0 {
358 kind = "remote"
359 }
360 out["runner"] = map[string]any{"id": jr.RunnerID, "name": jr.RunnerName, "kind": kind}
361 }
362 return out
363}
364
365func (s *Server) apiPipelines(w http.ResponseWriter, r *http.Request) {
366 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
367 if !ok {
368 return
369 }
370 if r.Method == http.MethodPost {
371 var body struct {
372 Ref string `json:"ref"`
373 }
374 if err := readJSON(r, &body); err != nil {
375 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
376 return
377 }
378 id, err := s.opRunPipeline(r.Context(), apiUser(r), repo, body.Ref, "api")
379 if !s.apiOK(w, r, err) {
380 return
381 }
382 if p, err := s.Store.PipelineByID(r.Context(), repo.ID, id); err == nil {
383 writeJSON(w, http.StatusCreated, s.pipeJSON(repo, p))
384 return
385 }
386 writeJSON(w, http.StatusCreated, map[string]any{
387 "id": id, "web_url": s.pageURL(repo, "/-/pipelines/"+strconv.FormatInt(id, 10)),
388 })
389 return
390 }
391 f, err := s.pipelineFilter(r, repo)
392 if !s.apiOK(w, r, err) {
393 return
394 }
395 list, err := s.Store.FindPipelines(r.Context(), repo.ID, f)
396 if !s.apiOK(w, r, err) {
397 return
398 }
399 page, per := pageQuery(r)
400 rows := make([]map[string]any, 0, len(list))
401 for _, p := range list {
402 rows = append(rows, s.pipeJSON(repo, p))
403 }
404 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
405}
406
407func (s *Server) apiPipeline(w http.ResponseWriter, r *http.Request) {
408 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
409 if !ok {
410 return
411 }
412 id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
413 if err != nil {
414 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
415 return
416 }
417 p, err := s.Store.PipelineByID(r.Context(), repo.ID, id)
418 if err != nil {
419 s.writeAPIErr(w, r, notFound(err))
420 return
421 }
422 writeJSON(w, http.StatusOK, s.pipeJSON(repo, p))
423}
424
425func (s *Server) apiRetryPipeline(w http.ResponseWriter, r *http.Request) {
426 repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)
427 if !ok {
428 return
429 }
430 pipeID, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
431 if err != nil {
432 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
433 return
434 }
435 var body struct {
436 Mode string `json:"mode"`
437 }
438 if err := readJSON(r, &body); err != nil {
439 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
440 return
441 }
442 id, err := s.opRetryPipeline(r.Context(), apiUser(r), repo, pipeID, body.Mode)
443 if !s.apiOK(w, r, err) {
444 return
445 }
446 p, err := s.Store.PipelineByID(r.Context(), repo.ID, id)
447 if err != nil {
448 s.writeAPIErr(w, r, notFound(err))
449 return
450 }
451 code := http.StatusOK
452 if body.Mode == "all" {
453 code = http.StatusCreated
454 }
455 writeJSON(w, code, s.pipeJSON(repo, p))
456}
457
458func (s *Server) apiCancelPipeline(w http.ResponseWriter, r *http.Request) {
459 repo, _, ok := s.apiRepo(w, r, access.RoleDeveloper)
460 if !ok {
461 return
462 }
463 id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
464 if err != nil {
465 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
466 return
467 }
468 if !s.apiOK(w, r, s.opCancelPipeline(r.Context(), apiUser(r), repo, id)) {
469 return
470 }
471 if p, err := s.Store.PipelineByID(r.Context(), repo.ID, id); err == nil {
472 writeJSON(w, http.StatusOK, s.pipeJSON(repo, p))
473 return
474 }
475 writeJSON(w, http.StatusOK, map[string]any{
476 "id": id, "status": "canceled", "web_url": s.pageURL(repo, "/-/pipelines/"+strconv.FormatInt(id, 10)),
477 })
478}
479
480func (s *Server) apiJobs(w http.ResponseWriter, r *http.Request) {
481 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
482 if !ok {
483 return
484 }
485 id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
486 if err != nil {
487 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
488 return
489 }
490 p, err := s.Store.PipelineByID(r.Context(), repo.ID, id)
491 if err != nil {
492 s.writeAPIErr(w, r, notFound(err))
493 return
494 }
495 jobs, err := s.Store.JobsByPipeline(r.Context(), p.ID)
496 if !s.apiOK(w, r, err) {
497 return
498 }
499 rows := make([]map[string]any, 0, len(jobs))
500 for _, j := range jobs {
501 rows = append(rows, s.jobJSON(r.Context(), j))
502 }
503 writeJSON(w, http.StatusOK, rows)
504}
505
506func (s *Server) apiJob(w http.ResponseWriter, r *http.Request) {
507 _, job, ok := s.apiFindJob(w, r)
508 if !ok {
509 return
510 }
511 writeJSON(w, http.StatusOK, s.jobJSON(r.Context(), job))
512}
513
514func (s *Server) apiJobLog(w http.ResponseWriter, r *http.Request) {
515 _, job, ok := s.apiFindJob(w, r)
516 if !ok {
517 return
518 }
519 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
520 w.WriteHeader(http.StatusOK)
521 _, _ = w.Write([]byte(job.Log))
522}
523
524func (s *Server) apiJobAction(w http.ResponseWriter, r *http.Request, action string) {
525 repo, p, job, ok := s.apiFindJobFull(w, r, access.RoleDeveloper)
526 if !ok {
527 return
528 }
529 updated, err := s.opJobAction(r.Context(), apiUser(r), repo, p.ID, job.ID, action)
530 if !s.apiOK(w, r, err) {
531 return
532 }
533 writeJSON(w, http.StatusOK, s.jobJSON(r.Context(), updated))
534}
535
536func (s *Server) apiPlayJob(w http.ResponseWriter, r *http.Request) { s.apiJobAction(w, r, "play") }
537func (s *Server) apiRetryJob(w http.ResponseWriter, r *http.Request) {
538 s.apiJobAction(w, r, "retry")
539}
540func (s *Server) apiCancelJob(w http.ResponseWriter, r *http.Request) {
541 s.apiJobAction(w, r, "cancel")
542}
543
544func (s *Server) apiFindJob(w http.ResponseWriter, r *http.Request) (store.Pipeline, store.Job, bool) {
545 _, p, job, ok := s.apiFindJobFull(w, r, access.RoleReadonly)
546 return p, job, ok
547}
548
549func (s *Server) apiFindJobFull(w http.ResponseWriter, r *http.Request, min access.Role) (store.Repo, store.Pipeline, store.Job, bool) {
550 repo, _, ok := s.apiRepo(w, r, min)
551 if !ok {
552 return store.Repo{}, store.Pipeline{}, store.Job{}, false
553 }
554 pid, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
555 if err != nil {
556 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
557 return store.Repo{}, store.Pipeline{}, store.Job{}, false
558 }
559 jid, err := strconv.ParseInt(r.PathValue("job"), 10, 64)
560 if err != nil {
561 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
562 return store.Repo{}, store.Pipeline{}, store.Job{}, false
563 }
564 p, err := s.Store.PipelineByID(r.Context(), repo.ID, pid)
565 if err != nil {
566 s.writeAPIErr(w, r, notFound(err))
567 return store.Repo{}, store.Pipeline{}, store.Job{}, false
568 }
569 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
570 for _, j := range jobs {
571 if j.ID == jid {
572 return repo, p, j, true
573 }
574 }
575 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
576 return store.Repo{}, store.Pipeline{}, store.Job{}, false
577}
578
579func (s *Server) apiArtifacts(w http.ResponseWriter, r *http.Request) {
580 _, job, ok := s.apiFindJob(w, r)
581 if !ok {
582 return
583 }
584 list, err := s.Store.ListArtifacts(r.Context(), job.ID)
585 if !s.apiOK(w, r, err) {
586 return
587 }
588 rows := make([]map[string]any, 0, len(list))
589 for _, a := range list {
590 rows = append(rows, map[string]any{"path": a.Path, "size": a.Size})
591 }
592 writeJSON(w, http.StatusOK, rows)
593}
594
595func (s *Server) apiArtifact(w http.ResponseWriter, r *http.Request) {
596 _, job, ok := s.apiFindJob(w, r)
597 if !ok {
598 return
599 }
600 want := r.PathValue("path")
601 if strings.Contains(want, "..") {
602 s.writeAPIErr(w, r, fail(400, i18n.E("err.sugFile")))
603 return
604 }
605 list, err := s.Store.ListArtifacts(r.Context(), job.ID)
606 if !s.apiOK(w, r, err) {
607 return
608 }
609 for _, a := range list {
610 if a.Path == want {
611 http.ServeFile(w, r, a.StoragePath)
612 return
613 }
614 }
615 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
616}
617
618func (s *Server) apiCIFile(w http.ResponseWriter, r *http.Request) {
619 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
620 if !ok {
621 return
622 }
623 if r.Method == http.MethodPut {
624 var body struct {
625 Branch string `json:"branch"`
626 Base string `json:"base"`
627 Message string `json:"message"`
628 Content string `json:"content"`
629 OpenMR bool `json:"open_mr"`
630 }
631 if err := readJSON(r, &body); err != nil {
632 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
633 return
634 }
635 sha, number, err := s.opSaveCI(r.Context(), apiUser(r), repo, body.Base, body.Branch, body.Message, body.Content, body.OpenMR)
636 if !s.apiOK(w, r, err) {
637 return
638 }
639 writeJSON(w, http.StatusOK, map[string]any{"id": sha, "merge_request": number})
640 return
641 }
642 body, err := s.opCIFile(r.Context(), repo, r.URL.Query().Get("ref"))
643 if !s.apiOK(w, r, err) {
644 return
645 }
646 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
647 w.WriteHeader(http.StatusOK)
648 _, _ = w.Write([]byte(body))
649}
650
651func (s *Server) apiLintCI(w http.ResponseWriter, r *http.Request) {
652 repo, _, ok := s.apiRepo(w, r, access.RoleReadonly)
653 if !ok {
654 return
655 }
656 var body struct {
657 Body string `json:"body"`
658 Ref string `json:"ref"`
659 }
660 if err := readJSON(r, &body); err != nil {
661 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
662 return
663 }
664 out, err := s.opLintCI(r, repo, body.Body, body.Ref)
665 if !s.apiOK(w, r, err) {
666 return
667 }
668 writeJSON(w, http.StatusOK, out)
669}
670
671func (s *Server) apiPresets(w http.ResponseWriter, r *http.Request) {
672 set, err := images.Effective(s.Cfg.CI.ImageOverrides())
673 if err != nil {
674 s.writeAPIErr(w, r, fail(500, err))
675 return
676 }
677 rows := make([]map[string]any, 0, len(set))
678 for _, p := range set {
679 rows = append(rows, map[string]any{"name": "bluegit/" + p.Name, "from": p.From, "runtime": p.Runtime, "tools": p.Tools, "direct": p.Direct})
680 }
681 writeJSON(w, http.StatusOK, rows)
682}
683
684func (s *Server) apiReleases(w http.ResponseWriter, r *http.Request) {
685 repo, role, ok := s.apiRepoReleases(w, r)
686 if !ok {
687 return
688 }
689 list, err := s.Store.ListReleases(r.Context(), repo.ID)
690 if !s.apiOK(w, r, err) {
691 return
692 }
693 page, per := pageQuery(r)
694 rows := make([]map[string]any, 0, len(list))
695 for _, rel := range list {
696 assets := make([]map[string]any, 0, len(rel.Assets))
697 for _, a := range rel.Assets {
698 assets = append(assets, map[string]any{"name": a.Name, "size": a.Size})
699 }
700 row := map[string]any{
701 "id": rel.ID, "tag": rel.Tag, "commit": rel.CommitSHA, "name": rel.Name, "body": rel.Body,
702 "created_at": rfcTime(rel.CreatedAt), "assets": assets,
703 }
704 if releaseOnly(repo, role) {
705 delete(row, "commit") // geen verwijzing naar broncode voor niet-leden
706 }
707 rows = append(rows, row)
708 }
709 writePage(w, r, slicePage(rows, page, per), len(rows), page, per)
710}
711
712func (s *Server) apiReleaseAsset(w http.ResponseWriter, r *http.Request) {
713 repo, _, ok := s.apiRepoReleases(w, r)
714 if !ok {
715 return
716 }
717 rel, err := s.Store.ReleaseByTag(r.Context(), repo.ID, r.PathValue("tag"))
718 if err != nil {
719 s.writeAPIErr(w, r, notFound(err))
720 return
721 }
722 name := r.PathValue("name")
723 for _, a := range rel.Assets {
724 if a.Name == name {
725 w.Header().Set("Content-Disposition", "attachment; filename=\""+strings.ReplaceAll(name, "\"", "")+"\"")
726 http.ServeFile(w, r, a.StoragePath)
727 return
728 }
729 }
730 s.writeAPIErr(w, r, fail(404, i18n.E("err.notFound")))
731}
732
733// pipelineFilter leest sha, ref en mr uit de query.
734// ref mag een branchnaam zijn (refs/heads/ en refs/tags/ worden geprobeerd)
735// of een volledige ref. mr is het MR-nummer: pipelines van die MR plus
736// pushes naar de bronbranch.
737func (s *Server) pipelineFilter(r *http.Request, repo store.Repo) (store.PipelineFilter, error) {
738 q := r.URL.Query()
739 var f store.PipelineFilter
740 if sha := strings.TrimSpace(q.Get("sha")); sha != "" {
741 if len(sha) < 7 || len(sha) > 40 || strings.Trim(strings.ToLower(sha), "0123456789abcdef") != "" {
742 return f, fail(400, i18n.E("err.badSHA"))
743 }
744 f.SHA = sha
745 }
746 if ref := strings.TrimSpace(q.Get("ref")); ref != "" {
747 if strings.HasPrefix(ref, "refs/") {
748 f.Refs = []string{ref}
749 } else {
750 f.Refs = []string{"refs/heads/" + ref, "refs/tags/" + ref}
751 }
752 }
753 if raw := strings.TrimSpace(q.Get("mr")); raw != "" {
754 n, err := strconv.ParseInt(raw, 10, 64)
755 if err != nil || n < 1 {
756 return f, fail(400, i18n.E("err.notFound"))
757 }
758 mr, err := s.Store.MergeRequestByNumber(r.Context(), repo.ID, n)
759 if err != nil {
760 return f, notFound(err)
761 }
762 if len(f.Refs) > 0 {
763 return f, fail(400, i18n.E("err.pipeFilter"))
764 }
765 f.MRID = mr.ID
766 f.Refs = []string{"refs/heads/" + mr.SourceBranch}
767 }
768 return f, nil
769}