Skip to content

Commit ce168f9

Browse files
authored
feat: allow overriding of GITHUB_ env variables (#1582)
* allow overriding of GITHUB_ env variables * bug fix for overriding env vars with empty string * revert step.go * refactor github_context to prevent lint failures. added more setters * added ability to override github env variables * handled base and head ref
1 parent c4b64ec commit ce168f9

File tree

3 files changed

+199
-74
lines changed

3 files changed

+199
-74
lines changed

pkg/model/github_context.go

+65-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package model
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
"github.com/nektos/act/pkg/common"
89
"github.com/nektos/act/pkg/common/git"
@@ -89,26 +90,22 @@ func withDefaultBranch(ctx context.Context, b string, event map[string]interface
8990
var findGitRef = git.FindGitRef
9091
var findGitRevision = git.FindGitRevision
9192

92-
func (ghc *GithubContext) SetRefAndSha(ctx context.Context, defaultBranch string, repoPath string) {
93+
func (ghc *GithubContext) SetRef(ctx context.Context, defaultBranch string, repoPath string) {
9394
logger := common.Logger(ctx)
95+
9496
// https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows
9597
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads
9698
switch ghc.EventName {
9799
case "pull_request_target":
98100
ghc.Ref = fmt.Sprintf("refs/heads/%s", ghc.BaseRef)
99-
ghc.Sha = asString(nestedMapLookup(ghc.Event, "pull_request", "base", "sha"))
100101
case "pull_request", "pull_request_review", "pull_request_review_comment":
101102
ghc.Ref = fmt.Sprintf("refs/pull/%.0f/merge", ghc.Event["number"])
102103
case "deployment", "deployment_status":
103104
ghc.Ref = asString(nestedMapLookup(ghc.Event, "deployment", "ref"))
104-
ghc.Sha = asString(nestedMapLookup(ghc.Event, "deployment", "sha"))
105105
case "release":
106106
ghc.Ref = asString(nestedMapLookup(ghc.Event, "release", "tag_name"))
107107
case "push", "create", "workflow_dispatch":
108108
ghc.Ref = asString(ghc.Event["ref"])
109-
if deleted, ok := ghc.Event["deleted"].(bool); ok && !deleted {
110-
ghc.Sha = asString(ghc.Event["after"])
111-
}
112109
default:
113110
defaultBranch := asString(nestedMapLookup(ghc.Event, "repository", "default_branch"))
114111
if defaultBranch != "" {
@@ -136,6 +133,23 @@ func (ghc *GithubContext) SetRefAndSha(ctx context.Context, defaultBranch string
136133
ghc.Ref = fmt.Sprintf("refs/heads/%s", asString(nestedMapLookup(ghc.Event, "repository", "default_branch")))
137134
}
138135
}
136+
}
137+
138+
func (ghc *GithubContext) SetSha(ctx context.Context, repoPath string) {
139+
logger := common.Logger(ctx)
140+
141+
// https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows
142+
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads
143+
switch ghc.EventName {
144+
case "pull_request_target":
145+
ghc.Sha = asString(nestedMapLookup(ghc.Event, "pull_request", "base", "sha"))
146+
case "deployment", "deployment_status":
147+
ghc.Sha = asString(nestedMapLookup(ghc.Event, "deployment", "sha"))
148+
case "push", "create", "workflow_dispatch":
149+
if deleted, ok := ghc.Event["deleted"].(bool); ok && !deleted {
150+
ghc.Sha = asString(ghc.Event["after"])
151+
}
152+
}
139153

140154
if ghc.Sha == "" {
141155
_, sha, err := findGitRevision(ctx, repoPath)
@@ -146,3 +160,48 @@ func (ghc *GithubContext) SetRefAndSha(ctx context.Context, defaultBranch string
146160
}
147161
}
148162
}
163+
164+
func (ghc *GithubContext) SetRepositoryAndOwner(ctx context.Context, githubInstance string, remoteName string, repoPath string) {
165+
if ghc.Repository == "" {
166+
repo, err := git.FindGithubRepo(ctx, repoPath, githubInstance, remoteName)
167+
if err != nil {
168+
common.Logger(ctx).Warningf("unable to get git repo: %v", err)
169+
return
170+
}
171+
ghc.Repository = repo
172+
}
173+
ghc.RepositoryOwner = strings.Split(ghc.Repository, "/")[0]
174+
}
175+
176+
func (ghc *GithubContext) SetRefTypeAndName() {
177+
var refType, refName string
178+
179+
// https://docs.github.com/en/actions/learn-github-actions/environment-variables
180+
if strings.HasPrefix(ghc.Ref, "refs/tags/") {
181+
refType = "tag"
182+
refName = ghc.Ref[len("refs/tags/"):]
183+
} else if strings.HasPrefix(ghc.Ref, "refs/heads/") {
184+
refType = "branch"
185+
refName = ghc.Ref[len("refs/heads/"):]
186+
}
187+
188+
if ghc.RefType == "" {
189+
ghc.RefType = refType
190+
}
191+
192+
if ghc.RefName == "" {
193+
ghc.RefName = refName
194+
}
195+
}
196+
197+
func (ghc *GithubContext) SetBaseAndHeadRef() {
198+
if ghc.EventName == "pull_request" || ghc.EventName == "pull_request_target" {
199+
if ghc.BaseRef == "" {
200+
ghc.BaseRef = asString(nestedMapLookup(ghc.Event, "pull_request", "base", "ref"))
201+
}
202+
203+
if ghc.HeadRef == "" {
204+
ghc.HeadRef = asString(nestedMapLookup(ghc.Event, "pull_request", "head", "ref"))
205+
}
206+
}
207+
}

pkg/model/github_context_test.go

+94-25
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/stretchr/testify/assert"
1010
)
1111

12-
func TestSetRefAndSha(t *testing.T) {
12+
func TestSetRef(t *testing.T) {
1313
log.SetLevel(log.DebugLevel)
1414

1515
oldFindGitRef := findGitRef
@@ -29,38 +29,27 @@ func TestSetRefAndSha(t *testing.T) {
2929
eventName string
3030
event map[string]interface{}
3131
ref string
32-
sha string
3332
}{
3433
{
3534
eventName: "pull_request_target",
36-
event: map[string]interface{}{
37-
"pull_request": map[string]interface{}{
38-
"base": map[string]interface{}{
39-
"sha": "pr-base-sha",
40-
},
41-
},
42-
},
43-
ref: "refs/heads/master",
44-
sha: "pr-base-sha",
35+
event: map[string]interface{}{},
36+
ref: "refs/heads/master",
4537
},
4638
{
4739
eventName: "pull_request",
4840
event: map[string]interface{}{
4941
"number": 1234.,
5042
},
5143
ref: "refs/pull/1234/merge",
52-
sha: "1234fakesha",
5344
},
5445
{
5546
eventName: "deployment",
5647
event: map[string]interface{}{
5748
"deployment": map[string]interface{}{
5849
"ref": "refs/heads/somebranch",
59-
"sha": "deployment-sha",
6050
},
6151
},
6252
ref: "refs/heads/somebranch",
63-
sha: "deployment-sha",
6453
},
6554
{
6655
eventName: "release",
@@ -70,17 +59,13 @@ func TestSetRefAndSha(t *testing.T) {
7059
},
7160
},
7261
ref: "v1.0.0",
73-
sha: "1234fakesha",
7462
},
7563
{
7664
eventName: "push",
7765
event: map[string]interface{}{
78-
"ref": "refs/heads/somebranch",
79-
"after": "push-sha",
80-
"deleted": false,
66+
"ref": "refs/heads/somebranch",
8167
},
8268
ref: "refs/heads/somebranch",
83-
sha: "push-sha",
8469
},
8570
{
8671
eventName: "unknown",
@@ -90,13 +75,11 @@ func TestSetRefAndSha(t *testing.T) {
9075
},
9176
},
9277
ref: "refs/heads/main",
93-
sha: "1234fakesha",
9478
},
9579
{
9680
eventName: "no-event",
9781
event: map[string]interface{}{},
9882
ref: "refs/heads/master",
99-
sha: "1234fakesha",
10083
},
10184
}
10285

@@ -108,10 +91,9 @@ func TestSetRefAndSha(t *testing.T) {
10891
Event: table.event,
10992
}
11093

111-
ghc.SetRefAndSha(context.Background(), "main", "/some/dir")
94+
ghc.SetRef(context.Background(), "main", "/some/dir")
11295

11396
assert.Equal(t, table.ref, ghc.Ref)
114-
assert.Equal(t, table.sha, ghc.Sha)
11597
})
11698
}
11799

@@ -125,9 +107,96 @@ func TestSetRefAndSha(t *testing.T) {
125107
Event: map[string]interface{}{},
126108
}
127109

128-
ghc.SetRefAndSha(context.Background(), "", "/some/dir")
110+
ghc.SetRef(context.Background(), "", "/some/dir")
129111

130112
assert.Equal(t, "refs/heads/master", ghc.Ref)
131-
assert.Equal(t, "1234fakesha", ghc.Sha)
132113
})
133114
}
115+
116+
func TestSetSha(t *testing.T) {
117+
log.SetLevel(log.DebugLevel)
118+
119+
oldFindGitRef := findGitRef
120+
oldFindGitRevision := findGitRevision
121+
defer func() { findGitRef = oldFindGitRef }()
122+
defer func() { findGitRevision = oldFindGitRevision }()
123+
124+
findGitRef = func(ctx context.Context, file string) (string, error) {
125+
return "refs/heads/master", nil
126+
}
127+
128+
findGitRevision = func(ctx context.Context, file string) (string, string, error) {
129+
return "", "1234fakesha", nil
130+
}
131+
132+
tables := []struct {
133+
eventName string
134+
event map[string]interface{}
135+
sha string
136+
}{
137+
{
138+
eventName: "pull_request_target",
139+
event: map[string]interface{}{
140+
"pull_request": map[string]interface{}{
141+
"base": map[string]interface{}{
142+
"sha": "pr-base-sha",
143+
},
144+
},
145+
},
146+
sha: "pr-base-sha",
147+
},
148+
{
149+
eventName: "pull_request",
150+
event: map[string]interface{}{
151+
"number": 1234.,
152+
},
153+
sha: "1234fakesha",
154+
},
155+
{
156+
eventName: "deployment",
157+
event: map[string]interface{}{
158+
"deployment": map[string]interface{}{
159+
"sha": "deployment-sha",
160+
},
161+
},
162+
sha: "deployment-sha",
163+
},
164+
{
165+
eventName: "release",
166+
event: map[string]interface{}{},
167+
sha: "1234fakesha",
168+
},
169+
{
170+
eventName: "push",
171+
event: map[string]interface{}{
172+
"after": "push-sha",
173+
"deleted": false,
174+
},
175+
sha: "push-sha",
176+
},
177+
{
178+
eventName: "unknown",
179+
event: map[string]interface{}{},
180+
sha: "1234fakesha",
181+
},
182+
{
183+
eventName: "no-event",
184+
event: map[string]interface{}{},
185+
sha: "1234fakesha",
186+
},
187+
}
188+
189+
for _, table := range tables {
190+
t.Run(table.eventName, func(t *testing.T) {
191+
ghc := &GithubContext{
192+
EventName: table.eventName,
193+
BaseRef: "master",
194+
Event: table.event,
195+
}
196+
197+
ghc.SetSha(context.Background(), "/some/dir")
198+
199+
assert.Equal(t, table.sha, ghc.Sha)
200+
})
201+
}
202+
}

0 commit comments

Comments
 (0)