Skip to content

Commit

Permalink
Merge branch 'master' into add-raw-output-to-slack
Browse files Browse the repository at this point in the history
  • Loading branch information
drlau authored Dec 14, 2022
2 parents 8cc74e3 + e60974f commit ab7648b
Show file tree
Hide file tree
Showing 23 changed files with 1,419 additions and 127 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# https://help.github.com/articles/about-codeowners/
* @b4b4r07 @dtan4 @drlau @micnncim
* @b4b4r07 @dtan4 @drlau @micnncim @KeisukeYamashita @tyuhara
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: '1.14.x'
go-version: '1.18.x'
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: '1.14.x'
go-version: '1.18.x'
- name: Checkout
uses: actions/checkout@v2
- name: Test
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ GitHub private repositories require the `repo` and `write:discussion` permission
- `BUILD_ID`
- `PROJECT_ID`
- `_PR_NUMBER`
- `_REGION`
- Recommended trigger events
- `terraform plan`: Pull request
- `terraform apply`: Push to branch
Expand Down
30 changes: 29 additions & 1 deletion ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import (
"strings"
)

const (
defaultCloudBuildRegion = "global"
)

var (
// https://help.github.com/en/actions/reference/events-that-trigger-workflows#pull-request-event-pull_request
githubActionsPRRefRegexp = regexp.MustCompile(`refs/pull/\d+/merge`)
)

// CI represents a common information obtained from all CI platforms
type CI struct {
PR PullRequest
Expand Down Expand Up @@ -144,14 +153,33 @@ func githubActions() (ci CI, err error) {
os.Getenv("GITHUB_RUN_ID"),
)
ci.PR.Revision = os.Getenv("GITHUB_SHA")
ci.PR.Number = 0

if githubActionsPRRefRegexp.MatchString(os.Getenv("GITHUB_REF")) {
s := strings.Split(os.Getenv("GITHUB_REF"), "/")[2]
pr, err := strconv.Atoi(s)
if err != nil {
return ci, err
}

ci.PR.Number = pr
}

return ci, err
}

func cloudbuild() (ci CI, err error) {
ci.PR.Number = 0
ci.PR.Revision = os.Getenv("COMMIT_SHA")

region := os.Getenv("_REGION")
if region == "" {
region = defaultCloudBuildRegion
}

ci.URL = fmt.Sprintf(
"https://console.cloud.google.com/cloud-build/builds/%s?project=%s",
"https://console.cloud.google.com/cloud-build/builds;region=%s/%s?project=%s",
region,
os.Getenv("BUILD_ID"),
os.Getenv("PROJECT_ID"),
)
Expand Down
26 changes: 23 additions & 3 deletions ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ func TestGitHubActions(t *testing.T) {
"GITHUB_SHA",
"GITHUB_REPOSITORY",
"GITHUB_RUN_ID",
"GITHUB_REF",
}
saveEnvs := make(map[string]string)
for _, key := range envs {
Expand Down Expand Up @@ -746,6 +747,22 @@ func TestGitHubActions(t *testing.T) {
},
ok: true,
},
{
fn: func() {
os.Setenv("GITHUB_SHA", "abcdefg")
os.Setenv("GITHUB_REPOSITORY", "mercari/tfnotify")
os.Setenv("GITHUB_RUN_ID", "12345")
os.Setenv("GITHUB_REF", "refs/pull/123/merge")
},
ci: CI{
PR: PullRequest{
Revision: "abcdefg",
Number: 123,
},
URL: "https://github.com/mercari/tfnotify/actions/runs/12345",
},
ok: true,
},
}

for _, testCase := range testCases {
Expand All @@ -766,6 +783,7 @@ func TestCloudBuild(t *testing.T) {
"BUILD_ID",
"PROJECT_ID",
"_PR_NUMBER",
"REGION",
}
saveEnvs := make(map[string]string)
for _, key := range envs {
Expand All @@ -790,13 +808,14 @@ func TestCloudBuild(t *testing.T) {
os.Setenv("BUILD_ID", "build-id")
os.Setenv("PROJECT_ID", "gcp-project-id")
os.Setenv("_PR_NUMBER", "123")
os.Setenv("_REGION", "asia-northeast1")
},
ci: CI{
PR: PullRequest{
Revision: "abcdefg",
Number: 123,
},
URL: "https://console.cloud.google.com/cloud-build/builds/build-id?project=gcp-project-id",
URL: "https://console.cloud.google.com/cloud-build/builds;region=asia-northeast1/build-id?project=gcp-project-id",
},
ok: true,
},
Expand All @@ -806,13 +825,14 @@ func TestCloudBuild(t *testing.T) {
os.Setenv("BUILD_ID", "build-id")
os.Setenv("PROJECT_ID", "gcp-project-id")
os.Setenv("_PR_NUMBER", "")
os.Setenv("_REGION", "")
},
ci: CI{
PR: PullRequest{
Revision: "",
Number: 0,
},
URL: "https://console.cloud.google.com/cloud-build/builds/build-id?project=gcp-project-id",
URL: "https://console.cloud.google.com/cloud-build/builds;region=global/build-id?project=gcp-project-id",
},
ok: true,
},
Expand All @@ -828,7 +848,7 @@ func TestCloudBuild(t *testing.T) {
Revision: "",
Number: 0,
},
URL: "https://console.cloud.google.com/cloud-build/builds/build-id?project=gcp-project-id",
URL: "https://console.cloud.google.com/cloud-build/builds;region=global/build-id?project=gcp-project-id",
},
ok: false,
},
Expand Down
18 changes: 12 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ type TypetalkNotifier struct {

// Terraform represents terraform configurations
type Terraform struct {
Default Default `yaml:"default"`
Fmt Fmt `yaml:"fmt"`
Plan Plan `yaml:"plan"`
Apply Apply `yaml:"apply"`
UseRawOutput bool `yaml:"use_raw_output,omitempty"`
Default Default `yaml:"default"`
Fmt Fmt `yaml:"fmt"`
Plan Plan `yaml:"plan"`
Apply Apply `yaml:"apply"`
UseRawOutput bool `yaml:"use_raw_output,omitempty"`
Validate Validate `yaml:"validate"`
}

// Default is a default setting for terraform commands
Expand All @@ -79,6 +80,11 @@ type Fmt struct {
Template string `yaml:"template"`
}

// Validate is a terraform validate config
type Validate struct {
Template string `yaml:"template"`
}

// Plan is a terraform plan config
type Plan struct {
Template string `yaml:"template"`
Expand Down Expand Up @@ -174,7 +180,7 @@ func (cfg *Config) Validation() error {
}
if cfg.isDefinedTypetalk() {
if cfg.Notifier.Typetalk.TopicID == "" {
return fmt.Errorf("Typetalk topic id is missing")
return fmt.Errorf("typetalk topic id is missing")
}
}
notifier := cfg.GetNotifierType()
Expand Down
11 changes: 10 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func TestLoadFile(t *testing.T) {
Fmt: Fmt{
Template: "",
},
Validate: Validate{
Template: "",
},
Plan: Plan{
Template: "{{ .Title }}\n{{ .Message }}\n{{if .Result}}\n<pre><code>{{ .Result }}\n</pre></code>\n{{end}}\n<details><summary>Details (Click me)</summary>\n\n<pre><code>{{ .Body }}\n</pre></code></details>\n",
WhenDestroy: WhenDestroy{},
Expand Down Expand Up @@ -91,6 +94,9 @@ func TestLoadFile(t *testing.T) {
Fmt: Fmt{
Template: "",
},
Validate: Validate{
Template: "",
},
Plan: Plan{
Template: "{{ .Title }}\n{{ .Message }}\n{{if .Result}}\n<pre><code>{{ .Result }}\n</pre></code>\n{{end}}\n<details><summary>Details (Click me)</summary>\n\n<pre><code>{{ .Body }}\n</pre></code></details>\n",
WhenAddOrUpdateOnly: WhenAddOrUpdateOnly{
Expand Down Expand Up @@ -145,6 +151,9 @@ func TestLoadFile(t *testing.T) {
Fmt: Fmt{
Template: "",
},
Validate: Validate{
Template: "",
},
Plan: Plan{
Template: "{{ .Title }}\n{{ .Message }}\n{{if .Result}}\n<pre><code>{{ .Result }}\n</pre></code>\n{{end}}\n<details><summary>Details (Click me)</summary>\n\n<pre><code>{{ .Body }}\n</pre></code></details>\n",
WhenDestroy: WhenDestroy{},
Expand Down Expand Up @@ -342,7 +351,7 @@ notifier:
typetalk:
token: token
`),
expected: "Typetalk topic id is missing",
expected: "typetalk topic id is missing",
},
{
contents: []byte(`
Expand Down
20 changes: 11 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ module github.com/mercari/tfnotify
go 1.12

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/google/go-github v17.0.0+incompatible
github.com/kr/pretty v0.2.0 // indirect
github.com/lestrrat-go/pdebug v0.0.0-20210111095411-35b07dbf089b // indirect
github.com/lestrrat-go/slack v0.0.0-20190827134815-1aaae719550a
github.com/mattn/go-colorable v0.1.4
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-colorable v0.1.12
github.com/nulab/go-typetalk v2.1.1+incompatible
github.com/urfave/cli v1.22.2
github.com/xanzy/go-gitlab v0.22.3
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
golang.org/x/sys v0.0.0-20200121082415-34d275377bf9 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/urfave/cli v1.22.9
github.com/xanzy/go-gitlab v0.69.0
golang.org/x/net v0.0.0-20220708220712-1185a9018129 // indirect
golang.org/x/oauth2 v0.0.0-20220718184931-c8730f7fcb92
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.7
gopkg.in/yaml.v2 v2.4.0
)
Loading

0 comments on commit ab7648b

Please sign in to comment.