Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The job should always run when if is always() #29464

Merged
merged 10 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ require (
github.com/prometheus/client_golang v1.18.0
github.com/quasoft/websspi v1.1.2
github.com/redis/go-redis/v9 v9.4.0
github.com/rhysd/actionlint v1.6.26
github.com/robfig/cron/v3 v3.0.1
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/sassoftware/go-rpmutils v0.2.1-0.20240124161140-277b154961dd
Expand Down Expand Up @@ -249,7 +250,6 @@ require (
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.46.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rhysd/actionlint v1.6.26 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/xid v1.5.0 // indirect
Expand Down
37 changes: 36 additions & 1 deletion services/actions/job_emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
"context"
"errors"
"fmt"
"strings"

actions_model "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/queue"

"github.com/nektos/act/pkg/jobparser"
"github.com/rhysd/actionlint"
"xorm.io/builder"
)

Expand Down Expand Up @@ -76,12 +79,15 @@
type jobStatusResolver struct {
statuses map[int64]actions_model.Status
needs map[int64][]int64
jobMap map[int64]*actions_model.ActionRunJob
}

func newJobStatusResolver(jobs actions_model.ActionJobList) *jobStatusResolver {
idToJobs := make(map[string][]*actions_model.ActionRunJob, len(jobs))
jobMap := make(map[int64]*actions_model.ActionRunJob)
for _, job := range jobs {
idToJobs[job.JobID] = append(idToJobs[job.JobID], job)
jobMap[job.ID] = job
}

statuses := make(map[int64]actions_model.Status, len(jobs))
Expand All @@ -97,6 +103,7 @@
return &jobStatusResolver{
statuses: statuses,
needs: needs,
jobMap: jobMap,
}
}

Expand Down Expand Up @@ -135,7 +142,35 @@
if allSucceed {
ret[id] = actions_model.StatusWaiting
} else {
ret[id] = actions_model.StatusSkipped
// If a job's "if" condition is "always()", the job should always run even if some of its dependencies did not succeed.
// See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idneeds
always := false
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
wfJobs, _ := jobparser.Parse(r.jobMap[id].WorkflowPayload)
if len(wfJobs) == 1 {
_, wfJob := wfJobs[0].Job()
if wfJob.If.Value != "" {
// We use "actionlint" to check whether the value of "if" is the "always()" function
value := strings.TrimPrefix(wfJob.If.Value, "${{")
if !strings.HasSuffix(value, "}}") {
// "}}" is necessary since lexer lexes it as end of tokens
// See https://github.com/rhysd/actionlint/blob/3e2f8eab86d3490068c620638bb2955598438492/rule_expression.go#L622
value = value + "}}"

Check failure on line 157 in services/actions/job_emitter.go

View workflow job for this annotation

GitHub Actions / lint-backend

assignOp: replace `value = value + "}}"` with `value += "}}"` (gocritic)

Check failure on line 157 in services/actions/job_emitter.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

assignOp: replace `value = value + "}}"` with `value += "}}"` (gocritic)

Check failure on line 157 in services/actions/job_emitter.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

assignOp: replace `value = value + "}}"` with `value += "}}"` (gocritic)
}
exprParser := actionlint.NewExprParser()
exprNode, _ := exprParser.Parse(actionlint.NewExprLexer(value))
if funcNode, ok := (exprNode).(*actionlint.FuncCallNode); ok {
if funcNode.Callee == "always" {
always = true
}
}
}
}

if always {
ret[id] = actions_model.StatusWaiting
} else {
ret[id] = actions_model.StatusSkipped
}
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions services/actions/job_emitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,33 @@ func Test_jobStatusResolver_Resolve(t *testing.T) {
},
want: map[int64]actions_model.Status{},
},
{
name: "with ${{ always() }} condition",
jobs: actions_model.ActionJobList{
{ID: 1, JobID: "job1", Status: actions_model.StatusFailure, Needs: []string{}},
{ID: 2, JobID: "job2", Status: actions_model.StatusBlocked, Needs: []string{"job1"}, WorkflowPayload: []byte(
"name: test\non: push\njobs:\n job2:\n runs-on: ubuntu-latest\n needs: job1\n if: ${{ always() }}\n steps:\n - run: echo \"always run\"")},
Zettat123 marked this conversation as resolved.
Show resolved Hide resolved
},
want: map[int64]actions_model.Status{2: actions_model.StatusWaiting},
},
{
name: "with always() condition",
jobs: actions_model.ActionJobList{
{ID: 1, JobID: "job1", Status: actions_model.StatusFailure, Needs: []string{}},
{ID: 2, JobID: "job2", Status: actions_model.StatusBlocked, Needs: []string{"job1"}, WorkflowPayload: []byte(
"name: test\non: push\njobs:\n job2:\n runs-on: ubuntu-latest\n needs: job1\n if: always()\n steps:\n - run: echo \"always run\"")},
},
want: map[int64]actions_model.Status{2: actions_model.StatusWaiting},
},
{
name: "without always() condition",
jobs: actions_model.ActionJobList{
{ID: 1, JobID: "job1", Status: actions_model.StatusFailure, Needs: []string{}},
{ID: 2, JobID: "job2", Status: actions_model.StatusBlocked, Needs: []string{"job1"}, WorkflowPayload: []byte(
"name: test\non: push\njobs:\n job2:\n runs-on: ubuntu-latest\n needs: job1\n steps:\n - run: echo \"always run\"")},
},
want: map[int64]actions_model.Status{2: actions_model.StatusSkipped},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading