Repository: jenkins-x/lighthouse
Pull request #873 (2020-07-31 13:38:03)

chore: Move various env-var-related functions to util
Signed-off-by: Andrew Bayer <andrew.bayer@gmail.com>


Patch:
diff --git a/cmd/keeper/main.go b/cmd/keeper/main.go
index 05c38a58..bac448c2 100644
--- a/cmd/keeper/main.go
+++ b/cmd/keeper/main.go
@@ -109,7 +109,7 @@ func main() {
 	var err error
 	botName := o.botName
 	if botName == "" {
-		botName = os.Getenv("GIT_USER")
+		botName = util.GetBotName()
 	}
 	if util.GetGitHubAppSecretDir() != "" {
 		botName, err = util.GetGitHubAppAPIUser()
@@ -122,19 +122,16 @@ func main() {
 	}
 	serverURL := o.gitServerURL
 	if serverURL == "" {
-		serverURL = os.Getenv("GIT_SERVER")
-	}
-	if serverURL == "" {
-		serverURL = "https://github.com"
+		serverURL = util.GetGitServer()
 	}
 	gitKind := o.gitKind
 	if gitKind == "" {
-		gitKind = os.Getenv("GIT_KIND")
+		gitKind = util.GitKind()
 	}
-	if gitKind == "" {
-		gitKind = "github"
+	gitToken, err := util.GetSCMToken(gitKind)
+	if err != nil {
+		logrus.WithError(err).Fatal("Error creating Keeper controller.")
 	}
-	gitToken := os.Getenv("GIT_TOKEN")
 
 	cfg := configAgent.Config
 	c, err := githubapp.NewKeeperController(configAgent, botName, gitKind, gitToken, serverURL, o.maxRecordsPerPool, o.historyURI, o.statusURI, o.namespace)
diff --git a/pkg/foghorn/controller.go b/pkg/foghorn/controller.go
index aef5b0ee..91cdb7fd 100644
--- a/pkg/foghorn/controller.go
+++ b/pkg/foghorn/controller.go
@@ -11,14 +11,12 @@ import (
 	"time"
 
 	"github.com/jenkins-x/go-scm/scm"
-	"github.com/jenkins-x/go-scm/scm/factory"
 	"github.com/jenkins-x/lighthouse/pkg/apis/lighthouse/v1alpha1"
 	clientset "github.com/jenkins-x/lighthouse/pkg/client/clientset/versioned"
 	lhinformers "github.com/jenkins-x/lighthouse/pkg/client/informers/externalversions/lighthouse/v1alpha1"
 	lhlisters "github.com/jenkins-x/lighthouse/pkg/client/listers/lighthouse/v1alpha1"
 	"github.com/jenkins-x/lighthouse/pkg/config"
 	"github.com/jenkins-x/lighthouse/pkg/plugins"
-	"github.com/jenkins-x/lighthouse/pkg/scmprovider"
 	"github.com/jenkins-x/lighthouse/pkg/scmprovider/reporter"
 	"github.com/jenkins-x/lighthouse/pkg/util"
 	"github.com/jenkins-x/lighthouse/pkg/watcher"
@@ -329,7 +327,7 @@ func (c *Controller) reportStatus(ns string, activity *v1alpha1.ActivityRecord,
 	repo := activity.Repo
 	gitURL := activity.GitURL
 	activityStatus := activity.Status
-	statusInfo := toScmStatusDescriptionRunningStages(activity, c.gitKind())
+	statusInfo := toScmStatusDescriptionRunningStages(activity, util.GitKind())
 
 	fields := map[string]interface{}{
 		"name":        activity.Name,
@@ -382,7 +380,7 @@ func (c *Controller) reportStatus(ns string, activity *v1alpha1.ActivityRecord,
 
 	// Trigger external plugins if appropriate
 	if external := util.ExternalPluginsForEvent(c.pluginConfig, util.LighthousePayloadTypeActivity, fmt.Sprintf("%s/%s", owner, repo)); len(external) > 0 {
-		go util.CallExternalPluginsWithActivityRecord(c.logger, external, activity, c.hmacToken(), c.wg)
+		go util.CallExternalPluginsWithActivityRecord(c.logger, external, activity, util.HMACToken(), c.wg)
 	}
 
 	pipelineContext := activity.Context
@@ -396,7 +394,7 @@ func (c *Controller) reportStatus(ns string, activity *v1alpha1.ActivityRecord,
 		Desc:   statusInfo.description,
 		Target: job.Status.ReportURL,
 	}
-	scmClient, _, _, err := c.createSCMClient(owner)
+	scmClient, _, _, _, err := util.GetSCMClient(owner)
 	if err != nil {
 		c.logger.WithFields(fields).WithError(err).Warnf("failed to create SCM client")
 		return
@@ -470,62 +468,6 @@ func durationString(start *metav1.Time, end *metav1.Time) string {
 	return end.Sub(start.Time).Round(time.Second).String()
 }
 
-func (c *Controller) createSCMClient(owner string) (scmprovider.SCMClient, string, string, error) {
-	kind := c.gitKind()
-	serverURL := os.Getenv("GIT_SERVER")
-	ghaSecretDir := util.GetGitHubAppSecretDir()
-
-	var token string
-	var err error
-	if ghaSecretDir != "" {
-		tokenFinder := util.NewOwnerTokensDir(serverURL, ghaSecretDir)
-		token, err = tokenFinder.FindToken(owner)
-		if err != nil {
-			logrus.Errorf("failed to read owner token: %s", err.Error())
-			return nil, "", "", errors.Wrapf(err, "failed to read owner token for owner %s", owner)
-		}
-	} else {
-		token, err = c.createSCMToken(kind)
-		if err != nil {
-			return nil, serverURL, token, err
-		}
-	}
-
-	client, err := factory.NewClient(kind, serverURL, token)
-	scmClient := scmprovider.ToClient(client, c.GetBotName())
-	return scmClient, serverURL, token, err
-}
-
-func (c *Controller) gitKind() string {
-	kind := os.Getenv("GIT_KIND")
-	if kind == "" {
-		kind = "github"
-	}
-	return kind
-}
-
-// GetBotName returns the bot name
-func (c *Controller) GetBotName() string {
-	botName := os.Getenv("GIT_USER")
-	if botName == "" {
-		botName = "jenkins-x-bot"
-	}
-	return botName
-}
-
-func (c *Controller) createSCMToken(gitKind string) (string, error) {
-	envName := "GIT_TOKEN"
-	value := os.Getenv(envName)
-	if value == "" {
-		return value, fmt.Errorf("No token available for git kind %s at environment variable $%s", gitKind, envName)
-	}
-	return value, nil
-}
-
-func (c *Controller) hmacToken() string {
-	return os.Getenv("HMAC_TOKEN")
-}
-
 // stopper returns a channel that remains open until an interrupt is received.
 func stopper() chan struct{} {
 	stop := make(chan struct{})
diff --git a/pkg/util/scmclient.go b/pkg/util/scmclient.go
index 61b5dd73..ce2bf071 100644
--- a/pkg/util/scmclient.go
+++ b/pkg/util/scmclient.go
@@ -2,10 +2,16 @@ package util
 
 import (
 	"context"
+	"fmt"
 	"net/http"
+	"os"
 
 	"github.com/jenkins-x/go-scm/scm"
+	"github.com/jenkins-x/go-scm/scm/factory"
 	"github.com/jenkins-x/go-scm/scm/transport"
+	"github.com/jenkins-x/lighthouse/pkg/scmprovider"
+	"github.com/pkg/errors"
+	"github.com/sirupsen/logrus"
 	"golang.org/x/oauth2"
 )
 
@@ -46,3 +52,80 @@ func defaultScmTransport(scmClient *scm.Client) {
 		scmClient.Client.Transport = http.DefaultTransport
 	}
 }
+
+// GetGitServer returns the git server base URL from the environment
+func GetGitServer() string {
+	serverURL := os.Getenv("GIT_SERVER")
+
+	if serverURL == "" {
+		serverURL = "https://github.com"
+	}
+	return serverURL
+}
+
+// GetSCMClient gets the Lighthouse SCM client, go-scm client, server URL, and token for the current user and server
+func GetSCMClient(owner string) (scmprovider.SCMClient, *scm.Client, string, string, error) {
+	kind := GitKind()
+	serverURL := GetGitServer()
+	ghaSecretDir := GetGitHubAppSecretDir()
+
+	var token string
+	var err error
+	if ghaSecretDir != "" && owner != "" {
+		tokenFinder := NewOwnerTokensDir(serverURL, ghaSecretDir)
+		token, err = tokenFinder.FindToken(owner)
+		if err != nil {
+			logrus.Errorf("failed to read owner token: %s", err.Error())
+			return nil, nil, "", "", errors.Wrapf(err, "failed to read owner token for owner %s", owner)
+		}
+	} else {
+		token, err = GetSCMToken(kind)
+		if err != nil {
+			return nil, nil, serverURL, token, err
+		}
+	}
+
+	client, err := factory.NewClient(kind, serverURL, token)
+	scmClient := scmprovider.ToClient(client, GetBotName())
+	return scmClient, client, serverURL, token, err
+}
+
+// GitKind gets the git kind from the environment
+func GitKind() string {
+	kind := os.Getenv("GIT_KIND")
+	if kind == "" {
+		kind = "github"
+	}
+	return kind
+}
+
+// GetBotName returns the bot name from the environment
+func GetBotName() string {
+	if GetGitHubAppSecretDir() != "" {
+		ghaBotName, err := GetGitHubAppAPIUser()
+		// TODO: Probably should handle error cases here better, but for now, just fall through.
+		if err == nil && ghaBotName != "" {
+			return ghaBotName
+		}
+	}
+	botName := os.Getenv("GIT_USER")
+	if botName == "" {
+		botName = "jenkins-x-bot"
+	}
+	return botName
+}
+
+// GetSCMToken gets the SCM secret from the environment
+func GetSCMToken(gitKind string) (string, error) {
+	envName := "GIT_TOKEN"
+	value := os.Getenv(envName)
+	if value == "" {
+		return value, fmt.Errorf("no token available for git kind %s at environment variable $%s", gitKind, envName)
+	}
+	return value, nil
+}
+
+// HMACToken gets the HMAC token from the environment
+func HMACToken() string {
+	return os.Getenv("HMAC_TOKEN")
+}
diff --git a/pkg/webhook/webhook.go b/pkg/webhook/webhook.go
index e57427cc..b93bb0c3 100644
--- a/pkg/webhook/webhook.go
+++ b/pkg/webhook/webhook.go
@@ -13,7 +13,6 @@ import (
 	"syscall"
 
 	"github.com/jenkins-x/go-scm/scm"
-	"github.com/jenkins-x/go-scm/scm/factory"
 	"github.com/jenkins-x/lighthouse/pkg/clients"
 	"github.com/jenkins-x/lighthouse/pkg/cmd/helper"
 	"github.com/jenkins-x/lighthouse/pkg/config"
@@ -95,12 +94,8 @@ func (o *Options) Run() error {
 	}
 	defer o.configMapWatcher.Stop()
 
-	_, o.gitServerURL, err = o.createSCMClient()
-	if err != nil {
-		return errors.Wrapf(err, "failed to create ScmClient")
-	}
-
-	gitClient, err := git.NewClient(o.gitServerURL, o.gitKind())
+	o.gitServerURL = util.GetGitServer()
+	gitClient, err := git.NewClient(o.gitServerURL, util.GitKind())
 	if err != nil {
 		logrus.WithError(err).Fatal("Error getting git client.")
 	}
@@ -187,7 +182,7 @@ func (o *Options) handleWebHookRequests(w http.ResponseWriter, r *http.Request)
 	}
 
 	r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
-	scmClient, serverURL, err := o.createSCMClient()
+	_, scmClient, serverURL, _, err := util.GetSCMClient("")
 	if err != nil {
 		logrus.Errorf("failed to create SCM scmClient: %s", err.Error())
 		responseHTTPError(w, http.StatusInternalServerError, fmt.Sprintf("500 Internal Server Error: Failed to parse webhook: %s", err.Error()))
@@ -222,8 +217,8 @@ func (o *Options) handleWebHookRequests(w http.ResponseWriter, r *http.Request)
 			return
 		}
 	} else {
-		gitCloneUser = o.GetBotName()
-		token, err = o.createSCMToken(o.gitKind())
+		gitCloneUser = util.GetBotName()
+		token, err = util.GetSCMToken(util.GitKind())
 		if err != nil {
 			logrus.Errorf("no scm token specified: %s", err.Error())
 			responseHTTPError(w, http.StatusInternalServerError, fmt.Sprintf("500 Internal Server Error: no scm token specified: %s", err.Error()))
@@ -241,7 +236,7 @@ func (o *Options) handleWebHookRequests(w http.ResponseWriter, r *http.Request)
 	util.AddAuthToSCMClient(scmClient, token, ghaSecretDir != "")
 
 	o.server.ClientAgent = &plugins.ClientAgent{
-		BotName:           o.GetBotName(),
+		BotName:           util.GetBotName(),
 		SCMProviderClient: scmClient,
 		KubernetesClient:  kubeClient,
 		GitClient:         o.gitClient,
@@ -254,7 +249,7 @@ func (o *Options) handleWebHookRequests(w http.ResponseWriter, r *http.Request)
 	}
 	// Demux events only to external plugins that require this event.
 	if external := util.ExternalPluginsForEvent(o.server.Plugins, string(webhook.Kind()), webhook.Repository().FullName); len(external) > 0 {
-		go util.CallExternalPluginsWithWebhook(l, external, webhook, o.hmacToken(), &o.server.wg)
+		go util.CallExternalPluginsWithWebhook(l, external, webhook, util.HMACToken(), &o.server.wg)
 	}
 
 	_, err = w.Write([]byte(output))
@@ -405,53 +400,8 @@ func (o *Options) ProcessWebHook(l *logrus.Entry, webhook scm.Webhook) (*logrus.
 	return l, fmt.Sprintf("unknown hook %s", webhook.Kind()), nil
 }
 
-func (o *Options) hmacToken() string {
-	return os.Getenv("HMAC_TOKEN")
-}
-
 func (o *Options) secretFn(webhook scm.Webhook) (string, error) {
-	return o.hmacToken(), nil
-}
-
-func (o *Options) createSCMClient() (*scm.Client, string, error) {
-	kind := o.gitKind()
-	serverURL := os.Getenv("GIT_SERVER")
-
-	client, err := factory.NewClient(kind, serverURL, "")
-	return client, serverURL, err
-}
-
-func (o *Options) gitKind() string {
-	kind := os.Getenv("GIT_KIND")
-	if kind == "" {
-		kind = "github"
-	}
-	return kind
-}
-
-// GetBotName returns the bot name
-func (o *Options) GetBotName() string {
-	if util.GetGitHubAppSecretDir() != "" {
-		ghaBotName, err := util.GetGitHubAppAPIUser()
-		// TODO: Probably should handle error cases here better, but for now, just fall through.
-		if err == nil && ghaBotName != "" {
-			return ghaBotName
-		}
-	}
-	o.botName = os.Getenv("GIT_USER")
-	if o.botName == "" {
-		o.botName = "jenkins-x-bot"
-	}
-	return o.botName
-}
-
-func (o *Options) createSCMToken(gitKind string) (string, error) {
-	envName := "GIT_TOKEN"
-	value := os.Getenv(envName)
-	if value == "" {
-		return value, fmt.Errorf("No token available for git kind %s at environment variable $%s", gitKind, envName)
-	}
-	return value, nil
+	return util.HMACToken(), nil
 }
 
 func (o *Options) createHookServer() (*Server, error) {


Test patch:
diff --git a/pkg/webhook/webhook_test.go b/pkg/webhook/webhook_test.go
index 2a078ed5..1bc228a2 100644
--- a/pkg/webhook/webhook_test.go
+++ b/pkg/webhook/webhook_test.go
@@ -120,7 +120,6 @@ func (suite *WebhookTestSuite) TestProcessWebhookUnknownRepo() {
 }
 
 func (suite *WebhookTestSuite) SetupSuite() {
-	options := &Options{}
 	t := suite.T()
 	configAgent := &config.Agent{}
 	pluginAgent := &plugins.ConfigAgent{}
@@ -142,12 +141,12 @@ func (suite *WebhookTestSuite) SetupSuite() {
 	var objs []runtime.Object
 	kubeClient := kubefake.NewSimpleClientset(objs...)
 	lhClient := fake.NewSimpleClientset()
-	scmClient, serverURL, err := options.createSCMClient()
+	_, scmClient, serverURL, _, err := util.GetSCMClient("")
 	assert.NoError(t, err)
-	gitClient, err := git.NewClient(serverURL, options.gitKind())
+	gitClient, err := git.NewClient(serverURL, util.GitKind())
 	assert.NoError(t, err)
-	user := options.GetBotName()
-	token, err := options.createSCMToken(options.gitKind())
+	user := util.GetBotName()
+	token, err := util.GetSCMToken(util.GitKind())
 	gitClient.SetCredentials(user, func() []byte {
 		return []byte(token)
 	})
@@ -157,7 +156,7 @@ func (suite *WebhookTestSuite) SetupSuite() {
 			ConfigAgent: configAgent,
 			Plugins:     pluginAgent,
 			ClientAgent: &plugins.ClientAgent{
-				BotName:           options.GetBotName(),
+				BotName:           util.GetBotName(),
 				SCMProviderClient: scmClient,
 				KubernetesClient:  kubeClient,
 				GitClient:         gitClient,