Skip to content

Commit

Permalink
Merge pull request #55 from AndresCidoncha/add_gitlab_plugin_support
Browse files Browse the repository at this point in the history
Add gitlab plugin support
  • Loading branch information
b4b4r07 authored Mar 2, 2020
2 parents 303e085 + a1e31bf commit b030441
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,14 @@ func drone() (ci CI, err error) {
func jenkins() (ci CI, err error) {
ci.PR.Number = 0
ci.PR.Revision = os.Getenv("GIT_COMMIT")
if ci.PR.Revision == "" {
ci.PR.Revision = os.Getenv("gitlabBefore")
}
ci.URL = os.Getenv("BUILD_URL")
pr := os.Getenv("PULL_REQUEST_NUMBER")
if pr == "" {
pr = os.Getenv("gitlabMergeRequestIid")
}
if pr == "" {
pr = os.Getenv("PULL_REQUEST_URL")
}
Expand All @@ -106,7 +112,7 @@ func jenkins() (ci CI, err error) {
re := regexp.MustCompile(`[1-9]\d*$`)
ci.PR.Number, err = strconv.Atoi(re.FindString(pr))
if err != nil {
return ci, fmt.Errorf("%v: cannot get env", pr)
return ci, fmt.Errorf("%v: Invalid PullRequest number or MergeRequest ID", pr)
}
return ci, err
}
Expand Down
67 changes: 67 additions & 0 deletions ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,73 @@ func TestJenkins(t *testing.T) {
}
}

func TestJenkinsGitLab(t *testing.T) {
envs := []string{
"BUILD_URL",
"gitlabBefore",
"gitlabMergeRequestIid",
}
saveEnvs := make(map[string]string)
for _, key := range envs {
saveEnvs[key] = os.Getenv(key)
os.Unsetenv(key)
}
defer func() {
for key, value := range saveEnvs {
os.Setenv(key, value)
}
}()

// https://wiki.jenkins.io/display/JENKINS/Building+a+software+project#Buildingasoftwareproject-belowJenkinsSetEnvironmentVariables
testCases := []struct {
fn func()
ci CI
ok bool
}{
{
fn: func() {
os.Setenv("gitlabBefore", "abcdefg")
os.Setenv("gitlabMergeRequestIid", "123")
os.Setenv("BUILD_URL", "http://jenkins.example.com/jenkins/job/test-job/1")
},
ci: CI{
PR: PullRequest{
Revision: "abcdefg",
Number: 123,
},
URL: "http://jenkins.example.com/jenkins/job/test-job/1",
},
ok: true,
},
{
fn: func() {
os.Setenv("gitlabMergeRequestIid", "")
os.Setenv("gitlabBefore", "abcdefg")
os.Setenv("BUILD_URL", "http://jenkins.example.com/jenkins/job/test-job/456")
},
ci: CI{
PR: PullRequest{
Revision: "abcdefg",
Number: 0,
},
URL: "http://jenkins.example.com/jenkins/job/test-job/456",
},
ok: true,
},
}

for _, testCase := range testCases {
testCase.fn()
ci, err := jenkins()
if !reflect.DeepEqual(ci, testCase.ci) {
t.Errorf("got %q but want %q", ci, testCase.ci)
}
if (err == nil) != testCase.ok {
t.Errorf("got error %q", err)
}
}
}

func TestGitLabCI(t *testing.T) {
envs := []string{
"CI_COMMIT_SHA",
Expand Down

0 comments on commit b030441

Please sign in to comment.