BlueGit

timfa bluegit

Files

bluegit / internal / server / ops_ci.go

ops_ci.go

ops_ci.go 9.0 KiB Download Raw
1package server
2
3import (
4 "context"
5 "net/http"
6 "os"
7 "path/filepath"
8 "strings"
9
10 "gitlab.com/Timfa/bluegit/internal/access"
11 "gitlab.com/Timfa/bluegit/internal/gitrepo"
12 "gitlab.com/Timfa/bluegit/internal/i18n"
13 "gitlab.com/Timfa/bluegit/internal/names"
14 "gitlab.com/Timfa/bluegit/internal/store"
15)
16
17func (s *Server) opRunPipeline(ctx context.Context, user store.User, repo store.Repo, ref, source string) (int64, error) {
18 role, err := s.repoRole(ctx, user, repo)
19 if err != nil {
20 return 0, err
21 }
22 if !role.CanWriteCode() {
23 return 0, fail(403, i18n.E("err.forbidden"))
24 }
25 ref = safeRev(ref, repo.DefaultBranch)
26 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
27 sha := gitOne(ctx, s.Git, dir, "rev-parse", "--verify", ref+"^{commit}")
28 if sha == "" {
29 return 0, fail(400, i18n.E("err.branchMissing"))
30 }
31 if source == "" {
32 source = "api"
33 }
34 id := s.startPipeline(ctx, repo, sha, "refs/heads/"+ref, user.Username, source, "", nil)
35 return id, nil
36}
37
38func (s *Server) opJobAction(ctx context.Context, user store.User, repo store.Repo, pipeID, jobID int64, action string) (store.Job, error) {
39 role, err := s.repoRole(ctx, user, repo)
40 if err != nil {
41 return store.Job{}, err
42 }
43 if !role.CanWriteCode() {
44 return store.Job{}, fail(403, i18n.E("err.forbidden"))
45 }
46 p, err := s.Store.PipelineByID(ctx, repo.ID, pipeID)
47 if err != nil {
48 return store.Job{}, notFound(err)
49 }
50 jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
51 var job store.Job
52 found := false
53 for _, j := range jobs {
54 if j.ID == jobID {
55 job = j
56 found = true
57 break
58 }
59 }
60 if !found {
61 return store.Job{}, fail(404, i18n.E("err.notFound"))
62 }
63 switch action {
64 case "play":
65 ok, err := s.Store.QueueJob(ctx, job.ID)
66 if err != nil {
67 return store.Job{}, fail(400, err)
68 }
69 if !ok {
70 return store.Job{}, fail(409, i18n.E("err.jobState"))
71 }
72 case "retry":
73 ok, err := s.Store.RetryJob(ctx, job.ID)
74 if err != nil {
75 return store.Job{}, fail(400, err)
76 }
77 if !ok {
78 return store.Job{}, fail(409, i18n.E("err.jobState"))
79 }
80 case "cancel":
81 ok, err := s.Store.CancelJob(ctx, job.ID)
82 if err != nil {
83 return store.Job{}, fail(400, err)
84 }
85 if !ok {
86 return store.Job{}, fail(409, i18n.E("err.jobState"))
87 }
88 if s.Runner != nil {
89 s.Runner.Interrupt([]int64{job.ID})
90 }
91 _, _ = s.Store.RefreshPipelineStatus(ctx, p.ID)
92 default:
93 return store.Job{}, fail(400, i18n.E("err.jobState"))
94 }
95 jobs, _ = s.Store.JobsByPipeline(ctx, p.ID)
96 for _, j := range jobs {
97 if j.ID == jobID {
98 return j, nil
99 }
100 }
101 return job, nil
102}
103
104func (s *Server) opRetryPipeline(ctx context.Context, user store.User, repo store.Repo, pipeID int64, mode string) (int64, error) {
105 role, err := s.repoRole(ctx, user, repo)
106 if err != nil {
107 return 0, err
108 }
109 if !role.CanWriteCode() {
110 return 0, fail(403, i18n.E("err.forbidden"))
111 }
112 p, err := s.Store.PipelineByID(ctx, repo.ID, pipeID)
113 if err != nil {
114 return 0, notFound(err)
115 }
116 if mode == "all" || mode == "failed" {
117 // Opnieuw starten kan alleen als er minstens één job gefaald is
118 // (ook runner_system_failure); zelfde regel als de knop in de UI.
119 jobs, err := s.Store.JobsByPipeline(ctx, p.ID)
120 if err != nil {
121 return 0, err
122 }
123 if !jobsFailed(jobs) {
124 return 0, fail(409, i18n.E("err.pipeNoFailed"))
125 }
126 }
127 switch mode {
128 case "all":
129 id, err := s.Store.ClonePipeline(ctx, repo.ID, p.ID)
130 if err != nil {
131 if store.IsNotFound(err) {
132 return 0, notFound(err)
133 }
134 return 0, fail(400, err)
135 }
136 return id, nil
137 case "failed":
138 ok, err := s.Store.RetryFailedJobs(ctx, p.ID)
139 if err != nil {
140 return 0, fail(400, err)
141 }
142 if !ok {
143 return 0, fail(409, i18n.E("err.pipeNoFailed"))
144 }
145 return p.ID, nil
146 default:
147 return 0, fail(400, i18n.E("err.pipeRetryMode"))
148 }
149}
150
151func (s *Server) opCancelPipeline(ctx context.Context, user store.User, repo store.Repo, pipeID int64) error {
152 role, err := s.repoRole(ctx, user, repo)
153 if err != nil {
154 return err
155 }
156 if !role.CanWriteCode() {
157 return fail(403, i18n.E("err.forbidden"))
158 }
159 p, err := s.Store.PipelineByID(ctx, repo.ID, pipeID)
160 if err != nil {
161 return notFound(err)
162 }
163 jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
164 var running []int64
165 for _, j := range jobs {
166 if j.Status == "queued" || j.Status == "running" || j.Status == "manual" || j.Status == "pending" || j.Status == "created" {
167 ok, _ := s.Store.CancelJob(ctx, j.ID)
168 if ok && (j.Status == "running" || j.Status == "queued") {
169 running = append(running, j.ID)
170 }
171 }
172 }
173 if s.Runner != nil {
174 s.Runner.Interrupt(running)
175 }
176 _, _ = s.Store.RefreshPipelineStatus(ctx, p.ID)
177 return nil
178}
179
180func (s *Server) opLintCI(r *http.Request, repo store.Repo, body, ref string) (map[string]any, error) {
181 if len(body) > ciMaxBytes {
182 return nil, fail(400, i18n.E("edit.fail"))
183 }
184 ref = safeRev(ref, repo.DefaultBranch)
185 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
186 model := s.ciEditorModel(r, body, s.includeFrom(r, dir, ref))
187 lines := make([]string, len(model.Rows))
188 var bad []int
189 for i, row := range model.Rows {
190 lines[i] = string(row.HTML)
191 if row.Bad {
192 bad = append(bad, row.N)
193 }
194 }
195 return map[string]any{
196 "status": model.Status, "statusClass": model.Class, "errors": model.Errors,
197 "warnings": model.Warnings, "issues": model.Issues, "lines": lines, "bad": bad,
198 }, nil
199}
200
201func (s *Server) opSaveCI(ctx context.Context, user store.User, repo store.Repo, base, branch, message, body string, openMR bool) (string, int64, error) {
202 role, err := s.repoRole(ctx, user, repo)
203 if err != nil {
204 return "", 0, err
205 }
206 base = safeRev(base, repo.DefaultBranch)
207 if !validBranchName(branch) {
208 return "", 0, fail(400, i18n.E("edit.badBranch"))
209 }
210 if len(body) > ciMaxBytes {
211 return "", 0, fail(400, i18n.E("edit.fail"))
212 }
213 patterns, _ := s.Store.ProtectedPatterns(ctx, repo.ID)
214 if openMR && (branch == base || access.BranchProtected(patterns, branch)) {
215 return "", 0, fail(400, i18n.E("edit.badBranch"))
216 }
217 sha, err := s.opWriteFiles(ctx, user, repo, role, branch, base, message, "", map[string]string{ciFile: body}, nil)
218 if err != nil {
219 return "", 0, err
220 }
221 if !openMR {
222 return sha, 0, nil
223 }
224 existing, err := s.Store.OpenMergeRequestsBySource(ctx, repo.ID, branch)
225 if err != nil {
226 return sha, 0, fail(400, err)
227 }
228 if len(existing) > 0 {
229 return sha, existing[0].Number, nil
230 }
231 mr, err := s.opCreateMR(ctx, user, repo, message, "", branch, base, false)
232 if err != nil {
233 return sha, 0, err
234 }
235 return sha, mr.Number, nil
236}
237
238func (s *Server) opPublishRelease(ctx context.Context, repo store.Repo, author store.User, job store.Job, req releaseRequest) (int64, error) {
239 if s.StoragePath() == "" {
240 return 0, fail(503, i18n.E("err.noStorage"))
241 }
242 if err := names.ValidTag(req.Tag); err != nil {
243 return 0, fail(400, err)
244 }
245 if !safeSHA(req.Commit) {
246 return 0, fail(400, i18n.E("err.commitAbsent"))
247 }
248 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
249 if !gitrepo.HasCommit(ctx, s.Git, dir, req.Commit) {
250 return 0, fail(400, i18n.E("err.commitMissing"))
251 }
252 if gitrepo.HasTag(ctx, s.Git, dir, req.Tag) {
253 return 0, fail(409, i18n.E("err.tagExists", req.Tag))
254 }
255 if req.Name == "" {
256 req.Name = req.Tag
257 }
258 if job.ID == 0 && len(req.Assets) > 0 {
259 return 0, fail(400, i18n.E("err.assetsJob"))
260 }
261 var assets []store.ReleaseAsset
262 for _, a := range req.Assets {
263 if a.Name == "" {
264 a.Name = filepath.Base(a.Path)
265 }
266 if strings.Contains(a.Name, "/") || strings.Contains(a.Name, "..") || a.Path == "" {
267 return 0, fail(400, i18n.E("err.assetName"))
268 }
269 src, err := s.Store.ArtifactPath(ctx, job.PipelineID, a.Job, a.Path)
270 if err != nil {
271 return 0, fail(400, i18n.E("err.artifactMissing", a.Job, a.Path))
272 }
273 info, err := os.Stat(src)
274 if err != nil {
275 return 0, fail(400, i18n.E("err.assetDisk"))
276 }
277 dst := filepath.Join(s.StoragePath(), "releases", repo.OwnerPath, repo.Name, req.Tag, a.Name)
278 if err := copyRelease(src, dst); err != nil {
279 return 0, fail(500, i18n.E("err.assetCopy"))
280 }
281 assets = append(assets, store.ReleaseAsset{Name: a.Name, StoragePath: dst, Size: info.Size()})
282 }
283 if err := s.postReceive(ctx, repo, author, dir, func() error {
284 name, email := gitrepo.FallbackName, gitrepo.FallbackEmail
285 if author.Username != "" {
286 name, email = author.Username, userEmail(author.Username)
287 } else if job.ID != 0 {
288 if p, err := s.Store.PipelineByID(ctx, repo.ID, job.PipelineID); err == nil && p.Actor != "" {
289 name, email = p.Actor, userEmail(p.Actor)
290 }
291 }
292 return gitrepo.Tag(ctx, s.Git, dir, req.Tag, req.Commit, req.Name, name, email)
293 }); err != nil {
294 return 0, fail(400, i18n.E("err.tagFail", err.Error()))
295 }
296 authorID := author.ID
297 id, err := s.Store.CreateRelease(ctx, repo.ID, req.Tag, req.Commit, req.Name, req.Body, authorID, job.ID, assets)
298 if err != nil {
299 return 0, fail(400, err)
300 }
301 s.Dispatch(repo.ID, s.releaseEvent(repo, req.Tag, req.Name, req.Commit, assets))
302 return id, nil
303}
304
305func (s *Server) opCIFile(ctx context.Context, repo store.Repo, ref string) (string, error) {
306 ref = safeRev(ref, repo.DefaultBranch)
307 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
308 body := gitOne(ctx, s.Git, dir, "show", ref+":"+ciFile)
309 return body, nil
310}