Skip to content

Commit

Permalink
feat: GetCurrentRef
Browse files Browse the repository at this point in the history
GetCurrentRef allows user to get the reference the commit is on. This would be a branch or a tag. Returns empty if the CI is not supported.
  • Loading branch information
fallion committed Jun 25, 2020
1 parent 0de440e commit 38e18e0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
26 changes: 26 additions & 0 deletions get_current_ref.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package integrations

import "os"

// GetCurrentRef will return the Reference provided by gitlab, github or drone.
func GetCurrentRef() string {
gitlabRef := os.Getenv("CI_COMMIT_REF_NAME")

if gitlabRef != "" {
return gitlabRef
}

githubRef := os.Getenv("GITHUB_REF")

if githubRef != "" {
return githubRef
}

droneRef := os.Getenv("DRONE_COMMIT_REF")

if droneRef != "" {
return droneRef
}

return ""
}
42 changes: 42 additions & 0 deletions get_current_ref_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package integrations

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetCurrentRef(t *testing.T) {
// Github action specific environment variable
os.Setenv("GITHUB_REF", "github-develop")

actionCompareBranch := GetCurrentRef()

assert.Equal(t, "github-develop", actionCompareBranch)

os.Setenv("GITHUB_REF", "")

// Gitlab specific environment variable
os.Setenv("CI_COMMIT_REF_NAME", "gitlab-develop")

gitlabCompareBranch := GetCurrentRef()

assert.Equal(t, "gitlab-develop", gitlabCompareBranch)

os.Setenv("CI_COMMIT_REF_NAME", "")

// Drone specific environment variable
os.Setenv("DRONE_COMMIT_REF", "drone-develop")

droneCompareBranch := GetCurrentRef()

assert.Equal(t, "drone-develop", droneCompareBranch)

os.Setenv("DRONE_COMMIT_REF", "")

// Should default to empty if no conditions are satisfied
defaultMaster := GetCurrentRef()

assert.Equal(t, "", defaultMaster)
}

0 comments on commit 38e18e0

Please sign in to comment.