Skip to content

Commit 38e18e0

Browse files
author
Fallion
committed
feat: GetCurrentRef
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.
1 parent 0de440e commit 38e18e0

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

get_current_ref.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package integrations
2+
3+
import "os"
4+
5+
// GetCurrentRef will return the Reference provided by gitlab, github or drone.
6+
func GetCurrentRef() string {
7+
gitlabRef := os.Getenv("CI_COMMIT_REF_NAME")
8+
9+
if gitlabRef != "" {
10+
return gitlabRef
11+
}
12+
13+
githubRef := os.Getenv("GITHUB_REF")
14+
15+
if githubRef != "" {
16+
return githubRef
17+
}
18+
19+
droneRef := os.Getenv("DRONE_COMMIT_REF")
20+
21+
if droneRef != "" {
22+
return droneRef
23+
}
24+
25+
return ""
26+
}

get_current_ref_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package integrations
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestGetCurrentRef(t *testing.T) {
11+
// Github action specific environment variable
12+
os.Setenv("GITHUB_REF", "github-develop")
13+
14+
actionCompareBranch := GetCurrentRef()
15+
16+
assert.Equal(t, "github-develop", actionCompareBranch)
17+
18+
os.Setenv("GITHUB_REF", "")
19+
20+
// Gitlab specific environment variable
21+
os.Setenv("CI_COMMIT_REF_NAME", "gitlab-develop")
22+
23+
gitlabCompareBranch := GetCurrentRef()
24+
25+
assert.Equal(t, "gitlab-develop", gitlabCompareBranch)
26+
27+
os.Setenv("CI_COMMIT_REF_NAME", "")
28+
29+
// Drone specific environment variable
30+
os.Setenv("DRONE_COMMIT_REF", "drone-develop")
31+
32+
droneCompareBranch := GetCurrentRef()
33+
34+
assert.Equal(t, "drone-develop", droneCompareBranch)
35+
36+
os.Setenv("DRONE_COMMIT_REF", "")
37+
38+
// Should default to empty if no conditions are satisfied
39+
defaultMaster := GetCurrentRef()
40+
41+
assert.Equal(t, "", defaultMaster)
42+
}

0 commit comments

Comments
 (0)