-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support upload
outputs
and use needs
context (#133)
See [Example usage of the needs context](https://docs.github.com/en/actions/learn-github-actions/contexts#example-usage-of-the-needs-context). Related to: - [actions-proto-def #5](https://gitea.com/gitea/actions-proto-def/pulls/5) - [gitea #24230](go-gitea/gitea#24230) Reviewed-on: https://gitea.com/gitea/act_runner/pulls/133 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: Zettat123 <zettat123@noreply.gitea.io> Co-authored-by: Jason Song <i@wolfogre.com> Co-committed-by: Jason Song <i@wolfogre.com>
- Loading branch information
Showing
4 changed files
with
199 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package run | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"sort" | ||
"strings" | ||
|
||
runnerv1 "code.gitea.io/actions-proto-go/runner/v1" | ||
"github.com/nektos/act/pkg/model" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func generateWorkflow(task *runnerv1.Task) (*model.Workflow, string, error) { | ||
workflow, err := model.ReadWorkflow(bytes.NewReader(task.WorkflowPayload)) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
jobIDs := workflow.GetJobIDs() | ||
if len(jobIDs) != 1 { | ||
return nil, "", fmt.Errorf("multiple jobs found: %v", jobIDs) | ||
} | ||
jobID := jobIDs[0] | ||
|
||
needJobIDs := make([]string, 0, len(task.Needs)) | ||
for id, need := range task.Needs { | ||
needJobIDs = append(needJobIDs, id) | ||
needJob := &model.Job{ | ||
Outputs: need.Outputs, | ||
Result: strings.ToLower(strings.TrimPrefix(need.Result.String(), "RESULT_")), | ||
} | ||
workflow.Jobs[id] = needJob | ||
} | ||
sort.Strings(needJobIDs) | ||
|
||
rawNeeds := yaml.Node{ | ||
Kind: yaml.SequenceNode, | ||
Content: make([]*yaml.Node, 0, len(needJobIDs)), | ||
} | ||
for _, id := range needJobIDs { | ||
rawNeeds.Content = append(rawNeeds.Content, &yaml.Node{ | ||
Kind: yaml.ScalarNode, | ||
Value: id, | ||
}) | ||
} | ||
|
||
workflow.Jobs[jobID].RawNeeds = rawNeeds | ||
|
||
return workflow, jobID, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package run | ||
|
||
import ( | ||
"testing" | ||
|
||
runnerv1 "code.gitea.io/actions-proto-go/runner/v1" | ||
"github.com/nektos/act/pkg/model" | ||
"github.com/stretchr/testify/require" | ||
"gotest.tools/v3/assert" | ||
) | ||
|
||
func Test_generateWorkflow(t *testing.T) { | ||
type args struct { | ||
task *runnerv1.Task | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
assert func(t *testing.T, wf *model.Workflow) | ||
want1 string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "has needs", | ||
args: args{ | ||
task: &runnerv1.Task{ | ||
WorkflowPayload: []byte(` | ||
name: Build and deploy | ||
on: push | ||
jobs: | ||
job9: | ||
needs: build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- run: ./deploy --build ${{ needs.job1.outputs.output1 }} | ||
- run: ./deploy --build ${{ needs.job2.outputs.output2 }} | ||
`), | ||
Needs: map[string]*runnerv1.TaskNeed{ | ||
"job1": { | ||
Outputs: map[string]string{ | ||
"output1": "output1 value", | ||
}, | ||
Result: runnerv1.Result_RESULT_SUCCESS, | ||
}, | ||
"job2": { | ||
Outputs: map[string]string{ | ||
"output2": "output2 value", | ||
}, | ||
Result: runnerv1.Result_RESULT_SUCCESS, | ||
}, | ||
}, | ||
}, | ||
}, | ||
assert: func(t *testing.T, wf *model.Workflow) { | ||
assert.DeepEqual(t, wf.GetJob("job9").Needs(), []string{"job1", "job2"}) | ||
}, | ||
want1: "job9", | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, got1, err := generateWorkflow(tt.args.task) | ||
require.NoError(t, err) | ||
tt.assert(t, got) | ||
assert.Equal(t, got1, tt.want1) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters