1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-09-15 18:57:01 +00:00

feat: support evaluation of concurrency clauses in runner (#827)

Pre-req for support `concurrency` clauses for Gitea Actions, later added to Gitea in PR: https://github.com/go-gitea/gitea/pull/32751. Unit tests added in this PR relative to upstream.

Squashes PRs https://gitea.com/gitea/act/pulls/124 & https://gitea.com/gitea/act/pulls/139, as noted in https://code.forgejo.org/forgejo/runner/issues/678

Reviewed-on: https://gitea.com/gitea/act/pulls/124
Reviewed-by: Lunny Xiao <lunny@noreply.gitea.com>
Co-authored-by: Zettat123 <zettat123@gmail.com>
Co-committed-by: Zettat123 <zettat123@gmail.com>
Reviewed-on: https://gitea.com/gitea/act/pulls/139
Reviewed-by: Zettat123 <zettat123@noreply.gitea.com>
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: ChristopherHX <christopher.homberger@web.de>
Co-committed-by: ChristopherHX <christopher.homberger@web.de>

<!--start release-notes-assistant-->
<!--URL:https://code.forgejo.org/forgejo/runner-->
- features
  - [PR](https://code.forgejo.org/forgejo/runner/pulls/827): <!--number 827 --><!--line 0 --><!--description ZmVhdDogc3VwcG9ydCBldmFsdWF0aW9uIG9mIGNvbmN1cnJlbmN5IGNsYXVzZXMgaW4gcnVubmVy-->feat: support evaluation of concurrency clauses in runner<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/827
Reviewed-by: earl-warren <earl-warren@noreply.code.forgejo.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
This commit is contained in:
Mathieu Fenniak 2025-08-07 21:47:01 +00:00 committed by earl-warren
parent 3f468733cb
commit 7a31b6a55e
No known key found for this signature in database
GPG key ID: F128CBE6AB3A7201
12 changed files with 330 additions and 4 deletions

View file

@ -25,7 +25,8 @@ type Workflow struct {
Jobs map[string]*Job `yaml:"jobs"`
Defaults Defaults `yaml:"defaults"`
RawNotifications yaml.Node `yaml:"enable-email-notifications"`
RawNotifications yaml.Node `yaml:"enable-email-notifications"`
RawConcurrency *RawConcurrency `yaml:"concurrency"` // For Gitea
}
// On events for the workflow
@ -806,3 +807,28 @@ func (w *Workflow) Notifications() (bool, error) {
return false, fmt.Errorf("enable-email-notifications: unknown type: %v", w.RawNotifications.Kind)
}
}
// For Gitea
// RawConcurrency represents a workflow concurrency or a job concurrency with uninterpolated options
type RawConcurrency struct {
Group string `yaml:"group,omitempty"`
CancelInProgress string `yaml:"cancel-in-progress,omitempty"`
RawExpression string `yaml:"-,omitempty"`
}
type objectConcurrency RawConcurrency
func (r *RawConcurrency) UnmarshalYAML(n *yaml.Node) error {
if err := n.Decode(&r.RawExpression); err == nil {
return nil
}
return n.Decode((*objectConcurrency)(r))
}
func (r *RawConcurrency) MarshalYAML() (interface{}, error) {
if r.RawExpression != "" {
return r.RawExpression, nil
}
return (*objectConcurrency)(r), nil
}

View file

@ -702,3 +702,48 @@ func TestStepUsesHash(t *testing.T) {
})
}
}
func TestReadWorkflow_Concurrency(t *testing.T) {
for _, testCase := range []struct {
expected *RawConcurrency
err string
snippet string
}{
{
expected: nil,
snippet: "# nothing",
},
{
expected: &RawConcurrency{Group: "${{ github.workflow }}-${{ github.ref }}", CancelInProgress: ""},
snippet: "concurrency: { group: \"${{ github.workflow }}-${{ github.ref }}\" }",
},
{
expected: &RawConcurrency{Group: "example-group", CancelInProgress: "true"},
snippet: "concurrency: { group: example-group, cancel-in-progress: true }",
},
} {
t.Run(testCase.snippet, func(t *testing.T) {
yaml := fmt.Sprintf(`
name: name-455
on: push
%s
jobs:
valid-JOB-Name-455:
runs-on: docker
steps:
- run: echo hi
`, testCase.snippet)
workflow, err := ReadWorkflow(strings.NewReader(yaml), true)
if testCase.err != "" {
assert.ErrorContains(t, err, testCase.err)
} else {
assert.NoError(t, err, "read workflow should succeed")
concurrency := workflow.RawConcurrency
// assert.NoError(t, err)
assert.Equal(t, testCase.expected, concurrency)
}
})
}
}