|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package actions |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + actions_model "code.gitea.io/gitea/models/actions" |
| 11 | + "code.gitea.io/gitea/models/db" |
| 12 | + repo_model "code.gitea.io/gitea/models/repo" |
| 13 | + "code.gitea.io/gitea/modules/util" |
| 14 | + |
| 15 | + "github.com/nektos/act/pkg/jobparser" |
| 16 | +) |
| 17 | + |
| 18 | +// InsertRun inserts a run |
| 19 | +// The title will be cut off at 255 characters if it's longer than 255 characters. |
| 20 | +func InsertRun(ctx context.Context, run *actions_model.ActionRun, jobs []*jobparser.SingleWorkflow) error { |
| 21 | + ctx, committer, err := db.TxContext(ctx) |
| 22 | + if err != nil { |
| 23 | + return err |
| 24 | + } |
| 25 | + defer committer.Close() |
| 26 | + |
| 27 | + index, err := db.GetNextResourceIndex(ctx, "action_run_index", run.RepoID) |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + run.Index = index |
| 32 | + run.Title, _ = util.SplitStringAtByteN(run.Title, 255) |
| 33 | + |
| 34 | + // check workflow concurrency |
| 35 | + if len(run.ConcurrencyGroup) > 0 { |
| 36 | + if run.ConcurrencyCancel { |
| 37 | + if err := actions_model.CancelPreviousJobsWithOpts(ctx, &actions_model.FindRunOptions{ |
| 38 | + RepoID: run.RepoID, |
| 39 | + ConcurrencyGroup: run.ConcurrencyGroup, |
| 40 | + Status: []actions_model.Status{ |
| 41 | + actions_model.StatusRunning, |
| 42 | + actions_model.StatusWaiting, |
| 43 | + actions_model.StatusBlocked, |
| 44 | + }, |
| 45 | + }); err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + } else { |
| 49 | + runningConcurrentRunsNum, err := db.Count[actions_model.ActionRun](ctx, &actions_model.FindRunOptions{ |
| 50 | + RepoID: run.RepoID, |
| 51 | + ConcurrencyGroup: run.ConcurrencyGroup, |
| 52 | + Status: []actions_model.Status{actions_model.StatusRunning}, |
| 53 | + }) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + if runningConcurrentRunsNum > 0 { |
| 58 | + run.Status = actions_model.StatusBlocked |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if err := db.Insert(ctx, run); err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + if run.Repo == nil { |
| 68 | + repo, err := repo_model.GetRepositoryByID(ctx, run.RepoID) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + run.Repo = repo |
| 73 | + } |
| 74 | + |
| 75 | + if err := actions_model.UpdateRepoRunsNumbers(ctx, run.Repo); err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + // query vars for evaluating job concurrency groups |
| 80 | + vars, err := actions_model.GetVariablesOfRun(ctx, run) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("get run %d variables: %w", run.ID, err) |
| 83 | + } |
| 84 | + |
| 85 | + runJobs := make([]*actions_model.ActionRunJob, 0, len(jobs)) |
| 86 | + var hasWaiting bool |
| 87 | + for _, v := range jobs { |
| 88 | + id, job := v.Job() |
| 89 | + needs := job.Needs() |
| 90 | + if err := v.SetJob(id, job.EraseNeeds()); err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + payload, _ := v.Marshal() |
| 94 | + status := actions_model.StatusWaiting |
| 95 | + if len(needs) > 0 || run.NeedApproval || run.Status == actions_model.StatusBlocked { |
| 96 | + status = actions_model.StatusBlocked |
| 97 | + } else { |
| 98 | + hasWaiting = true |
| 99 | + } |
| 100 | + job.Name, _ = util.SplitStringAtByteN(job.Name, 255) |
| 101 | + runJob := &actions_model.ActionRunJob{ |
| 102 | + RunID: run.ID, |
| 103 | + RepoID: run.RepoID, |
| 104 | + OwnerID: run.OwnerID, |
| 105 | + CommitSHA: run.CommitSHA, |
| 106 | + IsForkPullRequest: run.IsForkPullRequest, |
| 107 | + Name: job.Name, |
| 108 | + WorkflowPayload: payload, |
| 109 | + JobID: id, |
| 110 | + Needs: needs, |
| 111 | + RunsOn: job.RunsOn(), |
| 112 | + Status: status, |
| 113 | + } |
| 114 | + |
| 115 | + // check job concurrency |
| 116 | + if job.RawConcurrency != nil && len(job.RawConcurrency.Group) > 0 { |
| 117 | + runJob.RawConcurrencyGroup = job.RawConcurrency.Group |
| 118 | + runJob.RawConcurrencyCancel = job.RawConcurrency.CancelInProgress |
| 119 | + // we do not need to evaluate job concurrency if the job is blocked |
| 120 | + // because it will be checked by job emitter |
| 121 | + if runJob.Status != actions_model.StatusBlocked && len(needs) == 0 { |
| 122 | + var err error |
| 123 | + runJob.ConcurrencyGroup, runJob.ConcurrencyCancel, err = evaluateJobConcurrency(run, runJob, vars, map[string]*jobparser.JobResult{}) |
| 124 | + if err != nil { |
| 125 | + return fmt.Errorf("evaluate job concurrency: %w", err) |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + runJobs = append(runJobs, runJob) |
| 131 | + } |
| 132 | + |
| 133 | + if err := db.Insert(ctx, runJobs); err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + |
| 137 | + // if there is a job in the waiting status, increase tasks version. |
| 138 | + if hasWaiting { |
| 139 | + if err := actions_model.IncreaseTaskVersion(ctx, run.OwnerID, run.RepoID); err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return committer.Commit() |
| 145 | +} |
0 commit comments