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