Skip to content

Commit 678a970

Browse files
committed
Cleanup ActionRun creation
* share more logic
1 parent 40f71bc commit 678a970

File tree

4 files changed

+67
-155
lines changed

4 files changed

+67
-155
lines changed

services/actions/notifier_helper.go

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"code.gitea.io/gitea/services/convert"
3030
notify_service "code.gitea.io/gitea/services/notify"
3131

32-
"github.com/nektos/act/pkg/jobparser"
3332
"github.com/nektos/act/pkg/model"
3433
)
3534

@@ -346,44 +345,8 @@ func handleWorkflows(
346345

347346
run.NeedApproval = need
348347

349-
if err := run.LoadAttributes(ctx); err != nil {
350-
log.Error("LoadAttributes: %v", err)
351-
continue
352-
}
353-
354-
vars, err := actions_model.GetVariablesOfRun(ctx, run)
355-
if err != nil {
356-
log.Error("GetVariablesOfRun: %v", err)
357-
continue
358-
}
359-
360-
wfRawConcurrency, err := jobparser.ReadWorkflowRawConcurrency(dwf.Content)
361-
if err != nil {
362-
log.Error("ReadWorkflowRawConcurrency: %v", err)
363-
continue
364-
}
365-
if wfRawConcurrency != nil {
366-
err = EvaluateRunConcurrencyFillModel(ctx, run, wfRawConcurrency, vars)
367-
if err != nil {
368-
log.Error("EvaluateRunConcurrencyFillModel: %v", err)
369-
continue
370-
}
371-
}
372-
373-
giteaCtx := GenerateGiteaContext(run, nil)
374-
375-
jobs, err := jobparser.Parse(dwf.Content, jobparser.WithVars(vars), jobparser.WithGitContext(giteaCtx.ToGitHubContext()))
376-
if err != nil {
377-
log.Error("jobparser.Parse: %v", err)
378-
continue
379-
}
380-
381-
if len(jobs) > 0 && jobs[0].RunName != "" {
382-
run.Title = jobs[0].RunName
383-
}
384-
385-
if err := InsertRun(ctx, run, jobs); err != nil {
386-
log.Error("InsertRun: %v", err)
348+
if err := PrepareRun(ctx, dwf.Content, run, nil); err != nil {
349+
log.Error("PrepareRun: %v", err)
387350
continue
388351
}
389352

@@ -392,6 +355,7 @@ func handleWorkflows(
392355
log.Error("FindRunJobs: %v", err)
393356
continue
394357
}
358+
// FIXME PERF skip this for schedule, dispatch etc.
395359
CreateCommitStatus(ctx, alljobs...)
396360
if len(alljobs) > 0 {
397361
job := alljobs[0]
@@ -559,24 +523,6 @@ func handleSchedules(
559523
Content: dwf.Content,
560524
}
561525

562-
vars, err := actions_model.GetVariablesOfRun(ctx, run.ToActionRun())
563-
if err != nil {
564-
log.Error("GetVariablesOfRun: %v", err)
565-
continue
566-
}
567-
568-
giteaCtx := GenerateGiteaContext(run.ToActionRun(), nil)
569-
570-
jobs, err := jobparser.Parse(dwf.Content, jobparser.WithVars(vars), jobparser.WithGitContext(giteaCtx.ToGitHubContext()))
571-
if err != nil {
572-
log.Error("jobparser.Parse: %v", err)
573-
continue
574-
}
575-
576-
if len(jobs) > 0 && jobs[0].RunName != "" {
577-
run.Title = jobs[0].RunName
578-
}
579-
580526
crons = append(crons, run)
581527
}
582528

services/actions/run.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,71 @@ import (
99

1010
actions_model "code.gitea.io/gitea/models/actions"
1111
"code.gitea.io/gitea/models/db"
12+
"code.gitea.io/gitea/modules/log"
1213
"code.gitea.io/gitea/modules/util"
14+
notify_service "code.gitea.io/gitea/services/notify"
1315

1416
"github.com/nektos/act/pkg/jobparser"
1517
"gopkg.in/yaml.v3"
1618
)
1719

20+
// PrepareRun prepares a run before inserting it into the database
21+
// It parses the workflow content, evaluates concurrency if needed, and inserts the run and its jobs into the database.
22+
// The title will be cut off at 255 characters if it's longer than 255 characters.
23+
func PrepareRun(ctx context.Context, content []byte, run *actions_model.ActionRun, inputsWithDefaults map[string]any) error {
24+
if err := run.LoadAttributes(ctx); err != nil {
25+
return fmt.Errorf("LoadAttributes: %w", err)
26+
}
27+
28+
vars, err := actions_model.GetVariablesOfRun(ctx, run)
29+
if err != nil {
30+
return fmt.Errorf("GetVariablesOfRun: %w", err)
31+
}
32+
33+
wfRawConcurrency, err := jobparser.ReadWorkflowRawConcurrency(content)
34+
if err != nil {
35+
return fmt.Errorf("ReadWorkflowRawConcurrency: %w", err)
36+
}
37+
38+
if wfRawConcurrency != nil {
39+
err = EvaluateRunConcurrencyFillModel(ctx, run, wfRawConcurrency, vars)
40+
if err != nil {
41+
return fmt.Errorf("EvaluateRunConcurrencyFillModel: %w", err)
42+
}
43+
}
44+
45+
giteaCtx := GenerateGiteaContext(run, nil)
46+
47+
jobs, err := jobparser.Parse(content, jobparser.WithVars(vars), jobparser.WithGitContext(giteaCtx.ToGitHubContext()), jobparser.WithInputs(inputsWithDefaults))
48+
if err != nil {
49+
return fmt.Errorf("parse workflow: %w", err)
50+
}
51+
52+
if len(jobs) > 0 && jobs[0].RunName != "" {
53+
run.Title = jobs[0].RunName
54+
}
55+
56+
if err := InsertRun(ctx, run, jobs); err != nil {
57+
return fmt.Errorf("InsertRun: %w", err)
58+
}
59+
60+
allJobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{RunID: run.ID})
61+
if err != nil {
62+
log.Error("FindRunJobs: %v", err)
63+
}
64+
err = run.LoadAttributes(ctx)
65+
if err != nil {
66+
log.Error("LoadAttributes: %v", err)
67+
}
68+
notify_service.WorkflowRunStatusUpdate(ctx, run.Repo, run.TriggerUser, run)
69+
for _, job := range allJobs {
70+
notify_service.WorkflowJobStatusUpdate(ctx, run.Repo, run.TriggerUser, job, nil)
71+
}
72+
73+
// Return nil if no errors occurred
74+
return nil
75+
}
76+
1877
// InsertRun inserts a run
1978
// The title will be cut off at 255 characters if it's longer than 255 characters.
2079
func InsertRun(ctx context.Context, run *actions_model.ActionRun, jobs []*jobparser.SingleWorkflow) error {

services/actions/schedule_tasks.go

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ import (
1515
"code.gitea.io/gitea/modules/log"
1616
"code.gitea.io/gitea/modules/timeutil"
1717
webhook_module "code.gitea.io/gitea/modules/webhook"
18-
notify_service "code.gitea.io/gitea/services/notify"
19-
20-
"github.com/nektos/act/pkg/jobparser"
2118
)
2219

2320
// StartScheduleTasks start the task
@@ -119,44 +116,12 @@ func CreateScheduleTask(ctx context.Context, cron *actions_model.ActionSchedule)
119116
Status: actions_model.StatusWaiting,
120117
}
121118

122-
vars, err := actions_model.GetVariablesOfRun(ctx, run)
123-
if err != nil {
124-
log.Error("GetVariablesOfRun: %v", err)
125-
return err
126-
}
127-
128-
// Parse the workflow specification from the cron schedule
129-
workflows, err := jobparser.Parse(cron.Content, jobparser.WithVars(vars))
130-
if err != nil {
131-
return err
132-
}
133-
wfRawConcurrency, err := jobparser.ReadWorkflowRawConcurrency(cron.Content)
134-
if err != nil {
135-
return err
136-
}
137-
if wfRawConcurrency != nil {
138-
err = EvaluateRunConcurrencyFillModel(ctx, run, wfRawConcurrency, vars)
139-
if err != nil {
140-
return fmt.Errorf("EvaluateRunConcurrencyFillModel: %w", err)
141-
}
142-
}
143-
119+
// FIXME cron.Content might be outdated if the workflow file has been changed.
120+
// Load the latest sha from default branch
144121
// Insert the action run and its associated jobs into the database
145-
if err := InsertRun(ctx, run, workflows); err != nil {
122+
if err := PrepareRun(ctx, cron.Content, run, nil); err != nil {
146123
return err
147124
}
148-
allJobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{RunID: run.ID})
149-
if err != nil {
150-
log.Error("FindRunJobs: %v", err)
151-
}
152-
err = run.LoadAttributes(ctx)
153-
if err != nil {
154-
log.Error("LoadAttributes: %v", err)
155-
}
156-
notify_service.WorkflowRunStatusUpdate(ctx, run.Repo, run.TriggerUser, run)
157-
for _, job := range allJobs {
158-
notify_service.WorkflowJobStatusUpdate(ctx, run.Repo, run.TriggerUser, job, nil)
159-
}
160125

161126
// Return nil if no errors occurred
162127
return nil

services/actions/workflow.go

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,18 @@ import (
88
"strings"
99

1010
actions_model "code.gitea.io/gitea/models/actions"
11-
"code.gitea.io/gitea/models/db"
1211
"code.gitea.io/gitea/models/perm"
1312
access_model "code.gitea.io/gitea/models/perm/access"
1413
repo_model "code.gitea.io/gitea/models/repo"
1514
"code.gitea.io/gitea/models/unit"
1615
user_model "code.gitea.io/gitea/models/user"
1716
"code.gitea.io/gitea/modules/actions"
1817
"code.gitea.io/gitea/modules/git"
19-
"code.gitea.io/gitea/modules/log"
2018
"code.gitea.io/gitea/modules/reqctx"
2119
api "code.gitea.io/gitea/modules/structs"
2220
"code.gitea.io/gitea/modules/util"
2321
"code.gitea.io/gitea/services/context"
2422
"code.gitea.io/gitea/services/convert"
25-
notify_service "code.gitea.io/gitea/services/notify"
2623

2724
"github.com/nektos/act/pkg/jobparser"
2825
"github.com/nektos/act/pkg/model"
@@ -98,9 +95,7 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
9895
}
9996

10097
// find workflow from commit
101-
var workflows []*jobparser.SingleWorkflow
10298
var entry *git.TreeEntry
103-
var wfRawConcurrency *model.RawConcurrency
10499

105100
run := &actions_model.ActionRun{
106101
Title: strings.SplitN(runTargetCommit.CommitMessage, "\n", 2)[0],
@@ -153,29 +148,6 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
153148
}
154149
}
155150

156-
giteaCtx := GenerateGiteaContext(run, nil)
157-
158-
workflows, err = jobparser.Parse(content, jobparser.WithGitContext(giteaCtx.ToGitHubContext()), jobparser.WithInputs(inputsWithDefaults))
159-
if err != nil {
160-
return err
161-
}
162-
163-
if len(workflows) > 0 && workflows[0].RunName != "" {
164-
run.Title = workflows[0].RunName
165-
}
166-
167-
if len(workflows) == 0 {
168-
return util.ErrorWrapLocale(
169-
util.NewNotExistErrorf("workflow %q doesn't exist", workflowID),
170-
"actions.workflow.not_found", workflowID,
171-
)
172-
}
173-
174-
wfRawConcurrency, err = jobparser.ReadWorkflowRawConcurrency(content)
175-
if err != nil {
176-
return err
177-
}
178-
179151
// ctx.Req.PostForm -> WorkflowDispatchPayload.Inputs -> ActionRun.EventPayload -> runner: ghc.Event
180152
// https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
181153
// https://docs.github.com/en/webhooks/webhook-events-and-payloads#workflow_dispatch
@@ -193,39 +165,9 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
193165
}
194166
run.EventPayload = string(eventPayload)
195167

196-
// cancel running jobs of the same concurrency group
197-
if wfRawConcurrency != nil {
198-
vars, err := actions_model.GetVariablesOfRun(ctx, run)
199-
if err != nil {
200-
return fmt.Errorf("GetVariablesOfRun: %w", err)
201-
}
202-
err = EvaluateRunConcurrencyFillModel(ctx, run, wfRawConcurrency, vars)
203-
if err != nil {
204-
return fmt.Errorf("EvaluateRunConcurrencyFillModel: %w", err)
205-
}
206-
}
207-
208168
// Insert the action run and its associated jobs into the database
209-
if err := InsertRun(ctx, run, workflows); err != nil {
210-
return fmt.Errorf("InsertRun: %w", err)
211-
}
212-
213-
allJobs, err := db.Find[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{RunID: run.ID})
214-
if err != nil {
215-
log.Error("FindRunJobs: %v", err)
216-
}
217-
CreateCommitStatus(ctx, allJobs...)
218-
if len(allJobs) > 0 {
219-
job := allJobs[0]
220-
err := job.LoadRun(ctx)
221-
if err != nil {
222-
log.Error("LoadRun: %v", err)
223-
} else {
224-
notify_service.WorkflowRunStatusUpdate(ctx, job.Run.Repo, job.Run.TriggerUser, job.Run)
225-
}
226-
}
227-
for _, job := range allJobs {
228-
notify_service.WorkflowJobStatusUpdate(ctx, repo, doer, job, nil)
169+
if err := PrepareRun(ctx, content, run, inputsWithDefaults); err != nil {
170+
return fmt.Errorf("PrepareRun: %w", err)
229171
}
230172
return nil
231173
}

0 commit comments

Comments
 (0)