BlueGit

timfa bluegit

Files

bluegit / internal / server / runners_api.go

runners_api.go

runners_api.go 7.4 KiB Download Raw
1package server
2
3import (
4 "net/http"
5 "strconv"
6 "time"
7
8 "gitlab.com/Timfa/bluegit/internal/access"
9 "gitlab.com/Timfa/bluegit/internal/i18n"
10 "gitlab.com/Timfa/bluegit/internal/store"
11)
12
13// Runnerbeheer via de API: dezelfde regels als de UI. Account-runners
14// alleen voor de eigenaar zelf, groepsrunners alleen voor maintainers.
15// Scope runners (of api; read_api voor lezen). Job-tokens (bgj_) en
16// runner-tokens (bgr_) komen hier niet in: authUser accepteert alleen bg_.
17
18func runnerJSON(r store.Runner) map[string]any {
19 status := "offline"
20 if r.Paused {
21 status = "paused"
22 } else if r.Online(time.Now()) {
23 status = "online"
24 }
25 return map[string]any{
26 "id": r.ID, "name": r.Name, "owner_kind": r.OwnerKind, "owner_id": r.OwnerID,
27 "status": status, "online": r.Online(time.Now()), "paused": r.Paused,
28 "last_seen": rfcTime(r.LastSeen), "created_at": rfcTime(r.CreatedAt), "version": r.Version,
29 }
30}
31
32// runnerScope bepaalt de eigenaar uit het pad: /user/... of /groups/{path}/...
33func (s *Server) runnerScope(w http.ResponseWriter, r *http.Request) (string, int64, bool) {
34 if r.PathValue("path") != "" {
35 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
36 if !ok {
37 return "", 0, false
38 }
39 return "group", g.ID, true
40 }
41 return "user", apiUser(r).ID, true
42}
43
44func (s *Server) scopedRunner(w http.ResponseWriter, r *http.Request) (string, int64, store.Runner, bool) {
45 kind, owner, ok := s.runnerScope(w, r)
46 if !ok {
47 return "", 0, store.Runner{}, false
48 }
49 id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
50 rn, rerr := s.Store.RunnerByID(r.Context(), id)
51 if err != nil || rerr != nil || rn.Revoked || rn.OwnerKind != kind || rn.OwnerID != owner {
52 s.writeAPIErr(w, r, fail(http.StatusNotFound, i18n.E("err.notFound")))
53 return "", 0, store.Runner{}, false
54 }
55 return kind, owner, rn, true
56}
57
58func (s *Server) apiRunners(w http.ResponseWriter, r *http.Request) {
59 kind, owner, ok := s.runnerScope(w, r)
60 if !ok {
61 return
62 }
63 rs, err := s.Store.ListRunners(r.Context(), kind, owner)
64 if !s.apiOK(w, r, err) {
65 return
66 }
67 out := make([]map[string]any, 0, len(rs))
68 for _, rn := range rs {
69 out = append(out, runnerJSON(rn))
70 }
71 writeJSON(w, http.StatusOK, out)
72}
73
74func (s *Server) apiRunnerRegToken(w http.ResponseWriter, r *http.Request) {
75 kind, owner, ok := s.runnerScope(w, r)
76 if !ok {
77 return
78 }
79 plain, err := s.Store.CreateRunnerRegToken(r.Context(), kind, owner, apiUser(r).ID)
80 if !s.apiOK(w, r, err) {
81 return
82 }
83 writeJSON(w, http.StatusCreated, map[string]any{
84 "token": plain, "owner_kind": kind, "owner_id": owner,
85 "expires_at": rfcTime(time.Now().Add(24 * time.Hour)),
86 "register": "bluegit-runner register --url " + s.Cfg.BaseURL + " --token <token>",
87 })
88}
89
90func (s *Server) apiRunnerGet(w http.ResponseWriter, r *http.Request) {
91 if _, _, rn, ok := s.scopedRunner(w, r); ok {
92 writeJSON(w, http.StatusOK, runnerJSON(rn))
93 }
94}
95
96func (s *Server) apiRunnerPut(w http.ResponseWriter, r *http.Request) {
97 kind, owner, rn, ok := s.scopedRunner(w, r)
98 if !ok {
99 return
100 }
101 var body struct {
102 Name *string `json:"name"`
103 Paused *bool `json:"paused"`
104 }
105 if err := readJSON(r, &body); err != nil {
106 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
107 return
108 }
109 upd, err := s.Store.UpdateRunner(r.Context(), kind, owner, rn.ID, body.Name, body.Paused)
110 if err != nil {
111 s.writeAPIErr(w, r, fail(400, err))
112 return
113 }
114 writeJSON(w, http.StatusOK, runnerJSON(upd))
115}
116
117func (s *Server) setPaused(w http.ResponseWriter, r *http.Request, paused bool) {
118 kind, owner, rn, ok := s.scopedRunner(w, r)
119 if !ok {
120 return
121 }
122 upd, err := s.Store.UpdateRunner(r.Context(), kind, owner, rn.ID, nil, &paused)
123 if !s.apiOK(w, r, err) {
124 return
125 }
126 writeJSON(w, http.StatusOK, runnerJSON(upd))
127}
128
129func (s *Server) apiRunnerPause(w http.ResponseWriter, r *http.Request) { s.setPaused(w, r, true) }
130func (s *Server) apiRunnerResume(w http.ResponseWriter, r *http.Request) { s.setPaused(w, r, false) }
131
132func (s *Server) apiRunnerDelete(w http.ResponseWriter, r *http.Request) {
133 kind, owner, rn, ok := s.scopedRunner(w, r)
134 if !ok {
135 return
136 }
137 if !s.apiOK(w, r, s.Store.RevokeRunner(r.Context(), kind, owner, rn.ID)) {
138 return
139 }
140 w.WriteHeader(http.StatusNoContent)
141}
142
143func (s *Server) apiRunnerJobs(w http.ResponseWriter, r *http.Request) {
144 _, _, rn, ok := s.scopedRunner(w, r)
145 if !ok {
146 return
147 }
148 limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
149 jobs, err := s.Store.RunnerJobs(r.Context(), rn.ID, limit)
150 if !s.apiOK(w, r, err) {
151 return
152 }
153 uid := apiUser(r).ID
154 out := []map[string]any{}
155 for _, j := range jobs {
156 row := map[string]any{"id": j.JobID, "pipeline_id": j.PipelineID, "name": j.Name, "status": j.Status,
157 "started_at": rfcTime(j.StartedAt), "finished_at": rfcTime(j.FinishedAt), "project": nil}
158 // Alleen projectnamen die de vrager zelf mag zien.
159 if repo, err := s.Store.RepoByID(r.Context(), j.RepoID); err == nil {
160 if role, err := s.Store.EffectiveRole(r.Context(), uid, repo.ID); err == nil && (role.CanRead() || repo.Visibility == "public") {
161 row["project"] = repo.FullPath()
162 row["web_url"] = s.Cfg.BaseURL + "/" + repo.FullPath() + "/-/pipelines/" + strconv.FormatInt(j.PipelineID, 10) + "/jobs/" + strconv.FormatInt(j.JobID, 10)
163 }
164 }
165 out = append(out, row)
166 }
167 writeJSON(w, http.StatusOK, out)
168}
169
170func (s *Server) apiUserRunnerSettings(w http.ResponseWriter, r *http.Request) {
171 u := apiUser(r)
172 if r.Method == http.MethodPut {
173 var body struct {
174 Preference string `json:"preference"`
175 }
176 if err := readJSON(r, &body); err != nil || (body.Preference != store.RunnerPrefOwnFirst && body.Preference != store.RunnerPrefBuiltinFirst) {
177 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
178 return
179 }
180 if !s.apiOK(w, r, s.Store.SetUserRunnerPref(r.Context(), u.ID, body.Preference)) {
181 return
182 }
183 }
184 writeJSON(w, http.StatusOK, map[string]any{"preference": s.Store.UserRunnerPref(r.Context(), u.ID)})
185}
186
187func (s *Server) apiGroupRunnerSettings(w http.ResponseWriter, r *http.Request) {
188 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
189 if !ok {
190 return
191 }
192 if r.Method == http.MethodPut {
193 var body struct {
194 BuiltinFirst *bool `json:"builtin_first"`
195 }
196 if err := readJSON(r, &body); err != nil || body.BuiltinFirst == nil {
197 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
198 return
199 }
200 if !s.apiOK(w, r, s.Store.SetGroupBuiltinFirst(r.Context(), g.ID, *body.BuiltinFirst)) {
201 return
202 }
203 }
204 ids, _ := s.Store.RunnerOptIns(r.Context(), g.ID)
205 names := []string{}
206 for _, id := range ids {
207 if u, err := s.Store.UserByID(r.Context(), id); err == nil {
208 names = append(names, u.Username)
209 }
210 }
211 writeJSON(w, http.StatusOK, map[string]any{"builtin_first": s.Store.GroupBuiltinFirst(r.Context(), g.ID), "opted_in": names})
212}
213
214// apiGroupRunnerOptIn: alleen je eigen toestemming, en alleen als maintainer.
215func (s *Server) apiGroupRunnerOptIn(w http.ResponseWriter, r *http.Request) {
216 g, ok := s.apiGroup(w, r, access.RoleMaintainer)
217 if !ok {
218 return
219 }
220 u := apiUser(r)
221 if r.Method == http.MethodPut {
222 var body struct {
223 Enabled *bool `json:"enabled"`
224 }
225 if err := readJSON(r, &body); err != nil || body.Enabled == nil {
226 s.writeAPIErr(w, r, fail(400, i18n.E("err.json")))
227 return
228 }
229 if !s.apiOK(w, r, s.Store.SetRunnerOptIn(r.Context(), g.ID, u.ID, *body.Enabled)) {
230 return
231 }
232 }
233 ids, _ := s.Store.RunnerOptIns(r.Context(), g.ID)
234 on := false
235 for _, id := range ids {
236 on = on || id == u.ID
237 }
238 writeJSON(w, http.StatusOK, map[string]any{"enabled": on, "username": u.Username})
239}