BlueGit

timfa bluegit

Files

bluegit / internal / server / ops_ci.go

ops_ci.go

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