Skip to content

Commit ed01f46

Browse files
Björn BrauerMarkus Wolfmergify[bot]
authored
refactor: export and move shared contexts into pkg/model (nektos#931)
This commit moves the githubContext, jobContext and stepResult structs from the runner package to the model package in preparation for nektos#908 because the expression.go file lives in the runner package and would introduce cyclic dependencies with the exprparser package. Co-authored-by: Markus Wolf <markus.wolf@new-work.se> Co-authored-by: Markus Wolf <markus.wolf@new-work.se> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 5580812 commit ed01f46

10 files changed

Lines changed: 132 additions & 124 deletions

pkg/model/github_context.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package model
2+
3+
type GithubContext struct {
4+
Event map[string]interface{} `json:"event"`
5+
EventPath string `json:"event_path"`
6+
Workflow string `json:"workflow"`
7+
RunID string `json:"run_id"`
8+
RunNumber string `json:"run_number"`
9+
Actor string `json:"actor"`
10+
Repository string `json:"repository"`
11+
EventName string `json:"event_name"`
12+
Sha string `json:"sha"`
13+
Ref string `json:"ref"`
14+
HeadRef string `json:"head_ref"`
15+
BaseRef string `json:"base_ref"`
16+
Token string `json:"token"`
17+
Workspace string `json:"workspace"`
18+
Action string `json:"action"`
19+
ActionPath string `json:"action_path"`
20+
ActionRef string `json:"action_ref"`
21+
ActionRepository string `json:"action_repository"`
22+
Job string `json:"job"`
23+
JobName string `json:"job_name"`
24+
RepositoryOwner string `json:"repository_owner"`
25+
RetentionDays string `json:"retention_days"`
26+
RunnerPerflog string `json:"runner_perflog"`
27+
RunnerTrackingID string `json:"runner_tracking_id"`
28+
}

pkg/model/job_context.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package model
2+
3+
type JobContext struct {
4+
Status string `json:"status"`
5+
Container struct {
6+
ID string `json:"id"`
7+
Network string `json:"network"`
8+
} `json:"container"`
9+
Services map[string]struct {
10+
ID string `json:"id"`
11+
} `json:"services"`
12+
}

pkg/model/step_result.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package model
2+
3+
import "fmt"
4+
5+
type stepStatus int
6+
7+
const (
8+
StepStatusSuccess stepStatus = iota
9+
StepStatusFailure
10+
)
11+
12+
var stepStatusStrings = [...]string{
13+
"success",
14+
"failure",
15+
}
16+
17+
func (s stepStatus) MarshalText() ([]byte, error) {
18+
return []byte(s.String()), nil
19+
}
20+
21+
func (s *stepStatus) UnmarshalText(b []byte) error {
22+
str := string(b)
23+
for i, name := range stepStatusStrings {
24+
if name == str {
25+
*s = stepStatus(i)
26+
return nil
27+
}
28+
}
29+
return fmt.Errorf("invalid step status %q", str)
30+
}
31+
32+
func (s stepStatus) String() string {
33+
if int(s) >= len(stepStatusStrings) {
34+
return ""
35+
}
36+
return stepStatusStrings[s]
37+
}
38+
39+
type StepResult struct {
40+
Outputs map[string]string `json:"outputs"`
41+
Conclusion stepStatus `json:"conclusion"`
42+
Outcome stepStatus `json:"outcome"`
43+
}

pkg/runner/command_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/stretchr/testify/assert"
99

1010
"github.com/nektos/act/pkg/common"
11+
"github.com/nektos/act/pkg/model"
1112
)
1213

1314
func TestSetEnv(t *testing.T) {
@@ -24,11 +25,11 @@ func TestSetOutput(t *testing.T) {
2425
a := assert.New(t)
2526
ctx := context.Background()
2627
rc := new(RunContext)
27-
rc.StepResults = make(map[string]*stepResult)
28+
rc.StepResults = make(map[string]*model.StepResult)
2829
handler := rc.commandHandler(ctx)
2930

3031
rc.CurrentStep = "my-step"
31-
rc.StepResults[rc.CurrentStep] = &stepResult{
32+
rc.StepResults[rc.CurrentStep] = &model.StepResult{
3233
Outputs: make(map[string]string),
3334
}
3435
handler("::set-output name=x::valz\n")

pkg/runner/expression_test.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,24 @@ func TestEvaluate(t *testing.T) {
4747
"os": "Linux",
4848
"foo": "bar",
4949
},
50-
StepResults: map[string]*stepResult{
50+
StepResults: map[string]*model.StepResult{
5151
"idwithnothing": {
52-
Conclusion: stepStatusSuccess,
53-
Outcome: stepStatusFailure,
52+
Conclusion: model.StepStatusSuccess,
53+
Outcome: model.StepStatusFailure,
5454
Outputs: map[string]string{
5555
"foowithnothing": "barwithnothing",
5656
},
5757
},
5858
"id-with-hyphens": {
59-
Conclusion: stepStatusSuccess,
60-
Outcome: stepStatusFailure,
59+
Conclusion: model.StepStatusSuccess,
60+
Outcome: model.StepStatusFailure,
6161
Outputs: map[string]string{
6262
"foo-with-hyphens": "bar-with-hyphens",
6363
},
6464
},
6565
"id_with_underscores": {
66-
Conclusion: stepStatusSuccess,
67-
Outcome: stepStatusFailure,
66+
Conclusion: model.StepStatusSuccess,
67+
Outcome: model.StepStatusFailure,
6868
Outputs: map[string]string{
6969
"foo_with_underscores": "bar_with_underscores",
7070
},
@@ -232,6 +232,7 @@ func updateTestExpressionWorkflow(t *testing.T, tables []struct {
232232
envs += fmt.Sprintf(" %s: %s\n", k, rc.Env[k])
233233
}
234234

235+
// editorconfig-checker-disable
235236
workflow := fmt.Sprintf(`
236237
name: "Test how expressions are handled on GitHub"
237238
on: push
@@ -244,6 +245,7 @@ jobs:
244245
runs-on: ubuntu-latest
245246
steps:
246247
`, envs)
248+
// editorconfig-checker-enable
247249
for _, table := range tables {
248250
expressionPattern = regexp.MustCompile(`\${{\s*(.+?)\s*}}`)
249251

pkg/runner/run_context.go

Lines changed: 18 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type RunContext struct {
3535
Env map[string]string
3636
ExtraPath []string
3737
CurrentStep string
38-
StepResults map[string]*stepResult
38+
StepResults map[string]*model.StepResult
3939
ExprEval ExpressionEvaluator
4040
JobContainer container.Container
4141
OutputMappings map[MappableOutput]MappableOutput
@@ -53,7 +53,7 @@ func (rc *RunContext) Clone() *RunContext {
5353
clone.CurrentStep = ""
5454
clone.Composite = nil
5555
clone.Inputs = nil
56-
clone.StepResults = make(map[string]*stepResult)
56+
clone.StepResults = make(map[string]*model.StepResult)
5757
clone.Parent = rc
5858
return &clone
5959
}
@@ -67,46 +67,6 @@ func (rc *RunContext) String() string {
6767
return fmt.Sprintf("%s/%s", rc.Run.Workflow.Name, rc.Name)
6868
}
6969

70-
type stepStatus int
71-
72-
const (
73-
stepStatusSuccess stepStatus = iota
74-
stepStatusFailure
75-
)
76-
77-
var stepStatusStrings = [...]string{
78-
"success",
79-
"failure",
80-
}
81-
82-
func (s stepStatus) MarshalText() ([]byte, error) {
83-
return []byte(s.String()), nil
84-
}
85-
86-
func (s *stepStatus) UnmarshalText(b []byte) error {
87-
str := string(b)
88-
for i, name := range stepStatusStrings {
89-
if name == str {
90-
*s = stepStatus(i)
91-
return nil
92-
}
93-
}
94-
return fmt.Errorf("invalid step status %q", str)
95-
}
96-
97-
func (s stepStatus) String() string {
98-
if int(s) >= len(stepStatusStrings) {
99-
return ""
100-
}
101-
return stepStatusStrings[s]
102-
}
103-
104-
type stepResult struct {
105-
Outputs map[string]string `json:"outputs"`
106-
Conclusion stepStatus `json:"conclusion"`
107-
Outcome stepStatus `json:"outcome"`
108-
}
109-
11070
// GetEnv returns the env for the context
11171
func (rc *RunContext) GetEnv() map[string]string {
11272
if rc.Env == nil {
@@ -349,16 +309,16 @@ func (rc *RunContext) newStepExecutor(step *model.Step) common.Executor {
349309
}
350310
return func(ctx context.Context) error {
351311
rc.CurrentStep = sc.Step.ID
352-
rc.StepResults[rc.CurrentStep] = &stepResult{
353-
Outcome: stepStatusSuccess,
354-
Conclusion: stepStatusSuccess,
312+
rc.StepResults[rc.CurrentStep] = &model.StepResult{
313+
Outcome: model.StepStatusSuccess,
314+
Conclusion: model.StepStatusSuccess,
355315
Outputs: make(map[string]string),
356316
}
357317

358318
runStep, err := sc.isEnabled(ctx)
359319
if err != nil {
360-
rc.StepResults[rc.CurrentStep].Conclusion = stepStatusFailure
361-
rc.StepResults[rc.CurrentStep].Outcome = stepStatusFailure
320+
rc.StepResults[rc.CurrentStep].Conclusion = model.StepStatusFailure
321+
rc.StepResults[rc.CurrentStep].Outcome = model.StepStatusFailure
362322
return err
363323
}
364324

@@ -380,13 +340,13 @@ func (rc *RunContext) newStepExecutor(step *model.Step) common.Executor {
380340
} else {
381341
common.Logger(ctx).Errorf(" \u274C Failure - %s", sc.Step)
382342

383-
rc.StepResults[rc.CurrentStep].Outcome = stepStatusFailure
343+
rc.StepResults[rc.CurrentStep].Outcome = model.StepStatusFailure
384344
if sc.Step.ContinueOnError {
385345
common.Logger(ctx).Infof("Failed but continue next step")
386346
err = nil
387-
rc.StepResults[rc.CurrentStep].Conclusion = stepStatusSuccess
347+
rc.StepResults[rc.CurrentStep].Conclusion = model.StepStatusSuccess
388348
} else {
389-
rc.StepResults[rc.CurrentStep].Conclusion = stepStatusFailure
349+
rc.StepResults[rc.CurrentStep].Conclusion = model.StepStatusFailure
390350
}
391351
}
392352
return err
@@ -522,31 +482,20 @@ func trimToLen(s string, l int) string {
522482
return s
523483
}
524484

525-
type jobContext struct {
526-
Status string `json:"status"`
527-
Container struct {
528-
ID string `json:"id"`
529-
Network string `json:"network"`
530-
} `json:"container"`
531-
Services map[string]struct {
532-
ID string `json:"id"`
533-
} `json:"services"`
534-
}
535-
536-
func (rc *RunContext) getJobContext() *jobContext {
485+
func (rc *RunContext) getJobContext() *model.JobContext {
537486
jobStatus := "success"
538487
for _, stepStatus := range rc.StepResults {
539-
if stepStatus.Conclusion == stepStatusFailure {
488+
if stepStatus.Conclusion == model.StepStatusFailure {
540489
jobStatus = "failure"
541490
break
542491
}
543492
}
544-
return &jobContext{
493+
return &model.JobContext{
545494
Status: jobStatus,
546495
}
547496
}
548497

549-
func (rc *RunContext) getStepsContext() map[string]*stepResult {
498+
func (rc *RunContext) getStepsContext() map[string]*model.StepResult {
550499
return rc.StepResults
551500
}
552501

@@ -561,35 +510,8 @@ func (rc *RunContext) getNeedsTransitive(job *model.Job) []string {
561510
return needs
562511
}
563512

564-
type githubContext struct {
565-
Event map[string]interface{} `json:"event"`
566-
EventPath string `json:"event_path"`
567-
Workflow string `json:"workflow"`
568-
RunID string `json:"run_id"`
569-
RunNumber string `json:"run_number"`
570-
Actor string `json:"actor"`
571-
Repository string `json:"repository"`
572-
EventName string `json:"event_name"`
573-
Sha string `json:"sha"`
574-
Ref string `json:"ref"`
575-
HeadRef string `json:"head_ref"`
576-
BaseRef string `json:"base_ref"`
577-
Token string `json:"token"`
578-
Workspace string `json:"workspace"`
579-
Action string `json:"action"`
580-
ActionPath string `json:"action_path"`
581-
ActionRef string `json:"action_ref"`
582-
ActionRepository string `json:"action_repository"`
583-
Job string `json:"job"`
584-
JobName string `json:"job_name"`
585-
RepositoryOwner string `json:"repository_owner"`
586-
RetentionDays string `json:"retention_days"`
587-
RunnerPerflog string `json:"runner_perflog"`
588-
RunnerTrackingID string `json:"runner_tracking_id"`
589-
}
590-
591-
func (rc *RunContext) getGithubContext() *githubContext {
592-
ghc := &githubContext{
513+
func (rc *RunContext) getGithubContext() *model.GithubContext {
514+
ghc := &model.GithubContext{
593515
Event: make(map[string]interface{}),
594516
EventPath: ActPath + "/workflow/event.json",
595517
Workflow: rc.Run.Workflow.Name,
@@ -685,7 +607,7 @@ func (rc *RunContext) getGithubContext() *githubContext {
685607
return ghc
686608
}
687609

688-
func (ghc *githubContext) isLocalCheckout(step *model.Step) bool {
610+
func isLocalCheckout(ghc *model.GithubContext, step *model.Step) bool {
689611
if step.Type() == model.StepTypeInvalid {
690612
// This will be errored out by the executor later, we need this here to avoid a null panic though
691613
return false
@@ -839,7 +761,7 @@ func setActionRuntimeVars(rc *RunContext, env map[string]string) {
839761
func (rc *RunContext) localCheckoutPath() (string, bool) {
840762
ghContext := rc.getGithubContext()
841763
for _, step := range rc.Run.Job().Steps {
842-
if ghContext.isLocalCheckout(step) {
764+
if isLocalCheckout(ghContext, step) {
843765
return step.With["path"], true
844766
}
845767
}

pkg/runner/run_context_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ func TestRunContext_EvalBool(t *testing.T) {
5353
"os": "Linux",
5454
"foo": "bar",
5555
},
56-
StepResults: map[string]*stepResult{
56+
StepResults: map[string]*model.StepResult{
5757
"id1": {
58-
Conclusion: stepStatusSuccess,
59-
Outcome: stepStatusFailure,
58+
Conclusion: model.StepStatusSuccess,
59+
Outcome: model.StepStatusFailure,
6060
Outputs: map[string]string{
6161
"foo": "bar",
6262
},
@@ -312,7 +312,7 @@ func TestGetGitHubContext(t *testing.T) {
312312
Matrix: map[string]interface{}{},
313313
Env: map[string]string{},
314314
ExtraPath: []string{},
315-
StepResults: map[string]*stepResult{},
315+
StepResults: map[string]*model.StepResult{},
316316
OutputMappings: map[MappableOutput]MappableOutput{},
317317
}
318318

pkg/runner/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (runner *runnerImpl) newRunContext(run *model.Run, matrix map[string]interf
188188
Config: runner.config,
189189
Run: run,
190190
EventJSON: runner.eventJSON,
191-
StepResults: make(map[string]*stepResult),
191+
StepResults: make(map[string]*model.StepResult),
192192
Matrix: matrix,
193193
}
194194
rc.ExprEval = rc.NewExpressionEvaluator()

pkg/runner/step_context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (sc *StepContext) Executor(ctx context.Context) common.Executor {
7777
remoteAction.URL = rc.Config.GitHubInstance
7878

7979
github := rc.getGithubContext()
80-
if remoteAction.IsCheckout() && github.isLocalCheckout(step) {
80+
if remoteAction.IsCheckout() && isLocalCheckout(github, step) {
8181
return func(ctx context.Context) error {
8282
common.Logger(ctx).Debugf("Skipping local actions/checkout because workdir was already copied")
8383
return nil

0 commit comments

Comments
 (0)