Skip to content

Commit 88dfbed

Browse files
feat: add github action condition
1 parent 3965728 commit 88dfbed

File tree

7 files changed

+135
-11
lines changed

7 files changed

+135
-11
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ go 1.14
44

55
require (
66
github.com/Masterminds/semver v1.5.0
7+
github.com/christophwitzko/go-travis v0.0.0-20180115212951-5311d81d834b
8+
github.com/fatih/structs v1.1.0 // indirect
79
github.com/google/go-github/v30 v30.1.0
10+
github.com/oleiade/reflections v1.0.0 // indirect
11+
github.com/stretchr/testify v1.5.1 // indirect
812
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
913
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
22
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
33
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
4+
github.com/christophwitzko/go-travis v0.0.0-20180115212951-5311d81d834b h1:YKFJ/MSTMtRCR7UeF4I2Ovi8HDwsVA7F/qWxwr7J6tU=
5+
github.com/christophwitzko/go-travis v0.0.0-20180115212951-5311d81d834b/go.mod h1:0PHJRx/NZav97xUaOdeXLPZmPKBjSHVBAeiFFkaRSmc=
6+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
7+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
9+
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
410
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
511
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
612
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
713
github.com/google/go-github/v30 v30.1.0 h1:VLDx+UolQICEOKu2m4uAoMti1SxuEBAl7RSEG16L+Oo=
814
github.com/google/go-github/v30 v30.1.0/go.mod h1:n8jBpHl45a/rlBUtRJMOG4GhNADUQFEufcolZ95JfU8=
915
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
1016
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
17+
github.com/oleiade/reflections v1.0.0 h1:0ir4pc6v8/PJ0yw5AEtMddfXpWBXg9cnG7SgSoJuCgY=
18+
github.com/oleiade/reflections v1.0.0/go.mod h1:RbATFBbKYkVdqmSFtx13Bb/tVhR0lgOBXunWTZKeL4w=
19+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
20+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
21+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
22+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
23+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
1124
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
1225
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1326
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -23,3 +36,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
2336
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
2437
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
2538
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
39+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
40+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
41+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
42+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

pkg/condition/condition.go

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,58 @@ import (
66
"strings"
77
)
88

9-
func readGitHead() string {
9+
func ReadGitHead() string {
1010
data, err := ioutil.ReadFile(".git/HEAD")
1111
if err != nil {
1212
return ""
1313
}
1414
return strings.TrimSpace(strings.TrimPrefix(string(data), "ref: refs/heads/"))
1515
}
1616

17-
func GetCurrentBranch() string {
18-
if val := os.Getenv("TRAVIS_BRANCH"); val != "" {
17+
func GetDefaultRepoSlug() string {
18+
if val := os.Getenv("TRAVIS_REPO_SLUG"); val != "" {
1919
return val
2020
}
21-
return readGitHead()
21+
if val := os.Getenv("GITHUB_REPOSITORY"); val != "" {
22+
return val
23+
}
24+
return ""
2225
}
2326

24-
func GetCurrentSHA() string {
25-
if val := os.Getenv("TRAVIS_COMMIT"); val != "" {
26-
return val
27+
type CIConfig map[string]interface{}
28+
29+
type CI interface {
30+
Name() string
31+
RunCondition(config CIConfig) error
32+
GetCurrentBranch() string
33+
GetCurrentSHA() string
34+
}
35+
36+
type DefaultCI struct {
37+
}
38+
39+
func (d DefaultCI) Name() string {
40+
return "Default"
41+
}
42+
43+
func (d DefaultCI) RunCondition(config CIConfig) error {
44+
return nil
45+
}
46+
47+
func (d DefaultCI) GetCurrentBranch() string {
48+
return ReadGitHead()
49+
}
50+
51+
func (d DefaultCI) GetCurrentSHA() string {
52+
return ReadGitHead()
53+
}
54+
55+
func NewCI() CI {
56+
if os.Getenv("GITHUB_ACTIONS") == "true" {
57+
return &GitHubActions{}
58+
}
59+
if os.Getenv("TRAVIS") == "true" {
60+
return &TravisCI{}
2761
}
28-
// TODO: resolve ref
29-
return GetCurrentBranch()
62+
return &DefaultCI{}
3063
}

pkg/condition/github.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package condition
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
)
8+
9+
type GitHubActions struct {
10+
}
11+
12+
func (gha *GitHubActions) Name() string {
13+
return "GitHub Actions"
14+
}
15+
16+
func (gha *GitHubActions) GetCurrentBranch() string {
17+
return os.Getenv("GITHUB_REF")
18+
}
19+
20+
func (gha *GitHubActions) GetCurrentSHA() string {
21+
return os.Getenv("GITHUB_SHA")
22+
}
23+
24+
func (gha *GitHubActions) IsBranchRef() bool {
25+
if val := os.Getenv("GITHUB_REF"); val != "" {
26+
return strings.HasPrefix(val, "refs/heads/")
27+
}
28+
return false
29+
}
30+
31+
func (gha *GitHubActions) RunCondition(config CIConfig) error {
32+
if !gha.IsBranchRef() {
33+
return fmt.Errorf("not running on a branch")
34+
}
35+
return nil
36+
}

pkg/condition/github_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package condition
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestGithubValid(t *testing.T) {
9+
os.Setenv("GITHUB_REF", "")
10+
gha := GitHubActions{}
11+
err := gha.RunCondition(CIConfig{})
12+
if err == nil {
13+
t.Fail()
14+
}
15+
}

pkg/condition/travis.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,25 @@ import (
1111
"time"
1212
)
1313

14-
func Travis(token, defaultBranch string, private bool) error {
14+
type TravisCI struct {
15+
}
16+
17+
func (ci *TravisCI) Name() string {
18+
return "Travis CI"
19+
}
20+
21+
func (ci *TravisCI) GetCurrentBranch() string {
22+
return os.Getenv("TRAVIS_BRANCH")
23+
}
24+
25+
func (ci *TravisCI) GetCurrentSHA() string {
26+
return os.Getenv("TRAVIS_COMMIT")
27+
}
28+
29+
func (ci *TravisCI) RunCondition(config CIConfig) error {
30+
token := config["token"].(string)
31+
defaultBranch := config["defaultBranch"].(string)
32+
private := config["private"].(bool)
1533
logger := log.New(os.Stderr, "[condition-travis]: ", 0)
1634
if os.Getenv("TRAVIS") != "true" {
1735
return errors.New("semantic-release didn’t run on Travis CI and therefore a new version won’t be published.")

pkg/condition/travis_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import (
55
)
66

77
func TestTravisValid(t *testing.T) {
8-
err := Travis("", "", false)
8+
travis := TravisCI{}
9+
err := travis.RunCondition(CIConfig{"token": "", "defaultBranch": "", "private": false})
910
if err == nil {
1011
t.Fail()
1112
}

0 commit comments

Comments
 (0)