Skip to content

Commit

Permalink
chore: migrate logic from commitsar
Browse files Browse the repository at this point in the history
  • Loading branch information
fallion committed Dec 4, 2019
1 parent e86af44 commit 2f423f0
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
27 changes: 27 additions & 0 deletions find_compare_branch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package integrations

import (
"fmt"
"os"
)

// FindCompareBranch tries to find the merge request compare branch based on environment variables used by different CIs.
func FindCompareBranch() string {
githubRef := os.Getenv("GITHUB_BASE_REF")
if githubRef != "" {
return fmt.Sprintf("origin/%v", githubRef)
}

gitlabRef := os.Getenv("CI_MERGE_REQUEST_TARGET_BRANCH_NAME")
if gitlabRef != "" {
return fmt.Sprintf("origin/%v", gitlabRef)
}

// Drone specific variable https://docker-runner.docs.drone.io/configuration/environment/variables/drone-target-branch/
droneRef := os.Getenv("DRONE_TARGET_BRANCH")
if droneRef != "" {
return fmt.Sprintf("origin/%v", droneRef)
}

return "origin/master"
}
42 changes: 42 additions & 0 deletions find_compare_branch_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 TestFindCompareBranch(t *testing.T) {
// Github action specific environment variable
os.Setenv("GITHUB_BASE_REF", "github-develop")

actionCompareBranch := FindCompareBranch()

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

os.Setenv("GITHUB_BASE_REF", "")

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

gitlabCompareBranch := FindCompareBranch()

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

os.Setenv("CI_MERGE_REQUEST_TARGET_BRANCH_NAME", "")

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

droneCompareBranch := FindCompareBranch()

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

os.Setenv("DRONE_TARGET_BRANCH", "")

// Should default to master if no conditions are satisfied
defaultMaster := FindCompareBranch()

assert.Equal(t, "origin/master", defaultMaster)
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/outillage/integrations

go 1.13

require github.com/stretchr/testify v1.4.0
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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/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.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
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=

0 comments on commit 2f423f0

Please sign in to comment.