From 7276f320f196ed963b2230d4c81773242735e9bb Mon Sep 17 00:00:00 2001
From: Christoph Witzko <github@christophwitzko.com>
Date: Mon, 17 Aug 2020 10:02:29 +0200
Subject: [PATCH] feat: also use environment for configuration

---
 pkg/provider/gitlab.go      | 22 +++++++++++++++++++++-
 pkg/provider/gitlab_test.go |  2 +-
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/pkg/provider/gitlab.go b/pkg/provider/gitlab.go
index ed53fd5..3e11460 100644
--- a/pkg/provider/gitlab.go
+++ b/pkg/provider/gitlab.go
@@ -1,7 +1,9 @@
 package provider
 
 import (
+	"errors"
 	"fmt"
+	"os"
 	"regexp"
 
 	"github.com/Masterminds/semver/v3"
@@ -18,11 +20,29 @@ type GitLabRepository struct {
 
 func (repo *GitLabRepository) Init(config map[string]string) error {
 	gitlabBaseUrl := config["gitlabBaseUrl"]
+	if gitlabBaseUrl == "" {
+		gitlabBaseUrl = os.Getenv("CI_SERVER_URL")
+	}
+
 	token := config["token"]
+	if token == "" {
+		token = os.Getenv("GITLAB_TOKEN")
+	}
+	if token == "" {
+		return errors.New("gitlab token missing")
+	}
+
 	branch := config["gitlabBranch"]
+	if branch == "" {
+		branch = os.Getenv("CI_COMMIT_BRANCH")
+	}
+
 	projectID := config["gitlabProjectID"]
 	if projectID == "" {
-		return fmt.Errorf("project id is required")
+		projectID = os.Getenv("CI_PROJECT_ID")
+	}
+	if projectID == "" {
+		return fmt.Errorf("gitlabProjectID is required")
 	}
 
 	repo.projectID = projectID
diff --git a/pkg/provider/gitlab_test.go b/pkg/provider/gitlab_test.go
index 09faa49..11e883a 100644
--- a/pkg/provider/gitlab_test.go
+++ b/pkg/provider/gitlab_test.go
@@ -20,7 +20,7 @@ func TestNewGitlabRepository(t *testing.T) {
 	var repo *GitLabRepository
 	repo = &GitLabRepository{}
 	err := repo.Init(map[string]string{})
-	require.EqualError(err, "project id is required")
+	require.EqualError(err, "gitlab token missing")
 
 	repo = &GitLabRepository{}
 	err = repo.Init(map[string]string{
-- 
GitLab