Skip to content

Commit

Permalink
feat: add github action condition
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwitzko committed Apr 1, 2020
1 parent 3965728 commit 88dfbed
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 11 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ go 1.14

require (
github.com/Masterminds/semver v1.5.0
github.com/christophwitzko/go-travis v0.0.0-20180115212951-5311d81d834b
github.com/fatih/structs v1.1.0 // indirect
github.com/google/go-github/v30 v30.1.0
github.com/oleiade/reflections v1.0.0 // indirect
github.com/stretchr/testify v1.5.1 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/christophwitzko/go-travis v0.0.0-20180115212951-5311d81d834b h1:YKFJ/MSTMtRCR7UeF4I2Ovi8HDwsVA7F/qWxwr7J6tU=
github.com/christophwitzko/go-travis v0.0.0-20180115212951-5311d81d834b/go.mod h1:0PHJRx/NZav97xUaOdeXLPZmPKBjSHVBAeiFFkaRSmc=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-github/v30 v30.1.0 h1:VLDx+UolQICEOKu2m4uAoMti1SxuEBAl7RSEG16L+Oo=
github.com/google/go-github/v30 v30.1.0/go.mod h1:n8jBpHl45a/rlBUtRJMOG4GhNADUQFEufcolZ95JfU8=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/oleiade/reflections v1.0.0 h1:0ir4pc6v8/PJ0yw5AEtMddfXpWBXg9cnG7SgSoJuCgY=
github.com/oleiade/reflections v1.0.0/go.mod h1:RbATFBbKYkVdqmSFtx13Bb/tVhR0lgOBXunWTZKeL4w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand All @@ -23,3 +36,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
51 changes: 42 additions & 9 deletions pkg/condition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,58 @@ import (
"strings"
)

func readGitHead() string {
func ReadGitHead() string {
data, err := ioutil.ReadFile(".git/HEAD")
if err != nil {
return ""
}
return strings.TrimSpace(strings.TrimPrefix(string(data), "ref: refs/heads/"))
}

func GetCurrentBranch() string {
if val := os.Getenv("TRAVIS_BRANCH"); val != "" {
func GetDefaultRepoSlug() string {
if val := os.Getenv("TRAVIS_REPO_SLUG"); val != "" {
return val
}
return readGitHead()
if val := os.Getenv("GITHUB_REPOSITORY"); val != "" {
return val
}
return ""
}

func GetCurrentSHA() string {
if val := os.Getenv("TRAVIS_COMMIT"); val != "" {
return val
type CIConfig map[string]interface{}

type CI interface {
Name() string
RunCondition(config CIConfig) error
GetCurrentBranch() string
GetCurrentSHA() string
}

type DefaultCI struct {
}

func (d DefaultCI) Name() string {
return "Default"
}

func (d DefaultCI) RunCondition(config CIConfig) error {
return nil
}

func (d DefaultCI) GetCurrentBranch() string {
return ReadGitHead()
}

func (d DefaultCI) GetCurrentSHA() string {
return ReadGitHead()
}

func NewCI() CI {
if os.Getenv("GITHUB_ACTIONS") == "true" {
return &GitHubActions{}
}
if os.Getenv("TRAVIS") == "true" {
return &TravisCI{}
}
// TODO: resolve ref
return GetCurrentBranch()
return &DefaultCI{}
}
36 changes: 36 additions & 0 deletions pkg/condition/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package condition

import (
"fmt"
"os"
"strings"
)

type GitHubActions struct {
}

func (gha *GitHubActions) Name() string {
return "GitHub Actions"
}

func (gha *GitHubActions) GetCurrentBranch() string {
return os.Getenv("GITHUB_REF")
}

func (gha *GitHubActions) GetCurrentSHA() string {
return os.Getenv("GITHUB_SHA")
}

func (gha *GitHubActions) IsBranchRef() bool {
if val := os.Getenv("GITHUB_REF"); val != "" {
return strings.HasPrefix(val, "refs/heads/")
}
return false
}

func (gha *GitHubActions) RunCondition(config CIConfig) error {
if !gha.IsBranchRef() {
return fmt.Errorf("not running on a branch")
}
return nil
}
15 changes: 15 additions & 0 deletions pkg/condition/github_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package condition

import (
"os"
"testing"
)

func TestGithubValid(t *testing.T) {
os.Setenv("GITHUB_REF", "")
gha := GitHubActions{}
err := gha.RunCondition(CIConfig{})
if err == nil {
t.Fail()
}
}
20 changes: 19 additions & 1 deletion pkg/condition/travis.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,25 @@ import (
"time"
)

func Travis(token, defaultBranch string, private bool) error {
type TravisCI struct {
}

func (ci *TravisCI) Name() string {
return "Travis CI"
}

func (ci *TravisCI) GetCurrentBranch() string {
return os.Getenv("TRAVIS_BRANCH")
}

func (ci *TravisCI) GetCurrentSHA() string {
return os.Getenv("TRAVIS_COMMIT")
}

func (ci *TravisCI) RunCondition(config CIConfig) error {
token := config["token"].(string)
defaultBranch := config["defaultBranch"].(string)
private := config["private"].(bool)
logger := log.New(os.Stderr, "[condition-travis]: ", 0)
if os.Getenv("TRAVIS") != "true" {
return errors.New("semantic-release didn’t run on Travis CI and therefore a new version won’t be published.")
Expand Down
3 changes: 2 additions & 1 deletion pkg/condition/travis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
)

func TestTravisValid(t *testing.T) {
err := Travis("", "", false)
travis := TravisCI{}
err := travis.RunCondition(CIConfig{"token": "", "defaultBranch": "", "private": false})
if err == nil {
t.Fail()
}
Expand Down

0 comments on commit 88dfbed

Please sign in to comment.