Skip to content

Commit

Permalink
Merge pull request #18 from tkak/add-ghe-support
Browse files Browse the repository at this point in the history
Add support for GitHub Enterprise
  • Loading branch information
b4b4r07 authored Sep 11, 2018
2 parents e15299a + 080bafe commit ee2ab24
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 4 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -122,6 +122,55 @@ terraform:
</details>
<details>
<summary>For GitHub Enterprise</summary>
```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 }} <sup>[CI link]( {{ .Link }} )</sup>
{{ .Message }}
{{if .Result}}
<pre><code> {{ .Result }}
</pre></code>
{{end}}
<details><summary>Details (Click me)</summary>
<pre><code> {{ .Body }}
</pre></code></details>
apply:
template: |
{{ .Title }}
{{ .Message }}
{{if .Result}}
<pre><code> {{ .Result }}
</pre></code>
{{end}}
<details><summary>Details (Click me)</summary>
<pre><code> {{ .Body }}
</pre></code></details>
```
</details>
<details>
<summary>For Slack</summary>
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions notifier/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down
83 changes: 83 additions & 0 deletions notifier/github/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ee2ab24

Please sign in to comment.