summaryrefslogtreecommitdiffstats
path: root/pkg/jobparser/interpeter.go
blob: 8aaf31961c488431dcfb6abdc6b04dabd3f32862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package jobparser

import (
	"github.com/nektos/act/pkg/exprparser"
	"github.com/nektos/act/pkg/model"
	"gopkg.in/yaml.v3"
)

// NewInterpeter returns an interpeter used in the server,
// need github, needs, strategy, matrix, inputs context only,
// see https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability
func NewInterpeter(
	jobID string,
	job *model.Job,
	matrix map[string]interface{},
	gitCtx *model.GithubContext,
	results map[string]*JobResult,
	vars map[string]string,
) exprparser.Interpreter {
	strategy := make(map[string]interface{})
	if job.Strategy != nil {
		strategy["fail-fast"] = job.Strategy.FailFast
		strategy["max-parallel"] = job.Strategy.MaxParallel
	}

	run := &model.Run{
		Workflow: &model.Workflow{
			Jobs: map[string]*model.Job{},
		},
		JobID: jobID,
	}
	for id, result := range results {
		need := yaml.Node{}
		_ = need.Encode(result.Needs)
		run.Workflow.Jobs[id] = &model.Job{
			RawNeeds: need,
			Result:   result.Result,
			Outputs:  result.Outputs,
		}
	}

	jobs := run.Workflow.Jobs
	jobNeeds := run.Job().Needs()

	using := map[string]exprparser.Needs{}
	for _, need := range jobNeeds {
		if v, ok := jobs[need]; ok {
			using[need] = exprparser.Needs{
				Outputs: v.Outputs,
				Result:  v.Result,
			}
		}
	}

	ee := &exprparser.EvaluationEnvironment{
		Github:   gitCtx,
		Env:      nil, // no need
		Job:      nil, // no need
		Steps:    nil, // no need
		Runner:   nil, // no need
		Secrets:  nil, // no need
		Strategy: strategy,
		Matrix:   matrix,
		Needs:    using,
		Inputs:   nil, // not supported yet
		Vars:     vars,
	}

	config := exprparser.Config{
		Run:        run,
		WorkingDir: "", // WorkingDir is used for  the function hashFiles, but it's not needed in the server
		Context:    "job",
	}

	return exprparser.NewInterpeter(ee, config)
}

// JobResult is the minimum requirement of job results for Interpeter
type JobResult struct {
	Needs   []string
	Result  string
	Outputs map[string]string
}