BlueGit

timfa bluegit

Files

bluegit / internal / server / ops_ci.go

ops_ci.go

ops_ci.go 11.4 KiB Download Raw
1package server
2
3import (
4 "context"
5 "net/http"
6 "os"
7 "path/filepath"
8 "strconv"
9 "strings"
10
11 "gitlab.com/Timfa/bluegit/internal/access"
12 "gitlab.com/Timfa/bluegit/internal/gitrepo"
13 "gitlab.com/Timfa/bluegit/internal/i18n"
14 "gitlab.com/Timfa/bluegit/internal/names"
15 "gitlab.com/Timfa/bluegit/internal/store"
16)
17
18func (s *Server) opRunPipeline(ctx context.Context, user store.User, repo store.Repo, ref, source string) (int64, error) {
19 role, err := s.repoRole(ctx, user, repo)
20 if err != nil {
21 return 0, err
22 }
23 if !role.CanWriteCode() {
24 return 0, fail(403, i18n.E("err.forbidden"))
25 }
26 if repo.CIPaused {
27 return 0, fail(409, i18n.E("err.ciPaused"))
28 }
29 ref = safeRev(ref, repo.DefaultBranch)
30 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
31 sha := gitOne(ctx, s.Git, dir, "rev-parse", "--verify", ref+"^{commit}")
32 if sha == "" {
33 return 0, fail(400, i18n.E("err.branchMissing"))
34 }
35 if source == "" {
36 source = "api"
37 }
38 id := s.startPipeline(ctx, repo, sha, "refs/heads/"+ref, user.Username, source, "", nil)
39 return id, nil
40}
41
42func (s *Server) opJobAction(ctx context.Context, user store.User, repo store.Repo, pipeID, jobID int64, action string) (store.Job, error) {
43 return s.opJobActionForce(ctx, user, repo, pipeID, jobID, action, false)
44}
45
46// opJobActionForce: force is nodig om een lopende doNotInterrupt-job te annuleren.
47func (s *Server) opJobActionForce(ctx context.Context, user store.User, repo store.Repo, pipeID, jobID int64, action string, force bool) (store.Job, error) {
48 role, err := s.repoRole(ctx, user, repo)
49 if err != nil {
50 return store.Job{}, err
51 }
52 if !role.CanWriteCode() {
53 return store.Job{}, fail(403, i18n.E("err.forbidden"))
54 }
55 p, err := s.Store.PipelineByID(ctx, repo.ID, pipeID)
56 if err != nil {
57 return store.Job{}, notFound(err)
58 }
59 jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
60 var job store.Job
61 found := false
62 for _, j := range jobs {
63 if j.ID == jobID {
64 job = j
65 found = true
66 break
67 }
68 }
69 if !found {
70 return store.Job{}, fail(404, i18n.E("err.notFound"))
71 }
72 if (action == "play" || action == "retry") && expiredManualJob(job) {
73 return store.Job{}, fail(409, i18n.E("err.jobExpired", job.Name))
74 }
75 switch action {
76 case "play":
77 ok, err := s.Store.QueueJob(ctx, job.ID)
78 if err != nil {
79 return store.Job{}, fail(400, err)
80 }
81 if !ok {
82 return store.Job{}, fail(409, i18n.E("err.jobState"))
83 }
84 case "retry":
85 ok, err := s.Store.RetryJob(ctx, job.ID)
86 if err != nil {
87 return store.Job{}, fail(400, err)
88 }
89 if !ok {
90 return store.Job{}, fail(409, i18n.E("err.jobState"))
91 }
92 case "cancel":
93 if job.Status == "running" && job.DoNotInterrupt && !force {
94 return store.Job{}, fail(409, i18n.E("err.doNotInterrupt", job.Name))
95 }
96 ok, err := s.Store.CancelJob(ctx, job.ID)
97 if err != nil {
98 return store.Job{}, fail(400, err)
99 }
100 if !ok {
101 return store.Job{}, fail(409, i18n.E("err.jobState"))
102 }
103 if s.Runner != nil {
104 s.Runner.Interrupt([]int64{job.ID})
105 }
106 _, _ = s.Store.RefreshPipelineStatus(ctx, p.ID)
107 default:
108 return store.Job{}, fail(400, i18n.E("err.jobState"))
109 }
110 jobs, _ = s.Store.JobsByPipeline(ctx, p.ID)
111 for _, j := range jobs {
112 if j.ID == jobID {
113 return j, nil
114 }
115 }
116 return job, nil
117}
118
119func (s *Server) opRetryPipeline(ctx context.Context, user store.User, repo store.Repo, pipeID int64, mode string) (int64, error) {
120 role, err := s.repoRole(ctx, user, repo)
121 if err != nil {
122 return 0, err
123 }
124 if !role.CanWriteCode() {
125 return 0, fail(403, i18n.E("err.forbidden"))
126 }
127 p, err := s.Store.PipelineByID(ctx, repo.ID, pipeID)
128 if err != nil {
129 return 0, notFound(err)
130 }
131 if mode == "all" || mode == "failed" {
132 // Opnieuw starten kan alleen als er minstens één job gefaald is
133 // (ook runner_system_failure); zelfde regel als de knop in de UI.
134 jobs, err := s.Store.JobsByPipeline(ctx, p.ID)
135 if err != nil {
136 return 0, err
137 }
138 if !jobsFailed(jobs) {
139 return 0, fail(409, i18n.E("err.pipeNoFailed"))
140 }
141 }
142 switch mode {
143 case "all":
144 id, err := s.Store.ClonePipeline(ctx, repo.ID, p.ID)
145 if err != nil {
146 if store.IsNotFound(err) {
147 return 0, notFound(err)
148 }
149 return 0, fail(400, err)
150 }
151 return id, nil
152 case "failed":
153 ok, err := s.Store.RetryFailedJobs(ctx, p.ID)
154 if err != nil {
155 return 0, fail(400, err)
156 }
157 if !ok {
158 return 0, fail(409, i18n.E("err.pipeNoFailed"))
159 }
160 return p.ID, nil
161 default:
162 return 0, fail(400, i18n.E("err.pipeRetryMode"))
163 }
164}
165
166func (s *Server) opCancelPipeline(ctx context.Context, user store.User, repo store.Repo, pipeID int64, force bool) error {
167 role, err := s.repoRole(ctx, user, repo)
168 if err != nil {
169 return err
170 }
171 if !role.CanWriteCode() {
172 return fail(403, i18n.E("err.forbidden"))
173 }
174 p, err := s.Store.PipelineByID(ctx, repo.ID, pipeID)
175 if err != nil {
176 return notFound(err)
177 }
178 jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
179 if prot := store.ProtectedRunning(jobs); len(prot) > 0 && !force {
180 return fail(409, i18n.E("err.doNotInterrupt", jobNames(prot)))
181 }
182 var running []int64
183 for _, j := range jobs {
184 if j.Status == "queued" || j.Status == "running" || j.Status == "manual" || j.Status == "pending" || j.Status == "created" {
185 ok, _ := s.Store.CancelJob(ctx, j.ID)
186 if ok && (j.Status == "running" || j.Status == "queued") {
187 running = append(running, j.ID)
188 }
189 }
190 }
191 if s.Runner != nil {
192 s.Runner.Interrupt(running)
193 }
194 _, _ = s.Store.RefreshPipelineStatus(ctx, p.ID)
195 return nil
196}
197
198func (s *Server) opLintCI(r *http.Request, repo store.Repo, body, ref string) (map[string]any, error) {
199 if len(body) > ciMaxBytes {
200 return nil, fail(400, i18n.E("edit.fail"))
201 }
202 ref = safeRev(ref, repo.DefaultBranch)
203 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
204 model := s.ciEditorModel(r, body, s.includeFrom(r, dir, ref))
205 lines := make([]string, len(model.Rows))
206 var bad []int
207 for i, row := range model.Rows {
208 lines[i] = string(row.HTML)
209 if row.Bad {
210 bad = append(bad, row.N)
211 }
212 }
213 return map[string]any{
214 "status": model.Status, "statusClass": model.Class, "errors": model.Errors,
215 "warnings": model.Warnings, "issues": model.Issues, "lines": lines, "bad": bad,
216 }, nil
217}
218
219func (s *Server) opSaveCI(ctx context.Context, user store.User, repo store.Repo, base, branch, message, body string, openMR bool) (string, int64, error) {
220 role, err := s.repoRole(ctx, user, repo)
221 if err != nil {
222 return "", 0, err
223 }
224 base = safeRev(base, repo.DefaultBranch)
225 if !validBranchName(branch) {
226 return "", 0, fail(400, i18n.E("edit.badBranch"))
227 }
228 if len(body) > ciMaxBytes {
229 return "", 0, fail(400, i18n.E("edit.fail"))
230 }
231 patterns, _ := s.Store.ProtectedPatterns(ctx, repo.ID)
232 if openMR && (branch == base || access.BranchProtected(patterns, branch)) {
233 return "", 0, fail(400, i18n.E("edit.badBranch"))
234 }
235 sha, err := s.opWriteFiles(ctx, user, repo, role, branch, base, message, "", map[string]string{ciFile: body}, nil)
236 if err != nil {
237 return "", 0, err
238 }
239 if !openMR {
240 return sha, 0, nil
241 }
242 existing, err := s.Store.OpenMergeRequestsBySource(ctx, repo.ID, branch)
243 if err != nil {
244 return sha, 0, fail(400, err)
245 }
246 if len(existing) > 0 {
247 return sha, existing[0].Number, nil
248 }
249 mr, err := s.opCreateMR(ctx, user, repo, message, "", branch, base, false)
250 if err != nil {
251 return sha, 0, err
252 }
253 return sha, mr.Number, nil
254}
255
256func (s *Server) opPublishRelease(ctx context.Context, repo store.Repo, author store.User, job store.Job, req releaseRequest) (int64, error) {
257 if s.StoragePath() == "" {
258 return 0, fail(503, i18n.E("err.noStorage"))
259 }
260 if err := names.ValidTag(req.Tag); err != nil {
261 return 0, fail(400, err)
262 }
263 if !safeSHA(req.Commit) {
264 return 0, fail(400, i18n.E("err.commitAbsent"))
265 }
266 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
267 if !gitrepo.HasCommit(ctx, s.Git, dir, req.Commit) {
268 return 0, fail(400, i18n.E("err.commitMissing"))
269 }
270 var replace *store.Release
271 if gitrepo.HasTag(ctx, s.Git, dir, req.Tag) {
272 if !req.Replace {
273 return 0, fail(409, i18n.E("err.tagExists", req.Tag))
274 }
275 rel, err := s.checkReplace(ctx, repo, job, req.Tag)
276 if err != nil {
277 return 0, err
278 }
279 replace = &rel
280 }
281 if req.Name == "" {
282 req.Name = req.Tag
283 }
284 if job.ID == 0 && len(req.Assets) > 0 {
285 return 0, fail(400, i18n.E("err.assetsJob"))
286 }
287 type srcAsset struct {
288 name, src string
289 size int64
290 }
291 var srcs []srcAsset
292 for _, a := range req.Assets {
293 if a.Name == "" {
294 a.Name = filepath.Base(a.Path)
295 }
296 if strings.Contains(a.Name, "/") || strings.Contains(a.Name, "..") || a.Path == "" {
297 return 0, fail(400, i18n.E("err.assetName"))
298 }
299 src, err := s.Store.ArtifactPath(ctx, job.PipelineID, a.Job, a.Path)
300 if err != nil {
301 return 0, fail(400, i18n.E("err.artifactMissing", a.Job, a.Path))
302 }
303 info, err := os.Stat(src)
304 if err != nil {
305 return 0, fail(400, i18n.E("err.assetDisk"))
306 }
307 srcs = append(srcs, srcAsset{a.Name, src, info.Size()})
308 }
309 // Pas na alle controles: oude release, assets en tag weg.
310 if replace != nil {
311 if err := s.doReplace(ctx, repo, job, *replace, req.Commit); err != nil {
312 return 0, err
313 }
314 }
315 var assets []store.ReleaseAsset
316 for _, sa := range srcs {
317 a := struct{ Name string }{sa.name}
318 src := sa.src
319 dst := filepath.Join(s.StoragePath(), "releases", repo.OwnerPath, repo.Name, req.Tag, a.Name)
320 if err := copyRelease(src, dst); err != nil {
321 return 0, fail(500, i18n.E("err.assetCopy"))
322 }
323 assets = append(assets, store.ReleaseAsset{Name: a.Name, StoragePath: dst, Size: sa.size})
324 }
325 if err := s.postReceive(ctx, repo, author, dir, func() error {
326 name, email := gitrepo.FallbackName, gitrepo.FallbackEmail
327 if author.Username != "" {
328 name, email = author.Username, userEmail(author.Username)
329 } else if job.ID != 0 {
330 if p, err := s.Store.PipelineByID(ctx, repo.ID, job.PipelineID); err == nil && p.Actor != "" {
331 name, email = p.Actor, userEmail(p.Actor)
332 }
333 }
334 return gitrepo.Tag(ctx, s.Git, dir, req.Tag, req.Commit, req.Name, name, email)
335 }); err != nil {
336 return 0, fail(400, i18n.E("err.tagFail", err.Error()))
337 }
338 authorID := author.ID
339 id, err := s.Store.CreateRelease(ctx, repo.ID, req.Tag, req.Commit, req.Name, req.Body, authorID, job.ID, assets)
340 if err != nil {
341 return 0, fail(400, err)
342 }
343 s.Dispatch(repo.ID, s.releaseEvent(repo, req.Tag, req.Name, req.Commit, assets))
344 return id, nil
345}
346
347func (s *Server) opCIFile(ctx context.Context, repo store.Repo, ref string) (string, error) {
348 ref = safeRev(ref, repo.DefaultBranch)
349 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
350 body := gitOne(ctx, s.Git, dir, "show", ref+":"+ciFile)
351 return body, nil
352}
353
354// jobNames noemt jobs als "a", "b" voor meldingen.
355func jobNames(jobs []store.Job) string {
356 names := make([]string, 0, len(jobs))
357 for _, j := range jobs {
358 names = append(names, strconv.Quote(j.Name))
359 }
360 return strings.Join(names, ", ")
361}
362
363// forceParam leest force=true|1 uit query of formulier.
364func forceParam(r *http.Request) bool {
365 v := r.URL.Query().Get("force")
366 if v == "" {
367 v = r.FormValue("force")
368 }
369 return v == "true" || v == "1"
370}
371
372// revokeRunner trekt een aparte runner in, behalve als hij een lopende
373// doNotInterrupt-job heeft en force uit staat.
374func (s *Server) revokeRunner(ctx context.Context, kind string, owner, id int64, force bool) error {
375 if prot, _ := s.Store.RunningProtectedJobs(ctx, 0, id); len(prot) > 0 && !force {
376 return fail(409, i18n.E("err.dniRunnerRevoke", jobNames(prot)))
377 }
378 return s.Store.RevokeRunner(ctx, kind, owner, id)
379}
380
381func (s *Server) revokeErr(r *http.Request, err error) string {
382 if statusOf(err) == http.StatusConflict {
383 return i18n.Text(s.langOf(r), err)
384 }
385 return s.tr(r, "runner.missing")
386}