BlueGit

timfa bluegit

Files

bluegit / internal / server / ops_ci.go

ops_ci.go

ops_ci.go 8.7 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 switch mode {
117 case "all":
118 id, err := s.Store.ClonePipeline(ctx, repo.ID, p.ID)
119 if err != nil {
120 if store.IsNotFound(err) {
121 return 0, notFound(err)
122 }
123 return 0, fail(400, err)
124 }
125 return id, nil
126 case "failed":
127 ok, err := s.Store.RetryFailedJobs(ctx, p.ID)
128 if err != nil {
129 return 0, fail(400, err)
130 }
131 if !ok {
132 return 0, fail(409, i18n.E("err.pipeNoFailed"))
133 }
134 return p.ID, nil
135 default:
136 return 0, fail(400, i18n.E("err.pipeRetryMode"))
137 }
138}
139
140func (s *Server) opCancelPipeline(ctx context.Context, user store.User, repo store.Repo, pipeID int64) error {
141 role, err := s.repoRole(ctx, user, repo)
142 if err != nil {
143 return err
144 }
145 if !role.CanWriteCode() {
146 return fail(403, i18n.E("err.forbidden"))
147 }
148 p, err := s.Store.PipelineByID(ctx, repo.ID, pipeID)
149 if err != nil {
150 return notFound(err)
151 }
152 jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
153 var running []int64
154 for _, j := range jobs {
155 if j.Status == "queued" || j.Status == "running" || j.Status == "manual" || j.Status == "pending" || j.Status == "created" {
156 ok, _ := s.Store.CancelJob(ctx, j.ID)
157 if ok && (j.Status == "running" || j.Status == "queued") {
158 running = append(running, j.ID)
159 }
160 }
161 }
162 if s.Runner != nil {
163 s.Runner.Interrupt(running)
164 }
165 _, _ = s.Store.RefreshPipelineStatus(ctx, p.ID)
166 return nil
167}
168
169func (s *Server) opLintCI(r *http.Request, repo store.Repo, body, ref string) (map[string]any, error) {
170 if len(body) > ciMaxBytes {
171 return nil, fail(400, i18n.E("edit.fail"))
172 }
173 ref = safeRev(ref, repo.DefaultBranch)
174 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
175 model := s.ciEditorModel(r, body, s.includeFrom(r, dir, ref))
176 lines := make([]string, len(model.Rows))
177 var bad []int
178 for i, row := range model.Rows {
179 lines[i] = string(row.HTML)
180 if row.Bad {
181 bad = append(bad, row.N)
182 }
183 }
184 return map[string]any{
185 "status": model.Status, "statusClass": model.Class, "errors": model.Errors,
186 "warnings": model.Warnings, "issues": model.Issues, "lines": lines, "bad": bad,
187 }, nil
188}
189
190func (s *Server) opSaveCI(ctx context.Context, user store.User, repo store.Repo, base, branch, message, body string, openMR bool) (string, int64, error) {
191 role, err := s.repoRole(ctx, user, repo)
192 if err != nil {
193 return "", 0, err
194 }
195 base = safeRev(base, repo.DefaultBranch)
196 if !validBranchName(branch) {
197 return "", 0, fail(400, i18n.E("edit.badBranch"))
198 }
199 if len(body) > ciMaxBytes {
200 return "", 0, fail(400, i18n.E("edit.fail"))
201 }
202 patterns, _ := s.Store.ProtectedPatterns(ctx, repo.ID)
203 if openMR && (branch == base || access.BranchProtected(patterns, branch)) {
204 return "", 0, fail(400, i18n.E("edit.badBranch"))
205 }
206 sha, err := s.opWriteFiles(ctx, user, repo, role, branch, base, message, "", map[string]string{ciFile: body}, nil)
207 if err != nil {
208 return "", 0, err
209 }
210 if !openMR {
211 return sha, 0, nil
212 }
213 existing, err := s.Store.OpenMergeRequestsBySource(ctx, repo.ID, branch)
214 if err != nil {
215 return sha, 0, fail(400, err)
216 }
217 if len(existing) > 0 {
218 return sha, existing[0].Number, nil
219 }
220 mr, err := s.opCreateMR(ctx, user, repo, message, "", branch, base, false)
221 if err != nil {
222 return sha, 0, err
223 }
224 return sha, mr.Number, nil
225}
226
227func (s *Server) opPublishRelease(ctx context.Context, repo store.Repo, author store.User, job store.Job, req releaseRequest) (int64, error) {
228 if s.StoragePath() == "" {
229 return 0, fail(503, i18n.E("err.noStorage"))
230 }
231 if err := names.ValidTag(req.Tag); err != nil {
232 return 0, fail(400, err)
233 }
234 if !safeSHA(req.Commit) {
235 return 0, fail(400, i18n.E("err.commitAbsent"))
236 }
237 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
238 if !gitrepo.HasCommit(ctx, s.Git, dir, req.Commit) {
239 return 0, fail(400, i18n.E("err.commitMissing"))
240 }
241 if gitrepo.HasTag(ctx, s.Git, dir, req.Tag) {
242 return 0, fail(409, i18n.E("err.tagExists", req.Tag))
243 }
244 if req.Name == "" {
245 req.Name = req.Tag
246 }
247 if job.ID == 0 && len(req.Assets) > 0 {
248 return 0, fail(400, i18n.E("err.assetsJob"))
249 }
250 var assets []store.ReleaseAsset
251 for _, a := range req.Assets {
252 if a.Name == "" {
253 a.Name = filepath.Base(a.Path)
254 }
255 if strings.Contains(a.Name, "/") || strings.Contains(a.Name, "..") || a.Path == "" {
256 return 0, fail(400, i18n.E("err.assetName"))
257 }
258 src, err := s.Store.ArtifactPath(ctx, job.PipelineID, a.Job, a.Path)
259 if err != nil {
260 return 0, fail(400, i18n.E("err.artifactMissing", a.Job, a.Path))
261 }
262 info, err := os.Stat(src)
263 if err != nil {
264 return 0, fail(400, i18n.E("err.assetDisk"))
265 }
266 dst := filepath.Join(s.StoragePath(), "releases", repo.OwnerPath, repo.Name, req.Tag, a.Name)
267 if err := copyRelease(src, dst); err != nil {
268 return 0, fail(500, i18n.E("err.assetCopy"))
269 }
270 assets = append(assets, store.ReleaseAsset{Name: a.Name, StoragePath: dst, Size: info.Size()})
271 }
272 if err := s.postReceive(ctx, repo, author, dir, func() error {
273 name, email := gitrepo.FallbackName, gitrepo.FallbackEmail
274 if author.Username != "" {
275 name, email = author.Username, userEmail(author.Username)
276 } else if job.ID != 0 {
277 if p, err := s.Store.PipelineByID(ctx, repo.ID, job.PipelineID); err == nil && p.Actor != "" {
278 name, email = p.Actor, userEmail(p.Actor)
279 }
280 }
281 return gitrepo.Tag(ctx, s.Git, dir, req.Tag, req.Commit, req.Name, name, email)
282 }); err != nil {
283 return 0, fail(400, i18n.E("err.tagFail", err.Error()))
284 }
285 authorID := author.ID
286 id, err := s.Store.CreateRelease(ctx, repo.ID, req.Tag, req.Commit, req.Name, req.Body, authorID, job.ID, assets)
287 if err != nil {
288 return 0, fail(400, err)
289 }
290 s.Dispatch(repo.ID, s.releaseEvent(repo, req.Tag, req.Name, req.Commit, assets))
291 return id, nil
292}
293
294func (s *Server) opCIFile(ctx context.Context, repo store.Repo, ref string) (string, error) {
295 ref = safeRev(ref, repo.DefaultBranch)
296 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
297 body := gitOne(ctx, s.Git, dir, "show", ref+":"+ciFile)
298 return body, nil
299}