Skip to content

Commit dc0d8b3

Browse files
committed
fix: Init gh actions logic
1 parent fefdee6 commit dc0d8b3

2 files changed

Lines changed: 112 additions & 15 deletions

File tree

cmd/utils.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ import (
1313

1414
// This function could use a better name
1515
func initGHSettings() (err error) {
16-
token, err = getGHToken()
17-
if err != nil {
18-
return err
16+
if repository == "" {
17+
repository, err = getRepository()
18+
if err != nil {
19+
return err
20+
}
1921
}
2022

21-
if prNumber == 0 {
22-
if !githubAction() {
23-
return errMissingPRNum
24-
}
23+
if !githubAction() && prNumber == 0 {
24+
return errMissingPRNum
25+
}
2526

27+
if pullRequest() {
2628
prNumber, err = getPRNumber()
2729
if err != nil {
2830
return fmt.Errorf("failed to get pull request number from github context GITHUB_REF_NAME, %v", err)
2931
}
3032
}
3133

32-
if repository == "" {
33-
repository, err = getRepository()
34-
if err != nil {
35-
return err
36-
}
34+
token, err = getGHToken()
35+
if err != nil {
36+
return err
3737
}
3838

3939
return nil
@@ -74,7 +74,7 @@ func githubAction() bool {
7474
}
7575

7676
func pullRequest() bool {
77-
if v := os.Getenv("GITHUB_EVENT_TYPE"); v == "pull_request" {
77+
if v := os.Getenv("GITHUB_EVENT_NAME"); v == "pull_request" {
7878
return true
7979
}
8080

cmd/utils_test.go

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,90 @@ import (
66
"testing"
77
)
88

9+
func TestInitGitHubSettings(t *testing.T) {
10+
t.Run("github action pull request", func(t *testing.T) {
11+
// Set this vars if running locally, othersie just use the ones from the workflow
12+
if _, ok := os.LookupEnv("GITHUB_ACTIONS"); !ok {
13+
_ = os.Setenv("GITHUB_ACTIONS", "true")
14+
defer os.Unsetenv("GITHUB_ACTIONS")
15+
_ = os.Setenv("GITHUB_TOKEN", "foo")
16+
defer os.Unsetenv("GITHUB_TOKEN")
17+
_ = os.Setenv("GITHUB_REPOSITORY", "coolapso/convcommitlint")
18+
defer os.Unsetenv("GITHUB_REPOSITORY")
19+
defer func(){ repository = "" }()
20+
_ = os.Setenv("GITHUB_REF_NAME", "25/merge")
21+
defer os.Unsetenv("GITHUB_REF_NAME")
22+
}
23+
24+
var tempType string
25+
if v, ok := os.LookupEnv("GITHUB_EVENT_NAME"); ok {
26+
if v != "pull_request" {
27+
tempType = v
28+
os.Setenv("GITHUB_EVENT_NAME", "pull_request")
29+
defer os.Setenv("GITHUB_EVENT_NAME", tempType)
30+
}
31+
} else {
32+
_ = os.Setenv("GITHUB_EVENT_NAME", "pull_request")
33+
defer os.Unsetenv("GITHUB_EVENT_NAME")
34+
}
35+
36+
got := initGHSettings()
37+
if got != nil {
38+
t.Fatal("Expected nil, got:", got)
39+
}
40+
})
41+
42+
t.Run("github action push", func(t *testing.T) {
43+
if _, ok := os.LookupEnv("GITHUB_ACTIONS"); !ok {
44+
_ = os.Setenv("GITHUB_ACTIONS", "true")
45+
defer os.Unsetenv("GITHUB_ACTIONS")
46+
_ = os.Setenv("GITHUB_TOKEN", "foo")
47+
defer os.Unsetenv("GITHUB_REPOSITORY")
48+
_ = os.Setenv("GITHUB_REPOSITORY", "coolapso/convcommitlint")
49+
defer os.Unsetenv("GITHUB_TOKEN")
50+
defer func(){ repository = ""}()
51+
}
52+
53+
var eventTemp string
54+
if v, ok := os.LookupEnv("GITHUB_EVENT_NAME"); ok {
55+
eventTemp = v
56+
os.Setenv("GITHUB_EVENT_NAME", "push")
57+
defer os.Setenv("GITHUB_EVENT_NAME", eventTemp)
58+
} else {
59+
os.Setenv("GITHUB_EVENT_NAME", "push")
60+
defer os.Setenv("GITHUB_EVENT_NAME", eventTemp)
61+
}
62+
63+
got := initGHSettings()
64+
if got != nil {
65+
t.Fatal("Expected nil, got:", got)
66+
}
67+
})
68+
69+
t.Run("missing github repository", func(t *testing.T) {
70+
got := initGHSettings()
71+
if got != errMissingRepository {
72+
t.Fatalf("Expected [ %v ] error, got [ %v ]", errMissingRepository, got)
73+
}
74+
})
75+
76+
77+
t.Run("Run locally", func(t *testing.T) {
78+
if _, ok := os.LookupEnv("GITHUB_ACTIONS"); !ok {
79+
_ = os.Setenv("GITHUB_TOKEN", "foo")
80+
defer os.Unsetenv("GITHUB_TOKEN")
81+
_ = os.Setenv("GITHUB_REPOSITORY", "coolapso/convcommitlint")
82+
defer os.Unsetenv("GITHUB_REPOSITORY")
83+
func(){ repository = "" }()
84+
}
85+
prNumber = 25
86+
got := initGHSettings()
87+
if got != nil {
88+
t.Fatal("Expected nil, got:", got)
89+
}
90+
})
91+
}
92+
993
func TestGetGHToken(t *testing.T) {
1094
t.Run("missing token", func(t *testing.T) {
1195
want := errMissingGHToken
@@ -132,7 +216,20 @@ func TestSplitOwnerRepo(t *testing.T) {
132216

133217
func TestPullRequest(t *testing.T) {
134218
t.Run("Is pull request", func(t *testing.T) {
135-
_ = os.Setenv("GITHUB_EVENT_TYPE", "pull_request")
219+
if _, ok := os.LookupEnv("GITHUB_ACTIONS"); !ok {
220+
_ = os.Setenv("GITHUB_EVENT_NAME", "pull_request")
221+
defer os.Unsetenv("GITHUB_EVENT_NAME")
222+
}
223+
224+
var eventTemp string
225+
if _, ok := os.LookupEnv("GITHUB_ACTIONS"); ok {
226+
if v := os.Getenv("GITHUB_EVENT_NAME"); v != "pull_request" {
227+
eventTemp = v
228+
os.Setenv("GITHUB_EVENT_NAME", "pull_request")
229+
defer func(){ os.Setenv("GITHUB_EVENT_NAME", eventTemp) }()
230+
}
231+
}
232+
136233
want := true
137234
got := pullRequest()
138235
if got != want {
@@ -141,7 +238,7 @@ func TestPullRequest(t *testing.T) {
141238
})
142239

143240
t.Run("Is not pull request", func(t *testing.T) {
144-
_ = os.Setenv("GITHUB_EVENT_TYPE", "push")
241+
_ = os.Setenv("GITHUB_EVENT_NAME", "push")
145242
want := false
146243
got := pullRequest()
147244
if got != want {

0 commit comments

Comments
 (0)