mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2024-12-18 20:02:45 +03:00
83ec0ba909
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](https://github.com/go-gitea/gitea/pull/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>
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
// 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
|
|
}
|