diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..61a02b7 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,44 @@ +name: Go +on: [push, pull_request] +jobs: + + build: + name: Build + runs-on: ubuntu-latest + steps: + + - name: tmp + run: | + echo $GITHUB_EVENT_NAME + cat $GITHUB_EVENT_PATH + + - name: Set up Go 1.12 + uses: actions/setup-go@v1 + with: + go-version: 1.12 + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + + - name: Lint + run: | + export PATH=$PATH:$(go env GOPATH)/bin + curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.16.0 + golangci-lint run ./... + + - name: Run tests + run: go test ./... + + - name: Build binary + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + go build -o build/go-semantic-release-temp ./cmd/go-semantic-release/ + go build -o build/go-semantic-release -ldflags "-w -s --X main.version=`./build/go-semantic-release-temp next`" ./cmd/go-semantic-release/ + GOOS=windows GOARCH=386 go build -o build/go-semantic-release.exe -ldflags "-w -s -X main.version=`./build/go-semantic-release-temp next`" ./cmd/go-semantic-release/ + + - name: Release + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: ./build/go-semantic-release-temp release --loglevel trace diff --git a/internal/ci/ci.go b/internal/ci/ci.go index 4f1acd6..066271c 100644 --- a/internal/ci/ci.go +++ b/internal/ci/ci.go @@ -42,6 +42,7 @@ func GetCIProvider(gitUtil *gitutil.GitUtil, envs map[string]string) (*ProviderC services := []Service{ Travis{}, + GithubActions{}, Git{gitUtil: gitUtil}, // GIt must be the last option to check } diff --git a/internal/ci/ci_test.go b/internal/ci/ci_test.go index 38a24a1..31ecd0c 100644 --- a/internal/ci/ci_test.go +++ b/internal/ci/ci_test.go @@ -89,6 +89,28 @@ func TestCi(t *testing.T) { result: &ci.ProviderConfig{IsPR: false, PR: "", PRBranch: "", Branch: "master", Tag: "TAG", Commit: "190bfd6aa60022afd0ef830342cfb07e33c45f37", BuildURL: "https://travis-ci.com/owner/repo/builds/1234", Service: "travis", Name: "Travis CI"}, hasError: false, }, + { + service: "Github Actions PR", + envs: map[string]string{ + "GITHUB_EVENT_NAME": "pull_request", + "GITHUB_SHA": "190bfd6aa60022afd0ef830342cfb07e33c45f37", + "GITHUB_REF": "master", + "GITHUB_ACTION": "action", + }, + result: &ci.ProviderConfig{IsPR: true, PR: "", PRBranch: "", Branch: "master", Tag: "", Commit: "190bfd6aa60022afd0ef830342cfb07e33c45f37", BuildURL: "", Service: "GithubActions", Name: "GithubActions CI"}, + hasError: false, + }, + { + service: "Github Actions Push", + envs: map[string]string{ + "GITHUB_EVENT_NAME": "push", + "GITHUB_SHA": "190bfd6aa60022afd0ef830342cfb07e33c45f37", + "GITHUB_REF": "refs/heads/feature-branch-1", + "GITHUB_ACTION": "action", + }, + result: &ci.ProviderConfig{IsPR: false, PR: "", PRBranch: "", Branch: "feature-branch-1", Tag: "", Commit: "190bfd6aa60022afd0ef830342cfb07e33c45f37", BuildURL: "", Service: "GithubActions", Name: "GithubActions CI"}, + hasError: false, + }, } for _, config := range testConfigs { diff --git a/internal/ci/github_actions.go b/internal/ci/github_actions.go new file mode 100644 index 0000000..38b1aa4 --- /dev/null +++ b/internal/ci/github_actions.go @@ -0,0 +1,43 @@ +package ci + +import ( + "fmt" + "strings" + + log "github.com/sirupsen/logrus" +) + +//GithubActions struct +type GithubActions struct{} + +//Detect if on GithubActions +func (t GithubActions) detect(envs map[string]string) (*ProviderConfig, error) { + + if _, exists := envs["GITHUB_ACTION"]; !exists { + return nil, fmt.Errorf("not running on Github Actions") + } + + isPR := false + + value := envs["GITHUB_EVENT_NAME"] + + if value == "pull_request" { + isPR = true + } else { + log.Debugf("GITHUB_EVENT_NAME=%s, not running on pr", value) + } + + branch := envs["GITHUB_REF"] + + if strings.HasPrefix(envs["GITHUB_REF"], "refs/heads/") { + branch = strings.Replace(branch, "refs/heads/", "", 1) + } + + return &ProviderConfig{ + Service: "GithubActions", + Name: "GithubActions CI", + Commit: envs["GITHUB_SHA"], + Branch: branch, + IsPR: isPR, + }, nil +} diff --git a/internal/releaser/github/github.go b/internal/releaser/github/github.go index e45186b..f279636 100644 --- a/internal/releaser/github/github.go +++ b/internal/releaser/github/github.go @@ -32,7 +32,7 @@ type Client struct { func New(c *config.GitHubProvider) (*Client, error) { var err error - if c.AccessToken, err = util.GetAccessToken(GITHUB); err != nil { + if c.AccessToken, err = util.GetAccessToken("GITHUB_TOKEN"); err != nil { return &Client{}, err } ctx := context.Background() diff --git a/internal/releaser/github/github_test.go b/internal/releaser/github/github_test.go index b3d4c6e..6813c11 100644 --- a/internal/releaser/github/github_test.go +++ b/internal/releaser/github/github_test.go @@ -143,19 +143,19 @@ func initHTTPServer(respCode int, body string) *httptest.Server { func TestNew(t *testing.T) { for _, testOject := range testNewClient { if testOject.valid { - os.Setenv("GITHUB_ACCESS_TOKEN", "XXX") + os.Setenv("GITHUB_TOKEN", "XXX") } _, err := github.New(&testOject.config) assert.Equal(t, testOject.valid, err == nil) - os.Unsetenv("GITHUB_ACCESS_TOKEN") + os.Unsetenv("GITHUB_TOKEN") } } func TestGetCommitURL(t *testing.T) { - os.Setenv("GITHUB_ACCESS_TOKEN", "XX") + os.Setenv("GITHUB_TOKEN", "XX") for _, testOject := range testNewClient { client, _ := github.New(&testOject.config) actualURL := client.GetCommitURL() @@ -168,12 +168,12 @@ func TestGetCommitURL(t *testing.T) { assert.EqualValues(t, expectedURL, actualURL) } } - os.Unsetenv("GITHUB_ACCESS_TOKEN") + os.Unsetenv("GITHUB_TOKEN") } func TestGetCompareURL(t *testing.T) { - os.Setenv("GITHUB_ACCESS_TOKEN", "XX") + os.Setenv("GITHUB_TOKEN", "XX") for _, testOject := range testNewClient { client, _ := github.New(&testOject.config) actualURL := client.GetCompareURL("1", "2") @@ -186,12 +186,12 @@ func TestGetCompareURL(t *testing.T) { assert.EqualValues(t, expectedURL, actualURL) } } - os.Unsetenv("GITHUB_ACCESS_TOKEN") + os.Unsetenv("GITHUB_TOKEN") } func TestValidateConfig(t *testing.T) { - os.Setenv("GITHUB_ACCESS_TOKEN", "XX") + os.Setenv("GITHUB_TOKEN", "XX") for _, testOject := range testHelperMethod { client, _ := github.New(&testOject.config) err := client.ValidateConfig() @@ -199,11 +199,11 @@ func TestValidateConfig(t *testing.T) { assert.Equal(t, testOject.valid, err == nil) } - os.Unsetenv("GITHUB_ACCESS_TOKEN") + os.Unsetenv("GITHUB_TOKEN") } func TestCreateRelease(t *testing.T) { - os.Setenv("GITHUB_ACCESS_TOKEN", "XX") + os.Setenv("GITHUB_TOKEN", "XX") for _, testObejct := range testReleases { if testObejct.valid { @@ -230,6 +230,6 @@ func TestCreateRelease(t *testing.T) { assert.Error(t, err) } } - os.Unsetenv("GITHUB_ACCESS_TOKEN") + os.Unsetenv("GITHUB_TOKEN") } diff --git a/internal/releaser/releaser.go b/internal/releaser/releaser.go index d96f49f..0aa21f3 100644 --- a/internal/releaser/releaser.go +++ b/internal/releaser/releaser.go @@ -2,6 +2,7 @@ package releaser import ( "fmt" + "strings" "github.com/Nightapes/go-semantic-release/internal/releaser/github" "github.com/Nightapes/go-semantic-release/internal/releaser/gitlab" @@ -41,7 +42,7 @@ func (r *Releasers) GetReleaser() (Releaser, error) { return github.New(&r.config.GitHubProvider) case gitlab.GITLAB: log.Debugf("initialize new %s-provider", gitlab.GITLAB) - accessToken, err := util.GetAccessToken(gitlab.GITLAB) + accessToken, err := util.GetAccessToken(fmt.Sprintf("%s_ACCESS_TOKEN", strings.ToUpper(gitlab.GITLAB))) if err != nil { return nil, err } diff --git a/internal/releaser/util/util.go b/internal/releaser/util/util.go index 5bde7e3..5e4b42b 100644 --- a/internal/releaser/util/util.go +++ b/internal/releaser/util/util.go @@ -49,11 +49,9 @@ func NewAddHeaderTransport(T http.RoundTripper, key, value string) *AddHeaderTra } // GetAccessToken lookup for the providers accesstoken -func GetAccessToken(providerName string) (string, error) { +func GetAccessToken(envName string) (string, error) { var token string var exists bool - envName := fmt.Sprintf("%s_ACCESS_TOKEN", strings.ToUpper(providerName)) - log.Debugf("check if %s environment variable is set", envName) if token, exists = os.LookupEnv(envName); !exists { diff --git a/internal/releaser/util/util_test.go b/internal/releaser/util/util_test.go index 7303cd4..7878fe2 100644 --- a/internal/releaser/util/util_test.go +++ b/internal/releaser/util/util_test.go @@ -43,7 +43,7 @@ func TestGetAccessToken(t *testing.T) { fmt.Println(err.Error()) } - _, err := util.GetAccessToken(testObject.providerName) + _, err := util.GetAccessToken(envName) assert.Equal(t, testObject.valid, err == nil) os.Unsetenv(envName) @@ -193,7 +193,7 @@ func TestDoAndRoundTrip(t *testing.T) { hasError: true, responseBody: &example{}, responseBodyType: &example{}, - path: "broken", + path: "4/broken", }, } diff --git a/pkg/semanticrelease/semantic-release.go b/pkg/semanticrelease/semantic-release.go index ed5e1ea..43efc5e 100644 --- a/pkg/semanticrelease/semantic-release.go +++ b/pkg/semanticrelease/semantic-release.go @@ -177,12 +177,12 @@ func (s *SemanticRelease) WriteChangeLog(changelogContent, file string) error { func (s *SemanticRelease) Release(provider *ci.ProviderConfig, force bool) error { if provider.IsPR { - log.Debugf("Will not perform a new release. This is a pull request") + log.Infof("Will not perform a new release. This is a pull request") return nil } if _, ok := s.config.Branch[provider.Branch]; !ok { - log.Debugf("Will not perform a new release. Current %s branch is not configured in release config", provider.Branch) + log.Infof("Will not perform a new release. Current %s branch is not configured in release config", provider.Branch) return nil }