mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-16 12:53:42 +00:00
refactor: move act/model and act/exprparser to actionslib (#1143)
Gitea needs the workflow model and the expression evaluator to parse workflows and to build the task payload this runner consumes, so today it depends on `gitea.com/gitea/runner` just for `act/model` and `act/exprparser`. Both packages now live in `gitea.dev/actionslib` (`pkg/model`, `pkg/exprparser`), the module both sides already share, and this repository consumes them from there. ### Changes - `act/model` and `act/exprparser` are deleted, all imports point at `gitea.dev/actionslib/pkg/...`. - New `act/ghcontext` package: the `GithubContext` helpers that need a git checkout on disk (`SetRef`, `SetSha`, `SetRepositoryAndOwner`) are runner only and would drag a git client plus the act context logger into the shared module, so they stay here as functions, with their tests. Only caller is `RunContext.getGithubContext`. - `act/common.CartesianProduct` moved to the shared model package, `act/model` was its only user. - `act/model/testdata/container-volumes` moved to `act/runner/testdata/container-volumes`, its only user is `runner_test.go`. - `internal/pkg/client.UUIDHeader` / `TokenHeader` now alias `pkg/protocol`, so the header names cannot drift apart from Gitea. ### Notes - No behaviour change intended: the moved files are unchanged apart from the import paths and the split described above. - `go.mod` depends on the released `gitea.dev/actionslib v0.7.0`, which carries both https://gitea.com/gitea/actionslib/pulls/11 and the `model.UsesHash` port in https://gitea.com/gitea/actionslib/pulls/14 that `main` needs after https://gitea.com/gitea/runner/pulls/1150. - Verified with `go build ./...`, `go vet ./...` and `go test ./act/... ./internal/...`; the docker based `act/runner` integration tests (`TestRunEvent`, `TestRunMatrixWithUserDefinedInclusions`) fail identically with and without this change in my environment. Assisted-by: Codet:GPT-5.1-Codex Reviewed-on: https://gitea.com/gitea/runner/pulls/1143 Reviewed-by: silverwind <[email protected]>
This commit is contained in:
1 parent
da9b559fb5
commit
a8dcd5b67c
87 files changed
+247
-5735
No files matched your search
@@ -1,60 +0,0 @@
|
|||||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
||||||
// Copyright 2020 The nektos/act Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package common
|
|
||||||
|
|
||||||
import "slices"
|
|
||||||
|
|
||||||
// CartesianProduct takes map of lists and returns list of unique tuples
|
|
||||||
func CartesianProduct(mapOfLists map[string][]any) []map[string]any {
|
|
||||||
listNames := make([]string, 0)
|
|
||||||
lists := make([][]any, 0)
|
|
||||||
for k, v := range mapOfLists {
|
|
||||||
listNames = append(listNames, k)
|
|
||||||
lists = append(lists, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
listCart := cartN(lists...)
|
|
||||||
|
|
||||||
rtn := make([]map[string]any, 0)
|
|
||||||
for _, list := range listCart {
|
|
||||||
vMap := make(map[string]any)
|
|
||||||
for i, v := range list {
|
|
||||||
vMap[listNames[i]] = v
|
|
||||||
}
|
|
||||||
rtn = append(rtn, vMap)
|
|
||||||
}
|
|
||||||
return rtn
|
|
||||||
}
|
|
||||||
|
|
||||||
func cartN(a ...[]any) [][]any {
|
|
||||||
c := 1
|
|
||||||
for _, a := range a {
|
|
||||||
c *= len(a)
|
|
||||||
}
|
|
||||||
if c == 0 || len(a) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
p := make([][]any, c)
|
|
||||||
b := make([]any, c*len(a))
|
|
||||||
n := make([]int, len(a))
|
|
||||||
s := 0
|
|
||||||
for i := range p {
|
|
||||||
e := s + len(a)
|
|
||||||
pi := b[s:e]
|
|
||||||
p[i] = pi
|
|
||||||
s = e
|
|
||||||
for j, n := range n {
|
|
||||||
pi[j] = a[j][n]
|
|
||||||
}
|
|
||||||
for j := range slices.Backward(n) {
|
|
||||||
n[j]++
|
|
||||||
if n[j] < len(a[j]) {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
n[j] = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return p
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
||||||
// Copyright 2020 The nektos/act Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package common
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestCartesianProduct(t *testing.T) {
|
|
||||||
assert := assert.New(t)
|
|
||||||
input := map[string][]any{
|
|
||||||
"foo": {1, 2, 3, 4},
|
|
||||||
"bar": {"a", "b", "c"},
|
|
||||||
"baz": {false, true},
|
|
||||||
}
|
|
||||||
|
|
||||||
output := CartesianProduct(input)
|
|
||||||
assert.Len(output, 24)
|
|
||||||
|
|
||||||
for _, v := range output {
|
|
||||||
assert.Len(v, 3)
|
|
||||||
|
|
||||||
assert.Contains(v, "foo")
|
|
||||||
assert.Contains(v, "bar")
|
|
||||||
assert.Contains(v, "baz")
|
|
||||||
}
|
|
||||||
|
|
||||||
input = map[string][]any{
|
|
||||||
"foo": {1, 2, 3, 4},
|
|
||||||
"bar": {},
|
|
||||||
"baz": {false, true},
|
|
||||||
}
|
|
||||||
output = CartesianProduct(input)
|
|
||||||
assert.Empty(output)
|
|
||||||
|
|
||||||
input = map[string][]any{}
|
|
||||||
output = CartesianProduct(input)
|
|
||||||
assert.Empty(output)
|
|
||||||
}
|
|
||||||
@@ -1,309 +0,0 @@
|
|||||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
||||||
// Copyright 2022 The nektos/act Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package exprparser
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"io/fs"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"reflect"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
|
|
||||||
"github.com/rhysd/actionlint"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (impl *interperterImpl) contains(search, item reflect.Value) (bool, error) {
|
|
||||||
switch search.Kind() {
|
|
||||||
case reflect.String, reflect.Int, reflect.Float64, reflect.Bool, reflect.Invalid:
|
|
||||||
return strings.Contains(
|
|
||||||
strings.ToLower(CoerceToString(search)),
|
|
||||||
strings.ToLower(CoerceToString(item)),
|
|
||||||
), nil
|
|
||||||
|
|
||||||
case reflect.Slice:
|
|
||||||
for i := 0; i < search.Len(); i++ {
|
|
||||||
arrayItem := search.Index(i).Elem()
|
|
||||||
result, err := impl.compareValues(arrayItem, item, actionlint.CompareOpNodeKindEq)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if isEqual, ok := result.(bool); ok && isEqual {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *interperterImpl) startsWith(searchString, searchValue reflect.Value) (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
|
||||||
return strings.HasPrefix(
|
|
||||||
strings.ToLower(CoerceToString(searchString)),
|
|
||||||
strings.ToLower(CoerceToString(searchValue)),
|
|
||||||
), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *interperterImpl) endsWith(searchString, searchValue reflect.Value) (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
|
||||||
return strings.HasSuffix(
|
|
||||||
strings.ToLower(CoerceToString(searchString)),
|
|
||||||
strings.ToLower(CoerceToString(searchValue)),
|
|
||||||
), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
passThrough = iota
|
|
||||||
bracketOpen
|
|
||||||
bracketClose
|
|
||||||
)
|
|
||||||
|
|
||||||
func (impl *interperterImpl) format(str reflect.Value, replaceValue ...reflect.Value) (string, error) {
|
|
||||||
input := CoerceToString(str)
|
|
||||||
var output strings.Builder
|
|
||||||
replacementIndex := ""
|
|
||||||
|
|
||||||
state := passThrough
|
|
||||||
for _, character := range input {
|
|
||||||
switch state {
|
|
||||||
case passThrough: // normal buffer output
|
|
||||||
switch character {
|
|
||||||
case '{':
|
|
||||||
state = bracketOpen
|
|
||||||
|
|
||||||
case '}':
|
|
||||||
state = bracketClose
|
|
||||||
|
|
||||||
default:
|
|
||||||
output.WriteRune(character)
|
|
||||||
}
|
|
||||||
|
|
||||||
case bracketOpen: // found {
|
|
||||||
switch character {
|
|
||||||
case '{':
|
|
||||||
output.WriteString("{")
|
|
||||||
replacementIndex = ""
|
|
||||||
state = passThrough
|
|
||||||
|
|
||||||
case '}':
|
|
||||||
index, err := strconv.ParseInt(replacementIndex, 10, 32)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("The following format string is invalid: '%s'", input)
|
|
||||||
}
|
|
||||||
|
|
||||||
replacementIndex = ""
|
|
||||||
|
|
||||||
if len(replaceValue) <= int(index) {
|
|
||||||
return "", fmt.Errorf("The following format string references more arguments than were supplied: '%s'", input)
|
|
||||||
}
|
|
||||||
|
|
||||||
output.WriteString(CoerceToString(replaceValue[index]))
|
|
||||||
|
|
||||||
state = passThrough
|
|
||||||
|
|
||||||
default:
|
|
||||||
replacementIndex += string(character)
|
|
||||||
}
|
|
||||||
|
|
||||||
case bracketClose: // found }
|
|
||||||
switch character {
|
|
||||||
case '}':
|
|
||||||
output.WriteString("}")
|
|
||||||
replacementIndex = ""
|
|
||||||
state = passThrough
|
|
||||||
|
|
||||||
default:
|
|
||||||
return "", fmt.Errorf("Closing bracket without opening one. The following format string is invalid: '%s'", input)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if state != passThrough {
|
|
||||||
switch state {
|
|
||||||
case bracketOpen:
|
|
||||||
return "", fmt.Errorf("Unclosed brackets. The following format string is invalid: '%s'", input)
|
|
||||||
|
|
||||||
case bracketClose:
|
|
||||||
return "", fmt.Errorf("Closing bracket without opening one. The following format string is invalid: '%s'", input)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return output.String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *interperterImpl) join(array, sep reflect.Value) (string, error) { //nolint:unparam // pre-existing issue from nektos/act
|
|
||||||
separator := CoerceToString(sep)
|
|
||||||
switch array.Kind() {
|
|
||||||
case reflect.Slice:
|
|
||||||
var items []string
|
|
||||||
for i := 0; i < array.Len(); i++ {
|
|
||||||
items = append(items, CoerceToString(array.Index(i)))
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.Join(items, separator), nil
|
|
||||||
default:
|
|
||||||
return strings.Join([]string{CoerceToString(array)}, separator), nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *interperterImpl) toJSON(value reflect.Value) (string, error) {
|
|
||||||
if value.Kind() == reflect.Invalid {
|
|
||||||
return "null", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
json, err := json.MarshalIndent(value.Interface(), "", " ")
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("Cannot convert value to JSON. Cause: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return string(json), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *interperterImpl) fromJSON(value reflect.Value) (any, error) {
|
|
||||||
if value.Kind() != reflect.String {
|
|
||||||
return nil, fmt.Errorf("Cannot parse non-string type %v as JSON", value.Kind())
|
|
||||||
}
|
|
||||||
|
|
||||||
var data any
|
|
||||||
|
|
||||||
err := json.Unmarshal([]byte(value.String()), &data)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Invalid JSON: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return data, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *interperterImpl) hashFiles(paths ...reflect.Value) (string, error) {
|
|
||||||
var ps []gitignore.Pattern
|
|
||||||
|
|
||||||
const cwdPrefix = "." + string(filepath.Separator)
|
|
||||||
const excludeCwdPrefix = "!" + cwdPrefix
|
|
||||||
for _, path := range paths {
|
|
||||||
if path.Kind() == reflect.String {
|
|
||||||
cleanPath := path.String()
|
|
||||||
if strings.HasPrefix(cleanPath, cwdPrefix) {
|
|
||||||
cleanPath = cleanPath[len(cwdPrefix):]
|
|
||||||
} else if strings.HasPrefix(cleanPath, excludeCwdPrefix) {
|
|
||||||
cleanPath = "!" + cleanPath[len(excludeCwdPrefix):]
|
|
||||||
}
|
|
||||||
ps = append(ps, gitignore.ParsePattern(cleanPath, nil))
|
|
||||||
} else {
|
|
||||||
return "", errors.New("Non-string path passed to hashFiles")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
matcher := gitignore.NewMatcher(ps)
|
|
||||||
|
|
||||||
var files []string
|
|
||||||
if err := filepath.Walk(impl.config.WorkingDir, func(path string, fi fs.FileInfo, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
sansPrefix := strings.TrimPrefix(path, impl.config.WorkingDir+string(filepath.Separator))
|
|
||||||
parts := strings.Split(sansPrefix, string(filepath.Separator))
|
|
||||||
if fi.IsDir() || !matcher.Match(parts, fi.IsDir()) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
files = append(files, path)
|
|
||||||
return nil
|
|
||||||
}); err != nil {
|
|
||||||
return "", fmt.Errorf("Unable to filepath.Walk: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(files) == 0 {
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
hasher := sha256.New()
|
|
||||||
|
|
||||||
for _, file := range files {
|
|
||||||
f, err := os.Open(file)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("Unable to os.Open: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := io.Copy(hasher, f); err != nil {
|
|
||||||
return "", fmt.Errorf("Unable to io.Copy: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := f.Close(); err != nil {
|
|
||||||
return "", fmt.Errorf("Unable to Close file: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return hex.EncodeToString(hasher.Sum(nil)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *interperterImpl) getNeedsTransitive(job *model.Job) []string {
|
|
||||||
needs := job.Needs()
|
|
||||||
|
|
||||||
for _, need := range needs {
|
|
||||||
parentNeeds := impl.getNeedsTransitive(impl.config.Run.Workflow.GetJob(need))
|
|
||||||
needs = append(needs, parentNeeds...)
|
|
||||||
}
|
|
||||||
|
|
||||||
return needs
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *interperterImpl) always() (bool, error) {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *interperterImpl) jobSuccess() (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
|
||||||
jobs := impl.config.Run.Workflow.Jobs
|
|
||||||
jobNeeds := impl.getNeedsTransitive(impl.config.Run.Job())
|
|
||||||
|
|
||||||
for _, needs := range jobNeeds {
|
|
||||||
if jobs[needs].NeedsResult() != "success" {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// jobStatus returns the current job status, treating a nil Job context as an
|
|
||||||
// empty status so status-check functions never panic on a nil dereference.
|
|
||||||
func (impl *interperterImpl) jobStatus() string {
|
|
||||||
if impl.env.Job == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return impl.env.Job.Status
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *interperterImpl) stepSuccess() (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
|
||||||
return impl.jobStatus() == "success", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *interperterImpl) jobFailure() (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
|
||||||
jobs := impl.config.Run.Workflow.Jobs
|
|
||||||
jobNeeds := impl.getNeedsTransitive(impl.config.Run.Job())
|
|
||||||
|
|
||||||
for _, needs := range jobNeeds {
|
|
||||||
if jobs[needs].NeedsResult() == "failure" {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *interperterImpl) stepFailure() (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
|
||||||
return impl.jobStatus() == "failure", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (impl *interperterImpl) cancelled() (bool, error) { //nolint:unparam // pre-existing issue from nektos/act
|
|
||||||
return impl.jobStatus() == "cancelled", nil
|
|
||||||
}
|
|
||||||
@@ -1,283 +0,0 @@
|
|||||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
||||||
// Copyright 2022 The nektos/act Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package exprparser
|
|
||||||
|
|
||||||
import (
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestFunctionContains(t *testing.T) {
|
|
||||||
table := []struct {
|
|
||||||
input string
|
|
||||||
expected any
|
|
||||||
name string
|
|
||||||
}{
|
|
||||||
{"contains('search', 'item') }}", false, "contains-str-str"},
|
|
||||||
{`cOnTaInS('Hello', 'll') }}`, true, "contains-str-casing"},
|
|
||||||
{`contains('HELLO', 'll') }}`, true, "contains-str-casing"},
|
|
||||||
{`contains('3.141592', 3.14) }}`, true, "contains-str-number"},
|
|
||||||
{`contains(3.141592, '3.14') }}`, true, "contains-number-str"},
|
|
||||||
{`contains(3.141592, 3.14) }}`, true, "contains-number-number"},
|
|
||||||
{`contains(true, 'u') }}`, true, "contains-bool-str"},
|
|
||||||
{`contains(null, '') }}`, true, "contains-null-str"},
|
|
||||||
{`contains(fromJSON('["first","second"]'), 'first') }}`, true, "contains-item"},
|
|
||||||
{`contains(fromJSON('[null,"second"]'), '') }}`, true, "contains-item-null-empty-str"},
|
|
||||||
{`contains(fromJSON('["","second"]'), null) }}`, true, "contains-item-empty-str-null"},
|
|
||||||
{`contains(fromJSON('[true,"second"]'), 'true') }}`, false, "contains-item-bool-arr"},
|
|
||||||
{`contains(fromJSON('["true","second"]'), true) }}`, false, "contains-item-str-bool"},
|
|
||||||
{`contains(fromJSON('[3.14,"second"]'), '3.14') }}`, true, "contains-item-number-str"},
|
|
||||||
{`contains(fromJSON('[3.14,"second"]'), 3.14) }}`, true, "contains-item-number-number"},
|
|
||||||
{`contains(fromJSON('["","second"]'), fromJSON('[]')) }}`, false, "contains-item-str-arr"},
|
|
||||||
{`contains(fromJSON('["","second"]'), fromJSON('{}')) }}`, false, "contains-item-str-obj"},
|
|
||||||
}
|
|
||||||
|
|
||||||
env := &EvaluationEnvironment{}
|
|
||||||
|
|
||||||
for _, tt := range table {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFunctionStartsWith(t *testing.T) {
|
|
||||||
table := []struct {
|
|
||||||
input string
|
|
||||||
expected any
|
|
||||||
name string
|
|
||||||
}{
|
|
||||||
{"startsWith('search', 'se') }}", true, "startswith-string"},
|
|
||||||
{"startsWith('search', 'sa') }}", false, "startswith-string"},
|
|
||||||
{"startsWith('123search', '123s') }}", true, "startswith-string"},
|
|
||||||
{"startsWith(123, 's') }}", false, "startswith-string"},
|
|
||||||
{"startsWith(123, '12') }}", true, "startswith-string"},
|
|
||||||
{"startsWith('123', 12) }}", true, "startswith-string"},
|
|
||||||
{"startsWith(null, '42') }}", false, "startswith-string"},
|
|
||||||
{"startsWith('null', null) }}", true, "startswith-string"},
|
|
||||||
{"startsWith('null', '') }}", true, "startswith-string"},
|
|
||||||
}
|
|
||||||
|
|
||||||
env := &EvaluationEnvironment{}
|
|
||||||
|
|
||||||
for _, tt := range table {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFunctionEndsWith(t *testing.T) {
|
|
||||||
table := []struct {
|
|
||||||
input string
|
|
||||||
expected any
|
|
||||||
name string
|
|
||||||
}{
|
|
||||||
{"endsWith('search', 'ch') }}", true, "endsWith-string"},
|
|
||||||
{"endsWith('search', 'sa') }}", false, "endsWith-string"},
|
|
||||||
{"endsWith('search123s', '123s') }}", true, "endsWith-string"},
|
|
||||||
{"endsWith(123, 's') }}", false, "endsWith-string"},
|
|
||||||
{"endsWith(123, '23') }}", true, "endsWith-string"},
|
|
||||||
{"endsWith('123', 23) }}", true, "endsWith-string"},
|
|
||||||
{"endsWith(null, '42') }}", false, "endsWith-string"},
|
|
||||||
{"endsWith('null', null) }}", true, "endsWith-string"},
|
|
||||||
{"endsWith('null', '') }}", true, "endsWith-string"},
|
|
||||||
}
|
|
||||||
|
|
||||||
env := &EvaluationEnvironment{}
|
|
||||||
|
|
||||||
for _, tt := range table {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFunctionJoin(t *testing.T) {
|
|
||||||
table := []struct {
|
|
||||||
input string
|
|
||||||
expected any
|
|
||||||
name string
|
|
||||||
}{
|
|
||||||
{"join(fromJSON('[\"a\", \"b\"]'), ',')", "a,b", "join-arr"},
|
|
||||||
{"join('string', ',')", "string", "join-str"},
|
|
||||||
{"join(1, ',')", "1", "join-number"},
|
|
||||||
{"join(null, ',')", "", "join-number"},
|
|
||||||
{"join(fromJSON('[\"a\", \"b\", null]'), null)", "ab", "join-number"},
|
|
||||||
{"join(fromJSON('[\"a\", \"b\"]'))", "a,b", "join-number"},
|
|
||||||
{"join(fromJSON('[\"a\", \"b\", null]'), 1)", "a1b1", "join-number"},
|
|
||||||
{"join(fromJSON('[1, true, null]'), '-')", "1-true-", "join-mixed-types"},
|
|
||||||
}
|
|
||||||
|
|
||||||
env := &EvaluationEnvironment{}
|
|
||||||
|
|
||||||
for _, tt := range table {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFunctionToJSON(t *testing.T) {
|
|
||||||
table := []struct {
|
|
||||||
input string
|
|
||||||
expected any
|
|
||||||
name string
|
|
||||||
}{
|
|
||||||
{"toJSON(env) }}", "{\n \"key\": \"value\"\n}", "toJSON"},
|
|
||||||
{"toJSON(null)", "null", "toJSON-null"},
|
|
||||||
}
|
|
||||||
|
|
||||||
env := &EvaluationEnvironment{
|
|
||||||
Env: map[string]string{
|
|
||||||
"key": "value",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range table {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFunctionFromJSON(t *testing.T) {
|
|
||||||
table := []struct {
|
|
||||||
input string
|
|
||||||
expected any
|
|
||||||
name string
|
|
||||||
}{
|
|
||||||
{"fromJSON('{\"foo\":\"bar\"}') }}", map[string]any{
|
|
||||||
"foo": "bar",
|
|
||||||
}, "fromJSON"},
|
|
||||||
}
|
|
||||||
|
|
||||||
env := &EvaluationEnvironment{}
|
|
||||||
|
|
||||||
for _, tt := range table {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFunctionHashFiles(t *testing.T) {
|
|
||||||
table := []struct {
|
|
||||||
input string
|
|
||||||
expected any
|
|
||||||
name string
|
|
||||||
}{
|
|
||||||
{"hashFiles('**/non-extant-files') }}", "", "hash-non-existing-file"},
|
|
||||||
{"hashFiles('**/non-extant-files', '**/more-non-extant-files') }}", "", "hash-multiple-non-existing-files"},
|
|
||||||
{"hashFiles('./for-hashing-1.txt') }}", "66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18", "hash-single-file"},
|
|
||||||
{"hashFiles('./for-hashing-*.txt') }}", "8e5935e7e13368cd9688fe8f48a0955293676a021562582c7e848dafe13fb046", "hash-multiple-files"},
|
|
||||||
{"hashFiles('./for-hashing-*.txt', '!./for-hashing-2.txt') }}", "66a045b452102c59d840ec097d59d9467e13a3f34f6494e539ffd32c1bb35f18", "hash-negative-pattern"},
|
|
||||||
{"hashFiles('./for-hashing-**') }}", "c418ba693753c84115ced0da77f876cddc662b9054f4b129b90f822597ee2f94", "hash-multiple-files-and-directories"},
|
|
||||||
{"hashFiles('./for-hashing-3/**') }}", "6f5696b546a7a9d6d42a449dc9a56bef244aaa826601ef27466168846139d2c2", "hash-nested-directories"},
|
|
||||||
{"hashFiles('./for-hashing-3/**/nested-data.txt') }}", "8ecadfb49f7f978d0a9f3a957e9c8da6cc9ab871f5203b5d9f9d1dc87d8af18c", "hash-nested-directories-2"},
|
|
||||||
}
|
|
||||||
|
|
||||||
env := &EvaluationEnvironment{}
|
|
||||||
|
|
||||||
for _, tt := range table {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
workdir, err := filepath.Abs("testdata")
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
output, err := NewInterpeter(env, Config{WorkingDir: workdir}).Evaluate(tt.input, DefaultStatusCheckNone)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
|
|
||||||
assert.Equal(t, tt.expected, output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFunctionFormat(t *testing.T) {
|
|
||||||
table := []struct {
|
|
||||||
input string
|
|
||||||
expected any
|
|
||||||
error any
|
|
||||||
name string
|
|
||||||
}{
|
|
||||||
{"format('text')", "text", nil, "format-plain-string"},
|
|
||||||
{"format('Hello {0} {1} {2}!', 'Mona', 'the', 'Octocat')", "Hello Mona the Octocat!", nil, "format-with-placeholders"},
|
|
||||||
{"format('{{Hello {0} {1} {2}!}}', 'Mona', 'the', 'Octocat')", "{Hello Mona the Octocat!}", nil, "format-with-escaped-braces"},
|
|
||||||
{"format('{{0}}', 'test')", "{0}", nil, "format-with-escaped-braces"},
|
|
||||||
{"format('{{{0}}}', 'test')", "{test}", nil, "format-with-escaped-braces-and-value"},
|
|
||||||
{"format('}}')", "}", nil, "format-output-closing-brace"},
|
|
||||||
{`format('Hello "{0}" {1} {2} {3} {4}', null, true, -3.14, NaN, Infinity)`, `Hello "" true -3.14 NaN Infinity`, nil, "format-with-primitives"},
|
|
||||||
{`format('Hello "{0}" {1} {2}', fromJSON('[0, true, "abc"]'), fromJSON('[{"a":1}]'), fromJSON('{"a":{"b":1}}'))`, `Hello "Array" Array Object`, nil, "format-with-complex-types"},
|
|
||||||
{"format(true)", "true", nil, "format-with-primitive-args"},
|
|
||||||
{"format('{0}', github)", "Object", nil, "format-with-context"},
|
|
||||||
{"format('echo Hello {0} ${{Test}}', github.undefined_property)", "echo Hello ${Test}", nil, "format-with-undefined-value"},
|
|
||||||
{"format('{0}}', '{1}', 'World')", nil, "Closing bracket without opening one. The following format string is invalid: '{0}}'", "format-invalid-format-string"},
|
|
||||||
{"format('a}b')", nil, "Closing bracket without opening one. The following format string is invalid: 'a}b'", "format-unmatched-closing-brace"},
|
|
||||||
{"format('{0', '{1}', 'World')", nil, "Unclosed brackets. The following format string is invalid: '{0'", "format-invalid-format-string"},
|
|
||||||
{"format('{2}', '{1}', 'World')", "", "The following format string references more arguments than were supplied: '{2}'", "format-invalid-replacement-reference"},
|
|
||||||
{"format('{2147483648}')", "", "The following format string is invalid: '{2147483648}'", "format-invalid-replacement-reference"},
|
|
||||||
{"format('{0} {1} {2} {3}', 1.0, 1.1, 1234567890.0, 12345678901234567890.0)", "1 1.1 1234567890 1.23456789012346E+19", nil, "format-floats"},
|
|
||||||
}
|
|
||||||
|
|
||||||
env := &EvaluationEnvironment{
|
|
||||||
Github: &model.GithubContext{},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range table {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
output, err := NewInterpeter(env, Config{}).Evaluate(tt.input, DefaultStatusCheckNone)
|
|
||||||
if tt.error != nil {
|
|
||||||
assert.Equal(t, tt.error, err.Error())
|
|
||||||
} else {
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
assert.Equal(t, tt.expected, output)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStatusFunctionsNilJob(t *testing.T) {
|
|
||||||
// A nil Job context must not panic: the status-check functions should treat
|
|
||||||
// it as an empty status and return false rather than dereferencing nil.
|
|
||||||
env := &EvaluationEnvironment{}
|
|
||||||
|
|
||||||
table := []struct {
|
|
||||||
input string
|
|
||||||
context string
|
|
||||||
name string
|
|
||||||
}{
|
|
||||||
{"cancelled()", "job", "cancelled-nil-job"},
|
|
||||||
{"success()", "step", "step-success-nil-job"},
|
|
||||||
{"failure()", "step", "step-failure-nil-job"},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range table {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
output, err := NewInterpeter(env, Config{Context: tt.context}).Evaluate(tt.input, DefaultStatusCheckNone)
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
assert.Equal(t, false, output)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
-1
@@ -1 +0,0 @@
|
|||||||
Hello
|
|
||||||
-1
@@ -1 +0,0 @@
|
|||||||
World!
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
Knock knock!
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
Anybody home?
|
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||||
|
// Copyright 2021 The nektos/act Authors. All rights reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
// Package ghcontext fills a model.GithubContext from the local git checkout.
|
||||||
|
// The pure data parts of the context live in the shared
|
||||||
|
// gitea.dev/actionslib/pkg/model package, only the helpers that need a
|
||||||
|
// git repository on disk are kept here.
|
||||||
|
package ghcontext
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitea.com/gitea/runner/act/common"
|
||||||
|
"gitea.com/gitea/runner/act/common/git"
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
findGitRef = git.FindGitRef
|
||||||
|
findGitRevision = git.FindGitRevision
|
||||||
|
findGithubRepo = git.FindGithubRepo
|
||||||
|
)
|
||||||
|
|
||||||
|
func withDefaultBranch(ctx context.Context, b string, event map[string]any) map[string]any {
|
||||||
|
repoI, ok := event["repository"]
|
||||||
|
if !ok {
|
||||||
|
repoI = make(map[string]any)
|
||||||
|
}
|
||||||
|
|
||||||
|
repo, ok := repoI.(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
common.Logger(ctx).Warnf("unable to set default branch to %v", b)
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the branch is already there return with no changes
|
||||||
|
if _, ok = repo["default_branch"]; ok {
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
repo["default_branch"] = b
|
||||||
|
event["repository"] = repo
|
||||||
|
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRef resolves the ref of the context from its event payload, falling back
|
||||||
|
// to the ref checked out in repoPath.
|
||||||
|
func SetRef(ctx context.Context, ghc *model.GithubContext, defaultBranch, repoPath string) {
|
||||||
|
logger := common.Logger(ctx)
|
||||||
|
|
||||||
|
// https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows
|
||||||
|
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads
|
||||||
|
switch ghc.EventName {
|
||||||
|
case "pull_request_target":
|
||||||
|
ghc.Ref = "refs/heads/" + ghc.BaseRef
|
||||||
|
case "pull_request", "pull_request_review", "pull_request_review_comment":
|
||||||
|
ghc.Ref = fmt.Sprintf("refs/pull/%.0f/merge", ghc.Event["number"])
|
||||||
|
case "deployment", "deployment_status":
|
||||||
|
ghc.Ref = model.AsString(model.NestedMapLookup(ghc.Event, "deployment", "ref"))
|
||||||
|
case "release":
|
||||||
|
ghc.Ref = "refs/tags/" + model.AsString(model.NestedMapLookup(ghc.Event, "release", "tag_name"))
|
||||||
|
case "push", "create", "workflow_dispatch":
|
||||||
|
ghc.Ref = model.AsString(ghc.Event["ref"])
|
||||||
|
default:
|
||||||
|
defaultBranch := model.AsString(model.NestedMapLookup(ghc.Event, "repository", "default_branch"))
|
||||||
|
if defaultBranch != "" {
|
||||||
|
ghc.Ref = "refs/heads/" + defaultBranch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ghc.Ref == "" {
|
||||||
|
ref, err := findGitRef(ctx, repoPath)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warningf("unable to get git ref: %v", err)
|
||||||
|
} else {
|
||||||
|
logger.Debugf("using github ref: %s", ref)
|
||||||
|
ghc.Ref = ref
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the branch in the event data
|
||||||
|
if defaultBranch != "" {
|
||||||
|
ghc.Event = withDefaultBranch(ctx, defaultBranch, ghc.Event)
|
||||||
|
} else {
|
||||||
|
ghc.Event = withDefaultBranch(ctx, "master", ghc.Event)
|
||||||
|
}
|
||||||
|
|
||||||
|
if ghc.Ref == "" {
|
||||||
|
ghc.Ref = "refs/heads/" + model.AsString(model.NestedMapLookup(ghc.Event, "repository", "default_branch"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetSha resolves the commit of the context from its event payload, falling
|
||||||
|
// back to the revision checked out in repoPath.
|
||||||
|
func SetSha(ctx context.Context, ghc *model.GithubContext, repoPath string) {
|
||||||
|
logger := common.Logger(ctx)
|
||||||
|
|
||||||
|
// https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows
|
||||||
|
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads
|
||||||
|
switch ghc.EventName {
|
||||||
|
case "pull_request_target":
|
||||||
|
ghc.Sha = model.AsString(model.NestedMapLookup(ghc.Event, "pull_request", "base", "sha"))
|
||||||
|
case "deployment", "deployment_status":
|
||||||
|
ghc.Sha = model.AsString(model.NestedMapLookup(ghc.Event, "deployment", "sha"))
|
||||||
|
case "push", "create", "workflow_dispatch":
|
||||||
|
if deleted, ok := ghc.Event["deleted"].(bool); ok && !deleted {
|
||||||
|
ghc.Sha = model.AsString(ghc.Event["after"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ghc.Sha == "" {
|
||||||
|
_, sha, err := findGitRevision(ctx, repoPath)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warningf("unable to get git revision: %v", err)
|
||||||
|
} else {
|
||||||
|
ghc.Sha = sha
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRepositoryAndOwner resolves the repository of the context from the git
|
||||||
|
// remote in repoPath when it is not set yet, and derives its owner.
|
||||||
|
func SetRepositoryAndOwner(ctx context.Context, ghc *model.GithubContext, githubInstance, remoteName, repoPath string) {
|
||||||
|
if ghc.Repository == "" {
|
||||||
|
repo, err := findGithubRepo(ctx, repoPath, githubInstance, remoteName)
|
||||||
|
if err != nil {
|
||||||
|
common.Logger(ctx).Warningf("unable to get git repo (githubInstance: %v; remoteName: %v, repoPath: %v): %v", githubInstance, remoteName, repoPath, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ghc.Repository = repo
|
||||||
|
}
|
||||||
|
ghc.RepositoryOwner = strings.Split(ghc.Repository, "/")[0]
|
||||||
|
}
|
||||||
@@ -2,13 +2,14 @@
|
|||||||
// Copyright 2022 The nektos/act Authors. All rights reserved.
|
// Copyright 2022 The nektos/act Authors. All rights reserved.
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
package model
|
package ghcontext
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
@@ -97,13 +98,13 @@ func TestSetRef(t *testing.T) {
|
|||||||
|
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
t.Run(table.eventName, func(t *testing.T) {
|
t.Run(table.eventName, func(t *testing.T) {
|
||||||
ghc := &GithubContext{
|
ghc := &model.GithubContext{
|
||||||
EventName: table.eventName,
|
EventName: table.eventName,
|
||||||
BaseRef: "master",
|
BaseRef: "master",
|
||||||
Event: table.event,
|
Event: table.event,
|
||||||
}
|
}
|
||||||
|
|
||||||
ghc.SetRef(context.Background(), "main", "/some/dir")
|
SetRef(context.Background(), ghc, "main", "/some/dir")
|
||||||
ghc.SetRefTypeAndName()
|
ghc.SetRefTypeAndName()
|
||||||
|
|
||||||
assert.Equal(t, table.ref, ghc.Ref)
|
assert.Equal(t, table.ref, ghc.Ref)
|
||||||
@@ -116,12 +117,12 @@ func TestSetRef(t *testing.T) {
|
|||||||
return "", errors.New("no default branch")
|
return "", errors.New("no default branch")
|
||||||
}
|
}
|
||||||
|
|
||||||
ghc := &GithubContext{
|
ghc := &model.GithubContext{
|
||||||
EventName: "no-default-branch",
|
EventName: "no-default-branch",
|
||||||
Event: map[string]any{},
|
Event: map[string]any{},
|
||||||
}
|
}
|
||||||
|
|
||||||
ghc.SetRef(context.Background(), "", "/some/dir")
|
SetRef(context.Background(), ghc, "", "/some/dir")
|
||||||
|
|
||||||
assert.Equal(t, "refs/heads/master", ghc.Ref)
|
assert.Equal(t, "refs/heads/master", ghc.Ref)
|
||||||
})
|
})
|
||||||
@@ -202,13 +203,13 @@ func TestSetSha(t *testing.T) {
|
|||||||
|
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
t.Run(table.eventName, func(t *testing.T) {
|
t.Run(table.eventName, func(t *testing.T) {
|
||||||
ghc := &GithubContext{
|
ghc := &model.GithubContext{
|
||||||
EventName: table.eventName,
|
EventName: table.eventName,
|
||||||
BaseRef: "master",
|
BaseRef: "master",
|
||||||
Event: table.event,
|
Event: table.event,
|
||||||
}
|
}
|
||||||
|
|
||||||
ghc.SetSha(context.Background(), "/some/dir")
|
SetSha(context.Background(), ghc, "/some/dir")
|
||||||
|
|
||||||
assert.Equal(t, table.sha, ghc.Sha)
|
assert.Equal(t, table.sha, ghc.Sha)
|
||||||
})
|
})
|
||||||
@@ -1,138 +0,0 @@
|
|||||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
||||||
// Copyright 2020 The nektos/act Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package model
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"go.yaml.in/yaml/v4"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ActionRunsUsing is the type of runner for the action
|
|
||||||
type ActionRunsUsing string
|
|
||||||
|
|
||||||
func (a *ActionRunsUsing) UnmarshalYAML(unmarshal func(any) error) error {
|
|
||||||
var using string
|
|
||||||
if err := unmarshal(&using); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Force input to lowercase for case insensitive comparison
|
|
||||||
format := ActionRunsUsing(strings.ToLower(using))
|
|
||||||
switch format {
|
|
||||||
case ActionRunsUsingNode24, ActionRunsUsingNode20, ActionRunsUsingNode16, ActionRunsUsingNode12, ActionRunsUsingDocker, ActionRunsUsingComposite, ActionRunsUsingGo:
|
|
||||||
*a = format
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("The runs.using key in action.yml must be one of: %v, got %s", []string{
|
|
||||||
ActionRunsUsingComposite,
|
|
||||||
ActionRunsUsingDocker,
|
|
||||||
ActionRunsUsingNode12,
|
|
||||||
ActionRunsUsingNode16,
|
|
||||||
ActionRunsUsingNode20,
|
|
||||||
ActionRunsUsingNode24,
|
|
||||||
ActionRunsUsingGo,
|
|
||||||
}, format)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
// ActionRunsUsingNode12 for running with node12
|
|
||||||
ActionRunsUsingNode12 = "node12"
|
|
||||||
// ActionRunsUsingNode16 for running with node16
|
|
||||||
ActionRunsUsingNode16 = "node16"
|
|
||||||
// ActionRunsUsingNode20 for running with node20
|
|
||||||
ActionRunsUsingNode20 = "node20"
|
|
||||||
// ActionRunsUsingNode24 for running with node24
|
|
||||||
ActionRunsUsingNode24 = "node24"
|
|
||||||
// ActionRunsUsingDocker for running with docker
|
|
||||||
ActionRunsUsingDocker = "docker"
|
|
||||||
// ActionRunsUsingComposite for running composite
|
|
||||||
ActionRunsUsingComposite = "composite"
|
|
||||||
// ActionRunsUsingGo for running with go
|
|
||||||
ActionRunsUsingGo = "go"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (a ActionRunsUsing) IsNode() bool {
|
|
||||||
switch a {
|
|
||||||
case ActionRunsUsingNode12, ActionRunsUsingNode16, ActionRunsUsingNode20, ActionRunsUsingNode24:
|
|
||||||
return true
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a ActionRunsUsing) IsDocker() bool {
|
|
||||||
return a == ActionRunsUsingDocker
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a ActionRunsUsing) IsComposite() bool {
|
|
||||||
return a == ActionRunsUsingComposite
|
|
||||||
}
|
|
||||||
|
|
||||||
// ActionRuns are a field in Action
|
|
||||||
type ActionRuns struct {
|
|
||||||
Using ActionRunsUsing `yaml:"using"`
|
|
||||||
Env map[string]string `yaml:"env"`
|
|
||||||
Main string `yaml:"main"`
|
|
||||||
Pre string `yaml:"pre"`
|
|
||||||
PreIf string `yaml:"pre-if"`
|
|
||||||
Post string `yaml:"post"`
|
|
||||||
PostIf string `yaml:"post-if"`
|
|
||||||
Image string `yaml:"image"`
|
|
||||||
PreEntrypoint string `yaml:"pre-entrypoint"`
|
|
||||||
Entrypoint string `yaml:"entrypoint"`
|
|
||||||
PostEntrypoint string `yaml:"post-entrypoint"`
|
|
||||||
Args []string `yaml:"args"`
|
|
||||||
Steps []Step `yaml:"steps"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Action describes a metadata file for GitHub actions. The metadata filename must be either action.yml or action.yaml. The data in the metadata file defines the inputs, outputs and main entrypoint for your action.
|
|
||||||
type Action struct {
|
|
||||||
Name string `yaml:"name"`
|
|
||||||
Author string `yaml:"author"`
|
|
||||||
Description string `yaml:"description"`
|
|
||||||
Inputs map[string]Input `yaml:"inputs"`
|
|
||||||
Outputs map[string]Output `yaml:"outputs"`
|
|
||||||
Runs ActionRuns `yaml:"runs"`
|
|
||||||
Branding struct {
|
|
||||||
Color string `yaml:"color"`
|
|
||||||
Icon string `yaml:"icon"`
|
|
||||||
} `yaml:"branding"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Input parameters allow you to specify data that the action expects to use during runtime. GitHub stores input parameters as environment variables. Input ids with uppercase letters are converted to lowercase during runtime. We recommended using lowercase input ids.
|
|
||||||
type Input struct {
|
|
||||||
Description string `yaml:"description"`
|
|
||||||
Required bool `yaml:"required"`
|
|
||||||
Default string `yaml:"default"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output parameters allow you to declare data that an action sets. Actions that run later in a workflow can use the output data set in previously run actions. For example, if you had an action that performed the addition of two inputs (x + y = z), the action could output the sum (z) for other actions to use as an input.
|
|
||||||
type Output struct {
|
|
||||||
Description string `yaml:"description"`
|
|
||||||
Value string `yaml:"value"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadAction reads an action from a reader
|
|
||||||
func ReadAction(in io.Reader) (*Action, error) {
|
|
||||||
a := new(Action)
|
|
||||||
err := yaml.NewDecoder(in).Decode(a)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// set defaults
|
|
||||||
if a.Runs.PreIf == "" {
|
|
||||||
a.Runs.PreIf = "always()"
|
|
||||||
}
|
|
||||||
if a.Runs.PostIf == "" {
|
|
||||||
a.Runs.PostIf = "always()"
|
|
||||||
}
|
|
||||||
|
|
||||||
return a, nil
|
|
||||||
}
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package model
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestReadActionDefaultsAndCaseInsensitiveUsing(t *testing.T) {
|
|
||||||
action, err := ReadAction(strings.NewReader(`
|
|
||||||
name: example
|
|
||||||
runs:
|
|
||||||
using: NoDe24
|
|
||||||
main: dist/index.js
|
|
||||||
`))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if action.Runs.Using != ActionRunsUsingNode24 {
|
|
||||||
t.Fatalf("using = %q, want %q", action.Runs.Using, ActionRunsUsingNode24)
|
|
||||||
}
|
|
||||||
if action.Runs.PreIf != "always()" {
|
|
||||||
t.Fatalf("pre-if = %q, want always()", action.Runs.PreIf)
|
|
||||||
}
|
|
||||||
if action.Runs.PostIf != "always()" {
|
|
||||||
t.Fatalf("post-if = %q, want always()", action.Runs.PostIf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadActionPreservesExplicitConditions(t *testing.T) {
|
|
||||||
action, err := ReadAction(strings.NewReader(`
|
|
||||||
runs:
|
|
||||||
using: composite
|
|
||||||
pre-if: success()
|
|
||||||
post-if: failure()
|
|
||||||
steps:
|
|
||||||
- run: echo hello
|
|
||||||
`))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if action.Runs.PreIf != "success()" || action.Runs.PostIf != "failure()" {
|
|
||||||
t.Fatalf("conditions = %q/%q, want explicit values", action.Runs.PreIf, action.Runs.PostIf)
|
|
||||||
}
|
|
||||||
if !action.Runs.Using.IsComposite() || action.Runs.Using.IsDocker() || action.Runs.Using.IsNode() {
|
|
||||||
t.Fatalf("unexpected using predicates for %q", action.Runs.Using)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadActionRejectsUnknownUsing(t *testing.T) {
|
|
||||||
_, err := ReadAction(strings.NewReader(`
|
|
||||||
runs:
|
|
||||||
using: node99
|
|
||||||
`))
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected unknown runs.using to fail")
|
|
||||||
}
|
|
||||||
if !strings.Contains(err.Error(), "node99") {
|
|
||||||
t.Fatalf("error = %q, want invalid value", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReadActionDockerEntrypoints(t *testing.T) {
|
|
||||||
action, err := ReadAction(strings.NewReader(`
|
|
||||||
runs:
|
|
||||||
using: docker
|
|
||||||
image: Dockerfile
|
|
||||||
pre-entrypoint: pre.sh
|
|
||||||
post-entrypoint: post.sh
|
|
||||||
`))
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if action.Runs.PreEntrypoint != "pre.sh" {
|
|
||||||
t.Fatalf("pre-entrypoint = %q, want pre.sh", action.Runs.PreEntrypoint)
|
|
||||||
}
|
|
||||||
if action.Runs.PostEntrypoint != "post.sh" {
|
|
||||||
t.Fatalf("post-entrypoint = %q, want post.sh", action.Runs.PostEntrypoint)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,222 +0,0 @@
|
|||||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
||||||
// Copyright 2021 The nektos/act Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package model
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
|
||||||
"gitea.com/gitea/runner/act/common/git"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GithubContext struct {
|
|
||||||
Event map[string]any `json:"event"`
|
|
||||||
EventPath string `json:"event_path"`
|
|
||||||
Workflow string `json:"workflow"`
|
|
||||||
RunID string `json:"run_id"`
|
|
||||||
RunNumber string `json:"run_number"`
|
|
||||||
Actor string `json:"actor"`
|
|
||||||
Repository string `json:"repository"`
|
|
||||||
EventName string `json:"event_name"`
|
|
||||||
Sha string `json:"sha"`
|
|
||||||
Ref string `json:"ref"`
|
|
||||||
RefName string `json:"ref_name"`
|
|
||||||
RefType string `json:"ref_type"`
|
|
||||||
HeadRef string `json:"head_ref"`
|
|
||||||
BaseRef string `json:"base_ref"`
|
|
||||||
Token string `json:"token"`
|
|
||||||
Workspace string `json:"workspace"`
|
|
||||||
Action string `json:"action"`
|
|
||||||
ActionPath string `json:"action_path"`
|
|
||||||
ActionRef string `json:"action_ref"`
|
|
||||||
ActionRepository string `json:"action_repository"`
|
|
||||||
Job string `json:"job"`
|
|
||||||
JobName string `json:"job_name"`
|
|
||||||
RepositoryOwner string `json:"repository_owner"`
|
|
||||||
RetentionDays string `json:"retention_days"`
|
|
||||||
RunnerPerflog string `json:"runner_perflog"`
|
|
||||||
RunnerTrackingID string `json:"runner_tracking_id"`
|
|
||||||
ServerURL string `json:"server_url"`
|
|
||||||
APIURL string `json:"api_url"`
|
|
||||||
GraphQLURL string `json:"graphql_url"`
|
|
||||||
|
|
||||||
// For Gitea
|
|
||||||
RunAttempt string `json:"run_attempt"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func asString(v any) string {
|
|
||||||
if v == nil {
|
|
||||||
return ""
|
|
||||||
} else if s, ok := v.(string); ok {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func nestedMapLookup(m map[string]any, ks ...string) (rval any) {
|
|
||||||
var ok bool
|
|
||||||
|
|
||||||
if len(ks) == 0 { // degenerate input
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if rval, ok = m[ks[0]]; !ok {
|
|
||||||
return nil
|
|
||||||
} else if len(ks) == 1 { // we've reached the final key
|
|
||||||
return rval
|
|
||||||
} else if m, ok = rval.(map[string]any); !ok {
|
|
||||||
return nil
|
|
||||||
} else { // 1+ more keys
|
|
||||||
return nestedMapLookup(m, ks[1:]...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func withDefaultBranch(ctx context.Context, b string, event map[string]any) map[string]any {
|
|
||||||
repoI, ok := event["repository"]
|
|
||||||
if !ok {
|
|
||||||
repoI = make(map[string]any)
|
|
||||||
}
|
|
||||||
|
|
||||||
repo, ok := repoI.(map[string]any)
|
|
||||||
if !ok {
|
|
||||||
common.Logger(ctx).Warnf("unable to set default branch to %v", b)
|
|
||||||
return event
|
|
||||||
}
|
|
||||||
|
|
||||||
// if the branch is already there return with no changes
|
|
||||||
if _, ok = repo["default_branch"]; ok {
|
|
||||||
return event
|
|
||||||
}
|
|
||||||
|
|
||||||
repo["default_branch"] = b
|
|
||||||
event["repository"] = repo
|
|
||||||
|
|
||||||
return event
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
findGitRef = git.FindGitRef
|
|
||||||
findGitRevision = git.FindGitRevision
|
|
||||||
)
|
|
||||||
|
|
||||||
func (ghc *GithubContext) SetRef(ctx context.Context, defaultBranch, repoPath string) {
|
|
||||||
logger := common.Logger(ctx)
|
|
||||||
|
|
||||||
// https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows
|
|
||||||
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads
|
|
||||||
switch ghc.EventName {
|
|
||||||
case "pull_request_target":
|
|
||||||
ghc.Ref = "refs/heads/" + ghc.BaseRef
|
|
||||||
case "pull_request", "pull_request_review", "pull_request_review_comment":
|
|
||||||
ghc.Ref = fmt.Sprintf("refs/pull/%.0f/merge", ghc.Event["number"])
|
|
||||||
case "deployment", "deployment_status":
|
|
||||||
ghc.Ref = asString(nestedMapLookup(ghc.Event, "deployment", "ref"))
|
|
||||||
case "release":
|
|
||||||
ghc.Ref = "refs/tags/" + asString(nestedMapLookup(ghc.Event, "release", "tag_name"))
|
|
||||||
case "push", "create", "workflow_dispatch":
|
|
||||||
ghc.Ref = asString(ghc.Event["ref"])
|
|
||||||
default:
|
|
||||||
defaultBranch := asString(nestedMapLookup(ghc.Event, "repository", "default_branch"))
|
|
||||||
if defaultBranch != "" {
|
|
||||||
ghc.Ref = "refs/heads/" + defaultBranch
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ghc.Ref == "" {
|
|
||||||
ref, err := findGitRef(ctx, repoPath)
|
|
||||||
if err != nil {
|
|
||||||
logger.Warningf("unable to get git ref: %v", err)
|
|
||||||
} else {
|
|
||||||
logger.Debugf("using github ref: %s", ref)
|
|
||||||
ghc.Ref = ref
|
|
||||||
}
|
|
||||||
|
|
||||||
// set the branch in the event data
|
|
||||||
if defaultBranch != "" {
|
|
||||||
ghc.Event = withDefaultBranch(ctx, defaultBranch, ghc.Event)
|
|
||||||
} else {
|
|
||||||
ghc.Event = withDefaultBranch(ctx, "master", ghc.Event)
|
|
||||||
}
|
|
||||||
|
|
||||||
if ghc.Ref == "" {
|
|
||||||
ghc.Ref = "refs/heads/" + asString(nestedMapLookup(ghc.Event, "repository", "default_branch"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ghc *GithubContext) SetSha(ctx context.Context, repoPath string) {
|
|
||||||
logger := common.Logger(ctx)
|
|
||||||
|
|
||||||
// https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows
|
|
||||||
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads
|
|
||||||
switch ghc.EventName {
|
|
||||||
case "pull_request_target":
|
|
||||||
ghc.Sha = asString(nestedMapLookup(ghc.Event, "pull_request", "base", "sha"))
|
|
||||||
case "deployment", "deployment_status":
|
|
||||||
ghc.Sha = asString(nestedMapLookup(ghc.Event, "deployment", "sha"))
|
|
||||||
case "push", "create", "workflow_dispatch":
|
|
||||||
if deleted, ok := ghc.Event["deleted"].(bool); ok && !deleted {
|
|
||||||
ghc.Sha = asString(ghc.Event["after"])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ghc.Sha == "" {
|
|
||||||
_, sha, err := findGitRevision(ctx, repoPath)
|
|
||||||
if err != nil {
|
|
||||||
logger.Warningf("unable to get git revision: %v", err)
|
|
||||||
} else {
|
|
||||||
ghc.Sha = sha
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ghc *GithubContext) SetRepositoryAndOwner(ctx context.Context, githubInstance, remoteName, repoPath string) {
|
|
||||||
if ghc.Repository == "" {
|
|
||||||
repo, err := git.FindGithubRepo(ctx, repoPath, githubInstance, remoteName)
|
|
||||||
if err != nil {
|
|
||||||
common.Logger(ctx).Warningf("unable to get git repo (githubInstance: %v; remoteName: %v, repoPath: %v): %v", githubInstance, remoteName, repoPath, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ghc.Repository = repo
|
|
||||||
}
|
|
||||||
ghc.RepositoryOwner = strings.Split(ghc.Repository, "/")[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ghc *GithubContext) SetRefTypeAndName() {
|
|
||||||
var refType, refName string
|
|
||||||
|
|
||||||
// https://docs.github.com/en/actions/learn-github-actions/environment-variables
|
|
||||||
if strings.HasPrefix(ghc.Ref, "refs/tags/") {
|
|
||||||
refType = "tag"
|
|
||||||
refName = ghc.Ref[len("refs/tags/"):]
|
|
||||||
} else if strings.HasPrefix(ghc.Ref, "refs/heads/") {
|
|
||||||
refType = "branch"
|
|
||||||
refName = ghc.Ref[len("refs/heads/"):]
|
|
||||||
} else if strings.HasPrefix(ghc.Ref, "refs/pull/") {
|
|
||||||
refType = ""
|
|
||||||
refName = ghc.Ref[len("refs/pull/"):]
|
|
||||||
}
|
|
||||||
|
|
||||||
if ghc.RefType == "" {
|
|
||||||
ghc.RefType = refType
|
|
||||||
}
|
|
||||||
|
|
||||||
if ghc.RefName == "" {
|
|
||||||
ghc.RefName = refName
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ghc *GithubContext) SetBaseAndHeadRef() {
|
|
||||||
if ghc.EventName == "pull_request" || ghc.EventName == "pull_request_target" {
|
|
||||||
if ghc.BaseRef == "" {
|
|
||||||
ghc.BaseRef = asString(nestedMapLookup(ghc.Event, "pull_request", "base", "ref"))
|
|
||||||
}
|
|
||||||
|
|
||||||
if ghc.HeadRef == "" {
|
|
||||||
ghc.HeadRef = asString(nestedMapLookup(ghc.Event, "pull_request", "head", "ref"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
||||||
// Copyright 2021 The nektos/act Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package model
|
|
||||||
|
|
||||||
type JobContext struct {
|
|
||||||
Status string `json:"status"`
|
|
||||||
Container JobContainerContext `json:"container"`
|
|
||||||
Services map[string]JobService `json:"services"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type JobContainerContext struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Network string `json:"network"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type JobService struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Network string `json:"network"`
|
|
||||||
Ports map[string]string `json:"ports"` // container port to the published host port
|
|
||||||
}
|
|
||||||
@@ -1,410 +0,0 @@
|
|||||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
||||||
// Copyright 2020 The nektos/act Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package model
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"io/fs"
|
|
||||||
"math"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
|
||||||
"slices"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
// WorkflowPlanner contains methods for creating plans
|
|
||||||
type WorkflowPlanner interface {
|
|
||||||
PlanEvent(eventName string) (*Plan, error)
|
|
||||||
PlanJob(jobName string) (*Plan, error)
|
|
||||||
PlanAll() (*Plan, error)
|
|
||||||
GetEvents() []string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Plan contains a list of stages to run in series
|
|
||||||
type Plan struct {
|
|
||||||
Stages []*Stage
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stage contains a list of runs to execute in parallel
|
|
||||||
type Stage struct {
|
|
||||||
Runs []*Run
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run represents a job from a workflow that needs to be run
|
|
||||||
type Run struct {
|
|
||||||
Workflow *Workflow
|
|
||||||
JobID string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Run) String() string {
|
|
||||||
jobName := r.Job().Name
|
|
||||||
if jobName == "" {
|
|
||||||
jobName = r.JobID
|
|
||||||
}
|
|
||||||
return jobName
|
|
||||||
}
|
|
||||||
|
|
||||||
// Job returns the job for this Run
|
|
||||||
func (r *Run) Job() *Job {
|
|
||||||
return r.Workflow.GetJob(r.JobID)
|
|
||||||
}
|
|
||||||
|
|
||||||
type WorkflowFiles struct {
|
|
||||||
workflowDirEntry os.DirEntry
|
|
||||||
dirPath string
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewWorkflowPlanner will load a specific workflow, all workflows from a directory or all workflows from a directory and its subdirectories
|
|
||||||
func NewWorkflowPlanner(path string, noWorkflowRecurse bool) (WorkflowPlanner, error) {
|
|
||||||
path, err := filepath.Abs(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
fi, err := os.Stat(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var workflows []WorkflowFiles
|
|
||||||
|
|
||||||
if fi.IsDir() {
|
|
||||||
log.Debugf("Loading workflows from '%s'", path)
|
|
||||||
if noWorkflowRecurse {
|
|
||||||
files, err := os.ReadDir(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, v := range files {
|
|
||||||
workflows = append(workflows, WorkflowFiles{
|
|
||||||
dirPath: path,
|
|
||||||
workflowDirEntry: v,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.Debug("Loading workflows recursively")
|
|
||||||
if err := filepath.Walk(path,
|
|
||||||
func(p string, f os.FileInfo, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !f.IsDir() {
|
|
||||||
log.Debugf("Found workflow '%s' in '%s'", f.Name(), p)
|
|
||||||
workflows = append(workflows, WorkflowFiles{
|
|
||||||
dirPath: filepath.Dir(p),
|
|
||||||
workflowDirEntry: fs.FileInfoToDirEntry(f),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.Debugf("Loading workflow '%s'", path)
|
|
||||||
dirname := filepath.Dir(path)
|
|
||||||
|
|
||||||
workflows = append(workflows, WorkflowFiles{
|
|
||||||
dirPath: dirname,
|
|
||||||
workflowDirEntry: fs.FileInfoToDirEntry(fi),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
wp := new(workflowPlanner)
|
|
||||||
for _, wf := range workflows {
|
|
||||||
ext := filepath.Ext(wf.workflowDirEntry.Name())
|
|
||||||
if ext == ".yml" || ext == ".yaml" {
|
|
||||||
f, err := os.Open(filepath.Join(wf.dirPath, wf.workflowDirEntry.Name()))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugf("Reading workflow '%s'", f.Name())
|
|
||||||
workflow, err := ReadWorkflow(f)
|
|
||||||
if err != nil {
|
|
||||||
_ = f.Close()
|
|
||||||
if err == io.EOF {
|
|
||||||
return nil, fmt.Errorf("unable to read workflow '%s': file is empty: %w", wf.workflowDirEntry.Name(), err)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("workflow is not valid. '%s': %w", wf.workflowDirEntry.Name(), err)
|
|
||||||
}
|
|
||||||
_, err = f.Seek(0, 0)
|
|
||||||
if err != nil {
|
|
||||||
_ = f.Close()
|
|
||||||
return nil, fmt.Errorf("error occurring when resetting io pointer in '%s': %w", wf.workflowDirEntry.Name(), err)
|
|
||||||
}
|
|
||||||
|
|
||||||
workflow.File = wf.workflowDirEntry.Name()
|
|
||||||
if workflow.Name == "" {
|
|
||||||
workflow.Name = wf.workflowDirEntry.Name()
|
|
||||||
}
|
|
||||||
|
|
||||||
err = validateJobName(workflow)
|
|
||||||
if err != nil {
|
|
||||||
_ = f.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
wp.workflows = append(wp.workflows, workflow)
|
|
||||||
_ = f.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return wp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// CombineWorkflowPlanner combines workflows to a WorkflowPlanner
|
|
||||||
func CombineWorkflowPlanner(workflows ...*Workflow) WorkflowPlanner {
|
|
||||||
return &workflowPlanner{
|
|
||||||
workflows: workflows,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSingleWorkflowPlanner(name string, f io.Reader) (WorkflowPlanner, error) {
|
|
||||||
wp := new(workflowPlanner)
|
|
||||||
|
|
||||||
log.Debugf("Reading workflow %s", name)
|
|
||||||
workflow, err := ReadWorkflow(f)
|
|
||||||
if err != nil {
|
|
||||||
if err == io.EOF {
|
|
||||||
return nil, fmt.Errorf("unable to read workflow '%s': file is empty: %w", name, err)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("workflow is not valid. '%s': %w", name, err)
|
|
||||||
}
|
|
||||||
workflow.File = name
|
|
||||||
if workflow.Name == "" {
|
|
||||||
workflow.Name = name
|
|
||||||
}
|
|
||||||
|
|
||||||
err = validateJobName(workflow)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
wp.workflows = append(wp.workflows, workflow)
|
|
||||||
|
|
||||||
return wp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateJobName(workflow *Workflow) error {
|
|
||||||
jobNameRegex := regexp.MustCompile(`^([[:alpha:]_][[:alnum:]_\-]*)$`)
|
|
||||||
for k := range workflow.Jobs {
|
|
||||||
if ok := jobNameRegex.MatchString(k); !ok {
|
|
||||||
return fmt.Errorf("workflow is not valid. '%s': Job name '%s' is invalid. Names must start with a letter or '_' and contain only alphanumeric characters, '-', or '_'", workflow.Name, k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type workflowPlanner struct {
|
|
||||||
workflows []*Workflow
|
|
||||||
}
|
|
||||||
|
|
||||||
// PlanEvent builds a new list of runs to execute in parallel for an event name
|
|
||||||
func (wp *workflowPlanner) PlanEvent(eventName string) (*Plan, error) {
|
|
||||||
plan := new(Plan)
|
|
||||||
if len(wp.workflows) == 0 {
|
|
||||||
log.Debug("no workflows found by planner")
|
|
||||||
return plan, nil
|
|
||||||
}
|
|
||||||
var lastErr error
|
|
||||||
|
|
||||||
for _, w := range wp.workflows {
|
|
||||||
events := w.On()
|
|
||||||
if len(events) == 0 {
|
|
||||||
log.Debugf("no events found for workflow: %s", w.File)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, e := range events {
|
|
||||||
if e == eventName {
|
|
||||||
stages, err := createStages(w, w.GetJobIDs()...)
|
|
||||||
if err != nil {
|
|
||||||
log.Warn(err)
|
|
||||||
lastErr = err
|
|
||||||
} else {
|
|
||||||
plan.mergeStages(stages)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return plan, lastErr
|
|
||||||
}
|
|
||||||
|
|
||||||
// PlanJob builds a new run to execute in parallel for a job name
|
|
||||||
func (wp *workflowPlanner) PlanJob(jobName string) (*Plan, error) {
|
|
||||||
plan := new(Plan)
|
|
||||||
if len(wp.workflows) == 0 {
|
|
||||||
log.Debugf("no jobs found for workflow: %s", jobName)
|
|
||||||
}
|
|
||||||
var lastErr error
|
|
||||||
|
|
||||||
for _, w := range wp.workflows {
|
|
||||||
stages, err := createStages(w, jobName)
|
|
||||||
if err != nil {
|
|
||||||
log.Warn(err)
|
|
||||||
lastErr = err
|
|
||||||
} else {
|
|
||||||
plan.mergeStages(stages)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return plan, lastErr
|
|
||||||
}
|
|
||||||
|
|
||||||
// PlanAll builds a new run to execute in parallel all
|
|
||||||
func (wp *workflowPlanner) PlanAll() (*Plan, error) {
|
|
||||||
plan := new(Plan)
|
|
||||||
if len(wp.workflows) == 0 {
|
|
||||||
log.Debug("no workflows found by planner")
|
|
||||||
return plan, nil
|
|
||||||
}
|
|
||||||
var lastErr error
|
|
||||||
|
|
||||||
for _, w := range wp.workflows {
|
|
||||||
stages, err := createStages(w, w.GetJobIDs()...)
|
|
||||||
if err != nil {
|
|
||||||
log.Warn(err)
|
|
||||||
lastErr = err
|
|
||||||
} else {
|
|
||||||
plan.mergeStages(stages)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return plan, lastErr
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetEvents gets all the events in the workflows file
|
|
||||||
func (wp *workflowPlanner) GetEvents() []string {
|
|
||||||
events := make([]string, 0)
|
|
||||||
for _, w := range wp.workflows {
|
|
||||||
found := false
|
|
||||||
for _, e := range events {
|
|
||||||
if slices.Contains(w.On(), e) {
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
if found {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !found {
|
|
||||||
events = append(events, w.On()...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sort the list based on depth of dependencies
|
|
||||||
slices.Sort(events)
|
|
||||||
|
|
||||||
return events
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxRunNameLen determines the max name length of all jobs
|
|
||||||
func (p *Plan) MaxRunNameLen() int {
|
|
||||||
maxRunNameLen := 0
|
|
||||||
for _, stage := range p.Stages {
|
|
||||||
for _, run := range stage.Runs {
|
|
||||||
runNameLen := len(run.String())
|
|
||||||
if runNameLen > maxRunNameLen {
|
|
||||||
maxRunNameLen = runNameLen
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return maxRunNameLen
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetJobIDs will get all the job names in the stage
|
|
||||||
func (s *Stage) GetJobIDs() []string {
|
|
||||||
names := make([]string, 0)
|
|
||||||
for _, r := range s.Runs {
|
|
||||||
names = append(names, r.JobID)
|
|
||||||
}
|
|
||||||
return names
|
|
||||||
}
|
|
||||||
|
|
||||||
// Merge stages with existing stages in plan
|
|
||||||
func (p *Plan) mergeStages(stages []*Stage) {
|
|
||||||
newStages := make([]*Stage, int(math.Max(float64(len(p.Stages)), float64(len(stages)))))
|
|
||||||
for i := range newStages {
|
|
||||||
newStages[i] = new(Stage)
|
|
||||||
if i >= len(p.Stages) {
|
|
||||||
newStages[i].Runs = append(newStages[i].Runs, stages[i].Runs...)
|
|
||||||
} else if i >= len(stages) {
|
|
||||||
newStages[i].Runs = append(newStages[i].Runs, p.Stages[i].Runs...)
|
|
||||||
} else {
|
|
||||||
newStages[i].Runs = append(newStages[i].Runs, p.Stages[i].Runs...)
|
|
||||||
newStages[i].Runs = append(newStages[i].Runs, stages[i].Runs...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
p.Stages = newStages
|
|
||||||
}
|
|
||||||
|
|
||||||
func createStages(w *Workflow, jobIDs ...string) ([]*Stage, error) {
|
|
||||||
// first, build a list of all the necessary jobs to run, and their dependencies
|
|
||||||
jobDependencies := make(map[string][]string)
|
|
||||||
for len(jobIDs) > 0 {
|
|
||||||
newJobIDs := make([]string, 0)
|
|
||||||
for _, jID := range jobIDs {
|
|
||||||
// make sure we haven't visited this job yet
|
|
||||||
if _, ok := jobDependencies[jID]; !ok {
|
|
||||||
if job := w.GetJob(jID); job != nil {
|
|
||||||
jobDependencies[jID] = job.Needs()
|
|
||||||
newJobIDs = append(newJobIDs, job.Needs()...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
jobIDs = newJobIDs
|
|
||||||
}
|
|
||||||
|
|
||||||
// next, build an execution graph
|
|
||||||
stages := make([]*Stage, 0)
|
|
||||||
for len(jobDependencies) > 0 {
|
|
||||||
stage := new(Stage)
|
|
||||||
for jID, jDeps := range jobDependencies {
|
|
||||||
// make sure all deps are in the graph already
|
|
||||||
if listInStages(jDeps, stages...) {
|
|
||||||
stage.Runs = append(stage.Runs, &Run{
|
|
||||||
Workflow: w,
|
|
||||||
JobID: jID,
|
|
||||||
})
|
|
||||||
delete(jobDependencies, jID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(stage.Runs) == 0 {
|
|
||||||
return nil, fmt.Errorf("unable to build dependency graph for %s (%s)", w.Name, w.File)
|
|
||||||
}
|
|
||||||
stages = append(stages, stage)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(stages) == 0 {
|
|
||||||
return nil, errors.New("Could not find any stages to run. View the valid jobs with `act --list`. Use `act --help` to find how to filter by Job ID/Workflow/Event Name")
|
|
||||||
}
|
|
||||||
|
|
||||||
return stages, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// return true iff all strings in srcList exist in at least one of the stages
|
|
||||||
func listInStages(srcList []string, stages ...*Stage) bool {
|
|
||||||
for _, src := range srcList {
|
|
||||||
found := false
|
|
||||||
for _, stage := range stages {
|
|
||||||
for _, search := range stage.GetJobIDs() {
|
|
||||||
if src == search {
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
@@ -1,199 +0,0 @@
|
|||||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
||||||
// Copyright 2021 The nektos/act Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package model
|
|
||||||
|
|
||||||
import (
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
type WorkflowPlanTest struct {
|
|
||||||
workflowPath string
|
|
||||||
errorMessage string
|
|
||||||
noWorkflowRecurse bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPlanner(t *testing.T) {
|
|
||||||
log.SetLevel(log.DebugLevel)
|
|
||||||
|
|
||||||
tables := []WorkflowPlanTest{
|
|
||||||
{"invalid-job-name/invalid-1.yml", "workflow is not valid. 'invalid-job-name-1': Job name 'invalid-JOB-Name-v1.2.3-docker_hub' is invalid. Names must start with a letter or '_' and contain only alphanumeric characters, '-', or '_'", false},
|
|
||||||
{"invalid-job-name/invalid-2.yml", "workflow is not valid. 'invalid-job-name-2': Job name '1234invalid-JOB-Name-v123-docker_hub' is invalid. Names must start with a letter or '_' and contain only alphanumeric characters, '-', or '_'", false},
|
|
||||||
{"invalid-job-name/valid-1.yml", "", false},
|
|
||||||
{"invalid-job-name/valid-2.yml", "", false},
|
|
||||||
{"empty-workflow", "unable to read workflow 'push.yml': file is empty: EOF", false},
|
|
||||||
{"nested", "unable to read workflow 'fail.yml': file is empty: EOF", false},
|
|
||||||
{"nested", "", true},
|
|
||||||
}
|
|
||||||
|
|
||||||
workdir, err := filepath.Abs("testdata")
|
|
||||||
assert.NoError(t, err, workdir) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
for _, table := range tables {
|
|
||||||
fullWorkflowPath := filepath.Join(workdir, table.workflowPath)
|
|
||||||
_, err = NewWorkflowPlanner(fullWorkflowPath, table.noWorkflowRecurse)
|
|
||||||
if table.errorMessage == "" {
|
|
||||||
assert.NoError(t, err, "WorkflowPlanner should exit without any error")
|
|
||||||
} else {
|
|
||||||
assert.EqualError(t, err, table.errorMessage)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWorkflow(t *testing.T) {
|
|
||||||
log.SetLevel(log.DebugLevel)
|
|
||||||
|
|
||||||
workflow := Workflow{
|
|
||||||
Jobs: map[string]*Job{
|
|
||||||
"valid_job": {
|
|
||||||
Name: "valid_job",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check that an invalid job id returns error
|
|
||||||
result, err := createStages(&workflow, "invalid_job_id")
|
|
||||||
assert.Error(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
assert.Nil(t, result)
|
|
||||||
|
|
||||||
// Check that an valid job id returns non-error
|
|
||||||
result, err = createStages(&workflow, "valid_job")
|
|
||||||
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act
|
|
||||||
assert.NotNil(t, result)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNewSingleWorkflowPlannerAndPlanMethods(t *testing.T) {
|
|
||||||
planner, err := NewSingleWorkflowPlanner("ci.yml", strings.NewReader(`
|
|
||||||
name: CI
|
|
||||||
on: [push, pull_request]
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
name: Build project
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: make build
|
|
||||||
test:
|
|
||||||
needs: build
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: make test
|
|
||||||
`))
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
assert.Equal(t, []string{"pull_request", "push"}, planner.GetEvents())
|
|
||||||
|
|
||||||
eventPlan, err := planner.PlanEvent("push")
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Len(t, eventPlan.Stages, 2)
|
|
||||||
assert.Equal(t, []string{"build"}, eventPlan.Stages[0].GetJobIDs())
|
|
||||||
assert.Equal(t, []string{"test"}, eventPlan.Stages[1].GetJobIDs())
|
|
||||||
assert.Equal(t, len("Build project"), eventPlan.MaxRunNameLen())
|
|
||||||
assert.Equal(t, "Build project", eventPlan.Stages[0].Runs[0].String())
|
|
||||||
assert.Equal(t, "build", eventPlan.Stages[0].Runs[0].JobID)
|
|
||||||
assert.NotNil(t, eventPlan.Stages[0].Runs[0].Job())
|
|
||||||
|
|
||||||
jobPlan, err := planner.PlanJob("test")
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Len(t, jobPlan.Stages, 2)
|
|
||||||
assert.Equal(t, []string{"build"}, jobPlan.Stages[0].GetJobIDs())
|
|
||||||
assert.Equal(t, []string{"test"}, jobPlan.Stages[1].GetJobIDs())
|
|
||||||
|
|
||||||
allPlan, err := planner.PlanAll()
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Len(t, allPlan.Stages, 2)
|
|
||||||
assert.Equal(t, []string{"build"}, allPlan.Stages[0].GetJobIDs())
|
|
||||||
assert.Equal(t, []string{"test"}, allPlan.Stages[1].GetJobIDs())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCombineWorkflowPlannerMergesWorkflowStages(t *testing.T) {
|
|
||||||
first := mustReadWorkflow(t, `
|
|
||||||
name: First
|
|
||||||
on: push
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: make build
|
|
||||||
`)
|
|
||||||
second := mustReadWorkflow(t, `
|
|
||||||
name: Second
|
|
||||||
on: push
|
|
||||||
jobs:
|
|
||||||
lint:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: make lint
|
|
||||||
test:
|
|
||||||
needs: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: make test
|
|
||||||
`)
|
|
||||||
|
|
||||||
planner := CombineWorkflowPlanner(first, second)
|
|
||||||
plan, err := planner.PlanEvent("push")
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Len(t, plan.Stages, 2)
|
|
||||||
assert.ElementsMatch(t, []string{"build", "lint"}, plan.Stages[0].GetJobIDs())
|
|
||||||
assert.Equal(t, []string{"test"}, plan.Stages[1].GetJobIDs())
|
|
||||||
|
|
||||||
empty, err := planner.PlanEvent("schedule")
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Empty(t, empty.Stages)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPlannerErrorsForMissingAndCyclicJobs(t *testing.T) {
|
|
||||||
workflow := mustReadWorkflow(t, `
|
|
||||||
name: Cyclic
|
|
||||||
on: push
|
|
||||||
jobs:
|
|
||||||
a:
|
|
||||||
needs: b
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: echo a
|
|
||||||
b:
|
|
||||||
needs: a
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: echo b
|
|
||||||
`)
|
|
||||||
planner := CombineWorkflowPlanner(workflow)
|
|
||||||
|
|
||||||
plan, err := planner.PlanJob("missing")
|
|
||||||
require.Error(t, err)
|
|
||||||
assert.Empty(t, plan.Stages)
|
|
||||||
assert.Contains(t, err.Error(), "Could not find any stages")
|
|
||||||
|
|
||||||
plan, err = planner.PlanEvent("push")
|
|
||||||
require.Error(t, err)
|
|
||||||
assert.Empty(t, plan.Stages)
|
|
||||||
assert.Contains(t, err.Error(), "unable to build dependency graph")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNewSingleWorkflowPlannerErrors(t *testing.T) {
|
|
||||||
_, err := NewSingleWorkflowPlanner("empty.yml", strings.NewReader(""))
|
|
||||||
require.Error(t, err)
|
|
||||||
assert.Contains(t, err.Error(), "file is empty")
|
|
||||||
|
|
||||||
_, err = NewSingleWorkflowPlanner("invalid.yml", strings.NewReader("jobs: ["))
|
|
||||||
require.Error(t, err)
|
|
||||||
assert.Contains(t, err.Error(), "workflow is not valid")
|
|
||||||
}
|
|
||||||
|
|
||||||
func mustReadWorkflow(t *testing.T, content string) *Workflow {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
workflow, err := ReadWorkflow(strings.NewReader(content))
|
|
||||||
require.NoError(t, err)
|
|
||||||
if workflow.Name == "" {
|
|
||||||
workflow.Name = "workflow"
|
|
||||||
}
|
|
||||||
return workflow
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
||||||
// Copyright 2021 The nektos/act Authors. All rights reserved.
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
package model
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
type stepStatus int
|
|
||||||
|
|
||||||
const (
|
|
||||||
StepStatusSuccess stepStatus = iota
|
|
||||||
StepStatusFailure
|
|
||||||
StepStatusSkipped
|
|
||||||
)
|
|
||||||
|
|
||||||
var stepStatusStrings = [...]string{
|
|
||||||
"success",
|
|
||||||
"failure",
|
|
||||||
"skipped",
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s stepStatus) MarshalText() ([]byte, error) {
|
|
||||||
return []byte(s.String()), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *stepStatus) UnmarshalText(b []byte) error {
|
|
||||||
str := string(b)
|
|
||||||
for i, name := range stepStatusStrings {
|
|
||||||
if name == str {
|
|
||||||
*s = stepStatus(i)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return fmt.Errorf("invalid step status %q", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s stepStatus) String() string {
|
|
||||||
if int(s) >= len(stepStatusStrings) {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return stepStatusStrings[s]
|
|
||||||
}
|
|
||||||
|
|
||||||
type StepResult struct {
|
|
||||||
Outputs map[string]string `json:"outputs"`
|
|
||||||
Conclusion stepStatus `json:"conclusion"`
|
|
||||||
Outcome stepStatus `json:"outcome"`
|
|
||||||
}
|
|
||||||
Whitespace-only changes.
@@ -1,12 +0,0 @@
|
|||||||
name: invalid-job-name-1
|
|
||||||
on: push
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
invalid-JOB-Name-v1.2.3-docker_hub:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: echo hi
|
|
||||||
valid-JOB-Name-v123-docker_hub:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: echo hi
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
name: invalid-job-name-2
|
|
||||||
on: push
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
1234invalid-JOB-Name-v123-docker_hub:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: echo hi
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
name: valid-job-name-1
|
|
||||||
on: push
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
valid-JOB-Name-v123-docker_hub:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: echo hi
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
name: valid-job-name-2
|
|
||||||
on: push
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
___valid-JOB-Name-v123-docker_hub:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: echo hi
|
|
||||||
-9
@@ -1,9 +0,0 @@
|
|||||||
name: Hello World Workflow
|
|
||||||
on: push
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
hello-world:
|
|
||||||
name: Hello World Job
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: echo "Hello World!"
|
|
||||||
Whitespace-only changes.
Vendored
-50
@@ -1,50 +0,0 @@
|
|||||||
---
|
|
||||||
jobs:
|
|
||||||
strategy-all:
|
|
||||||
name: ${{ matrix.node-version }} | ${{ matrix.site }} | ${{ matrix.datacenter }}
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: echo 'Hello!'
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
matrix:
|
|
||||||
datacenter:
|
|
||||||
- site-c
|
|
||||||
- site-d
|
|
||||||
exclude:
|
|
||||||
- datacenter: site-d
|
|
||||||
node-version: 14.x
|
|
||||||
site: staging
|
|
||||||
include:
|
|
||||||
- php-version: 5.4
|
|
||||||
- datacenter: site-a
|
|
||||||
node-version: 10.x
|
|
||||||
site: prod
|
|
||||||
- datacenter: site-b
|
|
||||||
node-version: 12.x
|
|
||||||
site: dev
|
|
||||||
node-version: [14.x, 16.x]
|
|
||||||
site:
|
|
||||||
- staging
|
|
||||||
max-parallel: 2
|
|
||||||
strategy-no-matrix:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: echo 'Hello!'
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
max-parallel: 2
|
|
||||||
strategy-only-fail-fast:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: echo 'Hello!'
|
|
||||||
strategy:
|
|
||||||
fail-fast: false
|
|
||||||
strategy-only-max-parallel:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- run: echo 'Hello!'
|
|
||||||
strategy:
|
|
||||||
max-parallel: 2
|
|
||||||
'on':
|
|
||||||
push: null
|
|
||||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -23,8 +23,8 @@ import (
|
|||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/common/git"
|
"gitea.com/gitea/runner/act/common/git"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/kballard/go-shellquote"
|
"github.com/kballard/go-shellquote"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func evaluateCompositeInputAndEnv(ctx context.Context, parent *RunContext, step actionStep) map[string]string {
|
func evaluateCompositeInputAndEnv(ctx context.Context, parent *RunContext, step actionStep) map[string]string {
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ import (
|
|||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/common/git"
|
"gitea.com/gitea/runner/act/common/git"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/exprparser"
|
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/exprparser"
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"go.yaml.in/yaml/v4"
|
"go.yaml.in/yaml/v4"
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/sirupsen/logrus/hooks/test"
|
"github.com/sirupsen/logrus/hooks/test"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|||||||
+14
-258
@@ -7,7 +7,6 @@ package runner
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"maps"
|
"maps"
|
||||||
"path"
|
"path"
|
||||||
@@ -18,12 +17,12 @@ import (
|
|||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
"gitea.com/gitea/runner/act/exprparser"
|
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
||||||
"github.com/rhysd/actionlint"
|
"gitea.dev/actionslib/pkg/expreval"
|
||||||
|
"gitea.dev/actionslib/pkg/exprparser"
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"go.yaml.in/yaml/v4"
|
"go.yaml.in/yaml/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -235,137 +234,16 @@ func (ee expressionEvaluator) evaluate(ctx context.Context, in string, defaultSt
|
|||||||
return evaluated, err
|
return evaluated, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ee expressionEvaluator) evaluateScalarYamlNode(ctx context.Context, node *yaml.Node) (*yaml.Node, error) {
|
// shared returns the evaluation layer of the shared library, bound to this context so the
|
||||||
var in string
|
// evaluation of every single expression is still logged and masked here.
|
||||||
if err := node.Decode(&in); err != nil {
|
func (ee expressionEvaluator) shared(ctx context.Context) expreval.Evaluator {
|
||||||
return nil, err
|
return expreval.New(func(in string, defaultStatusCheck exprparser.DefaultStatusCheck) (any, error) {
|
||||||
}
|
return ee.evaluate(ctx, in, defaultStatusCheck)
|
||||||
if !strings.Contains(in, "${{") || !strings.Contains(in, "}}") {
|
})
|
||||||
return nil, nil //nolint:nilnil // pre-existing issue from nektos/act
|
|
||||||
}
|
|
||||||
res, err := ee.evaluateScalar(ctx, in)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ret := &yaml.Node{}
|
|
||||||
if err := ret.Encode(res); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return ret, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ee expressionEvaluator) evaluateMappingYamlNode(ctx context.Context, node *yaml.Node) (*yaml.Node, error) {
|
|
||||||
var ret *yaml.Node
|
|
||||||
// GitHub has this undocumented feature to merge maps, called insert directive
|
|
||||||
insertDirective := regexp.MustCompile(`\${{\s*insert\s*}}`)
|
|
||||||
for i := 0; i < len(node.Content)/2; i++ {
|
|
||||||
changed := func() error {
|
|
||||||
if ret == nil {
|
|
||||||
ret = &yaml.Node{}
|
|
||||||
if err := ret.Encode(node); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
ret.Content = ret.Content[:i*2]
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
k := node.Content[i*2]
|
|
||||||
v := node.Content[i*2+1]
|
|
||||||
ev, err := ee.evaluateYamlNodeInternal(ctx, v)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if ev != nil {
|
|
||||||
if err := changed(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ev = v
|
|
||||||
}
|
|
||||||
var sk string
|
|
||||||
// Merge the nested map of the insert directive
|
|
||||||
if k.Decode(&sk) == nil && insertDirective.MatchString(sk) {
|
|
||||||
if ev.Kind != yaml.MappingNode {
|
|
||||||
return nil, fmt.Errorf("failed to insert node %v into mapping %v unexpected type %v expected MappingNode", ev, node, ev.Kind)
|
|
||||||
}
|
|
||||||
if err := changed(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ret.Content = append(ret.Content, ev.Content...)
|
|
||||||
} else {
|
|
||||||
ek, err := ee.evaluateYamlNodeInternal(ctx, k)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if ek != nil {
|
|
||||||
if err := changed(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ek = k
|
|
||||||
}
|
|
||||||
if ret != nil {
|
|
||||||
ret.Content = append(ret.Content, ek, ev)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ee expressionEvaluator) evaluateSequenceYamlNode(ctx context.Context, node *yaml.Node) (*yaml.Node, error) {
|
|
||||||
var ret *yaml.Node
|
|
||||||
for i := 0; i < len(node.Content); i++ {
|
|
||||||
v := node.Content[i]
|
|
||||||
// Preserve nested sequences
|
|
||||||
wasseq := v.Kind == yaml.SequenceNode
|
|
||||||
ev, err := ee.evaluateYamlNodeInternal(ctx, v)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if ev != nil {
|
|
||||||
if ret == nil {
|
|
||||||
ret = &yaml.Node{}
|
|
||||||
if err := ret.Encode(node); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ret.Content = ret.Content[:i]
|
|
||||||
}
|
|
||||||
// GitHub has this undocumented feature to merge sequences / arrays
|
|
||||||
// We have a nested sequence via evaluation, merge the arrays
|
|
||||||
if ev.Kind == yaml.SequenceNode && !wasseq {
|
|
||||||
ret.Content = append(ret.Content, ev.Content...)
|
|
||||||
} else {
|
|
||||||
ret.Content = append(ret.Content, ev)
|
|
||||||
}
|
|
||||||
} else if ret != nil {
|
|
||||||
ret.Content = append(ret.Content, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ee expressionEvaluator) evaluateYamlNodeInternal(ctx context.Context, node *yaml.Node) (*yaml.Node, error) {
|
|
||||||
switch node.Kind {
|
|
||||||
case yaml.ScalarNode:
|
|
||||||
return ee.evaluateScalarYamlNode(ctx, node)
|
|
||||||
case yaml.MappingNode:
|
|
||||||
return ee.evaluateMappingYamlNode(ctx, node)
|
|
||||||
case yaml.SequenceNode:
|
|
||||||
return ee.evaluateSequenceYamlNode(ctx, node)
|
|
||||||
default:
|
|
||||||
return nil, nil //nolint:nilnil // pre-existing issue from nektos/act
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ee expressionEvaluator) EvaluateYamlNode(ctx context.Context, node *yaml.Node) error {
|
func (ee expressionEvaluator) EvaluateYamlNode(ctx context.Context, node *yaml.Node) error {
|
||||||
ret, err := ee.evaluateYamlNodeInternal(ctx, node)
|
return ee.shared(ctx).EvaluateYamlNode(node)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if ret != nil {
|
|
||||||
return ret.Decode(node)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ee expressionEvaluator) Interpolate(ctx context.Context, in string) string {
|
func (ee expressionEvaluator) Interpolate(ctx context.Context, in string) string {
|
||||||
@@ -377,138 +255,16 @@ func (ee expressionEvaluator) Interpolate(ctx context.Context, in string) string
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// interpolate evaluates every part on its own, so a malformed one cannot restructure its neighbours
|
|
||||||
func (ee expressionEvaluator) interpolate(ctx context.Context, in string) (string, error) {
|
func (ee expressionEvaluator) interpolate(ctx context.Context, in string) (string, error) {
|
||||||
parts, err := splitSubExpressions(in)
|
return ee.shared(ctx).Interpolate(in)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if len(parts) == 1 && !parts[0].isExpr {
|
|
||||||
return in, nil
|
|
||||||
}
|
|
||||||
var out strings.Builder
|
|
||||||
out.Grow(len(in))
|
|
||||||
for _, part := range parts {
|
|
||||||
if !part.isExpr {
|
|
||||||
out.WriteString(part.text)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
evaluated, err := ee.evaluate(ctx, part.text, exprparser.DefaultStatusCheckNone)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
out.WriteString(exprparser.CoerceToString(evaluated))
|
|
||||||
}
|
|
||||||
return out.String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// evaluateScalar keeps the type of a lone expression, so `${{ fromJSON('[1,2]') }}` stays an array
|
|
||||||
func (ee expressionEvaluator) evaluateScalar(ctx context.Context, in string) (any, error) {
|
|
||||||
parts, err := splitSubExpressions(in)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(parts) == 1 && parts[0].isExpr {
|
|
||||||
return ee.evaluate(ctx, parts[0].text, exprparser.DefaultStatusCheckNone)
|
|
||||||
}
|
|
||||||
return ee.interpolate(ctx, in)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// EvalBool evaluates an expression against given evaluator. An `if:` is an expression even without
|
// EvalBool evaluates an expression against given evaluator. An `if:` is an expression even without
|
||||||
// `${{ }}`, while literal text around one makes the whole value a string.
|
// `${{ }}`, while literal text around one makes the whole value a string.
|
||||||
func EvalBool(ctx context.Context, evaluator ExpressionEvaluator, expr string, defaultStatusCheck exprparser.DefaultStatusCheck) (bool, error) {
|
func EvalBool(ctx context.Context, evaluator ExpressionEvaluator, expr string, defaultStatusCheck exprparser.DefaultStatusCheck) (bool, error) {
|
||||||
parts, err := splitSubExpressions(expr)
|
return expreval.New(func(in string, dsc exprparser.DefaultStatusCheck) (any, error) {
|
||||||
if err != nil {
|
return evaluator.evaluate(ctx, in, dsc)
|
||||||
return false, err
|
}).EvalBool(expr, defaultStatusCheck)
|
||||||
}
|
|
||||||
if len(parts) == 1 {
|
|
||||||
evaluated, err := evaluator.evaluate(ctx, parts[0].text, defaultStatusCheck)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
return exprparser.IsTruthy(evaluated), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// mixed content is a string, so the status check applies to it separately
|
|
||||||
if defaultStatusCheck != exprparser.DefaultStatusCheckNone && !callsStatusFunction(parts) {
|
|
||||||
status, err := evaluator.evaluate(ctx, "", defaultStatusCheck)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
if !exprparser.IsTruthy(status) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
interpolated, err := evaluator.interpolate(ctx, expr)
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
return exprparser.IsTruthy(interpolated), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// callsStatusFunction reports whether any part calls a status function. A part that does not parse
|
|
||||||
// counts as one, so the evaluation reports it against the real values.
|
|
||||||
func callsStatusFunction(parts []exprPart) bool {
|
|
||||||
for _, part := range parts {
|
|
||||||
if !part.isExpr {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// The lexer needs the closing `}}` that the scanner strips.
|
|
||||||
exprNode, err := actionlint.NewExprParser().Parse(actionlint.NewExprLexer(part.text + "}}"))
|
|
||||||
if err != nil || exprparser.CallsStatusFunction(exprNode) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
type exprPart struct {
|
|
||||||
text string
|
|
||||||
isExpr bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// splitSubExpressions splits in the way GitHub's template reader does, leaving a value without a
|
|
||||||
// complete expression literal.
|
|
||||||
func splitSubExpressions(in string) ([]exprPart, error) {
|
|
||||||
if !strings.Contains(in, "${{") || !strings.Contains(in, "}}") {
|
|
||||||
return []exprPart{{text: in}}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
parts := make([]exprPart, 0, 2*strings.Count(in, "${{")+1)
|
|
||||||
for {
|
|
||||||
start := strings.Index(in, "${{")
|
|
||||||
if start < 0 {
|
|
||||||
if in != "" {
|
|
||||||
parts = append(parts, exprPart{text: in})
|
|
||||||
}
|
|
||||||
return parts, nil
|
|
||||||
}
|
|
||||||
if start > 0 {
|
|
||||||
parts = append(parts, exprPart{text: in[:start]})
|
|
||||||
}
|
|
||||||
rest := in[start+len("${{"):]
|
|
||||||
end := indexExprEnd(rest)
|
|
||||||
if end < 0 {
|
|
||||||
return nil, errors.New("unclosed expression")
|
|
||||||
}
|
|
||||||
parts = append(parts, exprPart{text: strings.TrimSpace(rest[:end]), isExpr: true})
|
|
||||||
in = rest[end+len("}}"):]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// indexExprEnd returns the offset of the `}}` ending an expression, or -1. A quote toggles string
|
|
||||||
// state, so a `}}` inside a string does not end it.
|
|
||||||
func indexExprEnd(in string) int {
|
|
||||||
inString := false
|
|
||||||
for i := range len(in) {
|
|
||||||
switch {
|
|
||||||
case in[i] == '\'':
|
|
||||||
inString = !inString
|
|
||||||
case !inString && in[i] == '}' && i+1 < len(in) && in[i+1] == '}':
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getEvaluatorInputs(ctx context.Context, rc *RunContext, step step, ghc *model.GithubContext) map[string]any {
|
func getEvaluatorInputs(ctx context.Context, rc *RunContext, step step, ghc *model.GithubContext) map[string]any {
|
||||||
|
|||||||
@@ -9,9 +9,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/exprparser"
|
"gitea.dev/actionslib/pkg/exprparser"
|
||||||
"gitea.com/gitea/runner/act/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
|
|
||||||
assert "github.com/stretchr/testify/assert"
|
assert "github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
yaml "go.yaml.in/yaml/v4"
|
yaml "go.yaml.in/yaml/v4"
|
||||||
@@ -279,41 +278,6 @@ func TestInterpolate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSplitSubExpressions(t *testing.T) {
|
|
||||||
expr := func(text string) exprPart { return exprPart{text: text, isExpr: true} }
|
|
||||||
literal := func(text string) exprPart { return exprPart{text: text} }
|
|
||||||
|
|
||||||
for _, tt := range []struct {
|
|
||||||
in string
|
|
||||||
want []exprPart
|
|
||||||
}{
|
|
||||||
{"Hello World", []exprPart{literal("Hello World")}},
|
|
||||||
{"${{ true }}", []exprPart{expr("true")}},
|
|
||||||
{"${{ true }} ${{ false }}", []exprPart{expr("true"), literal(" "), expr("false")}},
|
|
||||||
{"Hello ${{ 'World' }}", []exprPart{literal("Hello "), expr("'World'")}},
|
|
||||||
// a quote toggles string state, so a `}}` inside a string does not end the expression
|
|
||||||
{"${{ '}}' }}", []exprPart{expr("'}}'")}},
|
|
||||||
{"${{ '''}}''' }}", []exprPart{expr("'''}}'''")}},
|
|
||||||
{"${{ '''' }}", []exprPart{expr("''''")}},
|
|
||||||
{`${{ fromJSON('"}}"') }}`, []exprPart{expr(`fromJSON('"}}"')`)}},
|
|
||||||
{`${{ fromJSON('"\"}}\""') }}`, []exprPart{expr(`fromJSON('"\"}}\""')`)}},
|
|
||||||
{`${{ fromJSON('"''}}"') }}`, []exprPart{expr(`fromJSON('"''}}"')`)}},
|
|
||||||
// without a complete literal the value stays text, as GitHub's template reader leaves it
|
|
||||||
{"${{ 1", []exprPart{literal("${{ 1")}},
|
|
||||||
// a malformed part stays one part, so it cannot restructure its neighbours
|
|
||||||
{"${{ 1) && (2 }}", []exprPart{expr("1) && (2")}},
|
|
||||||
} {
|
|
||||||
got, err := splitSubExpressions(tt.in)
|
|
||||||
require.NoError(t, err, tt.in)
|
|
||||||
assert.Equal(t, tt.want, got, tt.in)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, in := range []string{"${{ 'a' }} ${{ b", "${{ 'a }}"} {
|
|
||||||
_, err := splitSubExpressions(in)
|
|
||||||
assert.ErrorContains(t, err, "unclosed expression", in)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetEvaluatorInputsBoolean(t *testing.T) {
|
func TestGetEvaluatorInputsBoolean(t *testing.T) {
|
||||||
workflows := map[string]string{
|
workflows := map[string]string{
|
||||||
"workflow_call": `
|
"workflow_call": `
|
||||||
|
|||||||
@@ -23,8 +23,9 @@ import (
|
|||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
"gitea.com/gitea/runner/act/exprparser"
|
|
||||||
"gitea.com/gitea/runner/act/model"
|
"gitea.dev/actionslib/pkg/exprparser"
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
const maxJobSummaryBytes = 1024 * 1024
|
const maxJobSummaryBytes = 1024 * 1024
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ import (
|
|||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
logrustest "github.com/sirupsen/logrus/hooks/test"
|
logrustest "github.com/sirupsen/logrus/hooks/test"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/sirupsen/logrus/hooks/test"
|
"github.com/sirupsen/logrus/hooks/test"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ package runner
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/model"
|
"gitea.dev/actionslib/pkg/model"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"go.yaml.in/yaml/v4"
|
"go.yaml.in/yaml/v4"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ import (
|
|||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/common/git"
|
"gitea.com/gitea/runner/act/common/git"
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newLocalReusableWorkflowExecutor(rc *RunContext) common.Executor {
|
func newLocalReusableWorkflowExecutor(rc *RunContext) common.Executor {
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common/git"
|
"gitea.com/gitea/runner/act/common/git"
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -27,10 +27,11 @@ import (
|
|||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
"gitea.com/gitea/runner/act/exprparser"
|
"gitea.com/gitea/runner/act/ghcontext"
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
"gitea.com/gitea/runner/internal/pkg/lock"
|
"gitea.com/gitea/runner/internal/pkg/lock"
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/exprparser"
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/docker/cli/cli/compose/loader"
|
"github.com/docker/cli/cli/compose/loader"
|
||||||
"github.com/docker/go-connections/nat"
|
"github.com/docker/go-connections/nat"
|
||||||
"github.com/moby/moby/api/types/mount"
|
"github.com/moby/moby/api/types/mount"
|
||||||
@@ -1355,12 +1356,12 @@ func (rc *RunContext) getGithubContext(ctx context.Context) *model.GithubContext
|
|||||||
|
|
||||||
ghc.SetBaseAndHeadRef()
|
ghc.SetBaseAndHeadRef()
|
||||||
repoPath := rc.Config.Workdir
|
repoPath := rc.Config.Workdir
|
||||||
ghc.SetRepositoryAndOwner(ctx, rc.Config.GitHubInstance, rc.Config.RemoteName, repoPath)
|
ghcontext.SetRepositoryAndOwner(ctx, ghc, rc.Config.GitHubInstance, rc.Config.RemoteName, repoPath)
|
||||||
if ghc.Ref == "" {
|
if ghc.Ref == "" {
|
||||||
ghc.SetRef(ctx, rc.Config.DefaultBranch, repoPath)
|
ghcontext.SetRef(ctx, ghc, rc.Config.DefaultBranch, repoPath)
|
||||||
}
|
}
|
||||||
if ghc.Sha == "" {
|
if ghc.Sha == "" {
|
||||||
ghc.SetSha(ctx, repoPath)
|
ghcontext.SetSha(ctx, ghc, repoPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
ghc.SetRefTypeAndName()
|
ghc.SetRefTypeAndName()
|
||||||
|
|||||||
@@ -17,9 +17,9 @@ import (
|
|||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
"gitea.com/gitea/runner/act/exprparser"
|
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/exprparser"
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/docker/cli/cli/compose/loader"
|
"github.com/docker/cli/cli/compose/loader"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
assert "github.com/stretchr/testify/assert"
|
assert "github.com/stretchr/testify/assert"
|
||||||
|
|||||||
@@ -16,8 +16,8 @@ import (
|
|||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
docker_container "github.com/moby/moby/api/types/container"
|
docker_container "github.com/moby/moby/api/types/container"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
assert "github.com/stretchr/testify/assert"
|
assert "github.com/stretchr/testify/assert"
|
||||||
@@ -312,7 +312,7 @@ func TestRunEvent(t *testing.T) {
|
|||||||
{workdir, "workflow_dispatch-scalar", "workflow_dispatch", "", platforms, secrets},
|
{workdir, "workflow_dispatch-scalar", "workflow_dispatch", "", platforms, secrets},
|
||||||
{workdir, "workflow_dispatch-scalar-composite-action", "workflow_dispatch", "", platforms, secrets},
|
{workdir, "workflow_dispatch-scalar-composite-action", "workflow_dispatch", "", platforms, secrets},
|
||||||
{workdir, "job-needs-context-contains-result", "push", "", platforms, secrets},
|
{workdir, "job-needs-context-contains-result", "push", "", platforms, secrets},
|
||||||
{"../model/testdata", "container-volumes", "push", "", platforms, secrets},
|
{workdir, "container-volumes", "push", "", platforms, secrets},
|
||||||
{workdir, "path-handling", "push", "", platforms, secrets},
|
{workdir, "path-handling", "push", "", platforms, secrets},
|
||||||
{workdir, "do-not-leak-step-env-in-composite", "push", "", platforms, secrets},
|
{workdir, "do-not-leak-step-env-in-composite", "push", "", platforms, secrets},
|
||||||
{workdir, "set-env-step-env-override", "push", "", platforms, secrets},
|
{workdir, "set-env-step-env-override", "push", "", platforms, secrets},
|
||||||
|
|||||||
+3
-2
@@ -15,8 +15,9 @@ import (
|
|||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/container"
|
"gitea.com/gitea/runner/act/container"
|
||||||
"gitea.com/gitea/runner/act/exprparser"
|
|
||||||
"gitea.com/gitea/runner/act/model"
|
"gitea.dev/actionslib/pkg/exprparser"
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
type step interface {
|
type step interface {
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
type stepActionLocal struct {
|
type stepActionLocal struct {
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitea.com/gitea/runner/act/common"
|
"gitea.com/gitea/runner/act/common"
|
||||||
"gitea.com/gitea/runner/act/model"
|
|
||||||
|
|
||||||
|
"gitea.dev/actionslib/pkg/model"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
"go.yaml.in/yaml/v4"
|
"go.yaml.in/yaml/v4"
|
||||||
|
|||||||
Loaded 50 of 87 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user