| 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 | stamp, _ := gitrepo.CommitterTimestamp(ctx, s.Git, dir, sha) |
| 37 | env := ci.Variables(ci.Ref{ |
| 38 | SHA: sha, FullRef: ref, Name: name, Kind: kind, Title: title, Timestamp: stamp, |
| 39 | DefaultBranch: repo.DefaultBranch, Source: source, ProjectPath: repo.FullPath(), |
| 40 | ProjectURL: base + "/" + repo.FullPath(), APIURL: base + "/api/v1", MR: mr, |
| 41 | }) |
| 42 | ev := ci.Eval{Env: env, Changed: changed, Files: files} |
| 43 | allow, err := spec.Allow(ev) |
| 44 | if err != nil { |
| 45 | return "", nil, err |
| 46 | } |
| 47 | if !allow { |
| 48 | return title, nil, nil |
| 49 | } |
| 50 | active, err := spec.Select(ev) |
| 51 | if err != nil { |
| 52 | return "", nil, err |
| 53 | } |
| 54 | var jobs []store.NewJob |
| 55 | for i, j := range active { |
| 56 | vars := map[string]string{} |
| 57 | for k, v := range spec.Variables { |
| 58 | vars[k] = v |
| 59 | } |
| 60 | for k, v := range j.Variables { |
| 61 | vars[k] = v |
| 62 | } |
| 63 | needs := j.Needs |
| 64 | if needs == nil { |
| 65 | needs = []string{} |
| 66 | } |
| 67 | paths := j.Artifacts.Paths |
| 68 | if paths == nil { |
| 69 | paths = []string{} |
| 70 | } |
| 71 | var services []store.Service |
| 72 | for _, svc := range j.Services { |
| 73 | services = append(services, store.Service{ |
| 74 | Name: svc.Name, Alias: svc.Alias, Command: svc.Command, Entrypoint: svc.Entrypoint, Variables: svc.Variables, |
| 75 | }) |
| 76 | } |
| 77 | jobs = append(jobs, store.NewJob{ |
| 78 | Name: j.Name, Stage: j.Stage, Position: i, Image: j.Image, |
| 79 | Before: strings.Join(j.Before, "\n"), Script: strings.Join(j.Script, "\n"), After: strings.Join(j.After, "\n"), |
| 80 | Needs: needs, Optional: j.Optional, NeedsSet: j.NeedsSet, |
| 81 | Variables: vars, ArtifactPaths: paths, ArtifactExpire: j.Artifacts.ExpireIn, |
| 82 | AllowFailure: j.AllowFailure, When: j.When, Timeout: j.Timeout, Tags: j.Tags, |
| 83 | CachePaths: j.Cache.Paths, CacheKey: j.Cache.Key, ResourceGroup: j.ResourceGroup, |
| 84 | RetryMax: j.Retry.Max, RetryWhen: j.Retry.When, Services: services, |
| 85 | Environment: j.Environment.Name, EnvironmentURL: j.Environment.URL, Interruptible: j.Interruptible, |
| 86 | }) |
| 87 | } |
| 88 | return title, jobs, nil |
| 89 | } |
| 90 | |
| 91 | func gitShow(ctx context.Context, gitBin, dir, sha, path string) ([]byte, error) { |
| 92 | if path == "" || strings.Contains(path, "..") || strings.HasPrefix(path, "/") { |
| 93 | return nil, i18n.E("ci.err.rel", path) |
| 94 | } |
| 95 | return exec.CommandContext(ctx, gitBin, "-C", dir, "show", sha+":"+path).Output() |
| 96 | } |
| 97 | |
| 98 | func (s *Server) startMergeRequestPipelines(ctx context.Context, repo store.Repo, sha, branch, actor, before string) { |
| 99 | mrs, err := s.Store.OpenMergeRequestsBySource(ctx, repo.ID, branch) |
| 100 | if err != nil { |
| 101 | return |
| 102 | } |
| 103 | for _, mr := range mrs { |
| 104 | s.startOneMergePipeline(ctx, repo, sha, actor, before, mr) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // rerunMergeRequestsOnTarget start een nieuwe merge-request-pipeline op de |
| 109 | // broncommit wanneer de doelbranch zelf een push krijgt. |
| 110 | func (s *Server) rerunMergeRequestsOnTarget(ctx context.Context, repo store.Repo, branch, actor string) { |
| 111 | mrs, err := s.Store.OpenMergeRequestsByTarget(ctx, repo.ID, branch) |
| 112 | if err != nil { |
| 113 | return |
| 114 | } |
| 115 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 116 | for _, mr := range mrs { |
| 117 | sha := gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "refs/heads/"+mr.SourceBranch) |
| 118 | if sha == "" { |
| 119 | continue |
| 120 | } |
| 121 | s.startOneMergePipeline(ctx, repo, sha, actor, "", mr) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func (s *Server) startOneMergePipeline(ctx context.Context, repo store.Repo, sha, actor, before string, mr store.MergeRequest) { |
| 126 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 127 | base := strings.TrimRight(s.Cfg.BaseURL, "/") |
| 128 | info := &ci.MRInfo{ |
| 129 | ID: mr.ID, IID: mr.Number, Title: mr.Title, |
| 130 | SourceBranch: mr.SourceBranch, TargetBranch: mr.TargetBranch, |
| 131 | ProjectPath: repo.FullPath(), ProjectURL: base + "/" + repo.FullPath(), |
| 132 | TargetSHA: gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "refs/heads/"+mr.TargetBranch), |
| 133 | } |
| 134 | s.startPipeline(ctx, repo, sha, "refs/heads/"+mr.SourceBranch, actor, "merge_request_event", before, info) |
| 135 | } |
| 136 | |
| 137 | // mergeBlocks zijn de vertaalde redenen waarom mergen nog niet mag. |
| 138 | // Een pipeline is verplicht zodra blue-ci-cd.yml jobs voor deze merge request kiest. |
| 139 | func (s *Server) mergeBlocks(r *http.Request, repo store.Repo, mr store.MergeRequest) []string { |
| 140 | if mr.Status != "open" { |
| 141 | return nil |
| 142 | } |
| 143 | var blocks []string |
| 144 | if mr.Draft { |
| 145 | blocks = append(blocks, s.tr(r, "mr.block.draft")) |
| 146 | } |
| 147 | if s.Cfg.CI.Enabled && s.StoragePath() != "" { |
| 148 | blocks = append(blocks, s.pipelineBlocks(r, repo, mr)...) |
| 149 | } |
| 150 | blocks = append(blocks, s.reviewBlocks(r, repo, mr)...) |
| 151 | if s.StoragePath() != "" { |
| 152 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 153 | if conflict, known := s.branchConflict(r.Context(), dir, mr.TargetBranch, mr.SourceBranch); known && conflict { |
| 154 | blocks = append(blocks, s.tr(r, "mr.block.conflict")) |
| 155 | } |
| 156 | } |
| 157 | return blocks |
| 158 | } |
| 159 | |
| 160 | func (s *Server) branchConflict(ctx context.Context, dir, target, source string) (conflict, known bool) { |
| 161 | if gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+target) == "" { |
| 162 | return false, false |
| 163 | } |
| 164 | if gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "--quiet", "refs/heads/"+source) == "" { |
| 165 | return false, false |
| 166 | } |
| 167 | conflict, err := gitrepo.Conflicts(ctx, s.Git, dir, "refs/heads/"+target, "refs/heads/"+source) |
| 168 | if err != nil { |
| 169 | return false, false |
| 170 | } |
| 171 | return conflict, true |
| 172 | } |
| 173 | |
| 174 | func (s *Server) pipelineBlocks(r *http.Request, repo store.Repo, mr store.MergeRequest) []string { |
| 175 | ctx := r.Context() |
| 176 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 177 | sha := gitOne(ctx, s.Git, dir, "rev-parse", "--verify", "refs/heads/"+mr.SourceBranch) |
| 178 | required := false |
| 179 | if sha != "" { |
| 180 | info := &ci.MRInfo{ |
| 181 | ID: mr.ID, IID: mr.Number, Title: mr.Title, |
| 182 | SourceBranch: mr.SourceBranch, TargetBranch: mr.TargetBranch, |
| 183 | ProjectPath: repo.FullPath(), |
| 184 | } |
| 185 | _, jobs, broken := s.planPipeline(ctx, repo, sha, "refs/heads/"+mr.SourceBranch, "merge_request_event", "", info) |
| 186 | required = broken != nil || len(jobs) > 0 |
| 187 | } |
| 188 | p, err := s.Store.LatestMergePipeline(ctx, repo.ID, mr.ID) |
| 189 | if store.IsNotFound(err) { |
| 190 | if required { |
| 191 | return []string{s.tr(r, "mr.block.waiting")} |
| 192 | } |
| 193 | return nil |
| 194 | } |
| 195 | if err != nil { |
| 196 | return []string{s.tr(r, "mr.block.waiting")} |
| 197 | } |
| 198 | jobs, err := s.Store.JobsByPipeline(ctx, p.ID) |
| 199 | if err != nil { |
| 200 | return []string{s.tr(r, "mr.block.waiting")} |
| 201 | } |
| 202 | switch store.PipelineBlocksMerge(jobs) { |
| 203 | case "running": |
| 204 | return []string{s.tr(r, "mr.block.running")} |
| 205 | case "failed": |
| 206 | return []string{s.tr(r, "mr.block.failed")} |
| 207 | case "waiting": |
| 208 | if required { |
| 209 | return []string{s.tr(r, "mr.block.waiting")} |
| 210 | } |
| 211 | } |
| 212 | return nil |
| 213 | } |
| 214 | |
| 215 | func (s *Server) fillMRPipeline(r *http.Request, v *View, repo store.Repo, mr store.MergeRequest) { |
| 216 | p, err := s.Store.LatestMergePipeline(r.Context(), repo.ID, mr.ID) |
| 217 | if err != nil { |
| 218 | return |
| 219 | } |
| 220 | row := s.pipeRow(r, s.langOf(r), repo, p, map[string]store.User{}, s.queueRanks(r.Context())) |
| 221 | v.MRPipe = &row |
| 222 | } |