This repository has no description
1package xrpc
2
3import (
4 "fmt"
5 "net/http"
6 "strconv"
7
8 "tangled.org/core/api/tangled"
9 xrpcerr "tangled.org/core/xrpc/errors"
10)
11
12func (x *Xrpc) HandleCiQueryPipelines(w http.ResponseWriter, r *http.Request) {
13 l := x.Logger
14 fail := func(e xrpcerr.XrpcError, status int) {
15 l.Error("failed", "kind", e.Tag, "error", e.Message)
16 writeError(w, e, status)
17 }
18
19 repo := r.URL.Query().Get("repo")
20 if repo == "" {
21 fail(xrpcerr.GenericError(fmt.Errorf("missing repo parameter")), http.StatusBadRequest)
22 return
23 }
24
25 commits := r.URL.Query()["commits"]
26 cursor := r.URL.Query().Get("cursor")
27 kinds := r.URL.Query()["kinds"]
28 limitStr := r.URL.Query().Get("limit")
29
30 limit := 30
31 if limitStr != "" {
32 if val, err := strconv.Atoi(limitStr); err == nil && val > 0 {
33 limit = val
34 }
35 }
36
37 pipelines, nextCursor, total, err := x.Db.QueryPipelines(r.Context(), repo, commits, cursor, kinds, limit)
38 if err != nil {
39 fail(xrpcerr.GenericError(err), http.StatusInternalServerError)
40 return
41 }
42
43 output := tangled.CiQueryPipelines_Output{
44 Pipelines: pipelines,
45 Total: total,
46 }
47 if nextCursor != "" {
48 output.Cursor = &nextCursor
49 }
50
51 if err := writeJson(w, http.StatusOK, output); err != nil {
52 fail(xrpcerr.GenericError(err), http.StatusInternalServerError)
53 }
54}
55
56func (x *Xrpc) HandleCiGetPipeline(w http.ResponseWriter, r *http.Request) {
57 l := x.Logger
58 fail := func(e xrpcerr.XrpcError, status int) {
59 l.Error("failed", "kind", e.Tag, "error", e.Message)
60 writeError(w, e, status)
61 }
62
63 pipeline := r.URL.Query().Get("pipeline")
64 if pipeline == "" {
65 fail(xrpcerr.GenericError(fmt.Errorf("missing pipeline parameter")), http.StatusBadRequest)
66 return
67 }
68
69 p, err := x.Db.GetPipeline(r.Context(), pipeline)
70 if err != nil {
71 fail(xrpcerr.GenericError(err), http.StatusInternalServerError)
72 return
73 }
74
75 if err := writeJson(w, http.StatusOK, p); err != nil {
76 fail(xrpcerr.GenericError(err), http.StatusInternalServerError)
77 }
78}