Skip to content

Commit

Permalink
fix: parsing of CIRCLE_PULL_REQUEST
Browse files Browse the repository at this point in the history
  • Loading branch information
karlderkaefer committed Sep 1, 2021
1 parent 5ffaf2e commit 83e841b
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 15 deletions.
56 changes: 41 additions & 15 deletions config/app.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package config

import (
"errors"
"fmt"
"github.com/sirupsen/logrus"
"os"
"strconv"
"strings"
)

type AppConfig struct {
Expand All @@ -17,50 +18,75 @@ type AppConfig struct {
DeleteComment bool
}

type ConfigValidationError struct {
cliArg string
envVar string
}

func (e *ConfigValidationError) Error() string {
return fmt.Sprintf("missing argument. Set --%s argument or env var %s", e.cliArg, e.envVar)
}

const (
ENV_GITHUB_TOKEN = "GITHUB_TOKEN"
ENV_PULL_REQUEST_ID = "CIRCLE_PULL_REQUEST"
ENV_REPO_NAME = "CIRCLE_PROJECT_REPONAME"
ENV_REPO_OWNER = "CIRCLE_PROJECT_USERNAME"
)

func (a *AppConfig) Init() error {
if a.RepoName == "" {
a.RepoName = readFromEnv("CIRCLE_PROJECT_REPONAME")
a.RepoName = readFromEnv(ENV_REPO_NAME)
}
if a.RepoOwner == "" {
a.RepoOwner = readFromEnv("CIRCLE_PROJECT_USERNAME")
a.RepoOwner = readFromEnv(ENV_REPO_OWNER)
}
if a.GithubToken == "" {
a.GithubToken = readFromEnv("GITHUB_TOKEN")
a.GithubToken = readFromEnv(ENV_GITHUB_TOKEN)
}
if a.PullRequest == 0 {
a.PullRequest = readIntFromEnv("CIRCLE_PR_NUMBER")
prNumber, err := readPullRequestFromEnv()
if err != nil {
return err
}
a.PullRequest = prNumber
}
// validate
if a.RepoName == "" {
return errors.New("missing argument. Set github-repo argument or env var CIRCLE_PROJECT_REPONAME")
return &ConfigValidationError{"github-repo", ENV_REPO_NAME}
}
if a.RepoOwner == "" {
return errors.New("missing argument. Set github-owner argument or env var CIRCLE_PROJECT_USERNAME")
return &ConfigValidationError{"github-owner", ENV_REPO_OWNER}
}
if a.GithubToken == "" {
return errors.New("missing argument. Set github-token argument or env var GITHUB_TOKEN")
return &ConfigValidationError{"github-token", ENV_GITHUB_TOKEN}
}
if a.PullRequest == 0 {
return errors.New("missing argument. Set pull-request-id argument or env var CIRCLE_PR_NUMBER")
return &ConfigValidationError{"pull-request-id", ENV_PULL_REQUEST_ID}
}
return nil
}

func readFromEnv(env string) string {
val := os.Getenv(env)
if val != "" {
logrus.Debugf("Reading env var %s with value %s", env, val)
logrus.Debugf("Reading env var %s with value '%s'", env, val)
}
return val
}

func readIntFromEnv(env string) int {
val, err := strconv.ParseInt(os.Getenv(env), 10, 0)
// CIRCLE_PULL_REQUEST=https://github.com/signavio/suite-infrastructure/pull/1361
func readPullRequestFromEnv() (int, error) {
url := os.Getenv(ENV_PULL_REQUEST_ID)
elements := strings.Split(url, "/")
prNumber := elements[len(elements)-1]
val, err := strconv.ParseInt(prNumber, 10, 0)
if err != nil {
logrus.Fatalf("Can not parse int from env var %s", env)
logrus.Errorf("Could not parse env %s with value '%v' to int", ENV_PULL_REQUEST_ID, url)
return 0, err
}
if val != 0 {
logrus.Debugf("Reading env var %s with value %d", env, val)
logrus.Debugf("Reading env var %s with value '%d'", ENV_PULL_REQUEST_ID, val)
}
return int(val)
return int(val), nil
}
143 changes: 143 additions & 0 deletions config/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package config

import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"os"
"strconv"
"testing"
)

type testCase struct {
input string
expected int
err error
}

type testCaseInit struct {
description string
inputConfig AppConfig
envVars map[string]string
expectedConfig AppConfig
err error
}

func TestAppConfig_ReadPullRequestFromEnv(t *testing.T) {
logrus.SetLevel(7)
testCases := []testCase{
{
input: "https://github.com/pansenentertainment/uepsilon/pull/1361",
expected: 1361,
err: nil,
},
{
input: "/1361",
expected: 1361,
err: nil,
},
{
input: "1361",
expected: 1361,
err: nil,
},
{
input: "",
expected: 0,
err: &strconv.NumError{},
},
{
input: "https://github.com/pansenentertainment/uepsilon/pull",
expected: 0,
err: &strconv.NumError{},
},
}

for _, c := range testCases {
_ = os.Setenv(ENV_PULL_REQUEST_ID, c.input)
actual, err := readPullRequestFromEnv()
assert.IsType(t, c.err, err)
assert.Equal(t, c.expected, actual)
}
}

func TestAppConfig_Init(t *testing.T) {
testCasesInit := []testCaseInit{
{
description: "test set values by env variables",
inputConfig: AppConfig{
LogFile: "./cdk.log",
},
envVars: map[string]string{
ENV_PULL_REQUEST_ID: "23",
ENV_GITHUB_TOKEN: "some-token",
ENV_REPO_NAME: "Uepsilon",
ENV_REPO_OWNER: "pansenentertainment",
},
expectedConfig: AppConfig{
LogFile: "./cdk.log",
RepoName: "Uepsilon",
RepoOwner: "pansenentertainment",
GithubToken: "some-token",
PullRequest: 23,
},
err: nil,
},
{
description: "test override of env vars with cli arguments",
inputConfig: AppConfig{
LogFile: "./cdk.log",
RepoName: "changedRepo",
GithubToken: "changedToken",
RepoOwner: "changedOwner",
PullRequest: 12,
},
envVars: map[string]string{
ENV_PULL_REQUEST_ID: "23",
ENV_GITHUB_TOKEN: "some-token",
ENV_REPO_NAME: "Uepsilon",
ENV_REPO_OWNER: "pansenentertainment",
},
expectedConfig: AppConfig{
LogFile: "./cdk.log",
RepoName: "changedRepo",
GithubToken: "changedToken",
RepoOwner: "changedOwner",
PullRequest: 12,
},
err: nil,
},
{
description: "test missing github token values by env variables",
inputConfig: AppConfig{
LogFile: "./cdk.log",
DeleteComment: true,
},
envVars: map[string]string{
ENV_PULL_REQUEST_ID: "23",
ENV_REPO_NAME: "Uepsilon",
ENV_REPO_OWNER: "pansenentertainment",
},
expectedConfig: AppConfig{
LogFile: "./cdk.log",
DeleteComment: true,
RepoName: "Uepsilon",
RepoOwner: "pansenentertainment",
GithubToken: "",
PullRequest: 23,
},
err: &ConfigValidationError{"github-token", ENV_GITHUB_TOKEN},
},
}
for _, c := range testCasesInit {
t.Log(c.description)
for k, v := range c.envVars {
_ = os.Setenv(k, v)
}
err := c.inputConfig.Init()
assert.Equal(t, c.err, err)
assert.Equal(t, c.expectedConfig, c.inputConfig)
for k, _ := range c.envVars {
_ = os.Unsetenv(k)
}
}
}

0 comments on commit 83e841b

Please sign in to comment.