From 6baf05b22ade4f2ae805860b6718f4ab7e0592c0 Mon Sep 17 00:00:00 2001 From: Bismo Baruno Date: Sat, 20 Jun 2020 06:40:04 +0700 Subject: [PATCH 01/21] fix typo on readme and godoc --- README.md | 2 +- terraform/template.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ff004d1..cca9b85 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ $ go get -u github.com/mercari/tfnotify 2. Bind parsed results to Go templates 3. Notify it to any platform (e.g. GitHub) as you like -Detailed specifications such as templates and notification destinations can be customized from the configration files (described later). +Detailed specifications such as templates and notification destinations can be customized from the configuration files (described later). ## Usage diff --git a/terraform/template.go b/terraform/template.go index 490b946..0126acb 100644 --- a/terraform/template.go +++ b/terraform/template.go @@ -219,7 +219,7 @@ func generateOutput(kind, template string, data map[string]interface{}, useRawOu return b.String(), nil } -// Execute binds the execution result of terraform command into tepmlate +// Execute binds the execution result of terraform command into template func (t *DefaultTemplate) Execute() (string, error) { data := map[string]interface{}{ "Title": t.Title, @@ -237,7 +237,7 @@ func (t *DefaultTemplate) Execute() (string, error) { return resp, nil } -// Execute binds the execution result of terraform fmt into tepmlate +// Execute binds the execution result of terraform fmt into template func (t *FmtTemplate) Execute() (string, error) { data := map[string]interface{}{ "Title": t.Title, @@ -255,7 +255,7 @@ func (t *FmtTemplate) Execute() (string, error) { return resp, nil } -// Execute binds the execution result of terraform plan into tepmlate +// Execute binds the execution result of terraform plan into template func (t *PlanTemplate) Execute() (string, error) { data := map[string]interface{}{ "Title": t.Title, @@ -291,7 +291,7 @@ func (t *DestroyWarningTemplate) Execute() (string, error) { return resp, nil } -// Execute binds the execution result of terraform apply into tepmlate +// Execute binds the execution result of terraform apply into template func (t *ApplyTemplate) Execute() (string, error) { data := map[string]interface{}{ "Title": t.Title, From b99f49107755d6eb4a76ff217252928ed5e6bd0b Mon Sep 17 00:00:00 2001 From: Yuya Takeyama Date: Sun, 26 Jul 2020 16:02:32 +0900 Subject: [PATCH 02/21] Support Google Cloud Build --- ci.go | 16 ++++++++ ci_test.go | 86 +++++++++++++++++++++++++++++++++++++++++++ config/config.go | 2 + config/config_test.go | 8 ++++ main.go | 5 +++ 5 files changed, 117 insertions(+) diff --git a/ci.go b/ci.go index e6159c6..f77cc03 100644 --- a/ci.go +++ b/ci.go @@ -146,3 +146,19 @@ func githubActions() (ci CI, err error) { ci.PR.Revision = os.Getenv("GITHUB_SHA") return ci, err } + +func cloudbuild() (ci CI, err error) { + ci.PR.Number = 0 + ci.PR.Revision = os.Getenv("COMMIT_SHA") + ci.URL = fmt.Sprintf( + "https://console.cloud.google.com/cloud-build/builds/%s?project=%s", + os.Getenv("BUILD_ID"), + os.Getenv("PROJECT_ID"), + ) + pr := os.Getenv("_PR_NUMBER") + if pr == "" { + return ci, nil + } + ci.PR.Number, err = strconv.Atoi(pr) + return ci, err +} diff --git a/ci_test.go b/ci_test.go index 7bd29bb..7d0eac9 100644 --- a/ci_test.go +++ b/ci_test.go @@ -759,3 +759,89 @@ func TestGitHubActions(t *testing.T) { } } } + +func TestCloudBuild(t *testing.T) { + envs := []string{ + "COMMIT_SHA", + "BUILD_ID", + "PROJECT_ID", + "_PR_NUMBER", + } + saveEnvs := make(map[string]string) + for _, key := range envs { + saveEnvs[key] = os.Getenv(key) + os.Unsetenv(key) + } + defer func() { + for key, value := range saveEnvs { + os.Setenv(key, value) + } + }() + + // https://cloud.google.com/cloud-build/docs/configuring-builds/substitute-variable-values + testCases := []struct { + fn func() + ci CI + ok bool + }{ + { + fn: func() { + os.Setenv("COMMIT_SHA", "abcdefg") + os.Setenv("BUILD_ID", "build-id") + os.Setenv("PROJECT_ID", "gcp-project-id") + os.Setenv("_PR_NUMBER", "123") + }, + ci: CI{ + PR: PullRequest{ + Revision: "abcdefg", + Number: 123, + }, + URL: "https://console.cloud.google.com/cloud-build/builds/build-id?project=gcp-project-id", + }, + ok: true, + }, + { + fn: func() { + os.Setenv("COMMIT_SHA", "") + os.Setenv("BUILD_ID", "build-id") + os.Setenv("PROJECT_ID", "gcp-project-id") + os.Setenv("_PR_NUMBER", "") + }, + ci: CI{ + PR: PullRequest{ + Revision: "", + Number: 0, + }, + URL: "https://console.cloud.google.com/cloud-build/builds/build-id?project=gcp-project-id", + }, + ok: true, + }, + { + fn: func() { + os.Setenv("COMMIT_SHA", "") + os.Setenv("BUILD_ID", "build-id") + os.Setenv("PROJECT_ID", "gcp-project-id") + os.Setenv("_PR_NUMBER", "abc") + }, + ci: CI{ + PR: PullRequest{ + Revision: "", + Number: 0, + }, + URL: "https://console.cloud.google.com/cloud-build/builds/build-id?project=gcp-project-id", + }, + ok: false, + }, + } + + for _, testCase := range testCases { + testCase.fn() + ci, err := cloudbuild() + if !reflect.DeepEqual(ci, testCase.ci) { + t.Errorf("got %q but want %q", ci, testCase.ci) + } + if (err == nil) != testCase.ok { + t.Errorf("got error %q", err) + } + } +} diff --git a/config/config.go b/config/config.go index d4411d9..b80f79c 100644 --- a/config/config.go +++ b/config/config.go @@ -146,6 +146,8 @@ func (cfg *Config) Validation() error { // ok pattern case "github-actions": // ok pattern + case "cloud-build", "cloudbuild": + // ok pattern default: return fmt.Errorf("%s: not supported yet", cfg.CI) } diff --git a/config/config_test.go b/config/config_test.go index e296373..9a48269 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -220,6 +220,14 @@ func TestValidation(t *testing.T) { contents: []byte("ci: gitlabci\n"), expected: "notifier is missing", }, + { + contents: []byte("ci: cloudbuild\n"), + expected: "notifier is missing", + }, + { + contents: []byte("ci: cloud-build\n"), + expected: "notifier is missing", + }, { contents: []byte("ci: circleci\nnotifier:\n github:\n"), expected: "notifier is missing", diff --git a/main.go b/main.go index dd0436f..505c9d7 100644 --- a/main.go +++ b/main.go @@ -82,6 +82,11 @@ func (t *tfnotify) Run() error { if err != nil { return err } + case "cloud-build", "cloudbuild": + ci, err = cloudbuild() + if err != nil { + return err + } case "": return fmt.Errorf("CI service: required (e.g. circleci)") default: From 82be48b8569ff6585c903c8751806e9a68e045a0 Mon Sep 17 00:00:00 2001 From: Yuya Takeyama Date: Sun, 26 Jul 2020 16:07:43 +0900 Subject: [PATCH 03/21] Update README.md to describe Google Cloud Build usage --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index cca9b85..8fc58eb 100644 --- a/README.md +++ b/README.md @@ -380,6 +380,7 @@ Currently, supported CI are here: - Jenkins - GitLab CI - GitHub Actions +- Google Cloud Build ### Private Repository Considerations GitHub private repositories require the `repo` and `write:discussion` permissions. @@ -390,6 +391,17 @@ GitHub private repositories require the `repo` and `write:discussion` permission - Environment Variable - `PULL_REQUEST_NUMBER` or `PULL_REQUEST_URL` are required to set by user for Pull Request Usage +### Google Cloud Build Considerations + +- These environment variables are needed to be set using [substitutions](https://cloud.google.com/cloud-build/docs/configuring-builds/substitute-variable-values) + - `COMMIT_SHA` + - `BUILD_ID` + - `PROJECT_ID` + - `_PR_NUMBER` +- Recommended trigger events + - `terraform plan`: Pull request + - `terraform apply`: Push to branch + ## Committers * Masaki ISHIYAMA ([@b4b4r07](https://github.com/b4b4r07)) From e1fd0361faa0118b15a1e526e7200cdac1006020 Mon Sep 17 00:00:00 2001 From: Daisuke Fujita Date: Mon, 27 Jul 2020 20:08:45 +0900 Subject: [PATCH 04/21] Use Go 1.14 for release build --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 790f470..8a6fb1e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,7 +12,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v1 with: - go-version: 1.13 + go-version: '1.14.x' - name: Run GoReleaser uses: goreleaser/goreleaser-action@v1 with: From 5661b8536f574be0e569c81c6067771eebd00aeb Mon Sep 17 00:00:00 2001 From: tanakaworld Date: Mon, 17 May 2021 18:55:32 +0900 Subject: [PATCH 05/21] Remove Codecov usage --- README.md | 4 +--- config/config_test.go | 6 ------ 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/README.md b/README.md index 8fc58eb..0957dfd 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,12 @@ tfnotify ======== -[![][release-svg]][release] [![][test-svg]][test] [![][codecov-svg]][codecov] [![][goreportcard-svg]][goreportcard] +[![][release-svg]][release] [![][test-svg]][test] [![][goreportcard-svg]][goreportcard] [release]: https://github.com/mercari/tfnotify/actions?query=workflow%3Arelease [release-svg]: https://github.com/mercari/tfnotify/workflows/release/badge.svg [test]: https://github.com/mercari/tfnotify/actions?query=workflow%3Atest [test-svg]: https://github.com/mercari/tfnotify/workflows/test/badge.svg -[codecov]: https://codecov.io/gh/mercari/tfnotify -[codecov-svg]: https://codecov.io/gh/mercari/tfnotify/branch/master/graph/badge.svg [goreportcard]: https://goreportcard.com/report/github.com/mercari/tfnotify [goreportcard-svg]: https://goreportcard.com/badge/github.com/mercari/tfnotify diff --git a/config/config_test.go b/config/config_test.go index 9a48269..74a79e4 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -468,12 +468,6 @@ func TestFind(t *testing.T) { expect: "tfnotify.yml", ok: true, }, - { - // invalid config - file: "codecov.yml", - expect: "", - ok: false, - }, { // in case of no args passed file: "", From 3b4b2c746e84e7787a9fdc74edf2287aa8e44b53 Mon Sep 17 00:00:00 2001 From: tanakaworld Date: Mon, 17 May 2021 19:05:00 +0900 Subject: [PATCH 06/21] Remove Codecov config --- .codecov.yml | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 .codecov.yml diff --git a/.codecov.yml b/.codecov.yml deleted file mode 100644 index 0d496e0..0000000 --- a/.codecov.yml +++ /dev/null @@ -1,10 +0,0 @@ -coverage: - precision: 2 - round: down - range: 70...90 - -ignore: - - "main.go" - - "notifier/github/github.go" - - "notifier/slack/slack.go" - - "notifier/typetalk/typetalk.go" From 5319d1a89e4a16c6ec241a9bd1ba114cf1da0b5f Mon Sep 17 00:00:00 2001 From: d-lau Date: Tue, 31 Aug 2021 11:18:04 +0900 Subject: [PATCH 07/21] add drlau and micnncim to codeowners --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index ee7d337..138fbf2 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1,2 +1,2 @@ # https://help.github.com/articles/about-codeowners/ -* @b4b4r07 @dtan4 +* @b4b4r07 @dtan4 @drlau @micnncim From 0abddd9889a5382cd71d6c4817238d306e1a55d7 Mon Sep 17 00:00:00 2001 From: kondo takeshi Date: Thu, 4 Nov 2021 22:15:49 +0900 Subject: [PATCH 08/21] Add CI url as Link template value for slack notification --- notifier/slack/notify.go | 1 + 1 file changed, 1 insertion(+) diff --git a/notifier/slack/notify.go b/notifier/slack/notify.go index fe44b3a..6ca44ab 100644 --- a/notifier/slack/notify.go +++ b/notifier/slack/notify.go @@ -43,6 +43,7 @@ func (s *NotifyService) Notify(body string) (exit int, err error) { Message: cfg.Message, Result: result.Result, Body: body, + Link: cfg.CI, }) text, err := template.Execute() if err != nil { From 74a9b51f0bb512e22e0c5be47e2db806e8a4e368 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Fri, 14 Jan 2022 22:34:32 +0900 Subject: [PATCH 09/21] Support regional cloud build Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- ci.go | 9 ++++++++- ci_test.go | 9 ++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/ci.go b/ci.go index f77cc03..ecfb455 100644 --- a/ci.go +++ b/ci.go @@ -150,8 +150,15 @@ func githubActions() (ci CI, err error) { func cloudbuild() (ci CI, err error) { ci.PR.Number = 0 ci.PR.Revision = os.Getenv("COMMIT_SHA") + + region := os.Getenv("REGION") + if region == "" { + region = "global" + } + 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"), ) diff --git a/ci_test.go b/ci_test.go index 7d0eac9..9313e11 100644 --- a/ci_test.go +++ b/ci_test.go @@ -766,6 +766,7 @@ func TestCloudBuild(t *testing.T) { "BUILD_ID", "PROJECT_ID", "_PR_NUMBER", + "REGION", } saveEnvs := make(map[string]string) for _, key := range envs { @@ -790,13 +791,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, }, @@ -806,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", "") + 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, }, @@ -828,7 +831,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, }, From 2c1c2673e75d93eb63bbc932772b95db9b905c5f Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Fri, 14 Jan 2022 22:42:45 +0900 Subject: [PATCH 10/21] Use const for default cloud bulid region Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- ci.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ci.go b/ci.go index ecfb455..e7a6f9e 100644 --- a/ci.go +++ b/ci.go @@ -8,6 +8,10 @@ import ( "strings" ) +const ( + defaultCloudBuildRegion = "global" +) + // CI represents a common information obtained from all CI platforms type CI struct { PR PullRequest @@ -153,7 +157,7 @@ func cloudbuild() (ci CI, err error) { region := os.Getenv("REGION") if region == "" { - region = "global" + region = defaultCloudBuildRegion } ci.URL = fmt.Sprintf( From eafff0766891b481567ddac2f3092a0c3b82ee2e Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Mon, 17 Jan 2022 14:43:42 +0900 Subject: [PATCH 11/21] Fix region user defined substitution format Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- ci.go | 2 +- ci_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ci.go b/ci.go index e7a6f9e..2719296 100644 --- a/ci.go +++ b/ci.go @@ -155,7 +155,7 @@ func cloudbuild() (ci CI, err error) { ci.PR.Number = 0 ci.PR.Revision = os.Getenv("COMMIT_SHA") - region := os.Getenv("REGION") + region := os.Getenv("_REGION") if region == "" { region = defaultCloudBuildRegion } diff --git a/ci_test.go b/ci_test.go index 9313e11..1bf7e34 100644 --- a/ci_test.go +++ b/ci_test.go @@ -791,7 +791,7 @@ 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") + os.Setenv("_REGION", "asia-northeast1") }, ci: CI{ PR: PullRequest{ @@ -808,7 +808,7 @@ 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", "") + os.Setenv("_REGION", "") }, ci: CI{ PR: PullRequest{ From f3cbdc26224e5cc3e3efae66881729dceddba8c9 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Thu, 20 Jan 2022 23:11:39 +0900 Subject: [PATCH 12/21] Add testcase for 0.15 plan and apply error Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- terraform/parser_test.go | 84 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 4 deletions(-) diff --git a/terraform/parser_test.go b/terraform/parser_test.go index 6f13626..e4f1c56 100644 --- a/terraform/parser_test.go +++ b/terraform/parser_test.go @@ -86,7 +86,7 @@ can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run. ` -const planFailureResult = ` +const planFailureResult0_12 = ` xxxxxxxxx xxxxxxxxx xxxxxxxxx @@ -99,6 +99,19 @@ Error: Error refreshing state: 4 error(s) occurred: * google_sql_user.proxyuser_main: 1 error(s) occurred: ` +const planFailureResult0_15 = ` +xxxxxxxxx +xxxxxxxxx +xxxxxxxxx + +| Error: Error refreshing state: 4 error(s) occurred: +| +| * google_sql_database.main: 1 error(s) occurred: +| +| * google_sql_database.main: google_sql_database.main: Error reading SQL Database "main" in instance "main-master-instance": googleapi: Error 409: The instance or operation is not in an appropriate state to handle the request., invalidState +| * google_sql_user.proxyuser_main: 1 error(s) occurred: +` + const planNoChanges = ` google_bigquery_dataset.tfnotify_echo: Refreshing state... google_project.team: Refreshing state... @@ -300,7 +313,7 @@ google_dns_record_set.dev_tfnotifyapps_com: Refreshing state... Apply complete! Resources: 0 added, 0 changed, 0 destroyed. ` -const applyFailureResult = ` +const applyFailureResult0_12 = ` data.terraform_remote_state.teams_platform_development: Refreshing state... google_project.tfnotify_jp_tfnotify_prod: Refreshing state... google_project_services.tfnotify_jp_tfnotify_prod: Refreshing state... @@ -331,6 +344,37 @@ Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for reques ` +const applyFailureResult0_15 = ` +data.terraform_remote_state.teams_platform_development: Refreshing state... +google_project.tfnotify_jp_tfnotify_prod: Refreshing state... +google_project_services.tfnotify_jp_tfnotify_prod: Refreshing state... +google_bigquery_dataset.gateway_access_log: Refreshing state... +google_compute_global_address.reviews_web_tfnotify_in: Refreshing state... +google_compute_global_address.chartmuseum_tfnotifyapps_com: Refreshing state... +google_storage_bucket.chartmuseum: Refreshing state... +google_storage_bucket.ark_tfnotify_prod: Refreshing state... +google_compute_global_address.reviews_api_tfnotify_in: Refreshing state... +google_logging_project_sink.gateway_access_log_bigquery_sink: Refreshing state... +google_project_iam_member.gateway_access_log_bigquery_sink_writer_is_bigquery_data_editor: Refreshing state... +aws_s3_bucket.terraform_backend: Refreshing state... +aws_s3_bucket.teams_terraform_private_modules: Refreshing state... +aws_iam_policy.datadog_aws_integration: Refreshing state... +aws_iam_role.datadog_aws_integration: Refreshing state... +aws_iam_user.teams_terraform: Refreshing state... +aws_iam_user_policy.teams_terraform: Refreshing state... +aws_iam_role_policy_attachment.datadog_aws_integration: Refreshing state... +google_dns_managed_zone.tfnotifyapps_com: Refreshing state... +google_dns_record_set.dev_tfnotifyapps_com: Refreshing state... + + +| Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for request "Enable Project Services tfnotify-jp-tfnotify-prod: map[logging.googleapis.com:{}]" returned error: failed to send enable services request: googleapi: Error 403: The caller does not have permission, forbidden +| +| on .terraform/modules/tfnotify-jp-tfnotify-prod/google_project_service.tf line 6, in resource "google_project_service" "gcp_api_service": +| 6: resource "google_project_service" "gcp_api_service" { +| +| +` + func TestDefaultParserParse(t *testing.T) { testCases := []struct { body string @@ -429,7 +473,7 @@ func TestPlanParserParse(t *testing.T) { }, { name: "plan ng pattern", - body: planFailureResult, + body: planFailureResult0_12, result: ParseResult{ Result: `Error: Error refreshing state: 4 error(s) occurred: @@ -445,6 +489,24 @@ func TestPlanParserParse(t *testing.T) { Error: nil, }, }, + { + name: "plan ng pattern", + body: planFailureResult0_15, + result: ParseResult{ + Result: `| Error: Error refreshing state: 4 error(s) occurred: +| +| * google_sql_database.main: 1 error(s) occurred: +| +| * google_sql_database.main: google_sql_database.main: Error reading SQL Database "main" in instance "main-master-instance": googleapi: Error 409: The instance or operation is not in an appropriate state to handle the request., invalidState +| * google_sql_user.proxyuser_main: 1 error(s) occurred:`, + HasAddOrUpdateOnly: false, + HasDestroy: false, + HasNoChanges: false, + HasPlanError: true, + ExitCode: 1, + Error: nil, + }, + }, { name: "plan no changes", body: planNoChanges, @@ -532,13 +594,27 @@ func TestApplyParserParse(t *testing.T) { }, { name: "apply ng pattern", - body: applyFailureResult, + body: applyFailureResult0_12, result: ParseResult{ Result: `Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for request "Enable Project Services tfnotify-jp-tfnotify-prod: map[logging.googleapis.com:{}]" returned error: failed to send enable services request: googleapi: Error 403: The caller does not have permission, forbidden on .terraform/modules/tfnotify-jp-tfnotify-prod/google_project_service.tf line 6, in resource "google_project_service" "gcp_api_service": 6: resource "google_project_service" "gcp_api_service" { +`, + ExitCode: 1, + Error: nil, + }, + }, + { + name: "apply ng pattern", + body: applyFailureResult0_15, + result: ParseResult{ + Result: `| Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for request "Enable Project Services tfnotify-jp-tfnotify-prod: map[logging.googleapis.com:{}]" returned error: failed to send enable services request: googleapi: Error 403: The caller does not have permission, forbidden +| +| on .terraform/modules/tfnotify-jp-tfnotify-prod/google_project_service.tf line 6, in resource "google_project_service" "gcp_api_service": +| 6: resource "google_project_service" "gcp_api_service" { +| `, ExitCode: 1, Error: nil, From b364d7a5c33a27dece162b50a56d0402e273d013 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Thu, 20 Jan 2022 23:14:06 +0900 Subject: [PATCH 13/21] Update commet for testname Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- terraform/parser_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/terraform/parser_test.go b/terraform/parser_test.go index e4f1c56..f42c13e 100644 --- a/terraform/parser_test.go +++ b/terraform/parser_test.go @@ -472,7 +472,7 @@ func TestPlanParserParse(t *testing.T) { }, }, { - name: "plan ng pattern", + name: "plan ng pattern 0.12", body: planFailureResult0_12, result: ParseResult{ Result: `Error: Error refreshing state: 4 error(s) occurred: @@ -490,7 +490,7 @@ func TestPlanParserParse(t *testing.T) { }, }, { - name: "plan ng pattern", + name: "plan ng pattern 0.15", body: planFailureResult0_15, result: ParseResult{ Result: `| Error: Error refreshing state: 4 error(s) occurred: @@ -593,7 +593,7 @@ func TestApplyParserParse(t *testing.T) { }, }, { - name: "apply ng pattern", + name: "apply ng pattern 0.12", body: applyFailureResult0_12, result: ParseResult{ Result: `Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for request "Enable Project Services tfnotify-jp-tfnotify-prod: map[logging.googleapis.com:{}]" returned error: failed to send enable services request: googleapi: Error 403: The caller does not have permission, forbidden @@ -607,7 +607,7 @@ func TestApplyParserParse(t *testing.T) { }, }, { - name: "apply ng pattern", + name: "apply ng pattern 0.15", body: applyFailureResult0_15, result: ParseResult{ Result: `| Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for request "Enable Project Services tfnotify-jp-tfnotify-prod: map[logging.googleapis.com:{}]" returned error: failed to send enable services request: googleapi: Error 403: The caller does not have permission, forbidden From 823ebf5810321a00c1ec68a48c36fd2352aeae7d Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Thu, 20 Jan 2022 23:25:18 +0900 Subject: [PATCH 14/21] Update regex for fail parser Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- terraform/parser.go | 4 ++-- terraform/parser_test.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/terraform/parser.go b/terraform/parser.go index 80687dc..d86c1c3 100644 --- a/terraform/parser.go +++ b/terraform/parser.go @@ -62,7 +62,7 @@ func NewFmtParser() *FmtParser { func NewPlanParser() *PlanParser { return &PlanParser{ Pass: regexp.MustCompile(`(?m)^(Plan: \d|No changes.)`), - Fail: regexp.MustCompile(`(?m)^(Error: )`), + Fail: regexp.MustCompile(`(?m)^(\|\s{1})?(Error: )`), // "0 to destroy" should be treated as "no destroy" HasDestroy: regexp.MustCompile(`(?m)([1-9][0-9]* to destroy.)`), HasNoChanges: regexp.MustCompile(`(?m)^(No changes. Infrastructure is up-to-date.)`), @@ -73,7 +73,7 @@ func NewPlanParser() *PlanParser { func NewApplyParser() *ApplyParser { return &ApplyParser{ Pass: regexp.MustCompile(`(?m)^(Apply complete!)`), - Fail: regexp.MustCompile(`(?m)^(Error: )`), + Fail: regexp.MustCompile(`(?m)^(\|\s{1})?(Error: )`), } } diff --git a/terraform/parser_test.go b/terraform/parser_test.go index f42c13e..9ece0b9 100644 --- a/terraform/parser_test.go +++ b/terraform/parser_test.go @@ -368,7 +368,7 @@ google_dns_record_set.dev_tfnotifyapps_com: Refreshing state... | Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for request "Enable Project Services tfnotify-jp-tfnotify-prod: map[logging.googleapis.com:{}]" returned error: failed to send enable services request: googleapi: Error 403: The caller does not have permission, forbidden -| +| | on .terraform/modules/tfnotify-jp-tfnotify-prod/google_project_service.tf line 6, in resource "google_project_service" "gcp_api_service": | 6: resource "google_project_service" "gcp_api_service" { | @@ -612,10 +612,10 @@ func TestApplyParserParse(t *testing.T) { result: ParseResult{ Result: `| Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for request "Enable Project Services tfnotify-jp-tfnotify-prod: map[logging.googleapis.com:{}]" returned error: failed to send enable services request: googleapi: Error 403: The caller does not have permission, forbidden | -| on .terraform/modules/tfnotify-jp-tfnotify-prod/google_project_service.tf line 6, in resource "google_project_service" "gcp_api_service": -| 6: resource "google_project_service" "gcp_api_service" { +| on .terraform/modules/tfnotify-jp-tfnotify-prod/google_project_service.tf line 6, in resource "google_project_service" "gcp_api_service": +| 6: resource "google_project_service" "gcp_api_service" { | -`, +|`, ExitCode: 1, Error: nil, }, From 8dc30e80429e33e1a29b261e39adabaae151ad0b Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Thu, 20 Jan 2022 23:38:24 +0900 Subject: [PATCH 15/21] Add _REGION substitute to readme cloud build section Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0957dfd..8d4234c 100644 --- a/README.md +++ b/README.md @@ -396,6 +396,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 From 14251595e0a3131b5cbd6859c021cdc7c210129f Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Fri, 21 Jan 2022 18:05:09 +0900 Subject: [PATCH 16/21] Fix error parse regex char for Terraform >= 0.15 Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- terraform/parser.go | 4 ++-- terraform/parser_test.go | 51 ++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/terraform/parser.go b/terraform/parser.go index d86c1c3..9fd5f52 100644 --- a/terraform/parser.go +++ b/terraform/parser.go @@ -62,7 +62,7 @@ func NewFmtParser() *FmtParser { func NewPlanParser() *PlanParser { return &PlanParser{ Pass: regexp.MustCompile(`(?m)^(Plan: \d|No changes.)`), - Fail: regexp.MustCompile(`(?m)^(\|\s{1})?(Error: )`), + Fail: regexp.MustCompile(`(?m)^(│\s{1})?(Error: )`), // "0 to destroy" should be treated as "no destroy" HasDestroy: regexp.MustCompile(`(?m)([1-9][0-9]* to destroy.)`), HasNoChanges: regexp.MustCompile(`(?m)^(No changes. Infrastructure is up-to-date.)`), @@ -73,7 +73,7 @@ func NewPlanParser() *PlanParser { func NewApplyParser() *ApplyParser { return &ApplyParser{ Pass: regexp.MustCompile(`(?m)^(Apply complete!)`), - Fail: regexp.MustCompile(`(?m)^(\|\s{1})?(Error: )`), + Fail: regexp.MustCompile(`(?m)^(│\s{1})?(Error: )`), } } diff --git a/terraform/parser_test.go b/terraform/parser_test.go index 9ece0b9..605300e 100644 --- a/terraform/parser_test.go +++ b/terraform/parser_test.go @@ -104,12 +104,14 @@ xxxxxxxxx xxxxxxxxx xxxxxxxxx -| Error: Error refreshing state: 4 error(s) occurred: -| -| * google_sql_database.main: 1 error(s) occurred: -| -| * google_sql_database.main: google_sql_database.main: Error reading SQL Database "main" in instance "main-master-instance": googleapi: Error 409: The instance or operation is not in an appropriate state to handle the request., invalidState -| * google_sql_user.proxyuser_main: 1 error(s) occurred: +╷ +│ Error: Error refreshing state: 4 error(s) occurred: +│ +│ * google_sql_database.main: 1 error(s) occurred: +│ +│ * google_sql_database.main: google_sql_database.main: Error reading SQL Database "main" in instance "main-master-instance": googleapi: Error 409: The instance or operation is not in an appropriate state to handle the request., invalidState +│ * google_sql_user.proxyuser_main: 1 error(s) occurred: +╵ ` const planNoChanges = ` @@ -366,13 +368,12 @@ aws_iam_role_policy_attachment.datadog_aws_integration: Refreshing state... google_dns_managed_zone.tfnotifyapps_com: Refreshing state... google_dns_record_set.dev_tfnotifyapps_com: Refreshing state... - -| Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for request "Enable Project Services tfnotify-jp-tfnotify-prod: map[logging.googleapis.com:{}]" returned error: failed to send enable services request: googleapi: Error 403: The caller does not have permission, forbidden -| -| on .terraform/modules/tfnotify-jp-tfnotify-prod/google_project_service.tf line 6, in resource "google_project_service" "gcp_api_service": -| 6: resource "google_project_service" "gcp_api_service" { -| -| +╷ +│ Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for request "Enable Project Services tfnotify-jp-tfnotify-prod: map[logging.googleapis.com:{}]" returned error: failed to send enable services request: googleapi: Error 403: The caller does not have permission, forbidden +│ +│ on .terraform/modules/tfnotify-jp-tfnotify-prod/google_project_service.tf line 6, in resource "google_project_service" "gcp_api_service": +│ 6: resource "google_project_service" "gcp_api_service" { +╵ ` func TestDefaultParserParse(t *testing.T) { @@ -493,12 +494,13 @@ func TestPlanParserParse(t *testing.T) { name: "plan ng pattern 0.15", body: planFailureResult0_15, result: ParseResult{ - Result: `| Error: Error refreshing state: 4 error(s) occurred: -| -| * google_sql_database.main: 1 error(s) occurred: -| -| * google_sql_database.main: google_sql_database.main: Error reading SQL Database "main" in instance "main-master-instance": googleapi: Error 409: The instance or operation is not in an appropriate state to handle the request., invalidState -| * google_sql_user.proxyuser_main: 1 error(s) occurred:`, + Result: `│ Error: Error refreshing state: 4 error(s) occurred: +│ +│ * google_sql_database.main: 1 error(s) occurred: +│ +│ * google_sql_database.main: google_sql_database.main: Error reading SQL Database "main" in instance "main-master-instance": googleapi: Error 409: The instance or operation is not in an appropriate state to handle the request., invalidState +│ * google_sql_user.proxyuser_main: 1 error(s) occurred: +╵`, HasAddOrUpdateOnly: false, HasDestroy: false, HasNoChanges: false, @@ -610,12 +612,11 @@ func TestApplyParserParse(t *testing.T) { name: "apply ng pattern 0.15", body: applyFailureResult0_15, result: ParseResult{ - Result: `| Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for request "Enable Project Services tfnotify-jp-tfnotify-prod: map[logging.googleapis.com:{}]" returned error: failed to send enable services request: googleapi: Error 403: The caller does not have permission, forbidden -| -| on .terraform/modules/tfnotify-jp-tfnotify-prod/google_project_service.tf line 6, in resource "google_project_service" "gcp_api_service": -| 6: resource "google_project_service" "gcp_api_service" { -| -|`, + Result: `│ Error: Batch "project/tfnotify-jp-tfnotify-prod/services:batchEnable" for request "Enable Project Services tfnotify-jp-tfnotify-prod: map[logging.googleapis.com:{}]" returned error: failed to send enable services request: googleapi: Error 403: The caller does not have permission, forbidden +│ +│ on .terraform/modules/tfnotify-jp-tfnotify-prod/google_project_service.tf line 6, in resource "google_project_service" "gcp_api_service": +│ 6: resource "google_project_service" "gcp_api_service" { +╵`, ExitCode: 1, Error: nil, }, From 879269010bb02ddf8075cce0a925de5379487e38 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Tue, 1 Feb 2022 16:53:11 +0900 Subject: [PATCH 17/21] Support outputs changes only plan Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- terraform/parser.go | 2 +- terraform/parser_test.go | 100 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/terraform/parser.go b/terraform/parser.go index 9fd5f52..938afc4 100644 --- a/terraform/parser.go +++ b/terraform/parser.go @@ -61,7 +61,7 @@ func NewFmtParser() *FmtParser { // NewPlanParser is PlanParser initialized with its Regexp func NewPlanParser() *PlanParser { return &PlanParser{ - Pass: regexp.MustCompile(`(?m)^(Plan: \d|No changes.)`), + Pass: regexp.MustCompile(`(?m)^((Plan: \d|No changes.)|(Changes to Outputs:))`), Fail: regexp.MustCompile(`(?m)^(│\s{1})?(Error: )`), // "0 to destroy" should be treated as "no destroy" HasDestroy: regexp.MustCompile(`(?m)([1-9][0-9]* to destroy.)`), diff --git a/terraform/parser_test.go b/terraform/parser_test.go index 605300e..b5d5e7c 100644 --- a/terraform/parser_test.go +++ b/terraform/parser_test.go @@ -86,6 +86,80 @@ can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run. ` +const planOutputChangesOnlySuccessResult0_12 = ` +Refreshing Terraform state in-memory prior to plan... +The refreshed state will be used to calculate this plan, but will not be +persisted to local or remote state storage. + +data.terraform_remote_state.teams_platform_development: Refreshing state... +google_project.my_project: Refreshing state... +aws_iam_policy.datadog_aws_integration: Refreshing state... +aws_iam_user.teams_terraform: Refreshing state... +aws_iam_role.datadog_aws_integration: Refreshing state... +google_project_services.my_project: Refreshing state... +google_bigquery_dataset.gateway_access_log: Refreshing state... +aws_iam_role_policy_attachment.datadog_aws_integration: Refreshing state... +google_logging_project_sink.gateway_access_log_bigquery_sink: Refreshing state... +google_project_iam_member.gateway_access_log_bigquery_sink_writer_is_bigquery_data_editor: Refreshing state... +google_dns_managed_zone.tfnotifyapps_com: Refreshing state... +google_dns_record_set.dev_tfnotifyapps_com: Refreshing state... + +------------------------------------------------------------------------ + +An execution plan has been generated and is shown below. +Resource actions are indicated with the following symbols: + + create + +Terraform will perform the following actions: + +Plan: 0 to add, 0 to change, 0 to destroy. + +Changes to Outputs: + + aws_instance_name = "my-instance" + +------------------------------------------------------------------------ + +Note: You didn't specify an "-out" parameter to save this plan, so Terraform +can't guarantee that exactly these actions will be performed if +"terraform apply" is subsequently run. +` + +const planOutputChangesOnlySuccessResult0_15 = ` +Refreshing Terraform state in-memory prior to plan... +The refreshed state will be used to calculate this plan, but will not be +persisted to local or remote state storage. + +data.terraform_remote_state.teams_platform_development: Refreshing state... +google_project.my_project: Refreshing state... +aws_iam_policy.datadog_aws_integration: Refreshing state... +aws_iam_user.teams_terraform: Refreshing state... +aws_iam_role.datadog_aws_integration: Refreshing state... +google_project_services.my_project: Refreshing state... +google_bigquery_dataset.gateway_access_log: Refreshing state... +aws_iam_role_policy_attachment.datadog_aws_integration: Refreshing state... +google_logging_project_sink.gateway_access_log_bigquery_sink: Refreshing state... +google_project_iam_member.gateway_access_log_bigquery_sink_writer_is_bigquery_data_editor: Refreshing state... +google_dns_managed_zone.tfnotifyapps_com: Refreshing state... +google_dns_record_set.dev_tfnotifyapps_com: Refreshing state... + +------------------------------------------------------------------------ + +An execution plan has been generated and is shown below. +Resource actions are indicated with the following symbols: + + create + +Terraform will perform the following actions: + +Changes to Outputs: + + aws_instance_name = "my-instance" + +------------------------------------------------------------------------ + +Note: You didn't specify an "-out" parameter to save this plan, so Terraform +can't guarantee that exactly these actions will be performed if +"terraform apply" is subsequently run. +` + const planFailureResult0_12 = ` xxxxxxxxx xxxxxxxxx @@ -459,6 +533,32 @@ func TestPlanParserParse(t *testing.T) { Error: nil, }, }, + { + name: "plan output changes only pattern 0.12", + body: planOutputChangesOnlySuccessResult0_12, + result: ParseResult{ + Result: "Plan: 0 to add, 0 to change, 0 to destroy.", + HasAddOrUpdateOnly: true, + HasDestroy: false, + HasNoChanges: false, + HasPlanError: false, + ExitCode: 0, + Error: nil, + }, + }, + { + name: "plan output changes only pattern 0.15", + body: planOutputChangesOnlySuccessResult0_15, + result: ParseResult{ + Result: "Changes to Outputs:", + HasAddOrUpdateOnly: true, + HasDestroy: false, + HasNoChanges: false, + HasPlanError: false, + ExitCode: 0, + Error: nil, + }, + }, { name: "no stdin", body: "", From 163a31ef794a6d65ae8d2bc8211b56b46c40f30e Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Tue, 1 Feb 2022 16:53:49 +0900 Subject: [PATCH 18/21] Fix variable name Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- terraform/parser_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/terraform/parser_test.go b/terraform/parser_test.go index b5d5e7c..3ff0c79 100644 --- a/terraform/parser_test.go +++ b/terraform/parser_test.go @@ -86,7 +86,7 @@ can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run. ` -const planOutputChangesOnlySuccessResult0_12 = ` +const planOnlyOutputChangesSuccessResult0_12 = ` Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. @@ -124,7 +124,7 @@ can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run. ` -const planOutputChangesOnlySuccessResult0_15 = ` +const planOnlyOutputChangesSuccessResult0_15 = ` Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. @@ -535,7 +535,7 @@ func TestPlanParserParse(t *testing.T) { }, { name: "plan output changes only pattern 0.12", - body: planOutputChangesOnlySuccessResult0_12, + body: planOnlyOutputChangesSuccessResult0_12, result: ParseResult{ Result: "Plan: 0 to add, 0 to change, 0 to destroy.", HasAddOrUpdateOnly: true, @@ -548,7 +548,7 @@ func TestPlanParserParse(t *testing.T) { }, { name: "plan output changes only pattern 0.15", - body: planOutputChangesOnlySuccessResult0_15, + body: planOnlyOutputChangesSuccessResult0_15, result: ParseResult{ Result: "Changes to Outputs:", HasAddOrUpdateOnly: true, From 83e32057869cc58bac7b66debb6c0e3f90227ec6 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Tue, 1 Feb 2022 17:49:03 +0900 Subject: [PATCH 19/21] Add Merpay SRes to maintainer Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 138fbf2..46b27c7 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1,2 +1,2 @@ # https://help.github.com/articles/about-codeowners/ -* @b4b4r07 @dtan4 @drlau @micnncim +* @b4b4r07 @dtan4 @drlau @micnncim @KeisukeYamashita @tyuhara From 13d4a8b5d7c530a29c5ac687b32b5be22b2b05c7 Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Fri, 18 Feb 2022 21:51:16 +0900 Subject: [PATCH 20/21] Support custom env var GitHub Token name Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- notifier/github/client.go | 7 ++++--- notifier/github/client_test.go | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/notifier/github/client.go b/notifier/github/client.go index 1c62bca..ef1f288 100644 --- a/notifier/github/client.go +++ b/notifier/github/client.go @@ -69,10 +69,11 @@ type service struct { // NewClient returns Client initialized with Config func NewClient(cfg Config) (*Client, error) { token := cfg.Token - token = strings.TrimPrefix(token, "$") - if token == EnvToken { - token = os.Getenv(EnvToken) + + if strings.HasPrefix(token, "$") { + token = os.Getenv(strings.TrimPrefix(token, "$")) } + if token == "" { return &Client{}, errors.New("github token is missing") } diff --git a/notifier/github/client_test.go b/notifier/github/client_test.go index e84a117..d59c48c 100644 --- a/notifier/github/client_test.go +++ b/notifier/github/client_test.go @@ -2,6 +2,7 @@ package github import ( "os" + "strings" "testing" ) @@ -41,12 +42,24 @@ func TestNewClient(t *testing.T) { envToken: "", expect: "github token is missing", }, + { + // specify via env but not to be set env (part 3) + config: Config{Token: "$TFNOTIFY_GITHUB_TOKEN"}, + envToken: "", + expect: "github token is missing", + }, { // specify via env (part 2) config: Config{Token: "$GITHUB_TOKEN"}, envToken: "abcdefg", expect: "", }, + { + // specify via env (part 3) + config: Config{Token: "$TFNOTIFY_GITHUB_TOKEN"}, + envToken: "abcdefg", + expect: "", + }, { // no specification (part 1) config: Config{}, @@ -61,7 +74,13 @@ func TestNewClient(t *testing.T) { }, } for _, testCase := range testCases { - os.Setenv(EnvToken, testCase.envToken) + if strings.HasPrefix(testCase.config.Token, "$") { + key := strings.TrimPrefix(testCase.config.Token, "$") + os.Setenv(key, testCase.envToken) + } else { + os.Setenv(EnvToken, testCase.envToken) + } + _, err := NewClient(testCase.config) if err == nil { continue From e5e8b67178b2286088e63551c7bfa35b9d30006c Mon Sep 17 00:00:00 2001 From: KeisukeYamashita <19yamashita15@gmail.com> Date: Sun, 20 Feb 2022 14:48:51 +0900 Subject: [PATCH 21/21] Remove envToken Signed-off-by: KeisukeYamashita <19yamashita15@gmail.com> --- notifier/github/client.go | 3 --- notifier/github/client_test.go | 8 -------- 2 files changed, 11 deletions(-) diff --git a/notifier/github/client.go b/notifier/github/client.go index ef1f288..094e23d 100644 --- a/notifier/github/client.go +++ b/notifier/github/client.go @@ -10,9 +10,6 @@ import ( "golang.org/x/oauth2" ) -// 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" diff --git a/notifier/github/client_test.go b/notifier/github/client_test.go index d59c48c..79036d3 100644 --- a/notifier/github/client_test.go +++ b/notifier/github/client_test.go @@ -7,12 +7,6 @@ import ( ) func TestNewClient(t *testing.T) { - githubToken := os.Getenv(EnvToken) - defer func() { - os.Setenv(EnvToken, githubToken) - }() - os.Setenv(EnvToken, "") - testCases := []struct { config Config envToken string @@ -77,8 +71,6 @@ func TestNewClient(t *testing.T) { if strings.HasPrefix(testCase.config.Token, "$") { key := strings.TrimPrefix(testCase.config.Token, "$") os.Setenv(key, testCase.envToken) - } else { - os.Setenv(EnvToken, testCase.envToken) } _, err := NewClient(testCase.config)