| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "os/exec" |
| 7 | "strings" |
| 8 | |
| 9 | "gitlab.com/Timfa/bluegit/internal/ci" |
| 10 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 11 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 12 | "gitlab.com/Timfa/bluegit/internal/store" |
| 13 | ) |
| 14 | |
| 15 | // planPipeline leest blue-ci-cd.yml en kiest de jobs. |
| 16 | // Een ontbrekend bestand geeft geen jobs en geen fout. |
| 17 | // broken is een kapot bestand; de aanroeper bewaart dan een mislukte pipeline. |
| 18 | func (s *Server) planPipeline(ctx context.Context, repo store.Repo, sha, ref, source, before string, mr *ci.MRInfo) (string, []store.NewJob, error) { |
| 19 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 20 | raw, err := gitShow(ctx, s.Git, dir, sha, "blue-ci-cd.yml") |
| 21 | if err != nil || len(raw) == 0 { |
| 22 | return "", nil, nil |
| 23 | } |
| 24 | read := func(path string) ([]byte, error) { |
| 25 | return gitShow(ctx, s.Git, dir, sha, path) |
| 26 | } |
| 27 | spec, err := ci.ParseIn(raw, read) |
| 28 | if err != nil { |
| 29 | return "", nil, err |
| 30 | } |
| 31 | name, kind := ci.SplitRef(ref) |
| 32 | title := strings.TrimSpace(gitOne(ctx, s.Git, dir, "log", "-1", "--format=%s", sha)) |
| 33 | files := gitLines(ctx, s.Git, dir, "ls-tree", "-r", "--name-only", sha) |
| 34 | changed := changedFiles(ctx, s.Git, dir, sha, before, files) |
| 35 | base := strings.TrimRight(s.Cfg.BaseURL, "/") |
| 36 | env := ci.Variables(ci.Ref{ |
| 37 | SHA: sha, FullRef: ref, Name: name, Kind: kind, Title: title, |
| 38 | DefaultBranch: repo.DefaultBranch, Source: source, ProjectPath: repo.FullPath(), |
| 39 | ProjectURL: base + "/" + repo.FullPath(), APIURL: base + "/api/v1", MR: mr, |
| 40 | }) |
| 41 | active, err := spec.Select(ci.Eval{Env: env, Changed: changed, Files: files}) |
| 42 | if err != nil { |
| 43 | return "", nil, err |
| 44 | } |
| 45 | var jobs []store.NewJob |
| 46 | for i, j := range active { |
| 47 | vars := map[string]string{} |
| 48 | for k, v := range spec.Variables { |
| 49 | vars[k] = v |
| 50 | } |
| 51 | for k, v := range j.Variables { |
| 52 | vars[k] = v |
| 53 | } |
| 54 | needs := j.Needs |
| 55 | if needs == nil { |
| 56 | needs = []string{} |
| 57 | } |
| 58 | paths := j.Artifacts.Paths |
| 59 | if paths == nil { |
| 60 | paths = []string{} |
| 61 | } |
| 62 | jobs = append(jobs, store.NewJob{ |
| 63 | Name: j.Name, Stage: j.Stage, Position: i, Image: j.Image, |
| 64 | Before: strings.Join(j.Before, "\n"), Script: strings.Join(j.Script, "\n"), After: strings.Join(j.After, "\n"), |
| 65 | Needs: needs, Optional: j.Optional, NeedsSet: j.NeedsSet, |
| 66 | Variables: vars, ArtifactPaths: paths, ArtifactExpire: j.Artifacts.ExpireIn, |
| 67 | AllowFailure: j.AllowFailure, When: j.When, Timeout: j.Timeout, Tags: j.Tags, |
| 68 | CachePaths: j.Cache.Paths, CacheKey: j.Cache.Key, ResourceGroup: j.ResourceGroup, |
| 69 | }) |
| 70 | } |
| 71 | return title, jobs, nil |
| 72 | } |
| 73 | |
| 74 | func gitShow(ctx context.Context, gitBin, dir, sha, path string) ([]byte, error) { |
| 75 | if path == "" || strings.Contains(path, "..") || strings.HasPrefix(path, "/") { |
| 76 | return nil, i18n.E("ci.err.rel", path) |
| 77 | } |
| 78 | return exec.CommandContext(ctx, gitBin, "-C", dir, "show", sha+":"+path).Output() |
| 79 | } |
| 80 | |
| 81 | func (s *Server) startMergeRequestPipelines(ctx context.Context, repo store.Repo, sha, branch, actor, before string) { |
| 82 | mrs, err := s.Store.OpenMergeRequestsBySource(ctx, repo.ID, branch) |
| 83 | if err != nil { |
| 84 | return |
| 85 | } |
| 86 | for _, mr := range mrs { |
| 87 | s.startOneMergePipeline(ctx, repo, sha, actor, before, mr) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func (s *Server) startOneMergePipeline(ctx context.Context, repo store.Repo, sha, actor, before string, mr store.MergeRequest) { |
| 92 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 93 | base := strings.TrimRight(s.Cfg.BaseURL, "/") |
| 94 | info := &ci.MRInfo{ |
| 95 | ID: mr.ID, IID: mr.Number, Title: mr.Title, |
| 96 | SourceBranch: mr.SourceBranch, TargetBranch: mr.TargetBranch, |
| 97 | ProjectPath: repo.FullPath(), ProjectURL: base + "/" + repo.FullPath(), |
| 98 | TargetSHA: gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "refs/heads/"+mr.TargetBranch), |
| 99 | } |
| 100 | s.startPipeline(ctx, repo, sha, "refs/heads/"+mr.SourceBranch, actor, "merge_request_event", before, info) |
| 101 | } |
| 102 | |
| 103 | // mergeBlocks zijn de vertaalde redenen waarom mergen nog niet mag. |
| 104 | // Een pipeline is verplicht zodra blue-ci-cd.yml jobs voor deze merge request kiest. |
| 105 | func (s *Server) mergeBlocks(r *http.Request, repo store.Repo, mr store.MergeRequest) []string { |
| 106 | if mr.Status != "open" { |
| 107 | return nil |
| 108 | } |
| 109 | var blocks []string |
| 110 | if s.Cfg.CI.Enabled && s.StoragePath() != "" { |
| 111 | blocks = append(blocks, s.pipelineBlocks(r, repo, mr)...) |
| 112 | } |
| 113 | return append(blocks, s.reviewBlocks(r, repo, mr)...) |
| 114 | } |
| 115 | |
| 116 | func (s *Server) pipelineBlocks(r *http.Request, repo store.Repo, mr store.MergeRequest) []string { |
| 117 | ctx := r.Context() |
| 118 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 119 | sha := gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "refs/heads/"+mr.SourceBranch) |
| 120 | required := false |
| 121 | if sha != "" { |
| 122 | info := &ci.MRInfo{ |
| 123 | ID: mr.ID, IID: mr.Number, Title: mr.Title, |
| 124 | SourceBranch: mr.SourceBranch, TargetBranch: mr.TargetBranch, |
| 125 | ProjectPath: repo.FullPath(), |
| 126 | } |
| 127 | _, jobs, broken := s.planPipeline(ctx, repo, sha, "refs/heads/"+mr.SourceBranch, "merge_request_event", "", info) |
| 128 | required = broken != nil || len(jobs) > 0 |
| 129 | } |
| 130 | p, err := s.Store.LatestMergePipeline(ctx, repo.ID, mr.ID) |
| 131 | if store.IsNotFound(err) { |
| 132 | if required { |
| 133 | return []string{s.tr(r, "mr.block.waiting")} |
| 134 | } |
| 135 | return nil |
| 136 | } |
| 137 | if err != nil { |
| 138 | return []string{s.tr(r, "mr.block.waiting")} |
| 139 | } |
| 140 | jobs, err := s.Store.JobsByPipeline(ctx, p.ID) |
| 141 | if err != nil { |
| 142 | return []string{s.tr(r, "mr.block.waiting")} |
| 143 | } |
| 144 | switch store.PipelineBlocksMerge(jobs) { |
| 145 | case "running": |
| 146 | return []string{s.tr(r, "mr.block.running")} |
| 147 | case "failed": |
| 148 | return []string{s.tr(r, "mr.block.failed")} |
| 149 | case "waiting": |
| 150 | if required { |
| 151 | return []string{s.tr(r, "mr.block.waiting")} |
| 152 | } |
| 153 | } |
| 154 | return nil |
| 155 | } |
| 156 | |
| 157 | func (s *Server) fillMRPipeline(r *http.Request, v *View, repo store.Repo, mr store.MergeRequest) { |
| 158 | p, err := s.Store.LatestMergePipeline(r.Context(), repo.ID, mr.ID) |
| 159 | if err != nil { |
| 160 | return |
| 161 | } |
| 162 | row := s.pipeRow(r, s.langOf(r), repo, p, map[string]store.User{}) |
| 163 | v.MRPipe = &row |
| 164 | } |