From 371b36f58b4b83c7e057b56806fd84fc98741ad3 Mon Sep 17 00:00:00 2001 From: Takaaki Furukawa Date: Fri, 27 Jul 2018 12:42:01 +0900 Subject: [PATCH 1/2] Add support for GitHub Enterprise --- README.md | 51 ++++++++++++++++++++++++++++++++++++++- config/config.go | 1 + main.go | 7 +++--- notifier/github/client.go | 17 +++++++++++++ 4 files changed, 72 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9f02b4d..1bd47a8 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ For `plan` command, you also need to specify `plan` as the argument of tfnotify. When running tfnotify, you can specify the configuration path via `--config` option (if it's omitted, it defaults to `{.,}tfnotify.y{,a}ml`). -The example settings of GitHub and Slack are as follows. Incidentally, there is no need to replace TOKEN string such as `$GITHUB_TOKEN` with the actual token. Instead, it must be defined as environment variables in CI settings. +The example settings of GitHub, GitHub Enterprise and Slack are as follows. Incidentally, there is no need to replace TOKEN string such as `$GITHUB_TOKEN` with the actual token. Instead, it must be defined as environment variables in CI settings. [template](https://golang.org/pkg/text/template/) of Go can be used for `template`. The templates can be used in `tfnotify.yaml` are as follows: @@ -122,6 +122,55 @@ terraform: +
+For GitHub Enterprise + +```yaml +--- +ci: circleci +notifier: + github: + token: $GITHUB_TOKEN + base_url: $GITHUB_BASE_URL + repository: + owner: "mercari" + name: "tfnotify" +terraform: + fmt: + template: | + {{ .Title }} + + {{ .Message }} + + {{ .Result }} + + {{ .Body }} + plan: + template: | + {{ .Title }} [CI link]( {{ .Link }} ) + {{ .Message }} + {{if .Result}} +
 {{ .Result }}
+      
+ {{end}} +
Details (Click me) +
 {{ .Body }}
+      
+ apply: + template: | + {{ .Title }} + {{ .Message }} + {{if .Result}} +
 {{ .Result }}
+      
+ {{end}} +
Details (Click me) +
 {{ .Body }}
+      
+``` + +
+
For Slack diff --git a/config/config.go b/config/config.go index 951ed76..2d954cf 100644 --- a/config/config.go +++ b/config/config.go @@ -28,6 +28,7 @@ type Notifier struct { // GithubNotifier is a notifier for GitHub type GithubNotifier struct { Token string `yaml:"token"` + BaseURL string `yaml:"base_url"` Repository Repository `yaml:"repository"` } diff --git a/main.go b/main.go index 568e1b6..5e136cf 100644 --- a/main.go +++ b/main.go @@ -62,9 +62,10 @@ func (t *tfnotify) Run() error { switch selectedNotifier { case "github": client, err := github.NewClient(github.Config{ - Token: t.config.Notifier.Github.Token, - Owner: t.config.Notifier.Github.Repository.Owner, - Repo: t.config.Notifier.Github.Repository.Name, + Token: t.config.Notifier.Github.Token, + BaseURL: t.config.Notifier.Github.BaseURL, + Owner: t.config.Notifier.Github.Repository.Owner, + Repo: t.config.Notifier.Github.Repository.Name, PR: github.PullRequest{ Revision: ci.PR.Revision, Number: ci.PR.Number, diff --git a/notifier/github/client.go b/notifier/github/client.go index d36ecde..6e41179 100644 --- a/notifier/github/client.go +++ b/notifier/github/client.go @@ -13,6 +13,9 @@ import ( // EnvToken is GitHub API Token const EnvToken = "GITHUB_TOKEN" +// EnvBaseURL is GitHub base URL. This can be set to a domain endpoint to use with GitHub Enterprise. +const EnvBaseURL = "GITHUB_BASE_URL" + // Client is a API client for GitHub type Client struct { *github.Client @@ -32,6 +35,7 @@ type Client struct { // Config is a configuration for GitHub client type Config struct { Token string + BaseURL string Owner string Repo string PR PullRequest @@ -67,6 +71,19 @@ func NewClient(cfg Config) (*Client, error) { tc := oauth2.NewClient(oauth2.NoContext, ts) client := github.NewClient(tc) + baseURL := cfg.BaseURL + baseURL = strings.TrimPrefix(baseURL, "$") + if baseURL == EnvBaseURL { + baseURL = os.Getenv(EnvBaseURL) + } + if baseURL != "" { + var err error + client, err = github.NewEnterpriseClient(baseURL, baseURL, tc) + if err != nil { + return &Client{}, errors.New("failed to create a new github api client") + } + } + c := &Client{ Config: cfg, Client: client, From 080bafe32fe5fc61ec160c997580240a449207d0 Mon Sep 17 00:00:00 2001 From: Takaaki Furukawa Date: Thu, 30 Aug 2018 20:29:32 +0900 Subject: [PATCH 2/2] Add test case for when base_url property of YAML is specified --- notifier/github/client_test.go | 83 ++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/notifier/github/client_test.go b/notifier/github/client_test.go index 7c6e692..bbe7cf4 100644 --- a/notifier/github/client_test.go +++ b/notifier/github/client_test.go @@ -72,6 +72,89 @@ func TestNewClient(t *testing.T) { } } +func TestNewClientWithBaseURL(t *testing.T) { + githubBaseURL := os.Getenv(EnvBaseURL) + defer func() { + os.Setenv(EnvBaseURL, githubBaseURL) + }() + os.Setenv(EnvBaseURL, "") + + testCases := []struct { + config Config + envBaseURL string + expect string + }{ + { + // specify directly + config: Config{ + Token: "abcdefg", + BaseURL: "https://git.example.com/api/v3/", + }, + envBaseURL: "", + expect: "https://git.example.com/api/v3/", + }, + { + // specify via env but not to be set env (part 1) + config: Config{ + Token: "abcdefg", + BaseURL: "GITHUB_BASE_URL", + }, + envBaseURL: "", + expect: "https://api.github.com/", + }, + { + // specify via env (part 1) + config: Config{ + Token: "abcdefg", + BaseURL: "GITHUB_BASE_URL", + }, + envBaseURL: "https://git.example.com/api/v3/", + expect: "https://git.example.com/api/v3/", + }, + { + // specify via env but not to be set env (part 2) + config: Config{ + Token: "abcdefg", + BaseURL: "$GITHUB_BASE_URL", + }, + envBaseURL: "", + expect: "https://api.github.com/", + }, + { + // specify via env (part 2) + config: Config{ + Token: "abcdefg", + BaseURL: "$GITHUB_BASE_URL", + }, + envBaseURL: "https://git.example.com/api/v3/", + expect: "https://git.example.com/api/v3/", + }, + { + // no specification (part 1) + config: Config{Token: "abcdefg"}, + envBaseURL: "", + expect: "https://api.github.com/", + }, + { + // no specification (part 2) + config: Config{Token: "abcdefg"}, + envBaseURL: "https://git.example.com/api/v3/", + expect: "https://api.github.com/", + }, + } + for _, testCase := range testCases { + os.Setenv(EnvBaseURL, testCase.envBaseURL) + c, err := NewClient(testCase.config) + if err != nil { + continue + } + url := c.Client.BaseURL.String() + if url != testCase.expect { + t.Errorf("got %q but want %q", url, testCase.expect) + } + } +} + func TestIsNumber(t *testing.T) { testCases := []struct { pr PullRequest