Skip to content

Commit 25c4eb1

Browse files
Refactor ActionRunJob parsing into a reusable function (go-gitea#35623)
Use a helper method around the jobparser for parsing a single job structure from an ActionRunJob --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent b029ad4 commit 25c4eb1

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

models/actions/run_job.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/timeutil"
1515
"code.gitea.io/gitea/modules/util"
1616

17+
"github.com/nektos/act/pkg/jobparser"
1718
"xorm.io/builder"
1819
)
1920

@@ -99,6 +100,24 @@ func (job *ActionRunJob) LoadAttributes(ctx context.Context) error {
99100
return job.Run.LoadAttributes(ctx)
100101
}
101102

103+
// ParseJob parses the job structure from the ActionRunJob.WorkflowPayload
104+
func (job *ActionRunJob) ParseJob() (*jobparser.Job, error) {
105+
// job.WorkflowPayload is a SingleWorkflow created from an ActionRun's workflow, which exactly contains this job's YAML definition.
106+
// Ideally it shouldn't be called "Workflow", it is just a job with global workflow fields + trigger
107+
parsedWorkflows, err := jobparser.Parse(job.WorkflowPayload)
108+
if err != nil {
109+
return nil, fmt.Errorf("job %d single workflow: unable to parse: %w", job.ID, err)
110+
} else if len(parsedWorkflows) != 1 {
111+
return nil, fmt.Errorf("job %d single workflow: not single workflow", job.ID)
112+
}
113+
_, workflowJob := parsedWorkflows[0].Job()
114+
if workflowJob == nil {
115+
// it shouldn't happen, and since the callers don't check nil, so return an error instead of nil
116+
return nil, util.ErrorWrap(util.ErrNotExist, "job %d single workflow: payload doesn't contain a job", job.ID)
117+
}
118+
return workflowJob, nil
119+
}
120+
102121
func GetRunJobByID(ctx context.Context, id int64) (*ActionRunJob, error) {
103122
var job ActionRunJob
104123
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&job)

models/actions/task.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
2323
lru "github.com/hashicorp/golang-lru/v2"
24-
"github.com/nektos/act/pkg/jobparser"
2524
"google.golang.org/protobuf/types/known/timestamppb"
2625
"xorm.io/builder"
2726
)
@@ -278,13 +277,10 @@ func CreateTaskForRunner(ctx context.Context, runner *ActionRunner) (*ActionTask
278277
return nil, false, err
279278
}
280279

281-
parsedWorkflows, err := jobparser.Parse(job.WorkflowPayload)
280+
workflowJob, err := job.ParseJob()
282281
if err != nil {
283-
return nil, false, fmt.Errorf("parse workflow of job %d: %w", job.ID, err)
284-
} else if len(parsedWorkflows) != 1 {
285-
return nil, false, fmt.Errorf("workflow of job %d: not single workflow", job.ID)
282+
return nil, false, fmt.Errorf("load job %d: %w", job.ID, err)
286283
}
287-
_, workflowJob := parsedWorkflows[0].Job()
288284

289285
if _, err := e.Insert(task); err != nil {
290286
return nil, false, err

services/actions/concurrency.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package actions
55

66
import (
77
"context"
8-
"errors"
98
"fmt"
109

1110
actions_model "code.gitea.io/gitea/models/actions"
@@ -91,17 +90,12 @@ func EvaluateJobConcurrencyFillModel(ctx context.Context, run *actions_model.Act
9190
return fmt.Errorf("get inputs: %w", err)
9291
}
9392

94-
// singleWorkflows is created from an ActionJob, which always contains exactly a single job's YAML definition.
95-
// Ideally it shouldn't be called "Workflow", it is just a job with global workflow fields + trigger
96-
singleWorkflows, err := jobparser.Parse(actionRunJob.WorkflowPayload)
93+
workflowJob, err := actionRunJob.ParseJob()
9794
if err != nil {
98-
return fmt.Errorf("parse single workflow: %w", err)
99-
} else if len(singleWorkflows) != 1 {
100-
return errors.New("not single workflow")
95+
return fmt.Errorf("load job %d: %w", actionRunJob.ID, err)
10196
}
102-
_, singleWorkflowJob := singleWorkflows[0].Job()
10397

104-
actionRunJob.ConcurrencyGroup, actionRunJob.ConcurrencyCancel, err = jobparser.EvaluateConcurrency(&rawConcurrency, actionRunJob.JobID, singleWorkflowJob, actionsJobCtx, jobResults, vars, inputs)
98+
actionRunJob.ConcurrencyGroup, actionRunJob.ConcurrencyCancel, err = jobparser.EvaluateConcurrency(&rawConcurrency, actionRunJob.JobID, workflowJob, actionsJobCtx, jobResults, vars, inputs)
10599
if err != nil {
106100
return fmt.Errorf("evaluate concurrency: %w", err)
107101
}

services/actions/job_emitter.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"code.gitea.io/gitea/modules/util"
1919
notify_service "code.gitea.io/gitea/services/notify"
2020

21-
"github.com/nektos/act/pkg/jobparser"
2221
"xorm.io/builder"
2322
)
2423

@@ -305,9 +304,9 @@ func (r *jobStatusResolver) resolveCheckNeeds(id int64) (allDone, allSucceed boo
305304
}
306305

307306
func (r *jobStatusResolver) resolveJobHasIfCondition(actionRunJob *actions_model.ActionRunJob) (hasIf bool) {
308-
if wfJobs, _ := jobparser.Parse(actionRunJob.WorkflowPayload); len(wfJobs) == 1 {
309-
_, wfJob := wfJobs[0].Job()
310-
hasIf = len(wfJob.If.Value) > 0
307+
// FIXME evaluate this on the server side
308+
if job, err := actionRunJob.ParseJob(); err == nil {
309+
return len(job.If.Value) > 0
311310
}
312311
return hasIf
313312
}

0 commit comments

Comments
 (0)