From e6c4a9c2a375eb387a1350ad616c0ad17b088547 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 16:12:08 -0700 Subject: [PATCH 001/127] add samples testing tooling --- .github/workflows/config/nodejs.json | 0 .github/workflows/nodejs-lint.yaml | 34 +++++ .github/workflows/on-pull-request.yaml | 123 ++++++++++++++++++ .github/workflows/samples-tools/.gitignore | 25 ++++ .../samples-tools/cmd/affected/main.go | 51 ++++++++ .github/workflows/samples-tools/go.mod | 3 + .../workflows/samples-tools/pkg/utils/args.go | 37 ++++++ .../samples-tools/pkg/utils/config.go | 88 +++++++++++++ .../workflows/samples-tools/pkg/utils/git.go | 15 +++ 9 files changed, 376 insertions(+) create mode 100644 .github/workflows/config/nodejs.json create mode 100644 .github/workflows/nodejs-lint.yaml create mode 100644 .github/workflows/on-pull-request.yaml create mode 100644 .github/workflows/samples-tools/.gitignore create mode 100644 .github/workflows/samples-tools/cmd/affected/main.go create mode 100644 .github/workflows/samples-tools/go.mod create mode 100644 .github/workflows/samples-tools/pkg/utils/args.go create mode 100644 .github/workflows/samples-tools/pkg/utils/config.go create mode 100644 .github/workflows/samples-tools/pkg/utils/git.go diff --git a/.github/workflows/config/nodejs.json b/.github/workflows/config/nodejs.json new file mode 100644 index 0000000000..e69de29bb2 diff --git a/.github/workflows/nodejs-lint.yaml b/.github/workflows/nodejs-lint.yaml new file mode 100644 index 0000000000..3b7a1cc649 --- /dev/null +++ b/.github/workflows/nodejs-lint.yaml @@ -0,0 +1,34 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +on: + workflow_call: + inputs: + package: + required: true + type: string + timeout-minutes: + default: 5 + type: number + +jobs: + nodejs-lint: + runs-on: ubuntu-latest + timeout-minutes: ${{ inputs.timeout-minutes }} + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 20 + - run: make lint dir=${{ inputs.package }} diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml new file mode 100644 index 0000000000..59b5787f96 --- /dev/null +++ b/.github/workflows/on-pull-request.yaml @@ -0,0 +1,123 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: 🪿 +on: + push: + branches: + - main + pull_request: + types: + - opened + - reopened + - synchronize + - labeled + +jobs: + affected: + name: Finding affected tests + runs-on: ubuntu-latest + outputs: + nodejs: ${{ steps.nodejs.outputs.packages }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: actions/setup-go@v5 + with: + go-version: ^1.22.0 + - run: go install ./cmd/affected + working-directory: .github/workflows/samples-tools + - id: nodejs + run: | + PACKAGES=$(affected .github/workflows/config/nodejs.json) + echo "packages=$PACKAGES" + echo "packages=$PACKAGES " >> "$GITHUB_OUTPUT" + + nodejs-lint: + name: Node.js lint + needs: affected + runs-on: ubuntu-latest + strategy: + matrix: + package: ${{ fromJson(needs.affected.outputs.nodejs) }} + exclude: + - package: '.' + steps: + - uses: nodejs-lint.yaml + + # test: + # if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' + # runs-on: ubuntu-latest + # timeout-minutes: 120 + # permissions: + # contents: read + # id-token: write + # steps: + # - uses: actions/checkout@v4.1.7 + # with: + # ref: ${{github.event.pull_request.head.sha}} + # - uses: 'google-github-actions/auth@v2.1.5' + # with: + # workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider + # service_account: 'kokoro-system-test@long-door-651.iam.gserviceaccount.com' + # create_credentials_file: 'true' + # access_token_lifetime: 600s + # - id: secrets + # uses: 'google-github-actions/get-secretmanager-secrets@95a0b09b8348ef3d02c68c6ba5662a037e78d713' # v2 + # with: + # secrets: |- + # caip_id:nodejs-docs-samples-tests/nodejs-docs-samples-ai-platform-caip-project-id + # location:nodejs-docs-samples-tests/nodejs-docs-samples-ai-platform-location + # - uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0 + # with: + # node-version: 16 + # - name: Get npm cache directory + # id: npm-cache-dir + # shell: bash + # run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} + # - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4 + # id: npm-cache + # with: + # path: ${{ steps.npm-cache-dir.outputs.dir }} + # key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} + # restore-keys: "${{ runner.os }}-node- \n" + # - name: set env vars for scheduled run + # if: github.event.action == 'schedule' + # run: | + # echo "MOCHA_REPORTER_SUITENAME=ai-platform-snippets" >> $GITHUB_ENV + # echo "MOCHA_REPORTER_OUTPUT=${{github.run_id}}_sponge_log.xml" >> $GITHUB_ENV + # echo "MOCHA_REPORTER=xunit" >> $GITHUB_ENV + # - name: Run Tests + # run: make test dir=ai-platform/snippets + # env: + # GOOGLE_SAMPLES_PROJECT: 'long-door-651' + # LOCATION: ${{ steps.secrets.outputs.location }} + # CAIP_PROJECT_ID: ${{ steps.secrets.outputs.caip_id }} + # - name: upload test results for FlakyBot workflow + # if: github.event.action == 'schedule' && always() + # uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4 + # env: + # MOCHA_REPORTER_OUTPUT: '${{github.run_id}}_sponge_log.xml' + # with: + # name: test-results + # path: ai-platform/snippets/${{ env.MOCHA_REPORTER_OUTPUT }} + # retention-days: 1 + # flakybot: + # permissions: + # contents: 'read' + # id-token: 'write' + # if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail + # uses: ./.github/workflows/flakybot.yaml + # needs: [test] diff --git a/.github/workflows/samples-tools/.gitignore b/.github/workflows/samples-tools/.gitignore new file mode 100644 index 0000000000..6f72f89261 --- /dev/null +++ b/.github/workflows/samples-tools/.gitignore @@ -0,0 +1,25 @@ +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work +go.work.sum + +# env file +.env diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go new file mode 100644 index 0000000000..5b75311d64 --- /dev/null +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "encoding/json" + "fmt" + + "samples-tools/pkg/utils" +) + +func main() { + usage := "usage: affected path/to/config.json [head-commit] [main-commit]" + configPath := utils.ArgRequired(1, usage) + headCommit := utils.ArgWithDefault(2, "HEAD") + mainCommit := utils.ArgWithDefault(3, "origin/main") + + config, err := utils.LoadConfig(configPath) + if err != nil { + panic(err) + } + + diffs, err := utils.Diffs(headCommit, mainCommit) + if err != nil { + panic(err) + } + + matrix := affected(config, diffs) + matrixJson, err := json.Marshal(matrix) + if err != nil { + panic(err) + } + + fmt.Println(string(matrixJson)) +} + +func affected(config utils.Config, diffs []string) []string { + // TODO(dcavazos): Detect affected changes more granularly with the diffs. + uniquePackages := make(map[string]bool) + for _, diff := range diffs { + if !config.Matches(diff) { + continue + } + pkg := config.FindPackage(diff) + uniquePackages[pkg] = true + } + + packages := make([]string, 0, len(uniquePackages)) + for pkg := range uniquePackages { + packages = append(packages, pkg) + } + return packages +} diff --git a/.github/workflows/samples-tools/go.mod b/.github/workflows/samples-tools/go.mod new file mode 100644 index 0000000000..8da07aceac --- /dev/null +++ b/.github/workflows/samples-tools/go.mod @@ -0,0 +1,3 @@ +module samples-tools + +go 1.22.0 diff --git a/.github/workflows/samples-tools/pkg/utils/args.go b/.github/workflows/samples-tools/pkg/utils/args.go new file mode 100644 index 0000000000..d199109068 --- /dev/null +++ b/.github/workflows/samples-tools/pkg/utils/args.go @@ -0,0 +1,37 @@ +package utils + +import ( + "fmt" + "os" + "strings" +) + +func ArgRequired(index int, errorMessage string) string { + if len(os.Args) <= index { + panic(errorMessage) + } + return os.Args[index] +} + +func ArgWithDefault(index int, defaultValue string) string { + if len(os.Args) <= index { + return defaultValue + } + return os.Args[index] +} + +func InterpolateArgs(args []string, values []string) []string { + var result []string + for _, arg := range args { + for argIdx, value := range values { + arg = strings.ReplaceAll(arg, fmt.Sprintf("$%d", argIdx+1), value) + } + if arg := strings.TrimSpace(strings.TrimSuffix(arg, "$@")); arg != "" { + result = append(result, arg) + } + if strings.HasSuffix(arg, "$@") { + result = append(result, values...) + } + } + return result +} diff --git a/.github/workflows/samples-tools/pkg/utils/config.go b/.github/workflows/samples-tools/pkg/utils/config.go new file mode 100644 index 0000000000..78ab3f6191 --- /dev/null +++ b/.github/workflows/samples-tools/pkg/utils/config.go @@ -0,0 +1,88 @@ +package utils + +import ( + "encoding/json" + "errors" + "os" + "path/filepath" +) + +type Config struct { + // Filenames to ignore, can include wildcard `*` stars. + Match []string `json:"match"` + + // Filenames to ignore, can include wildcard `*` stars. + Ignore []string `json:"ignore"` + + // Filename to look for the root of a package. + Package []string `json:"package"` + + // Actions to run including the given commands. + Actions map[string][]struct { + Command string `json:"command"` + Args []string `json:"args"` + } `json:"actions"` +} + +func LoadConfig(path string) (Config, error) { + bytes, err := os.ReadFile(path) + if err != nil { + return Config{}, err + } + config, err := ParseConfig(bytes) + if err != nil { + return Config{}, err + } + return config, nil +} + +func ParseConfig(configFile []byte) (Config, error) { + var config Config + err := json.Unmarshal(configFile, &config) + if err != nil { + return Config{}, err + } + if config.Match == nil { + config.Match = []string{"*"} + } + return config, nil +} + +func match(patterns []string, path string) bool { + for _, pattern := range patterns { + if match, _ := filepath.Match(pattern, path); match { + return true + } + } + return false +} + +func (c Config) Matches(path string) bool { + filename := filepath.Base(path) + return match(c.Match, filename) && !match(c.Ignore, filename) +} + +func (c Config) IsPackageDir(dir string) bool { + for _, filename := range c.Package { + packageFile := filepath.Join(dir, filename) + if fileExists(packageFile) { + return true + } + } + return false +} + +func (c Config) FindPackage(path string) string { + dir := filepath.Dir(path) + if dir == "." || c.IsPackageDir(dir) { + return dir + } + return c.FindPackage(dir) +} + +func fileExists(path string) bool { + if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { + return false + } + return true +} diff --git a/.github/workflows/samples-tools/pkg/utils/git.go b/.github/workflows/samples-tools/pkg/utils/git.go new file mode 100644 index 0000000000..18030133ba --- /dev/null +++ b/.github/workflows/samples-tools/pkg/utils/git.go @@ -0,0 +1,15 @@ +package utils + +import ( + "os/exec" + "strings" +) + +func Diffs(head string, main string) ([]string, error) { + cmd := exec.Command("git", "--no-pager", "diff", "--name-only", head, main) + output, err := cmd.Output() + if err != nil { + return []string{}, err + } + return strings.Split(string(output), "\n"), nil +} From 4b4b3fd54dab4197c076ea8c12c0f2dda5088c4a Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 16:12:28 -0700 Subject: [PATCH 002/127] edit to trigger tests --- cloud-language/quickstart.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloud-language/quickstart.js b/cloud-language/quickstart.js index 7eb7b5a4a2..96a379cbf4 100644 --- a/cloud-language/quickstart.js +++ b/cloud-language/quickstart.js @@ -16,7 +16,7 @@ // [START language_quickstart] async function quickstart() { - // Imports the Google Cloud client library + // Imports the Google Cloud client library. const language = require('@google-cloud/language'); // Instantiates a client From f96d2cf88d43d9d37867bf6239f9fea8be201118 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 16:18:20 -0700 Subject: [PATCH 003/127] update reusable workflow path --- .github/workflows/on-pull-request.yaml | 2 +- .github/workflows/samples-tools/cmd/affected/main.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 59b5787f96..1722807283 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -55,7 +55,7 @@ jobs: exclude: - package: '.' steps: - - uses: nodejs-lint.yaml + - uses: .github/workflows/nodejs-lint.yaml # test: # if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index 5b75311d64..a2dece1e78 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -33,7 +33,6 @@ func main() { } func affected(config utils.Config, diffs []string) []string { - // TODO(dcavazos): Detect affected changes more granularly with the diffs. uniquePackages := make(map[string]bool) for _, diff := range diffs { if !config.Matches(diff) { From cb10e4fd3fd08e3d7d3fad0659b038730cfc9401 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 16:20:05 -0700 Subject: [PATCH 004/127] make relative path --- .github/workflows/on-pull-request.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 1722807283..e60ff05f3e 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -55,7 +55,7 @@ jobs: exclude: - package: '.' steps: - - uses: .github/workflows/nodejs-lint.yaml + - uses: ./.github/workflows/nodejs-lint.yaml # test: # if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' From 99ffa4927aeed968ce8e0292a526ff6ec230b85c Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 16:31:07 -0700 Subject: [PATCH 005/127] fix config --- .github/workflows/config/nodejs.json | 3 +++ .github/workflows/nodejs-lint.yaml | 5 +---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/config/nodejs.json b/.github/workflows/config/nodejs.json index e69de29bb2..7677d46d0d 100644 --- a/.github/workflows/config/nodejs.json +++ b/.github/workflows/config/nodejs.json @@ -0,0 +1,3 @@ +{ + "package": ["package.json"] +} diff --git a/.github/workflows/nodejs-lint.yaml b/.github/workflows/nodejs-lint.yaml index 3b7a1cc649..4563610b18 100644 --- a/.github/workflows/nodejs-lint.yaml +++ b/.github/workflows/nodejs-lint.yaml @@ -18,14 +18,11 @@ on: package: required: true type: string - timeout-minutes: - default: 5 - type: number jobs: nodejs-lint: runs-on: ubuntu-latest - timeout-minutes: ${{ inputs.timeout-minutes }} + timeout-minutes: 5 steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 From 011cac81c512648c2e101e57a95c90ee7c9fcf87 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 16:33:18 -0700 Subject: [PATCH 006/127] inline workflow --- .github/workflows/nodejs-lint.yaml | 31 -------------------------- .github/workflows/on-pull-request.yaml | 6 ++++- 2 files changed, 5 insertions(+), 32 deletions(-) delete mode 100644 .github/workflows/nodejs-lint.yaml diff --git a/.github/workflows/nodejs-lint.yaml b/.github/workflows/nodejs-lint.yaml deleted file mode 100644 index 4563610b18..0000000000 --- a/.github/workflows/nodejs-lint.yaml +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright 2023 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -on: - workflow_call: - inputs: - package: - required: true - type: string - -jobs: - nodejs-lint: - runs-on: ubuntu-latest - timeout-minutes: 5 - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 - with: - node-version: 20 - - run: make lint dir=${{ inputs.package }} diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index e60ff05f3e..941e2ddaea 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -55,7 +55,11 @@ jobs: exclude: - package: '.' steps: - - uses: ./.github/workflows/nodejs-lint.yaml + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 20 + - run: make lint dir=${{ matrix.package }} # test: # if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' From 24ae0ff0edfbc8dcc235d18598110cbac570e1f7 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 16:37:51 -0700 Subject: [PATCH 007/127] install before lint --- .github/workflows/on-pull-request.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 941e2ddaea..57cc605782 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -59,6 +59,8 @@ jobs: - uses: actions/setup-node@v4 with: node-version: 20 + - run: npm ci + working-directory: ${{ matrix.package }} - run: make lint dir=${{ matrix.package }} # test: From 8c15093c659e6aa28d7e419ec5f3209cdf514e53 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 16:39:54 -0700 Subject: [PATCH 008/127] install before lint on Makefile --- .github/workflows/on-pull-request.yaml | 2 -- Makefile | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 57cc605782..941e2ddaea 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -59,8 +59,6 @@ jobs: - uses: actions/setup-node@v4 with: node-version: 20 - - run: npm ci - working-directory: ${{ matrix.package }} - run: make lint dir=${{ matrix.package }} # test: diff --git a/Makefile b/Makefile index bb15fcb493..963cadfc87 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,7 @@ test: check-env build lint: cd ${dir} + npm install npx gts fix npx gts lint From f35e5a6e08d6b2637a569d318322c2b0330511c1 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 16:42:00 -0700 Subject: [PATCH 009/127] install gts --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 963cadfc87..3f1648da54 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ test: check-env build lint: cd ${dir} - npm install + npm install --dev gts npx gts fix npx gts lint From 3a1703ef90ff5c79fe854860c58f637f21c40cda Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 16:50:07 -0700 Subject: [PATCH 010/127] install repo wide deps --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3f1648da54..cde229232e 100644 --- a/Makefile +++ b/Makefile @@ -23,8 +23,9 @@ test: check-env build npm test lint: + npm install cd ${dir} - npm install --dev gts + npm install npx gts fix npx gts lint From 159548d838c11f2bebbd21928ba8516434b9df8e Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 16:52:59 -0700 Subject: [PATCH 011/127] add test action --- .github/workflows/on-pull-request.yaml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 941e2ddaea..297779239d 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -61,6 +61,22 @@ jobs: node-version: 20 - run: make lint dir=${{ matrix.package }} + nodejs-test: + name: Node.js test + needs: affected + runs-on: ubuntu-latest + strategy: + matrix: + package: ${{ fromJson(needs.affected.outputs.nodejs) }} + exclude: + - package: '.' + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 20 + - run: make test dir=${{ matrix.package }} + # test: # if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' # runs-on: ubuntu-latest From 1fc7db880da241fb55c37bccc455611726271d55 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 16:59:20 -0700 Subject: [PATCH 012/127] add project id --- .github/workflows/on-pull-request.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 297779239d..eebe25e3a7 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -60,6 +60,7 @@ jobs: with: node-version: 20 - run: make lint dir=${{ matrix.package }} + - run: ./.github/workflows/utils/region-tags-tests.sh nodejs-test: name: Node.js test @@ -76,6 +77,8 @@ jobs: with: node-version: 20 - run: make test dir=${{ matrix.package }} + env: + GOOGLE_SAMPLES_PROJECT: long-door-651 # test: # if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' From 2a31b480a818ca4c6986a1cd089c2989f7e06ead Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 17:02:45 -0700 Subject: [PATCH 013/127] add auth credentials --- .github/workflows/on-pull-request.yaml | 71 +++----------------------- 1 file changed, 6 insertions(+), 65 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index eebe25e3a7..015110c9a3 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -49,6 +49,7 @@ jobs: name: Node.js lint needs: affected runs-on: ubuntu-latest + timeout-minutes: 2 strategy: matrix: package: ${{ fromJson(needs.affected.outputs.nodejs) }} @@ -66,6 +67,7 @@ jobs: name: Node.js test needs: affected runs-on: ubuntu-latest + timeout-minutes: 10 strategy: matrix: package: ${{ fromJson(needs.affected.outputs.nodejs) }} @@ -76,71 +78,10 @@ jobs: - uses: actions/setup-node@v4 with: node-version: 20 + - uses: google-github-actions/auth@v2 + with: + workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider + access_token_lifetime: 600s - run: make test dir=${{ matrix.package }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 - - # test: - # if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' - # runs-on: ubuntu-latest - # timeout-minutes: 120 - # permissions: - # contents: read - # id-token: write - # steps: - # - uses: actions/checkout@v4.1.7 - # with: - # ref: ${{github.event.pull_request.head.sha}} - # - uses: 'google-github-actions/auth@v2.1.5' - # with: - # workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider - # service_account: 'kokoro-system-test@long-door-651.iam.gserviceaccount.com' - # create_credentials_file: 'true' - # access_token_lifetime: 600s - # - id: secrets - # uses: 'google-github-actions/get-secretmanager-secrets@95a0b09b8348ef3d02c68c6ba5662a037e78d713' # v2 - # with: - # secrets: |- - # caip_id:nodejs-docs-samples-tests/nodejs-docs-samples-ai-platform-caip-project-id - # location:nodejs-docs-samples-tests/nodejs-docs-samples-ai-platform-location - # - uses: actions/setup-node@8f152de45cc393bb48ce5d89d36b731f54556e65 # v4.0.0 - # with: - # node-version: 16 - # - name: Get npm cache directory - # id: npm-cache-dir - # shell: bash - # run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} - # - uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4 - # id: npm-cache - # with: - # path: ${{ steps.npm-cache-dir.outputs.dir }} - # key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} - # restore-keys: "${{ runner.os }}-node- \n" - # - name: set env vars for scheduled run - # if: github.event.action == 'schedule' - # run: | - # echo "MOCHA_REPORTER_SUITENAME=ai-platform-snippets" >> $GITHUB_ENV - # echo "MOCHA_REPORTER_OUTPUT=${{github.run_id}}_sponge_log.xml" >> $GITHUB_ENV - # echo "MOCHA_REPORTER=xunit" >> $GITHUB_ENV - # - name: Run Tests - # run: make test dir=ai-platform/snippets - # env: - # GOOGLE_SAMPLES_PROJECT: 'long-door-651' - # LOCATION: ${{ steps.secrets.outputs.location }} - # CAIP_PROJECT_ID: ${{ steps.secrets.outputs.caip_id }} - # - name: upload test results for FlakyBot workflow - # if: github.event.action == 'schedule' && always() - # uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4 - # env: - # MOCHA_REPORTER_OUTPUT: '${{github.run_id}}_sponge_log.xml' - # with: - # name: test-results - # path: ai-platform/snippets/${{ env.MOCHA_REPORTER_OUTPUT }} - # retention-days: 1 - # flakybot: - # permissions: - # contents: 'read' - # id-token: 'write' - # if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail - # uses: ./.github/workflows/flakybot.yaml - # needs: [test] From cb1af685ce43d20076ad7b94e1529dc515e7c2d2 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 17:04:34 -0700 Subject: [PATCH 014/127] add id-token permissions --- .github/workflows/on-pull-request.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 015110c9a3..0ff16840b1 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -68,6 +68,9 @@ jobs: needs: affected runs-on: ubuntu-latest timeout-minutes: 10 + permissions: + # contents: read + id-token: write # needed for google-github-actions/auth strategy: matrix: package: ${{ fromJson(needs.affected.outputs.nodejs) }} From 6fc0660bdbc4b76360c72c75339e421ffccd47c1 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 17:07:13 -0700 Subject: [PATCH 015/127] use service account --- .github/workflows/on-pull-request.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 0ff16840b1..4429e4383c 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -84,6 +84,7 @@ jobs: - uses: google-github-actions/auth@v2 with: workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider + service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com access_token_lifetime: 600s - run: make test dir=${{ matrix.package }} env: From c4b09fe257bf9cb7d8508bb23df7ada466106b37 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 20 Sep 2024 17:19:30 -0700 Subject: [PATCH 016/127] cleanup --- .github/workflows/on-pull-request.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 4429e4383c..903bb1ab9e 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -69,7 +69,6 @@ jobs: runs-on: ubuntu-latest timeout-minutes: 10 permissions: - # contents: read id-token: write # needed for google-github-actions/auth strategy: matrix: From 80827881bfbc2b41b3ccddef7840489d3658b1b3 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 23 Sep 2024 12:04:26 -0700 Subject: [PATCH 017/127] modify ignored file --- .github/workflows/config/nodejs.json | 1 + run/helloworld/README.md | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/.github/workflows/config/nodejs.json b/.github/workflows/config/nodejs.json index 7677d46d0d..a4b93947e2 100644 --- a/.github/workflows/config/nodejs.json +++ b/.github/workflows/config/nodejs.json @@ -1,3 +1,4 @@ { + "ignore": ["README.md"], "package": ["package.json"] } diff --git a/run/helloworld/README.md b/run/helloworld/README.md index e08f381e3d..dc3e060e33 100644 --- a/run/helloworld/README.md +++ b/run/helloworld/README.md @@ -2,7 +2,8 @@ This sample shows how to deploy a Hello World application to Cloud Run. -For more details on how to work with this sample read the [Google Cloud Run Node.js Samples README](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/main/run). +For more details on how to work with this sample read the +[Google Cloud Run Node.js Samples README](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/main/run). ## Local Development @@ -42,10 +43,10 @@ test/deploy.sh The `runner.sh` script: -* Deploys the service to Cloud Run based on the `deploy.sh` script. -* Sets the `BASE_URL` and `ID_TOKEN` environment variables. -* Runs any arguments passed to the `runner.sh` script. -* Tears down the Cloud Run service on completion. +- Deploys the service to Cloud Run based on the `deploy.sh` script. +- Sets the `BASE_URL` and `ID_TOKEN` environment variables. +- Runs any arguments passed to the `runner.sh` script. +- Tears down the Cloud Run service on completion. ```sh test/runner.sh sleep 20 @@ -53,12 +54,12 @@ test/runner.sh sleep 20 ## Environment Variables (Testing) -* `BASE_URL`: Specifies the Cloud Run service URL for end-to-end tests. -* `ID_TOKEN`: JWT token used to authenticate with Cloud Run's IAM-based authentication. -* `REGION`: [`us-central1`] Optional override region for the location of the Cloud Run service. -* `SERVICE_NAME`: The name of the deployed service, used in some API calls and test assertions. +- `BASE_URL`: Specifies the Cloud Run service URL for end-to-end tests. +- `ID_TOKEN`: JWT token used to authenticate with Cloud Run's IAM-based authentication. +- `REGION`: [`us-central1`] Optional override region for the location of the Cloud Run service. +- `SERVICE_NAME`: The name of the deployed service, used in some API calls and test assertions. ## Dependencies -* **express**: Web server framework. -* **got**: [Testing] Used to make HTTP requests of the running service in end-to-end testing. +- **express**: Web server framework. +- **got**: [Testing] Used to make HTTP requests of the running service in end-to-end testing. From 784e8e89088526c587333b20f4acd2b13aa76f38 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 23 Sep 2024 12:07:16 -0700 Subject: [PATCH 018/127] revert change --- run/helloworld/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/run/helloworld/README.md b/run/helloworld/README.md index dc3e060e33..26e490260c 100644 --- a/run/helloworld/README.md +++ b/run/helloworld/README.md @@ -2,8 +2,7 @@ This sample shows how to deploy a Hello World application to Cloud Run. -For more details on how to work with this sample read the -[Google Cloud Run Node.js Samples README](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/main/run). +For more details on how to work with this sample read the [Google Cloud Run Node.js Samples README](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/main/run). ## Local Development From 0a13f40943041b0a5bd0f9b47168f240e92384f8 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 23 Sep 2024 12:07:54 -0700 Subject: [PATCH 019/127] make change to trigger tests --- run/helloworld/.gcloudignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/run/helloworld/.gcloudignore b/run/helloworld/.gcloudignore index 93f1361991..1ca957177f 100644 --- a/run/helloworld/.gcloudignore +++ b/run/helloworld/.gcloudignore @@ -1,2 +1,2 @@ -node_modules +node_modules/ npm-debug.log From f7e547f313f4b1958d6e0604124e99f9eca3f52b Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 23 Sep 2024 12:24:26 -0700 Subject: [PATCH 020/127] include e2e-test --- .github/workflows/on-pull-request.yaml | 6 ++++++ Makefile | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 903bb1ab9e..c6a7167326 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -72,9 +72,15 @@ jobs: id-token: write # needed for google-github-actions/auth strategy: matrix: + action: ['test'] package: ${{ fromJson(needs.affected.outputs.nodejs) }} exclude: - package: '.' + include: + - action: e2e-test + package: run/helloworld + - action: e2e-test + package: run/hello-broken steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 diff --git a/Makefile b/Makefile index cde229232e..1ad81f0709 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,10 @@ test: check-env build cd ${dir} npm test +e2e-test: check-env build + cd ${dir} + npm system-test + lint: npm install cd ${dir} From 34fd1efa3c5caf73a773562d29eeffc501bcac8a Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 23 Sep 2024 12:36:44 -0700 Subject: [PATCH 021/127] check whether to e2e-test --- .github/workflows/on-pull-request.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index c6a7167326..a815003665 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -64,7 +64,7 @@ jobs: - run: ./.github/workflows/utils/region-tags-tests.sh nodejs-test: - name: Node.js test + name: Node.js needs: affected runs-on: ubuntu-latest timeout-minutes: 10 @@ -72,15 +72,14 @@ jobs: id-token: write # needed for google-github-actions/auth strategy: matrix: - action: ['test'] package: ${{ fromJson(needs.affected.outputs.nodejs) }} exclude: - package: '.' include: - - action: e2e-test - package: run/helloworld - - action: e2e-test - package: run/hello-broken + - package: run/helloworld + e2e-test: true + - package: run/hello-broken + e2e-test: true steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 @@ -91,6 +90,7 @@ jobs: workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com access_token_lifetime: 600s + - run: echo ${{ matrix.e2e-test }} - run: make test dir=${{ matrix.package }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 From 70ed258294aae78c53247bddf3a5a4c6599e86c8 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 23 Sep 2024 12:39:01 -0700 Subject: [PATCH 022/127] add e2e-test configuration --- .github/workflows/on-pull-request.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index a815003665..453b337da8 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -72,14 +72,15 @@ jobs: id-token: write # needed for google-github-actions/auth strategy: matrix: + action: ['test'] package: ${{ fromJson(needs.affected.outputs.nodejs) }} exclude: - package: '.' include: - package: run/helloworld - e2e-test: true + action: ['test', 'e2e-test'] - package: run/hello-broken - e2e-test: true + action: ['test', 'e2e-test'] steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 From 359bf41f0ca7e400ba31c27ccab262e2617fd5f0 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 23 Sep 2024 12:51:27 -0700 Subject: [PATCH 023/127] add e2e-test as extra config --- .github/workflows/config/nodejs-e2e.json | 5 +++ .github/workflows/on-pull-request.yaml | 41 +++++++++++++++++++----- 2 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/config/nodejs-e2e.json diff --git a/.github/workflows/config/nodejs-e2e.json b/.github/workflows/config/nodejs-e2e.json new file mode 100644 index 0000000000..629e206419 --- /dev/null +++ b/.github/workflows/config/nodejs-e2e.json @@ -0,0 +1,5 @@ +{ + "match": ["run/hello-broken", "run/helloworld"], + "ignore": ["README.md"], + "package": ["package.json"] +} diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 453b337da8..2f2e642512 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -30,6 +30,7 @@ jobs: runs-on: ubuntu-latest outputs: nodejs: ${{ steps.nodejs.outputs.packages }} + nodejs-e2e: ${{ steps.nodejs-e2e.outputs.packages }} steps: - uses: actions/checkout@v4 with: @@ -44,6 +45,11 @@ jobs: PACKAGES=$(affected .github/workflows/config/nodejs.json) echo "packages=$PACKAGES" echo "packages=$PACKAGES " >> "$GITHUB_OUTPUT" + - id: nodejs-e2e + run: | + PACKAGES=$(affected .github/workflows/config/nodejs-e2e.json) + echo "packages=$PACKAGES" + echo "packages=$PACKAGES " >> "$GITHUB_OUTPUT" nodejs-lint: name: Node.js lint @@ -64,7 +70,7 @@ jobs: - run: ./.github/workflows/utils/region-tags-tests.sh nodejs-test: - name: Node.js + name: Node.js test needs: affected runs-on: ubuntu-latest timeout-minutes: 10 @@ -72,15 +78,9 @@ jobs: id-token: write # needed for google-github-actions/auth strategy: matrix: - action: ['test'] package: ${{ fromJson(needs.affected.outputs.nodejs) }} exclude: - package: '.' - include: - - package: run/helloworld - action: ['test', 'e2e-test'] - - package: run/hello-broken - action: ['test', 'e2e-test'] steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 @@ -91,7 +91,32 @@ jobs: workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com access_token_lifetime: 600s - - run: echo ${{ matrix.e2e-test }} - run: make test dir=${{ matrix.package }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 + + nodejs-e2e-test: + name: Node.js e2e-test + needs: affected + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + id-token: write # needed for google-github-actions/auth + strategy: + matrix: + package: ${{ fromJson(needs.affected.outputs.nodejs-e2e) }} + exclude: + - package: '.' + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 20 + - uses: google-github-actions/auth@v2 + with: + workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider + service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com + access_token_lifetime: 600s + - run: make e2e-test dir=${{ matrix.package }} + env: + GOOGLE_SAMPLES_PROJECT: long-door-651 From 1dd8e9ee28bd754dc0a257f0914d2d9b8e8cc257 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 23 Sep 2024 12:58:58 -0700 Subject: [PATCH 024/127] also check for substring as match --- .github/workflows/config/nodejs-e2e.json | 2 +- .github/workflows/samples-tools/pkg/utils/config.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/config/nodejs-e2e.json b/.github/workflows/config/nodejs-e2e.json index 629e206419..d2c1471274 100644 --- a/.github/workflows/config/nodejs-e2e.json +++ b/.github/workflows/config/nodejs-e2e.json @@ -1,5 +1,5 @@ { - "match": ["run/hello-broken", "run/helloworld"], + "match": ["run/hello-broken/", "run/helloworld/"], "ignore": ["README.md"], "package": ["package.json"] } diff --git a/.github/workflows/samples-tools/pkg/utils/config.go b/.github/workflows/samples-tools/pkg/utils/config.go index 78ab3f6191..afaa35d440 100644 --- a/.github/workflows/samples-tools/pkg/utils/config.go +++ b/.github/workflows/samples-tools/pkg/utils/config.go @@ -5,6 +5,7 @@ import ( "errors" "os" "path/filepath" + "strings" ) type Config struct { @@ -53,6 +54,9 @@ func match(patterns []string, path string) bool { if match, _ := filepath.Match(pattern, path); match { return true } + if strings.Contains(path, pattern) { + return true + } } return false } From edc16928acbe8c5c242109d5938e84b091f81269 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 23 Sep 2024 13:00:08 -0700 Subject: [PATCH 025/127] pass entire path to match --- .github/workflows/samples-tools/pkg/utils/config.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/samples-tools/pkg/utils/config.go b/.github/workflows/samples-tools/pkg/utils/config.go index afaa35d440..51c3d58bb0 100644 --- a/.github/workflows/samples-tools/pkg/utils/config.go +++ b/.github/workflows/samples-tools/pkg/utils/config.go @@ -50,8 +50,9 @@ func ParseConfig(configFile []byte) (Config, error) { } func match(patterns []string, path string) bool { + filename := filepath.Base(path) for _, pattern := range patterns { - if match, _ := filepath.Match(pattern, path); match { + if match, _ := filepath.Match(pattern, filename); match { return true } if strings.Contains(path, pattern) { @@ -62,8 +63,7 @@ func match(patterns []string, path string) bool { } func (c Config) Matches(path string) bool { - filename := filepath.Base(path) - return match(c.Match, filename) && !match(c.Ignore, filename) + return match(c.Match, path) && !match(c.Ignore, path) } func (c Config) IsPackageDir(dir string) bool { From 5d730e2d3ef9ec17f3ba581ce8fdf98e3a2b668e Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 11:12:55 -0700 Subject: [PATCH 026/127] mark as experimental --- .github/workflows/on-pull-request.yaml | 2 +- .github/workflows/samples-tools/cmd/affected/main.go | 7 ++++--- Makefile | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 2f2e642512..21256660c6 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -name: 🪿 +name: experimental on: push: branches: diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index a2dece1e78..4bf874111e 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -42,9 +42,10 @@ func affected(config utils.Config, diffs []string) []string { uniquePackages[pkg] = true } - packages := make([]string, 0, len(uniquePackages)) + changed := make([]string, 0, len(uniquePackages)) for pkg := range uniquePackages { - packages = append(packages, pkg) + changed = append(changed, pkg) } - return packages + + return changed } diff --git a/Makefile b/Makefile index 1ad81f0709..711c1b09bb 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,7 @@ test: check-env build e2e-test: check-env build cd ${dir} - npm system-test + npm run system-test lint: npm install From 7e724a5d7f9cc044f8fd37f7d8887d37a25268b0 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 11:23:34 -0700 Subject: [PATCH 027/127] add experimental emoji --- .github/workflows/on-pull-request.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 21256660c6..ee980b5743 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -name: experimental +name: āš—ļø experimental on: push: branches: From 5c63239ff3688e7c7c0b2c2f854624548477901e Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 11:29:01 -0700 Subject: [PATCH 028/127] test on node 18 and 22 --- .github/workflows/on-pull-request.yaml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index ee980b5743..64e8a631f3 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -65,7 +65,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: 20 + node-version: 22 - run: make lint dir=${{ matrix.package }} - run: ./.github/workflows/utils/region-tags-tests.sh @@ -79,13 +79,14 @@ jobs: strategy: matrix: package: ${{ fromJson(needs.affected.outputs.nodejs) }} + node-version: [18, 22] exclude: - package: '.' steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: 20 + node-version: ${{ matrix.node-version }} - uses: google-github-actions/auth@v2 with: workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider @@ -105,13 +106,14 @@ jobs: strategy: matrix: package: ${{ fromJson(needs.affected.outputs.nodejs-e2e) }} + node-version: [18, 22] exclude: - package: '.' steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: 20 + node-version: ${{ matrix.node-version }} - uses: google-github-actions/auth@v2 with: workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider From 4665437fd8839848dad7c0f170b28bf9253ff67c Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 11:34:52 -0700 Subject: [PATCH 029/127] reorder matrix --- .github/workflows/on-pull-request.yaml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 64e8a631f3..7f1160eb5d 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -28,6 +28,7 @@ jobs: affected: name: Finding affected tests runs-on: ubuntu-latest + timeout-minutes: 1 outputs: nodejs: ${{ steps.nodejs.outputs.packages }} nodejs-e2e: ${{ steps.nodejs-e2e.outputs.packages }} @@ -55,7 +56,7 @@ jobs: name: Node.js lint needs: affected runs-on: ubuntu-latest - timeout-minutes: 2 + timeout-minutes: 5 strategy: matrix: package: ${{ fromJson(needs.affected.outputs.nodejs) }} @@ -73,13 +74,13 @@ jobs: name: Node.js test needs: affected runs-on: ubuntu-latest - timeout-minutes: 10 + timeout-minutes: 15 permissions: id-token: write # needed for google-github-actions/auth strategy: matrix: - package: ${{ fromJson(needs.affected.outputs.nodejs) }} node-version: [18, 22] + package: ${{ fromJson(needs.affected.outputs.nodejs) }} exclude: - package: '.' steps: @@ -100,13 +101,13 @@ jobs: name: Node.js e2e-test needs: affected runs-on: ubuntu-latest - timeout-minutes: 10 + timeout-minutes: 60 permissions: id-token: write # needed for google-github-actions/auth strategy: matrix: - package: ${{ fromJson(needs.affected.outputs.nodejs-e2e) }} node-version: [18, 22] + package: ${{ fromJson(needs.affected.outputs.nodejs-e2e) }} exclude: - package: '.' steps: From f7287f8f67f5a0d855ad48c0ea8e893fdffd8975 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 12:07:53 -0700 Subject: [PATCH 030/127] use wip directly without service account --- .github/workflows/on-pull-request.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 7f1160eb5d..e71ef75de7 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -90,9 +90,9 @@ jobs: node-version: ${{ matrix.node-version }} - uses: google-github-actions/auth@v2 with: + project_id: long-door-651 workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider - service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com - access_token_lifetime: 600s + access_token_lifetime: 300s # 5 minutes - run: make test dir=${{ matrix.package }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 @@ -117,9 +117,9 @@ jobs: node-version: ${{ matrix.node-version }} - uses: google-github-actions/auth@v2 with: + project_id: long-door-651 workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider - service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com - access_token_lifetime: 600s + access_token_lifetime: 600s # 10 minutes - run: make e2e-test dir=${{ matrix.package }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 From d8494d2c1b7d5097742a9f26df0244fd78c25d3b Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 12:15:55 -0700 Subject: [PATCH 031/127] use kokoro service account --- .github/workflows/cloud-language.yaml | 20 +------------------- .github/workflows/on-pull-request.yaml | 2 ++ 2 files changed, 3 insertions(+), 19 deletions(-) diff --git a/.github/workflows/cloud-language.yaml b/.github/workflows/cloud-language.yaml index 4f9a8bd250..802c993fa2 100644 --- a/.github/workflows/cloud-language.yaml +++ b/.github/workflows/cloud-language.yaml @@ -14,31 +14,13 @@ name: cloud-language on: - push: - branches: - - main - paths: - - 'cloud-language/**' - - '.github/workflows/cloud-language.yaml' - - '.github/workflows/test.yaml' - pull_request: - types: - - opened - - reopened - - synchronize - - labeled - paths: - - 'cloud-language/**' - - '.github/workflows/cloud-language.yaml' - - '.github/workflows/test.yaml' schedule: - - cron: '0 0 * * 0' + - cron: '0 0 * * 0' jobs: test: permissions: contents: 'read' id-token: 'write' - if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' uses: ./.github/workflows/test.yaml with: name: 'cloud-language' diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index e71ef75de7..14c2c66a0e 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -92,6 +92,7 @@ jobs: with: project_id: long-door-651 workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider + service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com access_token_lifetime: 300s # 5 minutes - run: make test dir=${{ matrix.package }} env: @@ -119,6 +120,7 @@ jobs: with: project_id: long-door-651 workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider + service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com access_token_lifetime: 600s # 10 minutes - run: make e2e-test dir=${{ matrix.package }} env: From a3e85be6488f3c96c1c4f8dc45a41c4a20876914 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 13:49:00 -0700 Subject: [PATCH 032/127] trigger all tests on root changes --- .github/workflows/config/nodejs-e2e.json | 2 +- .github/workflows/config/nodejs.json | 2 +- .../samples-tools/cmd/affected/main.go | 35 +++++++++++++++++-- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/.github/workflows/config/nodejs-e2e.json b/.github/workflows/config/nodejs-e2e.json index d2c1471274..7a0d6058ba 100644 --- a/.github/workflows/config/nodejs-e2e.json +++ b/.github/workflows/config/nodejs-e2e.json @@ -1,5 +1,5 @@ { "match": ["run/hello-broken/", "run/helloworld/"], - "ignore": ["README.md"], + "ignore": ["README.md", "node_modules/"], "package": ["package.json"] } diff --git a/.github/workflows/config/nodejs.json b/.github/workflows/config/nodejs.json index a4b93947e2..7b36631866 100644 --- a/.github/workflows/config/nodejs.json +++ b/.github/workflows/config/nodejs.json @@ -1,4 +1,4 @@ { - "ignore": ["README.md"], + "ignore": ["README.md", "node_modules/"], "package": ["package.json"] } diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index 4bf874111e..c6b7a62508 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -3,6 +3,9 @@ package main import ( "encoding/json" "fmt" + "os" + "path/filepath" + "slices" "samples-tools/pkg/utils" ) @@ -23,7 +26,11 @@ func main() { panic(err) } - matrix := affected(config, diffs) + matrix, err := affected(config, diffs) + if err != nil { + panic(err) + } + matrixJson, err := json.Marshal(matrix) if err != nil { panic(err) @@ -32,7 +39,7 @@ func main() { fmt.Println(string(matrixJson)) } -func affected(config utils.Config, diffs []string) []string { +func affected(config utils.Config, diffs []string) ([]string, error) { uniquePackages := make(map[string]bool) for _, diff := range diffs { if !config.Matches(diff) { @@ -47,5 +54,27 @@ func affected(config utils.Config, diffs []string) []string { changed = append(changed, pkg) } - return changed + if !slices.Contains(changed, ".") { + return changed, nil + } + + var packages []string + err := filepath.WalkDir(".", + func(path string, d os.DirEntry, err error) error { + if err != nil { + return err + } + if path == "." { + return nil + } + if d.IsDir() && config.Matches(path) && config.IsPackageDir(path) { + packages = append(packages, path) + return filepath.SkipDir + } + return nil + }) + if err != nil { + return []string{}, err + } + return packages, nil } From 6108a4aa78a27cccc003fddaa865dc655297a1ad Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 13:57:38 -0700 Subject: [PATCH 033/127] do not fail fast --- .github/workflows/on-pull-request.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 14c2c66a0e..6353982fe5 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -83,6 +83,7 @@ jobs: package: ${{ fromJson(needs.affected.outputs.nodejs) }} exclude: - package: '.' + fail-fast: false # TODO: remove this, only for testing! steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 From 27ba94727c358a69b735955b564f20f66d653105 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 14:22:27 -0700 Subject: [PATCH 034/127] make lint a global job --- .github/workflows/on-pull-request.yaml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 6353982fe5..57c0218f35 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -54,20 +54,14 @@ jobs: nodejs-lint: name: Node.js lint - needs: affected runs-on: ubuntu-latest timeout-minutes: 5 - strategy: - matrix: - package: ${{ fromJson(needs.affected.outputs.nodejs) }} - exclude: - - package: '.' steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: 22 - - run: make lint dir=${{ matrix.package }} + - run: make lint - run: ./.github/workflows/utils/region-tags-tests.sh nodejs-test: From 188e47d3a9ac05d15f458c2b51b633b1a57d7688 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 14:34:56 -0700 Subject: [PATCH 035/127] separate nodejs versions --- .github/workflows/on-pull-request.yaml | 45 +++++++++++++++--------- .github/workflows/test-nodejs.yaml | 48 ++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/test-nodejs.yaml diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 57c0218f35..ff97257850 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -1,4 +1,4 @@ -# Copyright 2023 Google LLC +# Copyright 2024 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -64,34 +64,45 @@ jobs: - run: make lint - run: ./.github/workflows/utils/region-tags-tests.sh - nodejs-test: - name: Node.js test + nodejs-low-test: + name: Node.js 18 test needs: affected runs-on: ubuntu-latest - timeout-minutes: 15 - permissions: - id-token: write # needed for google-github-actions/auth + # permissions: + # id-token: write # needed for google-github-actions/auth strategy: matrix: - node-version: [18, 22] package: ${{ fromJson(needs.affected.outputs.nodejs) }} exclude: - package: '.' fail-fast: false # TODO: remove this, only for testing! steps: - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 + - uses: ./.github/workflows/test-nodejs.yaml with: - node-version: ${{ matrix.node-version }} - - uses: google-github-actions/auth@v2 + node-version: 18 + path: ${{ matrix.package }} + timeout-minutes: 15 + + nodejs-high-test: + name: Node.js 22 test + needs: affected + runs-on: ubuntu-latest + # permissions: + # id-token: write # needed for google-github-actions/auth + strategy: + matrix: + package: ${{ fromJson(needs.affected.outputs.nodejs) }} + exclude: + - package: '.' + fail-fast: false # TODO: remove this, only for testing! + steps: + - uses: actions/checkout@v4 + - uses: ./.github/workflows/test-nodejs.yaml with: - project_id: long-door-651 - workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider - service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com - access_token_lifetime: 300s # 5 minutes - - run: make test dir=${{ matrix.package }} - env: - GOOGLE_SAMPLES_PROJECT: long-door-651 + node-version: 22 + path: ${{ matrix.package }} + timeout-minutes: 15 nodejs-e2e-test: name: Node.js e2e-test diff --git a/.github/workflows/test-nodejs.yaml b/.github/workflows/test-nodejs.yaml new file mode 100644 index 0000000000..40b7baee18 --- /dev/null +++ b/.github/workflows/test-nodejs.yaml @@ -0,0 +1,48 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: Node.js + +on: + workflow_call: + inputs: + node-version: + required: true + type: number + path: + required: true + type: string + timeout-minutes: + required: true + type: number + +jobs: + test: + runs-on: ubuntu-latest + timeout-minutes: ${{ inputs.timeout-minutes }} + permissions: + id-token: write # needed for google-github-actions/auth + steps: + - uses: actions/setup-node@v4 + with: + node-version: ${{ inputs.node-version }} + - uses: google-github-actions/auth@v2 + with: + project_id: long-door-651 + workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider + service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com + access_token_lifetime: 300s # 5 minutes + - run: make test dir=${{ inputs.path }} + env: + GOOGLE_SAMPLES_PROJECT: long-door-651 From 4c40bebe35a08c8fce3754ed743fcc6eae2d6161 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 14:42:13 -0700 Subject: [PATCH 036/127] add checkout to reusable workflow --- .github/workflows/samples-tools/cmd/affected/main.go | 4 ++++ .github/workflows/test-nodejs.yaml | 1 + 2 files changed, 5 insertions(+) diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index c6b7a62508..f472f22021 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -76,5 +76,9 @@ func affected(config utils.Config, diffs []string) ([]string, error) { if err != nil { return []string{}, err } + + if len(packages) > 256 { + return []string{}, fmt.Errorf("GitHub Actions only supports up to 256 packages, got %d packages", len(packages)) + } return packages, nil } diff --git a/.github/workflows/test-nodejs.yaml b/.github/workflows/test-nodejs.yaml index 40b7baee18..2917444319 100644 --- a/.github/workflows/test-nodejs.yaml +++ b/.github/workflows/test-nodejs.yaml @@ -34,6 +34,7 @@ jobs: permissions: id-token: write # needed for google-github-actions/auth steps: + - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: ${{ inputs.node-version }} From cb51d959a13b1b2f53da37a7596584ae91f0591d Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 14:45:42 -0700 Subject: [PATCH 037/127] add contents read permission --- .github/workflows/on-pull-request.yaml | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index ff97257850..4895ac5aa9 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -68,8 +68,9 @@ jobs: name: Node.js 18 test needs: affected runs-on: ubuntu-latest - # permissions: - # id-token: write # needed for google-github-actions/auth + permissions: + contents: read # needed to run local actions + # id-token: write # needed for google-github-actions/auth strategy: matrix: package: ${{ fromJson(needs.affected.outputs.nodejs) }} @@ -88,8 +89,9 @@ jobs: name: Node.js 22 test needs: affected runs-on: ubuntu-latest - # permissions: - # id-token: write # needed for google-github-actions/auth + permissions: + contents: read # needed to run local actions + # id-token: write # needed for google-github-actions/auth strategy: matrix: package: ${{ fromJson(needs.affected.outputs.nodejs) }} From 9811b3c3e60ebe5cde4189ffd529b248601bec96 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 14:48:55 -0700 Subject: [PATCH 038/127] skip testing everything on root changes --- .github/workflows/on-pull-request.yaml | 40 +++++++++---------- .../samples-tools/cmd/affected/main.go | 1 + 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 4895ac5aa9..3e1f0ba783 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -85,26 +85,26 @@ jobs: path: ${{ matrix.package }} timeout-minutes: 15 - nodejs-high-test: - name: Node.js 22 test - needs: affected - runs-on: ubuntu-latest - permissions: - contents: read # needed to run local actions - # id-token: write # needed for google-github-actions/auth - strategy: - matrix: - package: ${{ fromJson(needs.affected.outputs.nodejs) }} - exclude: - - package: '.' - fail-fast: false # TODO: remove this, only for testing! - steps: - - uses: actions/checkout@v4 - - uses: ./.github/workflows/test-nodejs.yaml - with: - node-version: 22 - path: ${{ matrix.package }} - timeout-minutes: 15 + # nodejs-high-test: + # name: Node.js 22 test + # needs: affected + # runs-on: ubuntu-latest + # permissions: + # contents: read # needed to run local actions + # # id-token: write # needed for google-github-actions/auth + # strategy: + # matrix: + # package: ${{ fromJson(needs.affected.outputs.nodejs) }} + # exclude: + # - package: '.' + # fail-fast: false # TODO: remove this, only for testing! + # steps: + # - uses: actions/checkout@v4 + # - uses: ./.github/workflows/test-nodejs.yaml + # with: + # node-version: 22 + # path: ${{ matrix.package }} + # timeout-minutes: 15 nodejs-e2e-test: name: Node.js e2e-test diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index f472f22021..65843fcb9b 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -54,6 +54,7 @@ func affected(config utils.Config, diffs []string) ([]string, error) { changed = append(changed, pkg) } + return changed, nil // TODO: REMOVE THIS if !slices.Contains(changed, ".") { return changed, nil } From 1b08c9aee2d996390ce1aac64b8e98f9cae5e822 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 14:55:59 -0700 Subject: [PATCH 039/127] give contents read permissions --- .github/workflows/on-pull-request.yaml | 56 +++++++++++++------------- .github/workflows/test-nodejs.yaml | 3 +- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 3e1f0ba783..9db675ea05 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -69,7 +69,7 @@ jobs: needs: affected runs-on: ubuntu-latest permissions: - contents: read # needed to run local actions + contents: read # id-token: write # needed for google-github-actions/auth strategy: matrix: @@ -106,30 +106,30 @@ jobs: # path: ${{ matrix.package }} # timeout-minutes: 15 - nodejs-e2e-test: - name: Node.js e2e-test - needs: affected - runs-on: ubuntu-latest - timeout-minutes: 60 - permissions: - id-token: write # needed for google-github-actions/auth - strategy: - matrix: - node-version: [18, 22] - package: ${{ fromJson(needs.affected.outputs.nodejs-e2e) }} - exclude: - - package: '.' - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 - with: - node-version: ${{ matrix.node-version }} - - uses: google-github-actions/auth@v2 - with: - project_id: long-door-651 - workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider - service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com - access_token_lifetime: 600s # 10 minutes - - run: make e2e-test dir=${{ matrix.package }} - env: - GOOGLE_SAMPLES_PROJECT: long-door-651 + # nodejs-e2e-test: + # name: Node.js e2e-test + # needs: affected + # runs-on: ubuntu-latest + # timeout-minutes: 60 + # permissions: + # id-token: write # needed for google-github-actions/auth + # strategy: + # matrix: + # node-version: [18, 22] + # package: ${{ fromJson(needs.affected.outputs.nodejs-e2e) }} + # exclude: + # - package: '.' + # steps: + # - uses: actions/checkout@v4 + # - uses: actions/setup-node@v4 + # with: + # node-version: ${{ matrix.node-version }} + # - uses: google-github-actions/auth@v2 + # with: + # project_id: long-door-651 + # workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider + # service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com + # access_token_lifetime: 600s # 10 minutes + # - run: make e2e-test dir=${{ matrix.package }} + # env: + # GOOGLE_SAMPLES_PROJECT: long-door-651 diff --git a/.github/workflows/test-nodejs.yaml b/.github/workflows/test-nodejs.yaml index 2917444319..f3ca502664 100644 --- a/.github/workflows/test-nodejs.yaml +++ b/.github/workflows/test-nodejs.yaml @@ -12,8 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -name: Node.js - on: workflow_call: inputs: @@ -32,6 +30,7 @@ jobs: runs-on: ubuntu-latest timeout-minutes: ${{ inputs.timeout-minutes }} permissions: + contents: read id-token: write # needed for google-github-actions/auth steps: - uses: actions/checkout@v4 From 5591184a1bdffc854775541cd488991258c45015 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 15:00:13 -0700 Subject: [PATCH 040/127] call reusable workflow correctly --- .github/workflows/on-pull-request.yaml | 21 +++++++++------------ .github/workflows/test-nodejs.yaml | 8 ++++---- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 9db675ea05..160789fedc 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -67,23 +67,20 @@ jobs: nodejs-low-test: name: Node.js 18 test needs: affected - runs-on: ubuntu-latest - permissions: - contents: read - # id-token: write # needed for google-github-actions/auth + # permissions: + # contents: read + # # id-token: write # needed for google-github-actions/auth strategy: matrix: package: ${{ fromJson(needs.affected.outputs.nodejs) }} exclude: - package: '.' fail-fast: false # TODO: remove this, only for testing! - steps: - - uses: actions/checkout@v4 - - uses: ./.github/workflows/test-nodejs.yaml - with: - node-version: 18 - path: ${{ matrix.package }} - timeout-minutes: 15 + uses: ./.github/workflows/test-nodejs.yaml + with: + path: ${{ matrix.package }} + node-version: 18 + timeout-minutes: 15 # nodejs-high-test: # name: Node.js 22 test @@ -102,8 +99,8 @@ jobs: # - uses: actions/checkout@v4 # - uses: ./.github/workflows/test-nodejs.yaml # with: - # node-version: 22 # path: ${{ matrix.package }} + # node-version: 22 # timeout-minutes: 15 # nodejs-e2e-test: diff --git a/.github/workflows/test-nodejs.yaml b/.github/workflows/test-nodejs.yaml index f3ca502664..f0821028d2 100644 --- a/.github/workflows/test-nodejs.yaml +++ b/.github/workflows/test-nodejs.yaml @@ -15,12 +15,12 @@ on: workflow_call: inputs: - node-version: - required: true - type: number path: required: true type: string + node-version: + required: true + type: number timeout-minutes: required: true type: number @@ -30,7 +30,7 @@ jobs: runs-on: ubuntu-latest timeout-minutes: ${{ inputs.timeout-minutes }} permissions: - contents: read + # contents: read id-token: write # needed for google-github-actions/auth steps: - uses: actions/checkout@v4 From 7848775a59e421c6f55ffbbfc891695fa7b2250f Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 15:02:27 -0700 Subject: [PATCH 041/127] reusable workflow for nodejs test --- .github/workflows/on-pull-request.yaml | 39 ++++++++++---------------- .github/workflows/test-nodejs.yaml | 1 + 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 160789fedc..2d85972ff6 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -65,11 +65,8 @@ jobs: - run: ./.github/workflows/utils/region-tags-tests.sh nodejs-low-test: - name: Node.js 18 test + # name: Node.js 18 test needs: affected - # permissions: - # contents: read - # # id-token: write # needed for google-github-actions/auth strategy: matrix: package: ${{ fromJson(needs.affected.outputs.nodejs) }} @@ -82,26 +79,20 @@ jobs: node-version: 18 timeout-minutes: 15 - # nodejs-high-test: - # name: Node.js 22 test - # needs: affected - # runs-on: ubuntu-latest - # permissions: - # contents: read # needed to run local actions - # # id-token: write # needed for google-github-actions/auth - # strategy: - # matrix: - # package: ${{ fromJson(needs.affected.outputs.nodejs) }} - # exclude: - # - package: '.' - # fail-fast: false # TODO: remove this, only for testing! - # steps: - # - uses: actions/checkout@v4 - # - uses: ./.github/workflows/test-nodejs.yaml - # with: - # path: ${{ matrix.package }} - # node-version: 22 - # timeout-minutes: 15 + nodejs-high-test: + name: Node.js 22 test + needs: affected + strategy: + matrix: + package: ${{ fromJson(needs.affected.outputs.nodejs) }} + exclude: + - package: '.' + fail-fast: false # TODO: remove this, only for testing! + uses: ./.github/workflows/test-nodejs.yaml + with: + path: ${{ matrix.package }} + node-version: 22 + timeout-minutes: 15 # nodejs-e2e-test: # name: Node.js e2e-test diff --git a/.github/workflows/test-nodejs.yaml b/.github/workflows/test-nodejs.yaml index f0821028d2..9af049bb36 100644 --- a/.github/workflows/test-nodejs.yaml +++ b/.github/workflows/test-nodejs.yaml @@ -27,6 +27,7 @@ on: jobs: test: + name: Node.js test ${{ inputs.node-version }} runs-on: ubuntu-latest timeout-minutes: ${{ inputs.timeout-minutes }} permissions: From fb8760dd39a5b92dc658f71c9171db3ca677c796 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 15:04:31 -0700 Subject: [PATCH 042/127] change names --- .github/workflows/on-pull-request.yaml | 4 ++-- .github/workflows/test-nodejs.yaml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 2d85972ff6..89e6ced0f2 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -65,7 +65,7 @@ jobs: - run: ./.github/workflows/utils/region-tags-tests.sh nodejs-low-test: - # name: Node.js 18 test + name: Node.js low test needs: affected strategy: matrix: @@ -80,7 +80,7 @@ jobs: timeout-minutes: 15 nodejs-high-test: - name: Node.js 22 test + name: Node.js high test needs: affected strategy: matrix: diff --git a/.github/workflows/test-nodejs.yaml b/.github/workflows/test-nodejs.yaml index 9af049bb36..dcd252452c 100644 --- a/.github/workflows/test-nodejs.yaml +++ b/.github/workflows/test-nodejs.yaml @@ -27,7 +27,7 @@ on: jobs: test: - name: Node.js test ${{ inputs.node-version }} + name: Test Node.js ${{ inputs.node-version }} runs-on: ubuntu-latest timeout-minutes: ${{ inputs.timeout-minutes }} permissions: From 6815794d36864bf43855d69b059aff3bb14b6a58 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 15:07:56 -0700 Subject: [PATCH 043/127] more explicit names --- .github/workflows/on-pull-request.yaml | 8 ++++---- .github/workflows/test-nodejs.yaml | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 89e6ced0f2..9298331c8d 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -64,8 +64,8 @@ jobs: - run: make lint - run: ./.github/workflows/utils/region-tags-tests.sh - nodejs-low-test: - name: Node.js low test + nodejs-test-old: + name: Node.js test needs: affected strategy: matrix: @@ -79,8 +79,8 @@ jobs: node-version: 18 timeout-minutes: 15 - nodejs-high-test: - name: Node.js high test + nodejs-test-new: + name: Node.js test needs: affected strategy: matrix: diff --git a/.github/workflows/test-nodejs.yaml b/.github/workflows/test-nodejs.yaml index dcd252452c..10ea78c446 100644 --- a/.github/workflows/test-nodejs.yaml +++ b/.github/workflows/test-nodejs.yaml @@ -27,11 +27,10 @@ on: jobs: test: - name: Test Node.js ${{ inputs.node-version }} + name: version ${{ inputs.node-version }} runs-on: ubuntu-latest timeout-minutes: ${{ inputs.timeout-minutes }} permissions: - # contents: read id-token: write # needed for google-github-actions/auth steps: - uses: actions/checkout@v4 From 42b40c91b570a773772684a0da6857e95a4e315f Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 15:10:17 -0700 Subject: [PATCH 044/127] only show version number --- .github/workflows/test-nodejs.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-nodejs.yaml b/.github/workflows/test-nodejs.yaml index 10ea78c446..87b1da93e8 100644 --- a/.github/workflows/test-nodejs.yaml +++ b/.github/workflows/test-nodejs.yaml @@ -27,7 +27,7 @@ on: jobs: test: - name: version ${{ inputs.node-version }} + name: ${{ inputs.node-version }} runs-on: ubuntu-latest timeout-minutes: ${{ inputs.timeout-minutes }} permissions: From 4ec1a19f6789d2745c678388d59e8558f8dac3c1 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 15:12:47 -0700 Subject: [PATCH 045/127] test all --- .github/workflows/on-pull-request.yaml | 28 +++++++++---------- .../samples-tools/cmd/affected/main.go | 1 - 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index 9298331c8d..c848afc0a8 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -64,20 +64,20 @@ jobs: - run: make lint - run: ./.github/workflows/utils/region-tags-tests.sh - nodejs-test-old: - name: Node.js test - needs: affected - strategy: - matrix: - package: ${{ fromJson(needs.affected.outputs.nodejs) }} - exclude: - - package: '.' - fail-fast: false # TODO: remove this, only for testing! - uses: ./.github/workflows/test-nodejs.yaml - with: - path: ${{ matrix.package }} - node-version: 18 - timeout-minutes: 15 + # nodejs-test-old: + # name: Node.js test + # needs: affected + # strategy: + # matrix: + # package: ${{ fromJson(needs.affected.outputs.nodejs) }} + # exclude: + # - package: '.' + # fail-fast: false # TODO: remove this, only for testing! + # uses: ./.github/workflows/test-nodejs.yaml + # with: + # path: ${{ matrix.package }} + # node-version: 18 + # timeout-minutes: 15 nodejs-test-new: name: Node.js test diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index 65843fcb9b..f472f22021 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -54,7 +54,6 @@ func affected(config utils.Config, diffs []string) ([]string, error) { changed = append(changed, pkg) } - return changed, nil // TODO: REMOVE THIS if !slices.Contains(changed, ".") { return changed, nil } From 9e3bc3e6b9e888d0a6d817b49d6ec41dd603c84c Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 15:26:15 -0700 Subject: [PATCH 046/127] use node 20 --- .../{test-nodejs.yaml => nodejs-test.yaml} | 5 +---- .github/workflows/on-pull-request.yaml | 14 ++++++-------- 2 files changed, 7 insertions(+), 12 deletions(-) rename .github/workflows/{test-nodejs.yaml => nodejs-test.yaml} (92%) diff --git a/.github/workflows/test-nodejs.yaml b/.github/workflows/nodejs-test.yaml similarity index 92% rename from .github/workflows/test-nodejs.yaml rename to .github/workflows/nodejs-test.yaml index 87b1da93e8..ed5ccc5a9d 100644 --- a/.github/workflows/test-nodejs.yaml +++ b/.github/workflows/nodejs-test.yaml @@ -21,15 +21,12 @@ on: node-version: required: true type: number - timeout-minutes: - required: true - type: number jobs: test: name: ${{ inputs.node-version }} runs-on: ubuntu-latest - timeout-minutes: ${{ inputs.timeout-minutes }} + timeout-minutes: 15 permissions: id-token: write # needed for google-github-actions/auth steps: diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/on-pull-request.yaml index c848afc0a8..92b18e1ac7 100644 --- a/.github/workflows/on-pull-request.yaml +++ b/.github/workflows/on-pull-request.yaml @@ -60,11 +60,11 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: 22 + node-version: 20 - run: make lint - run: ./.github/workflows/utils/region-tags-tests.sh - # nodejs-test-old: + # nodejs-test: # name: Node.js test # needs: affected # strategy: @@ -73,13 +73,12 @@ jobs: # exclude: # - package: '.' # fail-fast: false # TODO: remove this, only for testing! - # uses: ./.github/workflows/test-nodejs.yaml + # uses: ./.github/workflows/nodejs-test.yaml # with: # path: ${{ matrix.package }} # node-version: 18 - # timeout-minutes: 15 - nodejs-test-new: + nodejs-test-latest: name: Node.js test needs: affected strategy: @@ -88,11 +87,10 @@ jobs: exclude: - package: '.' fail-fast: false # TODO: remove this, only for testing! - uses: ./.github/workflows/test-nodejs.yaml + uses: ./.github/workflows/nodejs-test.yaml with: path: ${{ matrix.package }} - node-version: 22 - timeout-minutes: 15 + node-version: 20 # nodejs-e2e-test: # name: Node.js e2e-test From 04448a6057336986e6148678a07cf3e93f8dcfbe Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 15:34:04 -0700 Subject: [PATCH 047/127] create service account file --- .github/workflows/{on-pull-request.yaml => experimental.yaml} | 0 .github/workflows/nodejs-test.yaml | 1 + 2 files changed, 1 insertion(+) rename .github/workflows/{on-pull-request.yaml => experimental.yaml} (100%) diff --git a/.github/workflows/on-pull-request.yaml b/.github/workflows/experimental.yaml similarity index 100% rename from .github/workflows/on-pull-request.yaml rename to .github/workflows/experimental.yaml diff --git a/.github/workflows/nodejs-test.yaml b/.github/workflows/nodejs-test.yaml index ed5ccc5a9d..a60139f889 100644 --- a/.github/workflows/nodejs-test.yaml +++ b/.github/workflows/nodejs-test.yaml @@ -39,6 +39,7 @@ jobs: project_id: long-door-651 workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com + create_credentials_file: true access_token_lifetime: 300s # 5 minutes - run: make test dir=${{ inputs.path }} env: From 48cf1e622fcd2a7af08a9c40172f0e2e18f39ac4 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 15:38:33 -0700 Subject: [PATCH 048/127] adjust token lifetime --- .github/workflows/nodejs-test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nodejs-test.yaml b/.github/workflows/nodejs-test.yaml index a60139f889..120a0587a6 100644 --- a/.github/workflows/nodejs-test.yaml +++ b/.github/workflows/nodejs-test.yaml @@ -40,7 +40,7 @@ jobs: workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com create_credentials_file: true - access_token_lifetime: 300s # 5 minutes + access_token_lifetime: 600s # 10 minutes - run: make test dir=${{ inputs.path }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 From 88e2d387811775a71c7626207f5d9797dd735dca Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 15:42:31 -0700 Subject: [PATCH 049/127] lower node version --- .github/workflows/experimental.yaml | 2 +- .github/workflows/nodejs-test.yaml | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 92b18e1ac7..ec5151c117 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -60,7 +60,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: 20 + node-version: 16 - run: make lint - run: ./.github/workflows/utils/region-tags-tests.sh diff --git a/.github/workflows/nodejs-test.yaml b/.github/workflows/nodejs-test.yaml index 120a0587a6..52e106d3ac 100644 --- a/.github/workflows/nodejs-test.yaml +++ b/.github/workflows/nodejs-test.yaml @@ -31,6 +31,8 @@ jobs: id-token: write # needed for google-github-actions/auth steps: - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} - uses: actions/setup-node@v4 with: node-version: ${{ inputs.node-version }} From c21429e6dc0f3423538900badf058c75077ab306 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 15:43:48 -0700 Subject: [PATCH 050/127] do not test all --- .github/workflows/samples-tools/cmd/affected/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index f472f22021..65843fcb9b 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -54,6 +54,7 @@ func affected(config utils.Config, diffs []string) ([]string, error) { changed = append(changed, pkg) } + return changed, nil // TODO: REMOVE THIS if !slices.Contains(changed, ".") { return changed, nil } From 153c94c8984739a75a335593675fe89ee494f1eb Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 15:45:31 -0700 Subject: [PATCH 051/127] use node 16 --- .github/workflows/experimental.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index ec5151c117..f3b7608ac2 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -60,7 +60,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: - node-version: 16 + node-version: 20 - run: make lint - run: ./.github/workflows/utils/region-tags-tests.sh @@ -90,7 +90,7 @@ jobs: uses: ./.github/workflows/nodejs-test.yaml with: path: ${{ matrix.package }} - node-version: 20 + node-version: 16 # nodejs-e2e-test: # name: Node.js e2e-test From b236312cf6485bb5a679e53fcaa5baaff85d0b1e Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 15:46:35 -0700 Subject: [PATCH 052/127] use node 20 --- .github/workflows/experimental.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index f3b7608ac2..92b18e1ac7 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -90,7 +90,7 @@ jobs: uses: ./.github/workflows/nodejs-test.yaml with: path: ${{ matrix.package }} - node-version: 16 + node-version: 20 # nodejs-e2e-test: # name: Node.js e2e-test From c1f73002cbdde00570c48524d5987478ac3083db Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 24 Sep 2024 15:49:49 -0700 Subject: [PATCH 053/127] restore language test --- .github/workflows/cloud-language.yaml | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cloud-language.yaml b/.github/workflows/cloud-language.yaml index 802c993fa2..4f9a8bd250 100644 --- a/.github/workflows/cloud-language.yaml +++ b/.github/workflows/cloud-language.yaml @@ -14,13 +14,31 @@ name: cloud-language on: + push: + branches: + - main + paths: + - 'cloud-language/**' + - '.github/workflows/cloud-language.yaml' + - '.github/workflows/test.yaml' + pull_request: + types: + - opened + - reopened + - synchronize + - labeled + paths: + - 'cloud-language/**' + - '.github/workflows/cloud-language.yaml' + - '.github/workflows/test.yaml' schedule: - - cron: '0 0 * * 0' + - cron: '0 0 * * 0' jobs: test: permissions: contents: 'read' id-token: 'write' + if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' uses: ./.github/workflows/test.yaml with: name: 'cloud-language' From bc916202f2df4b2ddaf549e5bcde9d3f3deeb76d Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 25 Sep 2024 14:36:38 -0700 Subject: [PATCH 054/127] add install and more comments --- Makefile | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 711c1b09bb..076f8efda1 100644 --- a/Makefile +++ b/Makefile @@ -12,10 +12,15 @@ dir ?= $(shell pwd) export GOOGLE_CLOUD_PROJECT = ${GOOGLE_SAMPLES_PROJECT} -build: +install: + # Install root package dependencies, this incldues packages for testing. npm install + + # Install the code sample dependencies. cd ${dir} npm install + +build: install npm run build --if-present test: check-env build @@ -26,10 +31,7 @@ e2e-test: check-env build cd ${dir} npm run system-test -lint: - npm install - cd ${dir} - npm install +lint: install npx gts fix npx gts lint From 4f8a45b75064b5e528e2001c35be061166bc84b1 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 25 Sep 2024 14:38:49 -0700 Subject: [PATCH 055/127] assign and print variables in the same line --- .github/workflows/experimental.yaml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 92b18e1ac7..171cd17b58 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -44,13 +44,11 @@ jobs: - id: nodejs run: | PACKAGES=$(affected .github/workflows/config/nodejs.json) - echo "packages=$PACKAGES" - echo "packages=$PACKAGES " >> "$GITHUB_OUTPUT" + echo "packages=$PACKAGES" | tee -a $GITHUB_OUTPUT - id: nodejs-e2e run: | PACKAGES=$(affected .github/workflows/config/nodejs-e2e.json) - echo "packages=$PACKAGES" - echo "packages=$PACKAGES " >> "$GITHUB_OUTPUT" + echo "packages=$PACKAGES" | tee -a $GITHUB_OUTPUT nodejs-lint: name: Node.js lint From 4511504821bf4d2fa288e8f4761aeea980766439 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 26 Sep 2024 11:49:12 -0700 Subject: [PATCH 056/127] change to trigger tests --- .github/workflows/experimental.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 171cd17b58..c3a88cc80d 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -90,6 +90,7 @@ jobs: path: ${{ matrix.package }} node-version: 20 + # nodejs-e2e-test: # name: Node.js e2e-test # needs: affected From 8d169b5ce860b30b3231f49b4e5d86bdcaedcef2 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 26 Sep 2024 12:27:22 -0700 Subject: [PATCH 057/127] test all --- .github/workflows/experimental.yaml | 1 - .github/workflows/nodejs-test.yaml | 1 - .github/workflows/samples-tools/cmd/affected/main.go | 1 - 3 files changed, 3 deletions(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index c3a88cc80d..171cd17b58 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -90,7 +90,6 @@ jobs: path: ${{ matrix.package }} node-version: 20 - # nodejs-e2e-test: # name: Node.js e2e-test # needs: affected diff --git a/.github/workflows/nodejs-test.yaml b/.github/workflows/nodejs-test.yaml index 52e106d3ac..35e32cfbbf 100644 --- a/.github/workflows/nodejs-test.yaml +++ b/.github/workflows/nodejs-test.yaml @@ -41,7 +41,6 @@ jobs: project_id: long-door-651 workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com - create_credentials_file: true access_token_lifetime: 600s # 10 minutes - run: make test dir=${{ inputs.path }} env: diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index 65843fcb9b..f472f22021 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -54,7 +54,6 @@ func affected(config utils.Config, diffs []string) ([]string, error) { changed = append(changed, pkg) } - return changed, nil // TODO: REMOVE THIS if !slices.Contains(changed, ".") { return changed, nil } From a818e6e8b29db30a1f77fb40e766be73494ec6d2 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 26 Sep 2024 13:15:15 -0700 Subject: [PATCH 058/127] exclude special tests --- .github/workflows/experimental.yaml | 45 +++++++++++++++++++ .../samples-tools/cmd/affected/main.go | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 171cd17b58..bd7bb9ba8e 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -84,6 +84,51 @@ jobs: package: ${{ fromJson(needs.affected.outputs.nodejs) }} exclude: - package: '.' + - package: ai-platform/snippets # PERMISSION_DENIED: Permission denied: Consumer 'projects/undefined' has been suspended. + - package: appengine/analytics # [ERR_REQUIRE_ESM]: require() of ES Module + - package: appengine/metadata/flexible # [ERR_REQUIRE_ESM]: require() of ES Module + - package: appengine/metadata/standard # [ERR_REQUIRE_ESM]: require() of ES Module + - package: automl # FAILED_PRECONDITION: Google Cloud AutoML Natural Language was retired on March 15, 2024. Please migrate to Vertex AI instead + - package: cloud-sql/mysql/mysql # Error: expected 200 "OK", got 500 "Internal Server Error" + - package: cloud-sql/mysql/mysql2 # Error: Cannot find module './connect-connector-with-iam-authn.js' + - package: cloud-sql/postgres/knex # CloudSQLConnectorError: Malformed instance connection name provided: expected format of "PROJECT:REGION:INSTANCE", got undefined + - package: cloud-sql/sqlserver/mssql # TypeError: The "config.server" property is required and must be of type string. + - package: cloud-sql/sqlserver/tedious # TypeError: The "config.server" property is required and must be of type string. + - package: compute # GoogleError: The resource 'projects/long-door-651/zones/us-central1-a/disks/disk-from-pool-name' was not found + - package: dataproc # GoogleError: Error submitting create cluster request: Multiple validation errors + - package: datastore/functions # [ERR_REQUIRE_ESM]: require() of ES Module + - package: dialogflow-cx # NOT_FOUND: com.google.apps.framework.request.NotFoundException: Agent 'undefined' does not exist + - package: dlp # [ERR_REQUIRE_ESM]: require() of ES Module + - package: document-ai # [ERR_REQUIRE_ESM]: require() of ES Module + - package: eventarc/audit-storage # Environment Variable 'SERVICE_NAME' not found + - package: eventarc/pubsub # Environment Variable 'SERVICE_NAME' not found + - package: functions/concepts # npm error Missing script: "test" + - package: functions/firebase # npm error Missing script: "test" + - package: functions/helloworld # npm error Missing script: "test" + - package: functions/http # npm error Missing script: "test" + - package: functions/imagemagick # Error: A bucket name is needed to use Cloud Storage + - package: functions/log # npm error Missing script: "test" + - package: functions/ocr/app # Error: Bucket not provided. Make sure you have a "bucket" property in your request + - package: functions/pubsub # npm error Missing script: "test" + - package: functions/slack # TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of type ... Received undefined + - package: functions/v2/imagemagick # Error: A bucket name is needed to use Cloud Storage. + - package: generative-ai/snippets # [VertexAI.ClientError]: got status: 403 Forbidden. + - package: healthcare/consent # GaxiosError: dataset not initialized + - package: healthcare/dicom # GaxiosError: dataset not initialized + - package: healthcare/fhir # Error: Cannot find module 'whatwg-url' + - package: healthcare/hl7v2 # Error: Cannot find module 'whatwg-url' + - package: iam/deny # PERMISSION_DENIED: Permission iam.googleapis.com/denypolicies.create denied on resource cloudresourcemanager.googleapis.com/projects/long-door-651 + - package: memorystore/redis # npm error Missing script: "test" + - package: recaptcha_enterprise/demosite/app # Error: no test specified + - package: recaptcha_enterprise/snippets # Cannot use import statement outside a module + - package: run/idp-sql # Error: Invalid contents in the credentials file + - package: run/markdown-preview/editor # Error: could not create an identity token: Cannot fetch ID token in this environment, use GCE or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to a service account credentials JSON file + - package: run/system-package # Error: ENOENT: no such file or directory, access '/usr/bin/dot' + - package: scheduler # SyntaxError: Cannot use import statement outside a module + - package: storagetransfer # CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1 + - package: video-intelligence # PERMISSION_DENIED: The caller does not have permission + - package: vision # REDIS: Error: connect ECONNREFUSED 127.0.0.1:6379 + - package: workflows # SyntaxError: Cannot use import statement outside a module fail-fast: false # TODO: remove this, only for testing! uses: ./.github/workflows/nodejs-test.yaml with: diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index f472f22021..01505900da 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -69,7 +69,7 @@ func affected(config utils.Config, diffs []string) ([]string, error) { } if d.IsDir() && config.Matches(path) && config.IsPackageDir(path) { packages = append(packages, path) - return filepath.SkipDir + return nil } return nil }) From f451f211dd08a505bffecf813c82bdeef2e4c95e Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 26 Sep 2024 13:35:44 -0700 Subject: [PATCH 059/127] more packages to exclude --- .github/workflows/experimental.yaml | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index bd7bb9ba8e..c54597ba1e 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -62,21 +62,7 @@ jobs: - run: make lint - run: ./.github/workflows/utils/region-tags-tests.sh - # nodejs-test: - # name: Node.js test - # needs: affected - # strategy: - # matrix: - # package: ${{ fromJson(needs.affected.outputs.nodejs) }} - # exclude: - # - package: '.' - # fail-fast: false # TODO: remove this, only for testing! - # uses: ./.github/workflows/nodejs-test.yaml - # with: - # path: ${{ matrix.package }} - # node-version: 18 - - nodejs-test-latest: + nodejs-test: name: Node.js test needs: affected strategy: @@ -102,10 +88,12 @@ jobs: - package: document-ai # [ERR_REQUIRE_ESM]: require() of ES Module - package: eventarc/audit-storage # Environment Variable 'SERVICE_NAME' not found - package: eventarc/pubsub # Environment Variable 'SERVICE_NAME' not found + - package: functions/billing # Error: Request failed with status code 500 - package: functions/concepts # npm error Missing script: "test" - package: functions/firebase # npm error Missing script: "test" - package: functions/helloworld # npm error Missing script: "test" - package: functions/http # npm error Missing script: "test" + - package: functions/http/uploadFile # npm error Missing script: "test" - package: functions/imagemagick # Error: A bucket name is needed to use Cloud Storage - package: functions/log # npm error Missing script: "test" - package: functions/ocr/app # Error: Bucket not provided. Make sure you have a "bucket" property in your request @@ -129,6 +117,7 @@ jobs: - package: video-intelligence # PERMISSION_DENIED: The caller does not have permission - package: vision # REDIS: Error: connect ECONNREFUSED 127.0.0.1:6379 - package: workflows # SyntaxError: Cannot use import statement outside a module + - package: workflows/quickstart # [ERR_MODULE_NOT_FOUND]: Cannot find package 'ts-node' imported from ... fail-fast: false # TODO: remove this, only for testing! uses: ./.github/workflows/nodejs-test.yaml with: From 1262f3e581f2e9c749f681749920b7eef1d87105 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 30 Sep 2024 13:16:34 -0700 Subject: [PATCH 060/127] support exclude-packages and json comments --- .github/workflows/config/nodejs.json | 52 +++++++++++++++++- .github/workflows/experimental.yaml | 53 +------------------ .../samples-tools/cmd/affected/main.go | 26 +++++---- .../samples-tools/pkg/utils/config.go | 17 ++++-- 4 files changed, 83 insertions(+), 65 deletions(-) diff --git a/.github/workflows/config/nodejs.json b/.github/workflows/config/nodejs.json index 7b36631866..e7229fe858 100644 --- a/.github/workflows/config/nodejs.json +++ b/.github/workflows/config/nodejs.json @@ -1,4 +1,54 @@ { "ignore": ["README.md", "node_modules/"], - "package": ["package.json"] + "package": ["package.json"], + "exclude-packages": [ + "ai-platform/snippets", // PERMISSION_DENIED: Permission denied: Consumer 'projects/undefined' has been suspended. + "appengine/analytics", // [ERR_REQUIRE_ESM]: require() of ES Module + "appengine/metadata/flexible", // [ERR_REQUIRE_ESM]: require() of ES Module + "appengine/metadata/standard", // [ERR_REQUIRE_ESM]: require() of ES Module + "automl", // FAILED_PRECONDITION: Google Cloud AutoML Natural Language was retired on March 15, 2024. Please migrate to Vertex AI instead + "cloud-sql/mysql/mysql", // Error: expected 200 "OK", got 500 "Internal Server Error" + "cloud-sql/mysql/mysql2", // Error: Cannot find module './connect-connector-with-iam-authn.js' + "cloud-sql/postgres/knex", // CloudSQLConnectorError: Malformed instance connection name provided: expected format of "PROJECT:REGION:INSTANCE", got undefined + "cloud-sql/sqlserver/mssql", // TypeError: The "config.server" property is required and must be of type string. + "cloud-sql/sqlserver/tedious", // TypeError: The "config.server" property is required and must be of type string. + "compute", // GoogleError: The resource 'projects/long-door-651/zones/us-central1-a/disks/disk-from-pool-name' was not found + "dataproc", // GoogleError: Error submitting create cluster request: Multiple validation errors + "datastore/functions", // [ERR_REQUIRE_ESM]: require() of ES Module + "dialogflow-cx", // NOT_FOUND: com.google.apps.framework.request.NotFoundException: Agent 'undefined' does not exist + "dlp", // [ERR_REQUIRE_ESM]: require() of ES Module + "document-ai", // [ERR_REQUIRE_ESM]: require() of ES Module + "eventarc/audit-storage", // Environment Variable 'SERVICE_NAME' not found + "eventarc/pubsub", // Environment Variable 'SERVICE_NAME' not found + "functions/billing", // Error: Request failed with status code 500 + "functions/concepts", // npm error Missing script: "test" + "functions/firebase", // npm error Missing script: "test" + "functions/helloworld", // npm error Missing script: "test" + "functions/http", // npm error Missing script: "test" + "functions/http/uploadFile", // npm error Missing script: "test" + "functions/imagemagick", // Error: A bucket name is needed to use Cloud Storage + "functions/log", // npm error Missing script: "test" + "functions/ocr/app", // Error: Bucket not provided. Make sure you have a "bucket" property in your request + "functions/pubsub", // npm error Missing script: "test" + "functions/slack", // TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of type ... Received undefined + "functions/v2/imagemagick", // Error: A bucket name is needed to use Cloud Storage. + "generative-ai/snippets", // [VertexAI.ClientError]: got status: 403 Forbidden. + "healthcare/consent", // GaxiosError: dataset not initialized + "healthcare/dicom", // GaxiosError: dataset not initialized + "healthcare/fhir", // Error: Cannot find module 'whatwg-url' + "healthcare/hl7v2", // Error: Cannot find module 'whatwg-url' + "iam/deny", // PERMISSION_DENIED: Permission iam.googleapis.com/denypolicies.create denied on resource cloudresourcemanager.googleapis.com/projects/long-door-651 + "memorystore/redis", // npm error Missing script: "test" + "recaptcha_enterprise/demosite/app", // Error: no test specified + "recaptcha_enterprise/snippets", // Cannot use import statement outside a module + "run/idp-sql", // Error: Invalid contents in the credentials file + "run/markdown-preview/editor", // Error: could not create an identity token: Cannot fetch ID token in this environment, use GCE or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to a service account credentials JSON file + "run/system-package", // Error: ENOENT: no such file or directory, access '/usr/bin/dot' + "scheduler", // SyntaxError: Cannot use import statement outside a module + "storagetransfer", // CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1 + "video-intelligence", // PERMISSION_DENIED: The caller does not have permission + "vision", // REDIS: Error: connect ECONNREFUSED 127.0.0.1:6379 + "workflows", // SyntaxError: Cannot use import statement outside a module + "workflows/quickstart" // [ERR_MODULE_NOT_FOUND]: Cannot find package 'ts-node' imported from ... + ] } diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index c54597ba1e..89e75cd13a 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -43,11 +43,11 @@ jobs: working-directory: .github/workflows/samples-tools - id: nodejs run: | - PACKAGES=$(affected .github/workflows/config/nodejs.json) + PACKAGES=$(affected .github/workflows/config/nodejs.jsonc) echo "packages=$PACKAGES" | tee -a $GITHUB_OUTPUT - id: nodejs-e2e run: | - PACKAGES=$(affected .github/workflows/config/nodejs-e2e.json) + PACKAGES=$(affected .github/workflows/config/nodejs-e2e.jsonc) echo "packages=$PACKAGES" | tee -a $GITHUB_OUTPUT nodejs-lint: @@ -70,55 +70,6 @@ jobs: package: ${{ fromJson(needs.affected.outputs.nodejs) }} exclude: - package: '.' - - package: ai-platform/snippets # PERMISSION_DENIED: Permission denied: Consumer 'projects/undefined' has been suspended. - - package: appengine/analytics # [ERR_REQUIRE_ESM]: require() of ES Module - - package: appengine/metadata/flexible # [ERR_REQUIRE_ESM]: require() of ES Module - - package: appengine/metadata/standard # [ERR_REQUIRE_ESM]: require() of ES Module - - package: automl # FAILED_PRECONDITION: Google Cloud AutoML Natural Language was retired on March 15, 2024. Please migrate to Vertex AI instead - - package: cloud-sql/mysql/mysql # Error: expected 200 "OK", got 500 "Internal Server Error" - - package: cloud-sql/mysql/mysql2 # Error: Cannot find module './connect-connector-with-iam-authn.js' - - package: cloud-sql/postgres/knex # CloudSQLConnectorError: Malformed instance connection name provided: expected format of "PROJECT:REGION:INSTANCE", got undefined - - package: cloud-sql/sqlserver/mssql # TypeError: The "config.server" property is required and must be of type string. - - package: cloud-sql/sqlserver/tedious # TypeError: The "config.server" property is required and must be of type string. - - package: compute # GoogleError: The resource 'projects/long-door-651/zones/us-central1-a/disks/disk-from-pool-name' was not found - - package: dataproc # GoogleError: Error submitting create cluster request: Multiple validation errors - - package: datastore/functions # [ERR_REQUIRE_ESM]: require() of ES Module - - package: dialogflow-cx # NOT_FOUND: com.google.apps.framework.request.NotFoundException: Agent 'undefined' does not exist - - package: dlp # [ERR_REQUIRE_ESM]: require() of ES Module - - package: document-ai # [ERR_REQUIRE_ESM]: require() of ES Module - - package: eventarc/audit-storage # Environment Variable 'SERVICE_NAME' not found - - package: eventarc/pubsub # Environment Variable 'SERVICE_NAME' not found - - package: functions/billing # Error: Request failed with status code 500 - - package: functions/concepts # npm error Missing script: "test" - - package: functions/firebase # npm error Missing script: "test" - - package: functions/helloworld # npm error Missing script: "test" - - package: functions/http # npm error Missing script: "test" - - package: functions/http/uploadFile # npm error Missing script: "test" - - package: functions/imagemagick # Error: A bucket name is needed to use Cloud Storage - - package: functions/log # npm error Missing script: "test" - - package: functions/ocr/app # Error: Bucket not provided. Make sure you have a "bucket" property in your request - - package: functions/pubsub # npm error Missing script: "test" - - package: functions/slack # TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of type ... Received undefined - - package: functions/v2/imagemagick # Error: A bucket name is needed to use Cloud Storage. - - package: generative-ai/snippets # [VertexAI.ClientError]: got status: 403 Forbidden. - - package: healthcare/consent # GaxiosError: dataset not initialized - - package: healthcare/dicom # GaxiosError: dataset not initialized - - package: healthcare/fhir # Error: Cannot find module 'whatwg-url' - - package: healthcare/hl7v2 # Error: Cannot find module 'whatwg-url' - - package: iam/deny # PERMISSION_DENIED: Permission iam.googleapis.com/denypolicies.create denied on resource cloudresourcemanager.googleapis.com/projects/long-door-651 - - package: memorystore/redis # npm error Missing script: "test" - - package: recaptcha_enterprise/demosite/app # Error: no test specified - - package: recaptcha_enterprise/snippets # Cannot use import statement outside a module - - package: run/idp-sql # Error: Invalid contents in the credentials file - - package: run/markdown-preview/editor # Error: could not create an identity token: Cannot fetch ID token in this environment, use GCE or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to a service account credentials JSON file - - package: run/system-package # Error: ENOENT: no such file or directory, access '/usr/bin/dot' - - package: scheduler # SyntaxError: Cannot use import statement outside a module - - package: storagetransfer # CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1 - - package: video-intelligence # PERMISSION_DENIED: The caller does not have permission - - package: vision # REDIS: Error: connect ECONNREFUSED 127.0.0.1:6379 - - package: workflows # SyntaxError: Cannot use import statement outside a module - - package: workflows/quickstart # [ERR_MODULE_NOT_FOUND]: Cannot find package 'ts-node' imported from ... - fail-fast: false # TODO: remove this, only for testing! uses: ./.github/workflows/nodejs-test.yaml with: path: ${{ matrix.package }} diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index 01505900da..46e9c03937 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -26,17 +26,20 @@ func main() { panic(err) } - matrix, err := affected(config, diffs) + packages, err := affected(config, diffs) if err != nil { panic(err) } + if len(packages) > 256 { + panic(fmt.Errorf("GitHub Actions only supports up to 256 packages, got %d packages", len(packages))) + } - matrixJson, err := json.Marshal(matrix) + matrix, err := json.Marshal(packages) if err != nil { panic(err) } - fmt.Println(string(matrixJson)) + fmt.Println(string(matrix)) } func affected(config utils.Config, diffs []string) ([]string, error) { @@ -46,6 +49,9 @@ func affected(config utils.Config, diffs []string) ([]string, error) { continue } pkg := config.FindPackage(diff) + if slices.Contains(config.ExcludePackages, pkg) { + continue + } uniquePackages[pkg] = true } @@ -54,10 +60,13 @@ func affected(config utils.Config, diffs []string) ([]string, error) { changed = append(changed, pkg) } - if !slices.Contains(changed, ".") { - return changed, nil + if slices.Contains(changed, ".") { + return findAllPackages(config) } + return changed, nil +} +func findAllPackages(config utils.Config) ([]string, error) { var packages []string err := filepath.WalkDir(".", func(path string, d os.DirEntry, err error) error { @@ -67,6 +76,9 @@ func affected(config utils.Config, diffs []string) ([]string, error) { if path == "." { return nil } + if slices.Contains(config.ExcludePackages, path) { + return nil + } if d.IsDir() && config.Matches(path) && config.IsPackageDir(path) { packages = append(packages, path) return nil @@ -76,9 +88,5 @@ func affected(config utils.Config, diffs []string) ([]string, error) { if err != nil { return []string{}, err } - - if len(packages) > 256 { - return []string{}, fmt.Errorf("GitHub Actions only supports up to 256 packages, got %d packages", len(packages)) - } return packages, nil } diff --git a/.github/workflows/samples-tools/pkg/utils/config.go b/.github/workflows/samples-tools/pkg/utils/config.go index 51c3d58bb0..9855b7d4ff 100644 --- a/.github/workflows/samples-tools/pkg/utils/config.go +++ b/.github/workflows/samples-tools/pkg/utils/config.go @@ -5,19 +5,23 @@ import ( "errors" "os" "path/filepath" + "regexp" "strings" ) type Config struct { - // Filenames to ignore, can include wildcard `*` stars. + // Pattern to match filenames or directories. Match []string `json:"match"` - // Filenames to ignore, can include wildcard `*` stars. + // Pattern to ignore filenames or directories. Ignore []string `json:"ignore"` // Filename to look for the root of a package. Package []string `json:"package"` + // Packages to always exclude. + ExcludePackages []string `json:"exclude-packages"` + // Actions to run including the given commands. Actions map[string][]struct { Command string `json:"command"` @@ -37,9 +41,9 @@ func LoadConfig(path string) (Config, error) { return config, nil } -func ParseConfig(configFile []byte) (Config, error) { +func ParseConfig(source []byte) (Config, error) { var config Config - err := json.Unmarshal(configFile, &config) + err := json.Unmarshal(StripComments(source), &config) if err != nil { return Config{}, err } @@ -49,6 +53,11 @@ func ParseConfig(configFile []byte) (Config, error) { return config, nil } +func StripComments(src []byte) []byte { + re := regexp.MustCompile(`\s*// .*`) + return re.ReplaceAll(src, []byte{}) +} + func match(patterns []string, path string) bool { filename := filepath.Base(path) for _, pattern := range patterns { From 69192c83cbcb77766557ba4de4b0fab2e452eb24 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 30 Sep 2024 13:23:37 -0700 Subject: [PATCH 061/127] use jsonc extension --- .github/workflows/config/{nodejs-e2e.json => nodejs-e2e.jsonc} | 0 .github/workflows/config/{nodejs.json => nodejs.jsonc} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/config/{nodejs-e2e.json => nodejs-e2e.jsonc} (100%) rename .github/workflows/config/{nodejs.json => nodejs.jsonc} (100%) diff --git a/.github/workflows/config/nodejs-e2e.json b/.github/workflows/config/nodejs-e2e.jsonc similarity index 100% rename from .github/workflows/config/nodejs-e2e.json rename to .github/workflows/config/nodejs-e2e.jsonc diff --git a/.github/workflows/config/nodejs.json b/.github/workflows/config/nodejs.jsonc similarity index 100% rename from .github/workflows/config/nodejs.json rename to .github/workflows/config/nodejs.jsonc From 5b3f469b613a9e6357e3eec283f332c1a22c5f59 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 30 Sep 2024 13:41:15 -0700 Subject: [PATCH 062/127] use flag package --- .../samples-tools/cmd/affected/main.go | 17 ++++++--- .../workflows/samples-tools/pkg/utils/args.go | 37 ------------------- 2 files changed, 11 insertions(+), 43 deletions(-) delete mode 100644 .github/workflows/samples-tools/pkg/utils/args.go diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index 46e9c03937..e8db2ac9be 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "flag" "fmt" "os" "path/filepath" @@ -11,17 +12,21 @@ import ( ) func main() { - usage := "usage: affected path/to/config.json [head-commit] [main-commit]" - configPath := utils.ArgRequired(1, usage) - headCommit := utils.ArgWithDefault(2, "HEAD") - mainCommit := utils.ArgWithDefault(3, "origin/main") + configPath := flag.String("config", "", "path to the config file") + headCommit := flag.String("head", "HEAD", "commit with the changes") + mainCommit := flag.String("main", "origin/main", "commit of the main branch") + flag.Parse() - config, err := utils.LoadConfig(configPath) + if *configPath == "" { + panic("config path is required, please pass -config=path/to/config.jsonc") + } + + config, err := utils.LoadConfig(*configPath) if err != nil { panic(err) } - diffs, err := utils.Diffs(headCommit, mainCommit) + diffs, err := utils.Diffs(*headCommit, *mainCommit) if err != nil { panic(err) } diff --git a/.github/workflows/samples-tools/pkg/utils/args.go b/.github/workflows/samples-tools/pkg/utils/args.go deleted file mode 100644 index d199109068..0000000000 --- a/.github/workflows/samples-tools/pkg/utils/args.go +++ /dev/null @@ -1,37 +0,0 @@ -package utils - -import ( - "fmt" - "os" - "strings" -) - -func ArgRequired(index int, errorMessage string) string { - if len(os.Args) <= index { - panic(errorMessage) - } - return os.Args[index] -} - -func ArgWithDefault(index int, defaultValue string) string { - if len(os.Args) <= index { - return defaultValue - } - return os.Args[index] -} - -func InterpolateArgs(args []string, values []string) []string { - var result []string - for _, arg := range args { - for argIdx, value := range values { - arg = strings.ReplaceAll(arg, fmt.Sprintf("$%d", argIdx+1), value) - } - if arg := strings.TrimSpace(strings.TrimSuffix(arg, "$@")); arg != "" { - result = append(result, arg) - } - if strings.HasSuffix(arg, "$@") { - result = append(result, values...) - } - } - return result -} From 8bb4dc5d7abd650cb2ed99c6e2d67008aa109869 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 30 Sep 2024 13:45:15 -0700 Subject: [PATCH 063/127] remove unused actions for config --- .github/workflows/samples-tools/pkg/utils/config.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/samples-tools/pkg/utils/config.go b/.github/workflows/samples-tools/pkg/utils/config.go index 9855b7d4ff..9a4d136173 100644 --- a/.github/workflows/samples-tools/pkg/utils/config.go +++ b/.github/workflows/samples-tools/pkg/utils/config.go @@ -21,12 +21,6 @@ type Config struct { // Packages to always exclude. ExcludePackages []string `json:"exclude-packages"` - - // Actions to run including the given commands. - Actions map[string][]struct { - Command string `json:"command"` - Args []string `json:"args"` - } `json:"actions"` } func LoadConfig(path string) (Config, error) { From 0928722da3320b6a7b7d67369495effc58cfc7fe Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 30 Sep 2024 13:48:25 -0700 Subject: [PATCH 064/127] restore changes --- cloud-language/quickstart.js | 2 +- run/helloworld/.gcloudignore | 2 +- run/helloworld/README.md | 20 ++++++++++---------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cloud-language/quickstart.js b/cloud-language/quickstart.js index 96a379cbf4..7eb7b5a4a2 100644 --- a/cloud-language/quickstart.js +++ b/cloud-language/quickstart.js @@ -16,7 +16,7 @@ // [START language_quickstart] async function quickstart() { - // Imports the Google Cloud client library. + // Imports the Google Cloud client library const language = require('@google-cloud/language'); // Instantiates a client diff --git a/run/helloworld/.gcloudignore b/run/helloworld/.gcloudignore index 1ca957177f..93f1361991 100644 --- a/run/helloworld/.gcloudignore +++ b/run/helloworld/.gcloudignore @@ -1,2 +1,2 @@ -node_modules/ +node_modules npm-debug.log diff --git a/run/helloworld/README.md b/run/helloworld/README.md index 26e490260c..e08f381e3d 100644 --- a/run/helloworld/README.md +++ b/run/helloworld/README.md @@ -42,10 +42,10 @@ test/deploy.sh The `runner.sh` script: -- Deploys the service to Cloud Run based on the `deploy.sh` script. -- Sets the `BASE_URL` and `ID_TOKEN` environment variables. -- Runs any arguments passed to the `runner.sh` script. -- Tears down the Cloud Run service on completion. +* Deploys the service to Cloud Run based on the `deploy.sh` script. +* Sets the `BASE_URL` and `ID_TOKEN` environment variables. +* Runs any arguments passed to the `runner.sh` script. +* Tears down the Cloud Run service on completion. ```sh test/runner.sh sleep 20 @@ -53,12 +53,12 @@ test/runner.sh sleep 20 ## Environment Variables (Testing) -- `BASE_URL`: Specifies the Cloud Run service URL for end-to-end tests. -- `ID_TOKEN`: JWT token used to authenticate with Cloud Run's IAM-based authentication. -- `REGION`: [`us-central1`] Optional override region for the location of the Cloud Run service. -- `SERVICE_NAME`: The name of the deployed service, used in some API calls and test assertions. +* `BASE_URL`: Specifies the Cloud Run service URL for end-to-end tests. +* `ID_TOKEN`: JWT token used to authenticate with Cloud Run's IAM-based authentication. +* `REGION`: [`us-central1`] Optional override region for the location of the Cloud Run service. +* `SERVICE_NAME`: The name of the deployed service, used in some API calls and test assertions. ## Dependencies -- **express**: Web server framework. -- **got**: [Testing] Used to make HTTP requests of the running service in end-to-end testing. +* **express**: Web server framework. +* **got**: [Testing] Used to make HTTP requests of the running service in end-to-end testing. From 1710ed3b2584238053ba0b15c292b5ad7482f779 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 30 Sep 2024 14:59:15 -0700 Subject: [PATCH 065/127] use correct config --- .github/workflows/config/nodejs-e2e.jsonc | 1 - .github/workflows/experimental.yaml | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/config/nodejs-e2e.jsonc b/.github/workflows/config/nodejs-e2e.jsonc index 7a0d6058ba..7b36631866 100644 --- a/.github/workflows/config/nodejs-e2e.jsonc +++ b/.github/workflows/config/nodejs-e2e.jsonc @@ -1,5 +1,4 @@ { - "match": ["run/hello-broken/", "run/helloworld/"], "ignore": ["README.md", "node_modules/"], "package": ["package.json"] } diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 89e75cd13a..d669c80340 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -43,11 +43,11 @@ jobs: working-directory: .github/workflows/samples-tools - id: nodejs run: | - PACKAGES=$(affected .github/workflows/config/nodejs.jsonc) + PACKAGES=$(affected -config .github/workflows/config/nodejs.jsonc) echo "packages=$PACKAGES" | tee -a $GITHUB_OUTPUT - id: nodejs-e2e run: | - PACKAGES=$(affected .github/workflows/config/nodejs-e2e.jsonc) + PACKAGES=$(affected -config .github/workflows/config/nodejs-e2e.jsonc) echo "packages=$PACKAGES" | tee -a $GITHUB_OUTPUT nodejs-lint: From 6fe8059432c487e865f9b9a1174a27e1e8b8465d Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 1 Oct 2024 13:53:42 -0700 Subject: [PATCH 066/127] better error messages --- .../samples-tools/cmd/affected/main.go | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index e8db2ac9be..334a257e35 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -18,30 +18,40 @@ func main() { flag.Parse() if *configPath == "" { - panic("config path is required, please pass -config=path/to/config.jsonc") + fmt.Fprintf(os.Stderr, "config path is required, please pass -config=path/to/config.jsonc") + os.Exit(1) } config, err := utils.LoadConfig(*configPath) if err != nil { - panic(err) + fmt.Fprintf(os.Stderr, "error loading the config file: %v\n", err) + os.Exit(1) } diffs, err := utils.Diffs(*headCommit, *mainCommit) if err != nil { - panic(err) + fmt.Fprintf(os.Stderr, "error getting the diffs: %v\n", err) + os.Exit(1) } packages, err := affected(config, diffs) if err != nil { - panic(err) + fmt.Fprintf(os.Stderr, "error finding the affected packages: %v\n", err) + os.Exit(1) } if len(packages) > 256 { - panic(fmt.Errorf("GitHub Actions only supports up to 256 packages, got %d packages", len(packages))) + fmt.Fprintf(os.Stderr, + "GitHub Actions only supports up to 256 packages, got %v packages\nSee %v for more details.\n", + len(packages), + "https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow", + ) + os.Exit(1) } matrix, err := json.Marshal(packages) if err != nil { - panic(err) + fmt.Fprintf(os.Stderr, "error marshaling to JSON: %v\n", err) + os.Exit(1) } fmt.Println(string(matrix)) From 9b6ee804597e4df74154f0fb09fc1e5b8004feaf Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 1 Oct 2024 15:15:39 -0700 Subject: [PATCH 067/127] improve error messages --- .github/workflows/samples-tools/cmd/affected/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index 334a257e35..66af5d9074 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -24,7 +24,7 @@ func main() { config, err := utils.LoadConfig(*configPath) if err != nil { - fmt.Fprintf(os.Stderr, "error loading the config file: %v\n", err) + fmt.Fprintf(os.Stderr, "error loading the config file, make sure it exists and it's valid: %v\n", err) os.Exit(1) } @@ -41,7 +41,7 @@ func main() { } if len(packages) > 256 { fmt.Fprintf(os.Stderr, - "GitHub Actions only supports up to 256 packages, got %v packages\nSee %v for more details.\n", + "GitHub Actions only supports up to 256 packages, got %v packages, for more details see:\n%v\n", len(packages), "https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow", ) From 9afaa2a3b272ef196c67f5930438c2de8c56314d Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 11:51:42 -0700 Subject: [PATCH 068/127] use io/fs.WalkDir --- .github/workflows/samples-tools/cmd/affected/main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index 66af5d9074..5cbe5749a8 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -4,8 +4,8 @@ import ( "encoding/json" "flag" "fmt" + "io/fs" "os" - "path/filepath" "slices" "samples-tools/pkg/utils" @@ -76,14 +76,14 @@ func affected(config utils.Config, diffs []string) ([]string, error) { } if slices.Contains(changed, ".") { - return findAllPackages(config) + return findAllPackages(".", config) } return changed, nil } -func findAllPackages(config utils.Config) ([]string, error) { +func findAllPackages(root string, config utils.Config) ([]string, error) { var packages []string - err := filepath.WalkDir(".", + err := fs.WalkDir(os.DirFS(root), ".", func(path string, d os.DirEntry, err error) error { if err != nil { return err From 72e204c2eec966ccd1781d8a9c2644ca781ec56a Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 11:55:59 -0700 Subject: [PATCH 069/127] rename variable --- .github/workflows/samples-tools/cmd/affected/main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index 5cbe5749a8..4bb1ef1cf7 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -58,7 +58,7 @@ func main() { } func affected(config utils.Config, diffs []string) ([]string, error) { - uniquePackages := make(map[string]bool) + changedUnique := make(map[string]bool) for _, diff := range diffs { if !config.Matches(diff) { continue @@ -67,11 +67,11 @@ func affected(config utils.Config, diffs []string) ([]string, error) { if slices.Contains(config.ExcludePackages, pkg) { continue } - uniquePackages[pkg] = true + changedUnique[pkg] = true } - changed := make([]string, 0, len(uniquePackages)) - for pkg := range uniquePackages { + changed := make([]string, 0, len(changedUnique)) + for pkg := range changedUnique { changed = append(changed, pkg) } From 0854b068a9a8279dc97af7d276863a5616ca3435 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 12:53:46 -0700 Subject: [PATCH 070/127] experiment with nightly tests --- .github/workflows/config/nodejs-e2e.jsonc | 4 -- .github/workflows/experimental.yaml | 83 +++++++++++++---------- .github/workflows/nodejs-test.yaml | 47 ------------- 3 files changed, 48 insertions(+), 86 deletions(-) delete mode 100644 .github/workflows/config/nodejs-e2e.jsonc delete mode 100644 .github/workflows/nodejs-test.yaml diff --git a/.github/workflows/config/nodejs-e2e.jsonc b/.github/workflows/config/nodejs-e2e.jsonc deleted file mode 100644 index 7b36631866..0000000000 --- a/.github/workflows/config/nodejs-e2e.jsonc +++ /dev/null @@ -1,4 +0,0 @@ -{ - "ignore": ["README.md", "node_modules/"], - "package": ["package.json"] -} diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index d669c80340..5b806f5f32 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -23,12 +23,14 @@ on: - reopened - synchronize - labeled + schedule: + - cron: 0 0 * * 0 jobs: affected: name: Finding affected tests runs-on: ubuntu-latest - timeout-minutes: 1 + timeout-minutes: 2 outputs: nodejs: ${{ steps.nodejs.outputs.packages }} nodejs-e2e: ${{ steps.nodejs-e2e.outputs.packages }} @@ -63,42 +65,53 @@ jobs: - run: ./.github/workflows/utils/region-tags-tests.sh nodejs-test: - name: Node.js test + # TODO: enable this + # if: ${{ github.event_name == 'pull_request' }} + if: ${{ false }} + name: Node.js 20 test needs: affected + runs-on: ubuntu-latest + permissions: + id-token: write # needed for google-github-actions/auth strategy: matrix: package: ${{ fromJson(needs.affected.outputs.nodejs) }} - exclude: - - package: '.' - uses: ./.github/workflows/nodejs-test.yaml - with: - path: ${{ matrix.package }} - node-version: 20 + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + - uses: actions/setup-node@v4 + with: + node-version: 20 + - uses: google-github-actions/auth@v2 + with: + project_id: long-door-651 + workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider + service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com + access_token_lifetime: 600s # 10 minutes + - run: make test dir=${{ matrix.package }} + env: + GOOGLE_SAMPLES_PROJECT: long-door-651 - # nodejs-e2e-test: - # name: Node.js e2e-test - # needs: affected - # runs-on: ubuntu-latest - # timeout-minutes: 60 - # permissions: - # id-token: write # needed for google-github-actions/auth - # strategy: - # matrix: - # node-version: [18, 22] - # package: ${{ fromJson(needs.affected.outputs.nodejs-e2e) }} - # exclude: - # - package: '.' - # steps: - # - uses: actions/checkout@v4 - # - uses: actions/setup-node@v4 - # with: - # node-version: ${{ matrix.node-version }} - # - uses: google-github-actions/auth@v2 - # with: - # project_id: long-door-651 - # workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider - # service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com - # access_token_lifetime: 600s # 10 minutes - # - run: make e2e-test dir=${{ matrix.package }} - # env: - # GOOGLE_SAMPLES_PROJECT: long-door-651 + nodejs-nightly: + name: Node.js 20 nightly + runs-on: ubuntu-latest + permissions: + id-token: write # needed for google-github-actions/auth + strategy: + matrix: + run: ${{ true && true || false }} + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 20 + - uses: google-github-actions/auth@v2 + with: + project_id: long-door-651 + workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider + service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com + access_token_lifetime: 600s # 10 minutes + - run: echo "Running nightlies" + env: + GOOGLE_SAMPLES_PROJECT: long-door-651 diff --git a/.github/workflows/nodejs-test.yaml b/.github/workflows/nodejs-test.yaml deleted file mode 100644 index 35e32cfbbf..0000000000 --- a/.github/workflows/nodejs-test.yaml +++ /dev/null @@ -1,47 +0,0 @@ -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -on: - workflow_call: - inputs: - path: - required: true - type: string - node-version: - required: true - type: number - -jobs: - test: - name: ${{ inputs.node-version }} - runs-on: ubuntu-latest - timeout-minutes: 15 - permissions: - id-token: write # needed for google-github-actions/auth - steps: - - uses: actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.sha }} - - uses: actions/setup-node@v4 - with: - node-version: ${{ inputs.node-version }} - - uses: google-github-actions/auth@v2 - with: - project_id: long-door-651 - workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider - service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com - access_token_lifetime: 600s # 10 minutes - - run: make test dir=${{ inputs.path }} - env: - GOOGLE_SAMPLES_PROJECT: long-door-651 From 3f96c331d12b976678d1fb68e95853cd4e29c96a Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 12:54:56 -0700 Subject: [PATCH 071/127] remove e2e-test for now --- .github/workflows/experimental.yaml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 5b806f5f32..cccca8164e 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -47,10 +47,6 @@ jobs: run: | PACKAGES=$(affected -config .github/workflows/config/nodejs.jsonc) echo "packages=$PACKAGES" | tee -a $GITHUB_OUTPUT - - id: nodejs-e2e - run: | - PACKAGES=$(affected -config .github/workflows/config/nodejs-e2e.jsonc) - echo "packages=$PACKAGES" | tee -a $GITHUB_OUTPUT nodejs-lint: name: Node.js lint From f9954556a2206c36cdf2e462e3af8a1f33062a04 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 12:58:23 -0700 Subject: [PATCH 072/127] use ternary operator --- .github/workflows/experimental.yaml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index cccca8164e..b3c26266be 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -61,9 +61,6 @@ jobs: - run: ./.github/workflows/utils/region-tags-tests.sh nodejs-test: - # TODO: enable this - # if: ${{ github.event_name == 'pull_request' }} - if: ${{ false }} name: Node.js 20 test needs: affected runs-on: ubuntu-latest @@ -71,7 +68,7 @@ jobs: id-token: write # needed for google-github-actions/auth strategy: matrix: - package: ${{ fromJson(needs.affected.outputs.nodejs) }} + package: ${{ fromJson(true && needs.affected.outputs.nodejs || '[]') }} steps: - uses: actions/checkout@v4 with: @@ -96,7 +93,7 @@ jobs: id-token: write # needed for google-github-actions/auth strategy: matrix: - run: ${{ true && true || false }} + run: ${{ fromJson(true && '[true]' || '[]') }} steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 From 4aabf3b84e5099ecfde2edfd93261d319d76f913 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 12:59:34 -0700 Subject: [PATCH 073/127] disable pr tests temporarily --- .github/workflows/experimental.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index b3c26266be..3ef634b3d4 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -68,7 +68,7 @@ jobs: id-token: write # needed for google-github-actions/auth strategy: matrix: - package: ${{ fromJson(true && needs.affected.outputs.nodejs || '[]') }} + package: ${{ fromJson(false && needs.affected.outputs.nodejs || '[]') }} steps: - uses: actions/checkout@v4 with: @@ -87,13 +87,13 @@ jobs: GOOGLE_SAMPLES_PROJECT: long-door-651 nodejs-nightly: - name: Node.js 20 nightly + name: Node.js 20 runs-on: ubuntu-latest permissions: id-token: write # needed for google-github-actions/auth strategy: matrix: - run: ${{ fromJson(true && '[true]' || '[]') }} + run: ${{ fromJson(true && '["nightly"]' || '[]') }} steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 From e7f56a103b82d8620d144e94e299c0015d1e16f5 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 16:05:00 -0700 Subject: [PATCH 074/127] use diffs file --- .github/workflows/experimental.yaml | 23 +++++++++++----- .../samples-tools/cmd/affected/main.go | 27 ++++++++++++++----- .../samples-tools/cmd/run-batch/main.go | 7 +++++ diffs.txt | 8 ++++++ 4 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/samples-tools/cmd/run-batch/main.go create mode 100644 diffs.txt diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 3ef634b3d4..51979a830e 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -26,6 +26,9 @@ on: schedule: - cron: 0 0 * * 0 +env: + GO_VERSION: ^1.22.0 + jobs: affected: name: Finding affected tests @@ -40,13 +43,12 @@ jobs: fetch-depth: 0 - uses: actions/setup-go@v5 with: - go-version: ^1.22.0 + go-version: ${{ env.GO_VERSION }} - run: go install ./cmd/affected working-directory: .github/workflows/samples-tools + - run: git --no-pager diff --name-only HEAD origin/main > diffs.txt - id: nodejs - run: | - PACKAGES=$(affected -config .github/workflows/config/nodejs.jsonc) - echo "packages=$PACKAGES" | tee -a $GITHUB_OUTPUT + run: echo "packages=$(affected -config .github/workflows/config/nodejs.jsonc -diffs=diffs.txt)" | tee -a $GITHUB_OUTPUT nodejs-lint: name: Node.js lint @@ -68,7 +70,8 @@ jobs: id-token: write # needed for google-github-actions/auth strategy: matrix: - package: ${{ fromJson(false && needs.affected.outputs.nodejs || '[]') }} + package: ${{ fromJson(github.event_name == 'pull_request' && needs.affected.outputs.nodejs || '[]') }} + # package: ${{ fromJson('[]') }} steps: - uses: actions/checkout@v4 with: @@ -93,18 +96,24 @@ jobs: id-token: write # needed for google-github-actions/auth strategy: matrix: - run: ${{ fromJson(true && '["nightly"]' || '[]') }} + # run: ${{ fromJson(github.event_name == 'schedule' && '["nightly"]' || '[]') }} + run: ${{ fromJson('["nightly"]') }} steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: 20 + - uses: actions/setup-go@v5 + with: + go-version: ${{ env.GO_VERSION }} - uses: google-github-actions/auth@v2 with: project_id: long-door-651 workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com access_token_lifetime: 600s # 10 minutes - - run: echo "Running nightlies" + - run: go install ./cmd/run-batch + working-directory: .github/workflows/samples-tools + - run: run-batch ./ env: GOOGLE_SAMPLES_PROJECT: long-door-651 diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index 4bb1ef1cf7..479f10b21e 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -7,28 +7,33 @@ import ( "io/fs" "os" "slices" + "strings" "samples-tools/pkg/utils" ) func main() { - configPath := flag.String("config", "", "path to the config file") - headCommit := flag.String("head", "HEAD", "commit with the changes") - mainCommit := flag.String("main", "origin/main", "commit of the main branch") + configFile := flag.String("config", "", "path to the config file") + diffsFile := flag.String("diffs", "", "path to the diffs file") flag.Parse() - if *configPath == "" { - fmt.Fprintf(os.Stderr, "config path is required, please pass -config=path/to/config.jsonc") + if *configFile == "" { + fmt.Fprintf(os.Stderr, "config file is required, please pass -config=path/to/config.jsonc") os.Exit(1) } - config, err := utils.LoadConfig(*configPath) + if *diffsFile == "" { + fmt.Fprintf(os.Stderr, "diffs file is required, please pass -diffs=path/to/diffs.txt") + os.Exit(1) + } + + config, err := utils.LoadConfig(*configFile) if err != nil { fmt.Fprintf(os.Stderr, "error loading the config file, make sure it exists and it's valid: %v\n", err) os.Exit(1) } - diffs, err := utils.Diffs(*headCommit, *mainCommit) + diffs, err := readDiffs(*diffsFile) if err != nil { fmt.Fprintf(os.Stderr, "error getting the diffs: %v\n", err) os.Exit(1) @@ -57,6 +62,14 @@ func main() { fmt.Println(string(matrix)) } +func readDiffs(path string) ([]string, error) { + bytes, err := os.ReadFile(path) + if err != nil { + return []string{}, err + } + return strings.Split(string(bytes), "\n"), nil +} + func affected(config utils.Config, diffs []string) ([]string, error) { changedUnique := make(map[string]bool) for _, diff := range diffs { diff --git a/.github/workflows/samples-tools/cmd/run-batch/main.go b/.github/workflows/samples-tools/cmd/run-batch/main.go new file mode 100644 index 0000000000..db70b2ea7a --- /dev/null +++ b/.github/workflows/samples-tools/cmd/run-batch/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Running") +} diff --git a/diffs.txt b/diffs.txt new file mode 100644 index 0000000000..e5e241b728 --- /dev/null +++ b/diffs.txt @@ -0,0 +1,8 @@ +.github/workflows/config/nodejs.jsonc +.github/workflows/experimental.yaml +.github/workflows/samples-tools/.gitignore +.github/workflows/samples-tools/cmd/affected/main.go +.github/workflows/samples-tools/go.mod +.github/workflows/samples-tools/pkg/utils/config.go +.github/workflows/samples-tools/pkg/utils/git.go +Makefile From 9ee8f5285b347c272fd91fc396b9f68cde99abef Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 16:12:03 -0700 Subject: [PATCH 075/127] print git diff outputs as well --- .github/workflows/experimental.yaml | 11 +++++------ diffs.txt | 2 ++ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 51979a830e..18e58dd359 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -36,7 +36,6 @@ jobs: timeout-minutes: 2 outputs: nodejs: ${{ steps.nodejs.outputs.packages }} - nodejs-e2e: ${{ steps.nodejs-e2e.outputs.packages }} steps: - uses: actions/checkout@v4 with: @@ -46,7 +45,7 @@ jobs: go-version: ${{ env.GO_VERSION }} - run: go install ./cmd/affected working-directory: .github/workflows/samples-tools - - run: git --no-pager diff --name-only HEAD origin/main > diffs.txt + - run: git --no-pager diff --name-only HEAD origin/main | tee diffs.txt - id: nodejs run: echo "packages=$(affected -config .github/workflows/config/nodejs.jsonc -diffs=diffs.txt)" | tee -a $GITHUB_OUTPUT @@ -70,8 +69,8 @@ jobs: id-token: write # needed for google-github-actions/auth strategy: matrix: - package: ${{ fromJson(github.event_name == 'pull_request' && needs.affected.outputs.nodejs || '[]') }} - # package: ${{ fromJson('[]') }} + # package: ${{ fromJson(github.event_name == 'pull_request' && needs.affected.outputs.nodejs || '[]') }} + package: ${{ fromJson('[]') }} steps: - uses: actions/checkout@v4 with: @@ -96,8 +95,8 @@ jobs: id-token: write # needed for google-github-actions/auth strategy: matrix: - # run: ${{ fromJson(github.event_name == 'schedule' && '["nightly"]' || '[]') }} - run: ${{ fromJson('["nightly"]') }} + # run: ${{ fromJson(github.event_name == 'schedule' && '["nightly tests"]' || '[]') }} + run: ${{ fromJson('["nightly tests"]') }} steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 diff --git a/diffs.txt b/diffs.txt index e5e241b728..a01cd31ce1 100644 --- a/diffs.txt +++ b/diffs.txt @@ -2,7 +2,9 @@ .github/workflows/experimental.yaml .github/workflows/samples-tools/.gitignore .github/workflows/samples-tools/cmd/affected/main.go +.github/workflows/samples-tools/cmd/run-batch/main.go .github/workflows/samples-tools/go.mod .github/workflows/samples-tools/pkg/utils/config.go .github/workflows/samples-tools/pkg/utils/git.go Makefile +diffs.txt From 21b97dff4adea69ebc2ab0e786323d6326b79d52 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 16:20:30 -0700 Subject: [PATCH 076/127] remove git utils --- .github/workflows/samples-tools/pkg/utils/git.go | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 .github/workflows/samples-tools/pkg/utils/git.go diff --git a/.github/workflows/samples-tools/pkg/utils/git.go b/.github/workflows/samples-tools/pkg/utils/git.go deleted file mode 100644 index 18030133ba..0000000000 --- a/.github/workflows/samples-tools/pkg/utils/git.go +++ /dev/null @@ -1,15 +0,0 @@ -package utils - -import ( - "os/exec" - "strings" -) - -func Diffs(head string, main string) ([]string, error) { - cmd := exec.Command("git", "--no-pager", "diff", "--name-only", head, main) - output, err := cmd.Output() - if err != nil { - return []string{}, err - } - return strings.Split(string(output), "\n"), nil -} From 599175998c7a8e17cd0544d45a8439a862c2dc86 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 18:09:43 -0700 Subject: [PATCH 077/127] initial prototype for nightlies --- .github/workflows/experimental.yaml | 10 ++- .../samples-tools/cmd/affected/main.go | 32 +------- .../samples-tools/cmd/run-all/main.go | 74 +++++++++++++++++++ .../samples-tools/cmd/run-batch/main.go | 7 -- .../workflows/samples-tools/pkg/utils/map.go | 19 +++++ .../samples-tools/pkg/utils/packages.go | 32 ++++++++ 6 files changed, 134 insertions(+), 40 deletions(-) create mode 100644 .github/workflows/samples-tools/cmd/run-all/main.go delete mode 100644 .github/workflows/samples-tools/cmd/run-batch/main.go create mode 100644 .github/workflows/samples-tools/pkg/utils/map.go create mode 100644 .github/workflows/samples-tools/pkg/utils/packages.go diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 18e58dd359..dc76f5b6b9 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -45,8 +45,10 @@ jobs: go-version: ${{ env.GO_VERSION }} - run: go install ./cmd/affected working-directory: .github/workflows/samples-tools - - run: git --no-pager diff --name-only HEAD origin/main | tee diffs.txt - - id: nodejs + - name: Get diffs + run: git --no-pager diff --name-only HEAD origin/main | tee diffs.txt + - name: Find Node.js affected packages + id: nodejs run: echo "packages=$(affected -config .github/workflows/config/nodejs.jsonc -diffs=diffs.txt)" | tee -a $GITHUB_OUTPUT nodejs-lint: @@ -111,8 +113,8 @@ jobs: workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com access_token_lifetime: 600s # 10 minutes - - run: go install ./cmd/run-batch + - run: go install ./cmd/run-all working-directory: .github/workflows/samples-tools - - run: run-batch ./ + - run: run-all -config=.github/workflows/config/nodejs.jsonc -cmd="make test dir=%s" env: GOOGLE_SAMPLES_PROJECT: long-door-651 diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index 479f10b21e..f4ea8a0787 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -4,7 +4,6 @@ import ( "encoding/json" "flag" "fmt" - "io/fs" "os" "slices" "strings" @@ -18,12 +17,12 @@ func main() { flag.Parse() if *configFile == "" { - fmt.Fprintf(os.Stderr, "config file is required, please pass -config=path/to/config.jsonc") + fmt.Fprintf(os.Stderr, "config file is required, please pass -config=path/to/config.jsonc\n") os.Exit(1) } if *diffsFile == "" { - fmt.Fprintf(os.Stderr, "diffs file is required, please pass -diffs=path/to/diffs.txt") + fmt.Fprintf(os.Stderr, "diffs file is required, please pass -diffs=path/to/diffs.txt\n") os.Exit(1) } @@ -89,32 +88,7 @@ func affected(config utils.Config, diffs []string) ([]string, error) { } if slices.Contains(changed, ".") { - return findAllPackages(".", config) + return utils.FindAllPackages(".", config) } return changed, nil } - -func findAllPackages(root string, config utils.Config) ([]string, error) { - var packages []string - err := fs.WalkDir(os.DirFS(root), ".", - func(path string, d os.DirEntry, err error) error { - if err != nil { - return err - } - if path == "." { - return nil - } - if slices.Contains(config.ExcludePackages, path) { - return nil - } - if d.IsDir() && config.Matches(path) && config.IsPackageDir(path) { - packages = append(packages, path) - return nil - } - return nil - }) - if err != nil { - return []string{}, err - } - return packages, nil -} diff --git a/.github/workflows/samples-tools/cmd/run-all/main.go b/.github/workflows/samples-tools/cmd/run-all/main.go new file mode 100644 index 0000000000..b54d0d5b20 --- /dev/null +++ b/.github/workflows/samples-tools/cmd/run-all/main.go @@ -0,0 +1,74 @@ +package main + +import ( + "flag" + "fmt" + "os" + "os/exec" + "samples-tools/pkg/utils" + "strings" +) + +func main() { + configFile := flag.String("config", "", "path to the config file") + flag.Parse() + + if *configFile == "" { + fmt.Fprintf(os.Stderr, "config file is required, please pass -config=path/to/config.jsonc\n") + os.Exit(1) + } + + cmd := flag.Arg(0) + if cmd == "" { + fmt.Fprintf(os.Stderr, "command is required, use positional arguments with %%s for the package placeholder\n") + os.Exit(1) + } + + args := flag.Args()[1:] + + config, err := utils.LoadConfig(*configFile) + if err != nil { + fmt.Fprintf(os.Stderr, "error loading the config file, make sure it exists and it's valid: %v\n", err) + os.Exit(1) + } + + packages, err := utils.FindAllPackages(".", config) + if err != nil { + fmt.Fprintf(os.Stderr, "error finding packages: %v\n", err) + } + + failed := runAll(packages, cmd, args) + fmt.Printf("Total tests: %v\nFailed tests: %v\n", len(packages), failed) + if failed > 0 { + os.Exit(1) + } +} + +func runAll(packages []string, cmd string, args []string) int { + failures := utils.Map(packages, func(pkg string) string { + cmdArgs := make([]string, len(args)) + for i, arg := range args { + cmdArgs[i] = fmt.Sprintf(arg, pkg) + } + output, err := exec.Command(cmd, cmdArgs...).CombinedOutput() + if err != nil { + delim := strings.Repeat("=", 30) + fmt.Fprintf(os.Stderr, "%v TEST FAILED %v\n> %v\n%v\n%v\n\n", + delim, delim, + pkg, + err, + string(output), + ) + return pkg + } + return "" + }) + + failed := 0 + for _, failure := range failures { + if failure != "" { + failed++ + } + } + return failed +} diff --git a/.github/workflows/samples-tools/cmd/run-batch/main.go b/.github/workflows/samples-tools/cmd/run-batch/main.go deleted file mode 100644 index db70b2ea7a..0000000000 --- a/.github/workflows/samples-tools/cmd/run-batch/main.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "fmt" - -func main() { - fmt.Println("Running") -} diff --git a/.github/workflows/samples-tools/pkg/utils/map.go b/.github/workflows/samples-tools/pkg/utils/map.go new file mode 100644 index 0000000000..878ce59ea3 --- /dev/null +++ b/.github/workflows/samples-tools/pkg/utils/map.go @@ -0,0 +1,19 @@ +package utils + +import ( + "sync" +) + +func Map[a, b any](items []a, fn func(a) b) []b { + var wg sync.WaitGroup + result := make([]b, len(items)) + for i, item := range items { + wg.Add(1) + go func(i int, item a) { + defer wg.Done() + result[i] = fn(item) + }(i, item) + } + wg.Wait() + return result +} diff --git a/.github/workflows/samples-tools/pkg/utils/packages.go b/.github/workflows/samples-tools/pkg/utils/packages.go new file mode 100644 index 0000000000..768089f5a6 --- /dev/null +++ b/.github/workflows/samples-tools/pkg/utils/packages.go @@ -0,0 +1,32 @@ +package utils + +import ( + "io/fs" + "os" + "slices" +) + +func FindAllPackages(root string, config Config) ([]string, error) { + var packages []string + err := fs.WalkDir(os.DirFS(root), ".", + func(path string, d os.DirEntry, err error) error { + if err != nil { + return err + } + if path == "." { + return nil + } + if slices.Contains(config.ExcludePackages, path) { + return nil + } + if d.IsDir() && config.Matches(path) && config.IsPackageDir(path) { + packages = append(packages, path) + return nil + } + return nil + }) + if err != nil { + return []string{}, err + } + return packages, nil +} From 5f6cc38fbdc24639c8a088fe8b2aa5956e42beab Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 18:10:53 -0700 Subject: [PATCH 078/127] update workflow command --- .github/workflows/experimental.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index dc76f5b6b9..bef3c4f640 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -115,6 +115,6 @@ jobs: access_token_lifetime: 600s # 10 minutes - run: go install ./cmd/run-all working-directory: .github/workflows/samples-tools - - run: run-all -config=.github/workflows/config/nodejs.jsonc -cmd="make test dir=%s" + - run: run-all -config=.github/workflows/config/nodejs.jsonc make test dir=%s env: GOOGLE_SAMPLES_PROJECT: long-door-651 From 84223b27e3bcc1b0aac9ca6bf607a141f88ac71e Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 18:12:58 -0700 Subject: [PATCH 079/127] only sprintf when wildcard is there --- .github/workflows/samples-tools/cmd/run-all/main.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/samples-tools/cmd/run-all/main.go b/.github/workflows/samples-tools/cmd/run-all/main.go index b54d0d5b20..58b731c417 100644 --- a/.github/workflows/samples-tools/cmd/run-all/main.go +++ b/.github/workflows/samples-tools/cmd/run-all/main.go @@ -48,7 +48,11 @@ func runAll(packages []string, cmd string, args []string) int { failures := utils.Map(packages, func(pkg string) string { cmdArgs := make([]string, len(args)) for i, arg := range args { - cmdArgs[i] = fmt.Sprintf(arg, pkg) + if strings.Contains(arg, "%s") { + cmdArgs[i] = fmt.Sprintf(arg, pkg) + } else { + cmdArgs[i] = arg + } } output, err := exec.Command(cmd, cmdArgs...).CombinedOutput() if err != nil { From 7e69a04e9be4dd6a97465f260830f4faa55989eb Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 18:15:48 -0700 Subject: [PATCH 080/127] print on success too --- .github/workflows/samples-tools/cmd/run-all/main.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/samples-tools/cmd/run-all/main.go b/.github/workflows/samples-tools/cmd/run-all/main.go index 58b731c417..d456b62b71 100644 --- a/.github/workflows/samples-tools/cmd/run-all/main.go +++ b/.github/workflows/samples-tools/cmd/run-all/main.go @@ -56,14 +56,15 @@ func runAll(packages []string, cmd string, args []string) int { } output, err := exec.Command(cmd, cmdArgs...).CombinedOutput() if err != nil { - delim := strings.Repeat("=", 30) - fmt.Fprintf(os.Stderr, "%v TEST FAILED %v\n> %v\n%v\n%v\n\n", - delim, delim, + fmt.Fprintf(os.Stderr, "āŒ TEST FAILED %v\n> %v\n%v\n%v\n\n", + strings.Repeat("=", 60), pkg, err, string(output), ) return pkg + } else { + fmt.Printf("āœ… %v\n", pkg) } return "" }) From ca4a10089e4a9c2901a9bdeaf4b73a7b2c7dd46c Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 18:19:15 -0700 Subject: [PATCH 081/127] improve error messages --- .github/workflows/samples-tools/cmd/run-all/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/samples-tools/cmd/run-all/main.go b/.github/workflows/samples-tools/cmd/run-all/main.go index d456b62b71..fa5690a934 100644 --- a/.github/workflows/samples-tools/cmd/run-all/main.go +++ b/.github/workflows/samples-tools/cmd/run-all/main.go @@ -56,8 +56,8 @@ func runAll(packages []string, cmd string, args []string) int { } output, err := exec.Command(cmd, cmdArgs...).CombinedOutput() if err != nil { - fmt.Fprintf(os.Stderr, "āŒ TEST FAILED %v\n> %v\n%v\n%v\n\n", - strings.Repeat("=", 60), + fmt.Fprintf(os.Stderr, "%v TEST FAILED %v\nāŒ %v\n%v\n%v\n\n", + strings.Repeat("=", 30), strings.Repeat("=", 30), pkg, err, string(output), From 8cb15e0630960bb365c6ac89f5ef5f8e85d63415 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 2 Oct 2024 19:18:19 -0700 Subject: [PATCH 082/127] disable parallelism --- .../workflows/samples-tools/pkg/utils/map.go | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/.github/workflows/samples-tools/pkg/utils/map.go b/.github/workflows/samples-tools/pkg/utils/map.go index 878ce59ea3..c1dae338f8 100644 --- a/.github/workflows/samples-tools/pkg/utils/map.go +++ b/.github/workflows/samples-tools/pkg/utils/map.go @@ -1,19 +1,18 @@ package utils -import ( - "sync" -) - func Map[a, b any](items []a, fn func(a) b) []b { - var wg sync.WaitGroup + // var wg sync.WaitGroup result := make([]b, len(items)) + // for i, item := range items { + // wg.Add(1) + // go func(i int, item a) { + // defer wg.Done() + // result[i] = fn(item) + // }(i, item) + // } + // wg.Wait() for i, item := range items { - wg.Add(1) - go func(i int, item a) { - defer wg.Done() - result[i] = fn(item) - }(i, item) + result[i] = fn(item) } - wg.Wait() return result } From d335ec10f52e1c6d69c2299f057f0d8de8f36d79 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 3 Oct 2024 06:31:49 -0700 Subject: [PATCH 083/127] improve error reporting --- .github/workflows/samples-tools/cmd/run-all/main.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/samples-tools/cmd/run-all/main.go b/.github/workflows/samples-tools/cmd/run-all/main.go index fa5690a934..593cd69ff8 100644 --- a/.github/workflows/samples-tools/cmd/run-all/main.go +++ b/.github/workflows/samples-tools/cmd/run-all/main.go @@ -46,6 +46,7 @@ func main() { func runAll(packages []string, cmd string, args []string) int { failures := utils.Map(packages, func(pkg string) string { + // TODO(dcavazos): measure time spent on each test and print it along the results cmdArgs := make([]string, len(args)) for i, arg := range args { if strings.Contains(arg, "%s") { @@ -56,11 +57,14 @@ func runAll(packages []string, cmd string, args []string) int { } output, err := exec.Command(cmd, cmdArgs...).CombinedOutput() if err != nil { - fmt.Fprintf(os.Stderr, "%v TEST FAILED %v\nāŒ %v\n%v\n%v\n\n", - strings.Repeat("=", 30), strings.Repeat("=", 30), + fmt.Fprintf(os.Stderr, "%v\nāŒ FAILED: %v\n%v\n> %v %v\n%v\n%v\n%v\n\n", + strings.Repeat("=", 80), pkg, + strings.Repeat("-", 80), + cmd, cmdArgs, err, string(output), + strings.Repeat("-", 80), ) return pkg } else { From ee375a6bd07cd9257ead327c62117d20cc5c14f1 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 3 Oct 2024 06:35:32 -0700 Subject: [PATCH 084/127] clean before each test --- .github/workflows/experimental.yaml | 2 +- Makefile | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index bef3c4f640..f2280b17d8 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -115,6 +115,6 @@ jobs: access_token_lifetime: 600s # 10 minutes - run: go install ./cmd/run-all working-directory: .github/workflows/samples-tools - - run: run-all -config=.github/workflows/config/nodejs.jsonc make test dir=%s + - run: run-all -config=.github/workflows/config/nodejs.jsonc make clean-test dir=%s env: GOOGLE_SAMPLES_PROJECT: long-door-651 diff --git a/Makefile b/Makefile index 076f8efda1..3f3ef5130d 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,9 @@ dir ?= $(shell pwd) export GOOGLE_CLOUD_PROJECT = ${GOOGLE_SAMPLES_PROJECT} +clean: + rm -rf node_modules/ + install: # Install root package dependencies, this incldues packages for testing. npm install @@ -27,6 +30,8 @@ test: check-env build cd ${dir} npm test +clean-test: clean test + e2e-test: check-env build cd ${dir} npm run system-test From b99d1dcf5e4b170e6fe821c0e23e375454ca98c9 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 3 Oct 2024 06:41:24 -0700 Subject: [PATCH 085/127] include parallel flag --- .../samples-tools/cmd/run-all/main.go | 61 ++++++++++--------- .../workflows/samples-tools/pkg/utils/map.go | 29 +++++---- 2 files changed, 50 insertions(+), 40 deletions(-) diff --git a/.github/workflows/samples-tools/cmd/run-all/main.go b/.github/workflows/samples-tools/cmd/run-all/main.go index 593cd69ff8..2610eb9e63 100644 --- a/.github/workflows/samples-tools/cmd/run-all/main.go +++ b/.github/workflows/samples-tools/cmd/run-all/main.go @@ -11,6 +11,7 @@ import ( func main() { configFile := flag.String("config", "", "path to the config file") + parallel := flag.Bool("parallel", false, "set to true to run tests in parallel") flag.Parse() if *configFile == "" { @@ -37,40 +38,16 @@ func main() { fmt.Fprintf(os.Stderr, "error finding packages: %v\n", err) } - failed := runAll(packages, cmd, args) + failed := runAll(packages, cmd, args, *parallel) fmt.Printf("Total tests: %v\nFailed tests: %v\n", len(packages), failed) if failed > 0 { os.Exit(1) } } -func runAll(packages []string, cmd string, args []string) int { - failures := utils.Map(packages, func(pkg string) string { - // TODO(dcavazos): measure time spent on each test and print it along the results - cmdArgs := make([]string, len(args)) - for i, arg := range args { - if strings.Contains(arg, "%s") { - cmdArgs[i] = fmt.Sprintf(arg, pkg) - } else { - cmdArgs[i] = arg - } - } - output, err := exec.Command(cmd, cmdArgs...).CombinedOutput() - if err != nil { - fmt.Fprintf(os.Stderr, "%v\nāŒ FAILED: %v\n%v\n> %v %v\n%v\n%v\n%v\n\n", - strings.Repeat("=", 80), - pkg, - strings.Repeat("-", 80), - cmd, cmdArgs, - err, - string(output), - strings.Repeat("-", 80), - ) - return pkg - } else { - fmt.Printf("āœ… %v\n", pkg) - } - return "" +func runAll(packages []string, cmd string, args []string, parallel bool) int { + failures := utils.Map(parallel, packages, func(pkg string) string { + return runOne(pkg, cmd, args) }) failed := 0 @@ -81,3 +58,31 @@ func runAll(packages []string, cmd string, args []string) int { } return failed } + +func runOne(pkg string, cmd string, args []string) string { + // TODO(dcavazos): measure time spent on each test and print it along the results + cmdArgs := make([]string, len(args)) + for i, arg := range args { + if strings.Contains(arg, "%s") { + cmdArgs[i] = fmt.Sprintf(arg, pkg) + } else { + cmdArgs[i] = arg + } + } + output, err := exec.Command(cmd, cmdArgs...).CombinedOutput() + if err != nil { + fmt.Fprintf(os.Stderr, "%v\nāŒ FAILED: %v\n%v\n> %v %v\n%v\n%v\n%v\n\n", + strings.Repeat("=", 80), + pkg, + strings.Repeat("-", 80), + cmd, cmdArgs, + err, + string(output), + strings.Repeat("-", 80), + ) + return pkg + } else { + fmt.Printf("āœ… %v\n", pkg) + } + return "" +} diff --git a/.github/workflows/samples-tools/pkg/utils/map.go b/.github/workflows/samples-tools/pkg/utils/map.go index c1dae338f8..8b8e40f732 100644 --- a/.github/workflows/samples-tools/pkg/utils/map.go +++ b/.github/workflows/samples-tools/pkg/utils/map.go @@ -1,18 +1,23 @@ package utils -func Map[a, b any](items []a, fn func(a) b) []b { - // var wg sync.WaitGroup +import "sync" + +func Map[a, b any](parallel bool, items []a, fn func(a) b) []b { result := make([]b, len(items)) - // for i, item := range items { - // wg.Add(1) - // go func(i int, item a) { - // defer wg.Done() - // result[i] = fn(item) - // }(i, item) - // } - // wg.Wait() - for i, item := range items { - result[i] = fn(item) + if parallel { + var wg sync.WaitGroup + for i, item := range items { + wg.Add(1) + go func(i int, item a) { + defer wg.Done() + result[i] = fn(item) + }(i, item) + } + wg.Wait() + } else { + for i, item := range items { + result[i] = fn(item) + } } return result } From f6d05a2ac81c65b9b73a668d25ebac35acc41aef Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 3 Oct 2024 12:16:45 -0700 Subject: [PATCH 086/127] re-enable tests and disable nightlies experiment --- .github/workflows/experimental.yaml | 58 ++++++++++++++--------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index f2280b17d8..4d654e68cb 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -71,8 +71,7 @@ jobs: id-token: write # needed for google-github-actions/auth strategy: matrix: - # package: ${{ fromJson(github.event_name == 'pull_request' && needs.affected.outputs.nodejs || '[]') }} - package: ${{ fromJson('[]') }} + package: ${{ fromJson(github.event_name == 'pull_request' && needs.affected.outputs.nodejs || '[]') }} steps: - uses: actions/checkout@v4 with: @@ -90,31 +89,30 @@ jobs: env: GOOGLE_SAMPLES_PROJECT: long-door-651 - nodejs-nightly: - name: Node.js 20 - runs-on: ubuntu-latest - permissions: - id-token: write # needed for google-github-actions/auth - strategy: - matrix: - # run: ${{ fromJson(github.event_name == 'schedule' && '["nightly tests"]' || '[]') }} - run: ${{ fromJson('["nightly tests"]') }} - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 - with: - node-version: 20 - - uses: actions/setup-go@v5 - with: - go-version: ${{ env.GO_VERSION }} - - uses: google-github-actions/auth@v2 - with: - project_id: long-door-651 - workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider - service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com - access_token_lifetime: 600s # 10 minutes - - run: go install ./cmd/run-all - working-directory: .github/workflows/samples-tools - - run: run-all -config=.github/workflows/config/nodejs.jsonc make clean-test dir=%s - env: - GOOGLE_SAMPLES_PROJECT: long-door-651 + # nodejs-nightly: + # name: Node.js 20 + # runs-on: ubuntu-latest + # permissions: + # id-token: write # needed for google-github-actions/auth + # strategy: + # matrix: + # run: ${{ fromJson(github.event_name == 'schedule' && '["nightly tests"]' || '[]') }} + # steps: + # - uses: actions/checkout@v4 + # - uses: actions/setup-node@v4 + # with: + # node-version: 20 + # - uses: actions/setup-go@v5 + # with: + # go-version: ${{ env.GO_VERSION }} + # - uses: google-github-actions/auth@v2 + # with: + # project_id: long-door-651 + # workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider + # service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com + # access_token_lifetime: 600s # 10 minutes + # - run: go install ./cmd/run-all + # working-directory: .github/workflows/samples-tools + # - run: run-all -config=.github/workflows/config/nodejs.jsonc make clean-test dir=%s + # env: + # GOOGLE_SAMPLES_PROJECT: long-door-651 From 61412f393928217505389dae739c15d0cbc165c0 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 3 Oct 2024 12:23:52 -0700 Subject: [PATCH 087/127] disable fail-fast --- .github/workflows/config/nodejs.jsonc | 1 + .github/workflows/experimental.yaml | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/workflows/config/nodejs.jsonc b/.github/workflows/config/nodejs.jsonc index e7229fe858..ebaa061f87 100644 --- a/.github/workflows/config/nodejs.jsonc +++ b/.github/workflows/config/nodejs.jsonc @@ -45,6 +45,7 @@ "run/markdown-preview/editor", // Error: could not create an identity token: Cannot fetch ID token in this environment, use GCE or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to a service account credentials JSON file "run/system-package", // Error: ENOENT: no such file or directory, access '/usr/bin/dot' "scheduler", // SyntaxError: Cannot use import statement outside a module + "security-center/snippets", // Error: 3 INVALID_ARGUMENT: Fail to resolve resource 'organizations/undefined/locations/global' "storagetransfer", // CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1 "video-intelligence", // PERMISSION_DENIED: The caller does not have permission "vision", // REDIS: Error: connect ECONNREFUSED 127.0.0.1:6379 diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 4d654e68cb..2cd07cfb61 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -70,6 +70,7 @@ jobs: permissions: id-token: write # needed for google-github-actions/auth strategy: + fail-fast: false matrix: package: ${{ fromJson(github.event_name == 'pull_request' && needs.affected.outputs.nodejs || '[]') }} steps: From 30f24ee693abc5f048f449025d00691abf9c3b5f Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 3 Oct 2024 12:25:23 -0700 Subject: [PATCH 088/127] revert clean commands --- Makefile | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Makefile b/Makefile index 3f3ef5130d..076f8efda1 100644 --- a/Makefile +++ b/Makefile @@ -12,9 +12,6 @@ dir ?= $(shell pwd) export GOOGLE_CLOUD_PROJECT = ${GOOGLE_SAMPLES_PROJECT} -clean: - rm -rf node_modules/ - install: # Install root package dependencies, this incldues packages for testing. npm install @@ -30,8 +27,6 @@ test: check-env build cd ${dir} npm test -clean-test: clean test - e2e-test: check-env build cd ${dir} npm run system-test From 333f06ae4f636fb3c01c04e89339f53c0f301004 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 3 Oct 2024 12:31:32 -0700 Subject: [PATCH 089/127] add files to ignore --- .github/workflows/config/nodejs.jsonc | 31 ++++++++++++++++++++++++++- diffs.txt | 10 --------- 2 files changed, 30 insertions(+), 11 deletions(-) delete mode 100644 diffs.txt diff --git a/.github/workflows/config/nodejs.jsonc b/.github/workflows/config/nodejs.jsonc index ebaa061f87..470c8212b0 100644 --- a/.github/workflows/config/nodejs.jsonc +++ b/.github/workflows/config/nodejs.jsonc @@ -1,5 +1,34 @@ { - "ignore": ["README.md", "node_modules/"], + "ignore": [ + ".eslintignore", + ".eslintrc.json", + ".github/.OwlBot.lock.yaml", + ".github/.OwlBot.yaml", + ".github/ISSUE_TEMPLATE/", + ".github/PULL_REQUEST_TEMPLATE.md", + ".github/auto-label.yaml", + ".github/blunderbuss.yaml", + ".github/flakybot.yaml", + ".github/header-checker-lint.yaml", + ".github/snippet-bot.yml", + ".github/trusted-contribution.yml", + ".gitignore", + ".kokoro/", + ".prettierignore", + ".prettierrc.js", + "CODEOWNERS", + "CODE_OF_CONDUCT.md", + "CONTRIBUTING.md", + "LICENSE", + "Makefile", + "README.md", + "SECURITY.md", + "buildsetup.sh", + "linkinator.config.json", + "node_modules/", + "owlbot.py", + "renovate.json" + ], "package": ["package.json"], "exclude-packages": [ "ai-platform/snippets", // PERMISSION_DENIED: Permission denied: Consumer 'projects/undefined' has been suspended. diff --git a/diffs.txt b/diffs.txt deleted file mode 100644 index a01cd31ce1..0000000000 --- a/diffs.txt +++ /dev/null @@ -1,10 +0,0 @@ -.github/workflows/config/nodejs.jsonc -.github/workflows/experimental.yaml -.github/workflows/samples-tools/.gitignore -.github/workflows/samples-tools/cmd/affected/main.go -.github/workflows/samples-tools/cmd/run-batch/main.go -.github/workflows/samples-tools/go.mod -.github/workflows/samples-tools/pkg/utils/config.go -.github/workflows/samples-tools/pkg/utils/git.go -Makefile -diffs.txt From ba3c05773dd67bd32c13ca4758dca20385318524 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 3 Oct 2024 12:33:06 -0700 Subject: [PATCH 090/127] ignore speech package --- .github/workflows/config/nodejs.jsonc | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/config/nodejs.jsonc b/.github/workflows/config/nodejs.jsonc index 470c8212b0..5463ae1f6f 100644 --- a/.github/workflows/config/nodejs.jsonc +++ b/.github/workflows/config/nodejs.jsonc @@ -75,6 +75,7 @@ "run/system-package", // Error: ENOENT: no such file or directory, access '/usr/bin/dot' "scheduler", // SyntaxError: Cannot use import statement outside a module "security-center/snippets", // Error: 3 INVALID_ARGUMENT: Fail to resolve resource 'organizations/undefined/locations/global' + "speech", // AssertionError: expected 'Transcription: Okay, I\'m here.\n Hi…' to match /Terrific. It's on the way./ "storagetransfer", // CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1 "video-intelligence", // PERMISSION_DENIED: The caller does not have permission "vision", // REDIS: Error: connect ECONNREFUSED 127.0.0.1:6379 From a4e91f9b20dba8d514a27d7e09b5935d614ddcd1 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 3 Oct 2024 12:39:56 -0700 Subject: [PATCH 091/127] ignore translate package --- .github/workflows/config/nodejs.jsonc | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/config/nodejs.jsonc b/.github/workflows/config/nodejs.jsonc index 5463ae1f6f..8923830a04 100644 --- a/.github/workflows/config/nodejs.jsonc +++ b/.github/workflows/config/nodejs.jsonc @@ -77,6 +77,7 @@ "security-center/snippets", // Error: 3 INVALID_ARGUMENT: Fail to resolve resource 'organizations/undefined/locations/global' "speech", // AssertionError: expected 'Transcription: Okay, I\'m here.\n Hi…' to match /Terrific. It's on the way./ "storagetransfer", // CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1 + "translate", // AssertionError: expected 'Languages:\n{ code: \'ab\', name: \'A…' to match /{ code: 'af', name: 'afrikĆ”ans' }/ "video-intelligence", // PERMISSION_DENIED: The caller does not have permission "vision", // REDIS: Error: connect ECONNREFUSED 127.0.0.1:6379 "workflows", // SyntaxError: Cannot use import statement outside a module From e80bcd0dda91cbd1e87fc2e8f7fe3f95bbc9acda Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 4 Oct 2024 12:08:48 -0700 Subject: [PATCH 092/127] add license headers and support multi-line comments --- .github/workflows/config/nodejs.jsonc | 16 +++++++++++++ .../samples-tools/cmd/affected/main.go | 16 +++++++++++++ .../samples-tools/cmd/run-all/main.go | 16 +++++++++++++ .../samples-tools/pkg/utils/config.go | 23 +++++++++++++++++-- .../workflows/samples-tools/pkg/utils/map.go | 16 +++++++++++++ .../samples-tools/pkg/utils/packages.go | 16 +++++++++++++ 6 files changed, 101 insertions(+), 2 deletions(-) diff --git a/.github/workflows/config/nodejs.jsonc b/.github/workflows/config/nodejs.jsonc index 8923830a04..28ddb66141 100644 --- a/.github/workflows/config/nodejs.jsonc +++ b/.github/workflows/config/nodejs.jsonc @@ -1,3 +1,19 @@ +/* + Copyright 2024 Google LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + { "ignore": [ ".eslintignore", diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go index f4ea8a0787..0ab677eda7 100644 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ b/.github/workflows/samples-tools/cmd/affected/main.go @@ -1,3 +1,19 @@ +/* + Copyright 2024 Google LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + package main import ( diff --git a/.github/workflows/samples-tools/cmd/run-all/main.go b/.github/workflows/samples-tools/cmd/run-all/main.go index 2610eb9e63..42333a70b8 100644 --- a/.github/workflows/samples-tools/cmd/run-all/main.go +++ b/.github/workflows/samples-tools/cmd/run-all/main.go @@ -1,3 +1,19 @@ +/* + Copyright 2024 Google LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + package main import ( diff --git a/.github/workflows/samples-tools/pkg/utils/config.go b/.github/workflows/samples-tools/pkg/utils/config.go index 9a4d136173..0bb5593d85 100644 --- a/.github/workflows/samples-tools/pkg/utils/config.go +++ b/.github/workflows/samples-tools/pkg/utils/config.go @@ -1,3 +1,19 @@ +/* + Copyright 2024 Google LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + package utils import ( @@ -23,6 +39,9 @@ type Config struct { ExcludePackages []string `json:"exclude-packages"` } +var multiLineCommentsRegex = regexp.MustCompile(`(?s)\s*/\*.*?\*/\s*`) +var singleLineCommentsRegex = regexp.MustCompile(`\s*//.*\s*`) + func LoadConfig(path string) (Config, error) { bytes, err := os.ReadFile(path) if err != nil { @@ -48,8 +67,8 @@ func ParseConfig(source []byte) (Config, error) { } func StripComments(src []byte) []byte { - re := regexp.MustCompile(`\s*// .*`) - return re.ReplaceAll(src, []byte{}) + src = multiLineCommentsRegex.ReplaceAll(src, []byte{}) + return singleLineCommentsRegex.ReplaceAll(src, []byte{}) } func match(patterns []string, path string) bool { diff --git a/.github/workflows/samples-tools/pkg/utils/map.go b/.github/workflows/samples-tools/pkg/utils/map.go index 8b8e40f732..282bc53aad 100644 --- a/.github/workflows/samples-tools/pkg/utils/map.go +++ b/.github/workflows/samples-tools/pkg/utils/map.go @@ -1,3 +1,19 @@ +/* + Copyright 2024 Google LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + package utils import "sync" diff --git a/.github/workflows/samples-tools/pkg/utils/packages.go b/.github/workflows/samples-tools/pkg/utils/packages.go index 768089f5a6..2b19076a7c 100644 --- a/.github/workflows/samples-tools/pkg/utils/packages.go +++ b/.github/workflows/samples-tools/pkg/utils/packages.go @@ -1,3 +1,19 @@ +/* + Copyright 2024 Google LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + package utils import ( From 1fb450bccff9b2077a83237ab12ac3d4930ce1f0 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 4 Oct 2024 13:54:27 -0700 Subject: [PATCH 093/127] revert and add build to lint --- .github/workflows/samples-tools/README.md | 7 ++ .../workflows/samples-tools/cmd/affected.go | 107 +++++++++++++++++ .../samples-tools/cmd/affected/main.go | 110 ------------------ .github/workflows/samples-tools/cmd/main.go | 23 ++++ .../cmd/{run-all/main.go => run-all.go} | 59 +++++----- .../samples-tools/pkg/utils/config.go | 12 +- .../workflows/samples-tools/pkg/utils/map.go | 1 + .../samples-tools/pkg/utils/packages.go | 1 + Makefile | 12 +- 9 files changed, 180 insertions(+), 152 deletions(-) create mode 100644 .github/workflows/samples-tools/README.md create mode 100644 .github/workflows/samples-tools/cmd/affected.go delete mode 100644 .github/workflows/samples-tools/cmd/affected/main.go create mode 100644 .github/workflows/samples-tools/cmd/main.go rename .github/workflows/samples-tools/cmd/{run-all/main.go => run-all.go} (60%) diff --git a/.github/workflows/samples-tools/README.md b/.github/workflows/samples-tools/README.md new file mode 100644 index 0000000000..a8d68deaad --- /dev/null +++ b/.github/workflows/samples-tools/README.md @@ -0,0 +1,7 @@ +# Cloud Samples tools + +This is a collection of tools used for Cloud Samples maintenance and infrastructure. + +## Finding affected packages + +## Running on all packages diff --git a/.github/workflows/samples-tools/cmd/affected.go b/.github/workflows/samples-tools/cmd/affected.go new file mode 100644 index 0000000000..85b773abc2 --- /dev/null +++ b/.github/workflows/samples-tools/cmd/affected.go @@ -0,0 +1,107 @@ +/* + Copyright 2024 Google LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package main + +import ( + "os" + "slices" + "strings" + + "samples-tools/pkg/utils" +) + +// func main() { +// configFile := flag.String("config", "", "path to the config file") +// diffsFile := flag.String("diffs", "", "path to the diffs file") +// flag.Parse() + +// if *configFile == "" { +// fmt.Fprintf(os.Stderr, "config file is required, please pass -config=path/to/config.jsonc\n") +// os.Exit(1) +// } + +// if *diffsFile == "" { +// fmt.Fprintf(os.Stderr, "diffs file is required, please pass -diffs=path/to/diffs.txt\n") +// os.Exit(1) +// } + +// config, err := utils.LoadConfig(*configFile) +// if err != nil { +// fmt.Fprintf(os.Stderr, "error loading the config file, make sure it exists and it's valid: %v\n", err) +// os.Exit(1) +// } + +// diffs, err := readDiffs(*diffsFile) +// if err != nil { +// fmt.Fprintf(os.Stderr, "error getting the diffs: %v\n", err) +// os.Exit(1) +// } + +// packages, err := affected(config, diffs) +// if err != nil { +// fmt.Fprintf(os.Stderr, "error finding the affected packages: %v\n", err) +// os.Exit(1) +// } +// if len(packages) > 256 { +// fmt.Fprintf(os.Stderr, +// "GitHub Actions only supports up to 256 packages, got %v packages, for more details see:\n%v\n", +// len(packages), +// "https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow", +// ) +// os.Exit(1) +// } + +// matrix, err := json.Marshal(packages) +// if err != nil { +// fmt.Fprintf(os.Stderr, "error marshaling to JSON: %v\n", err) +// os.Exit(1) +// } + +// fmt.Println(string(matrix)) +// } + +func readDiffs(path string) ([]string, error) { + bytes, err := os.ReadFile(path) + if err != nil { + return []string{}, err + } + return strings.Split(string(bytes), "\n"), nil +} + +func affected(config utils.Config, diffs []string) ([]string, error) { + changedUnique := make(map[string]bool) + for _, diff := range diffs { + if !config.Matches(diff) { + continue + } + pkg := config.FindPackage(diff) + if slices.Contains(config.ExcludePackages, pkg) { + continue + } + changedUnique[pkg] = true + } + + changed := make([]string, 0, len(changedUnique)) + for pkg := range changedUnique { + changed = append(changed, pkg) + } + + if slices.Contains(changed, ".") { + return utils.FindAllPackages(".", config) + } + return changed, nil +} diff --git a/.github/workflows/samples-tools/cmd/affected/main.go b/.github/workflows/samples-tools/cmd/affected/main.go deleted file mode 100644 index 0ab677eda7..0000000000 --- a/.github/workflows/samples-tools/cmd/affected/main.go +++ /dev/null @@ -1,110 +0,0 @@ -/* - Copyright 2024 Google LLC - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package main - -import ( - "encoding/json" - "flag" - "fmt" - "os" - "slices" - "strings" - - "samples-tools/pkg/utils" -) - -func main() { - configFile := flag.String("config", "", "path to the config file") - diffsFile := flag.String("diffs", "", "path to the diffs file") - flag.Parse() - - if *configFile == "" { - fmt.Fprintf(os.Stderr, "config file is required, please pass -config=path/to/config.jsonc\n") - os.Exit(1) - } - - if *diffsFile == "" { - fmt.Fprintf(os.Stderr, "diffs file is required, please pass -diffs=path/to/diffs.txt\n") - os.Exit(1) - } - - config, err := utils.LoadConfig(*configFile) - if err != nil { - fmt.Fprintf(os.Stderr, "error loading the config file, make sure it exists and it's valid: %v\n", err) - os.Exit(1) - } - - diffs, err := readDiffs(*diffsFile) - if err != nil { - fmt.Fprintf(os.Stderr, "error getting the diffs: %v\n", err) - os.Exit(1) - } - - packages, err := affected(config, diffs) - if err != nil { - fmt.Fprintf(os.Stderr, "error finding the affected packages: %v\n", err) - os.Exit(1) - } - if len(packages) > 256 { - fmt.Fprintf(os.Stderr, - "GitHub Actions only supports up to 256 packages, got %v packages, for more details see:\n%v\n", - len(packages), - "https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow", - ) - os.Exit(1) - } - - matrix, err := json.Marshal(packages) - if err != nil { - fmt.Fprintf(os.Stderr, "error marshaling to JSON: %v\n", err) - os.Exit(1) - } - - fmt.Println(string(matrix)) -} - -func readDiffs(path string) ([]string, error) { - bytes, err := os.ReadFile(path) - if err != nil { - return []string{}, err - } - return strings.Split(string(bytes), "\n"), nil -} - -func affected(config utils.Config, diffs []string) ([]string, error) { - changedUnique := make(map[string]bool) - for _, diff := range diffs { - if !config.Matches(diff) { - continue - } - pkg := config.FindPackage(diff) - if slices.Contains(config.ExcludePackages, pkg) { - continue - } - changedUnique[pkg] = true - } - - changed := make([]string, 0, len(changedUnique)) - for pkg := range changedUnique { - changed = append(changed, pkg) - } - - if slices.Contains(changed, ".") { - return utils.FindAllPackages(".", config) - } - return changed, nil -} diff --git a/.github/workflows/samples-tools/cmd/main.go b/.github/workflows/samples-tools/cmd/main.go new file mode 100644 index 0000000000..6e165b7db3 --- /dev/null +++ b/.github/workflows/samples-tools/cmd/main.go @@ -0,0 +1,23 @@ +/* + Copyright 2024 Google LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package main + +import "flag" + +func main() { + flag.Parse() +} diff --git a/.github/workflows/samples-tools/cmd/run-all/main.go b/.github/workflows/samples-tools/cmd/run-all.go similarity index 60% rename from .github/workflows/samples-tools/cmd/run-all/main.go rename to .github/workflows/samples-tools/cmd/run-all.go index 42333a70b8..d72dfb7965 100644 --- a/.github/workflows/samples-tools/cmd/run-all/main.go +++ b/.github/workflows/samples-tools/cmd/run-all.go @@ -17,7 +17,6 @@ package main import ( - "flag" "fmt" "os" "os/exec" @@ -25,41 +24,41 @@ import ( "strings" ) -func main() { - configFile := flag.String("config", "", "path to the config file") - parallel := flag.Bool("parallel", false, "set to true to run tests in parallel") - flag.Parse() +// func main() { +// configFile := flag.String("config", "", "path to the config file") +// parallel := flag.Bool("parallel", false, "set to true to run tests in parallel") +// flag.Parse() - if *configFile == "" { - fmt.Fprintf(os.Stderr, "config file is required, please pass -config=path/to/config.jsonc\n") - os.Exit(1) - } +// if *configFile == "" { +// fmt.Fprintf(os.Stderr, "config file is required, please pass -config=path/to/config.jsonc\n") +// os.Exit(1) +// } - cmd := flag.Arg(0) - if cmd == "" { - fmt.Fprintf(os.Stderr, "command is required, use positional arguments with %%s for the package placeholder\n") - os.Exit(1) - } +// cmd := flag.Arg(0) +// if cmd == "" { +// fmt.Fprintf(os.Stderr, "command is required, use positional arguments with %%s for the package placeholder\n") +// os.Exit(1) +// } - args := flag.Args()[1:] +// args := flag.Args()[1:] - config, err := utils.LoadConfig(*configFile) - if err != nil { - fmt.Fprintf(os.Stderr, "error loading the config file, make sure it exists and it's valid: %v\n", err) - os.Exit(1) - } +// config, err := utils.LoadConfig(*configFile) +// if err != nil { +// fmt.Fprintf(os.Stderr, "error loading the config file, make sure it exists and it's valid: %v\n", err) +// os.Exit(1) +// } - packages, err := utils.FindAllPackages(".", config) - if err != nil { - fmt.Fprintf(os.Stderr, "error finding packages: %v\n", err) - } +// packages, err := utils.FindAllPackages(".", config) +// if err != nil { +// fmt.Fprintf(os.Stderr, "error finding packages: %v\n", err) +// } - failed := runAll(packages, cmd, args, *parallel) - fmt.Printf("Total tests: %v\nFailed tests: %v\n", len(packages), failed) - if failed > 0 { - os.Exit(1) - } -} +// failed := runAll(packages, cmd, args, *parallel) +// fmt.Printf("Total tests: %v\nFailed tests: %v\n", len(packages), failed) +// if failed > 0 { +// os.Exit(1) +// } +// } func runAll(packages []string, cmd string, args []string, parallel bool) int { failures := utils.Map(parallel, packages, func(pkg string) string { diff --git a/.github/workflows/samples-tools/pkg/utils/config.go b/.github/workflows/samples-tools/pkg/utils/config.go index 0bb5593d85..5d4d1672ef 100644 --- a/.github/workflows/samples-tools/pkg/utils/config.go +++ b/.github/workflows/samples-tools/pkg/utils/config.go @@ -42,19 +42,21 @@ type Config struct { var multiLineCommentsRegex = regexp.MustCompile(`(?s)\s*/\*.*?\*/\s*`) var singleLineCommentsRegex = regexp.MustCompile(`\s*//.*\s*`) +// LoadConfig loads the config from the given path. func LoadConfig(path string) (Config, error) { bytes, err := os.ReadFile(path) if err != nil { return Config{}, err } - config, err := ParseConfig(bytes) + config, err := parseConfig(bytes) if err != nil { return Config{}, err } return config, nil } -func ParseConfig(source []byte) (Config, error) { +// parseConfig parses the config from the given source. +func parseConfig(source []byte) (Config, error) { var config Config err := json.Unmarshal(StripComments(source), &config) if err != nil { @@ -66,11 +68,13 @@ func ParseConfig(source []byte) (Config, error) { return config, nil } +// StripComments removes comments from the given source. func StripComments(src []byte) []byte { src = multiLineCommentsRegex.ReplaceAll(src, []byte{}) return singleLineCommentsRegex.ReplaceAll(src, []byte{}) } +// match returns true if the path matches any of the patterns. func match(patterns []string, path string) bool { filename := filepath.Base(path) for _, pattern := range patterns { @@ -84,10 +88,12 @@ func match(patterns []string, path string) bool { return false } +// Matches returns true if the path matches the config. func (c Config) Matches(path string) bool { return match(c.Match, path) && !match(c.Ignore, path) } +// IsPackageDir returns true if the path is a package directory. func (c Config) IsPackageDir(dir string) bool { for _, filename := range c.Package { packageFile := filepath.Join(dir, filename) @@ -98,6 +104,7 @@ func (c Config) IsPackageDir(dir string) bool { return false } +// FindPackage returns the package name for the given path. func (c Config) FindPackage(path string) string { dir := filepath.Dir(path) if dir == "." || c.IsPackageDir(dir) { @@ -106,6 +113,7 @@ func (c Config) FindPackage(path string) string { return c.FindPackage(dir) } +// fileExists returns true if the file exists. func fileExists(path string) bool { if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { return false diff --git a/.github/workflows/samples-tools/pkg/utils/map.go b/.github/workflows/samples-tools/pkg/utils/map.go index 282bc53aad..6b1aacc57a 100644 --- a/.github/workflows/samples-tools/pkg/utils/map.go +++ b/.github/workflows/samples-tools/pkg/utils/map.go @@ -18,6 +18,7 @@ package utils import "sync" +// Map applies a function to each item in a slice, it can be done in parallel. func Map[a, b any](parallel bool, items []a, fn func(a) b) []b { result := make([]b, len(items)) if parallel { diff --git a/.github/workflows/samples-tools/pkg/utils/packages.go b/.github/workflows/samples-tools/pkg/utils/packages.go index 2b19076a7c..60602a564e 100644 --- a/.github/workflows/samples-tools/pkg/utils/packages.go +++ b/.github/workflows/samples-tools/pkg/utils/packages.go @@ -22,6 +22,7 @@ import ( "slices" ) +// FindAllPackages finds all the packages in the given root directory. func FindAllPackages(root string, config Config) ([]string, error) { var packages []string err := fs.WalkDir(os.DirFS(root), ".", diff --git a/Makefile b/Makefile index 076f8efda1..fd98ddc7d9 100644 --- a/Makefile +++ b/Makefile @@ -12,26 +12,18 @@ dir ?= $(shell pwd) export GOOGLE_CLOUD_PROJECT = ${GOOGLE_SAMPLES_PROJECT} -install: - # Install root package dependencies, this incldues packages for testing. +build: npm install - - # Install the code sample dependencies. cd ${dir} npm install - -build: install npm run build --if-present test: check-env build cd ${dir} npm test -e2e-test: check-env build +lint: build cd ${dir} - npm run system-test - -lint: install npx gts fix npx gts lint From 0f60ed10abe88d71c95784ba2c68c1b823657b63 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 4 Oct 2024 13:54:57 -0700 Subject: [PATCH 094/127] move tools to single command --- .../workflows/samples-tools/cmd/affected.go | 66 ++------- .github/workflows/samples-tools/cmd/main.go | 134 +++++++++++++++++- .../workflows/samples-tools/cmd/run-all.go | 103 -------------- .github/workflows/samples-tools/cmd/runAll.go | 75 ++++++++++ .../workflows/samples-tools/pkg/utils/map.go | 40 ------ .github/workflows/scripts/nodejs-test.sh | 20 +++ 6 files changed, 235 insertions(+), 203 deletions(-) delete mode 100644 .github/workflows/samples-tools/cmd/run-all.go create mode 100644 .github/workflows/samples-tools/cmd/runAll.go delete mode 100644 .github/workflows/samples-tools/pkg/utils/map.go create mode 100644 .github/workflows/scripts/nodejs-test.sh diff --git a/.github/workflows/samples-tools/cmd/affected.go b/.github/workflows/samples-tools/cmd/affected.go index 85b773abc2..1b7767c69b 100644 --- a/.github/workflows/samples-tools/cmd/affected.go +++ b/.github/workflows/samples-tools/cmd/affected.go @@ -24,64 +24,6 @@ import ( "samples-tools/pkg/utils" ) -// func main() { -// configFile := flag.String("config", "", "path to the config file") -// diffsFile := flag.String("diffs", "", "path to the diffs file") -// flag.Parse() - -// if *configFile == "" { -// fmt.Fprintf(os.Stderr, "config file is required, please pass -config=path/to/config.jsonc\n") -// os.Exit(1) -// } - -// if *diffsFile == "" { -// fmt.Fprintf(os.Stderr, "diffs file is required, please pass -diffs=path/to/diffs.txt\n") -// os.Exit(1) -// } - -// config, err := utils.LoadConfig(*configFile) -// if err != nil { -// fmt.Fprintf(os.Stderr, "error loading the config file, make sure it exists and it's valid: %v\n", err) -// os.Exit(1) -// } - -// diffs, err := readDiffs(*diffsFile) -// if err != nil { -// fmt.Fprintf(os.Stderr, "error getting the diffs: %v\n", err) -// os.Exit(1) -// } - -// packages, err := affected(config, diffs) -// if err != nil { -// fmt.Fprintf(os.Stderr, "error finding the affected packages: %v\n", err) -// os.Exit(1) -// } -// if len(packages) > 256 { -// fmt.Fprintf(os.Stderr, -// "GitHub Actions only supports up to 256 packages, got %v packages, for more details see:\n%v\n", -// len(packages), -// "https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow", -// ) -// os.Exit(1) -// } - -// matrix, err := json.Marshal(packages) -// if err != nil { -// fmt.Fprintf(os.Stderr, "error marshaling to JSON: %v\n", err) -// os.Exit(1) -// } - -// fmt.Println(string(matrix)) -// } - -func readDiffs(path string) ([]string, error) { - bytes, err := os.ReadFile(path) - if err != nil { - return []string{}, err - } - return strings.Split(string(bytes), "\n"), nil -} - func affected(config utils.Config, diffs []string) ([]string, error) { changedUnique := make(map[string]bool) for _, diff := range diffs { @@ -105,3 +47,11 @@ func affected(config utils.Config, diffs []string) ([]string, error) { } return changed, nil } + +func readDiffs(path string) ([]string, error) { + bytes, err := os.ReadFile(path) + if err != nil { + return []string{}, err + } + return strings.Split(string(bytes), "\n"), nil +} diff --git a/.github/workflows/samples-tools/cmd/main.go b/.github/workflows/samples-tools/cmd/main.go index 6e165b7db3..f3eeccfcc1 100644 --- a/.github/workflows/samples-tools/cmd/main.go +++ b/.github/workflows/samples-tools/cmd/main.go @@ -16,8 +16,138 @@ package main -import "flag" +import ( + "encoding/json" + "fmt" + "os" + "samples-tools/pkg/utils" + "strings" +) func main() { - flag.Parse() + command := "" + if len(os.Args) > 1 { + command = os.Args[1] + } else { + fmt.Fprintf(os.Stderr, "āŒ no command specified\n") + printUsage(os.Stderr) + os.Exit(1) + } + + switch command { + case "affected": + configFile := "" + if len(os.Args) > 2 { + configFile = os.Args[2] + } else { + fmt.Fprintf(os.Stderr, "āŒ no config file specified\n") + printUsage(os.Stderr) + os.Exit(1) + } + + diffsFile := "" + if len(os.Args) > 3 { + diffsFile = os.Args[3] + } else { + fmt.Fprintf(os.Stderr, "āŒ no diffs file specified\n") + printUsage(os.Stderr) + os.Exit(1) + } + + affectedCmd(configFile, diffsFile) + + case "run-all": + configFile := "" + if len(os.Args) > 2 { + configFile = os.Args[2] + } else { + fmt.Fprintf(os.Stderr, "āŒ no config file specified\n") + printUsage(os.Stderr) + os.Exit(1) + } + + script := "" + if len(os.Args) > 3 { + script = os.Args[3] + } else { + fmt.Fprintf(os.Stderr, "āŒ no script file specified\n") + printUsage(os.Stderr) + os.Exit(1) + } + + runAllCmd(configFile, script) + + default: + fmt.Fprintf(os.Stderr, "āŒ unknown command: %s\n", command) + printUsage(os.Stderr) + os.Exit(1) + } +} + +func printUsage(f *os.File) { + fmt.Fprintf(f, "usage: tools ...\n") + fmt.Fprintf(f, "\n") + fmt.Fprintf(f, "commands:\n") + fmt.Fprintf(f, " affected path/to/config.jsonc path/to/diffs.txt\n") + fmt.Fprintf(f, " run-all path/to/config.jsonc path/to/script.sh\n") +} + +func affectedCmd(configFile string, diffsFile string) { + config, err := utils.LoadConfig(configFile) + if err != nil { + fmt.Fprintf(os.Stderr, "āŒ error loading the config file\n%v\n", err) + os.Exit(1) + } + + diffs, err := readDiffs(diffsFile) + if err != nil { + fmt.Fprintf(os.Stderr, "āŒ error getting the diffs\n%v\n", err) + os.Exit(1) + } + + packages, err := affected(config, diffs) + if err != nil { + fmt.Fprintf(os.Stderr, "āŒ error finding the affected packages\n%v\n", err) + os.Exit(1) + } + if len(packages) > 256 { + fmt.Fprintf(os.Stderr, + "āŒ Error: GitHub Actions only supports up to 256 packages, got %v packages, for more details see:\n%v\n", + len(packages), + "https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow", + ) + os.Exit(1) + } + + packagesJson, err := json.Marshal(packages) + if err != nil { + fmt.Fprintf(os.Stderr, "āŒ error marshaling packages to JSON\n%v\n", err) + os.Exit(1) + } + + fmt.Println(string(packagesJson)) +} + +func runAllCmd(configFile string, script string) { + config, err := utils.LoadConfig(configFile) + if err != nil { + fmt.Fprintf(os.Stderr, "āŒ error loading the config file\n%v\n", err) + os.Exit(1) + } + + packages, err := utils.FindAllPackages(".", config) + if err != nil { + fmt.Fprintf(os.Stderr, "āŒ error finding packages\n%v\n", err) + } + + parallel := false + failed := runAll(packages, script, parallel) + + fmt.Printf(strings.Repeat("-", 80) + "\n") + fmt.Printf("Total tests: %v\n", len(packages)) + fmt.Printf("Failed tests: %v\n", failed) + + if failed > 0 { + os.Exit(1) + } } diff --git a/.github/workflows/samples-tools/cmd/run-all.go b/.github/workflows/samples-tools/cmd/run-all.go deleted file mode 100644 index d72dfb7965..0000000000 --- a/.github/workflows/samples-tools/cmd/run-all.go +++ /dev/null @@ -1,103 +0,0 @@ -/* - Copyright 2024 Google LLC - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package main - -import ( - "fmt" - "os" - "os/exec" - "samples-tools/pkg/utils" - "strings" -) - -// func main() { -// configFile := flag.String("config", "", "path to the config file") -// parallel := flag.Bool("parallel", false, "set to true to run tests in parallel") -// flag.Parse() - -// if *configFile == "" { -// fmt.Fprintf(os.Stderr, "config file is required, please pass -config=path/to/config.jsonc\n") -// os.Exit(1) -// } - -// cmd := flag.Arg(0) -// if cmd == "" { -// fmt.Fprintf(os.Stderr, "command is required, use positional arguments with %%s for the package placeholder\n") -// os.Exit(1) -// } - -// args := flag.Args()[1:] - -// config, err := utils.LoadConfig(*configFile) -// if err != nil { -// fmt.Fprintf(os.Stderr, "error loading the config file, make sure it exists and it's valid: %v\n", err) -// os.Exit(1) -// } - -// packages, err := utils.FindAllPackages(".", config) -// if err != nil { -// fmt.Fprintf(os.Stderr, "error finding packages: %v\n", err) -// } - -// failed := runAll(packages, cmd, args, *parallel) -// fmt.Printf("Total tests: %v\nFailed tests: %v\n", len(packages), failed) -// if failed > 0 { -// os.Exit(1) -// } -// } - -func runAll(packages []string, cmd string, args []string, parallel bool) int { - failures := utils.Map(parallel, packages, func(pkg string) string { - return runOne(pkg, cmd, args) - }) - - failed := 0 - for _, failure := range failures { - if failure != "" { - failed++ - } - } - return failed -} - -func runOne(pkg string, cmd string, args []string) string { - // TODO(dcavazos): measure time spent on each test and print it along the results - cmdArgs := make([]string, len(args)) - for i, arg := range args { - if strings.Contains(arg, "%s") { - cmdArgs[i] = fmt.Sprintf(arg, pkg) - } else { - cmdArgs[i] = arg - } - } - output, err := exec.Command(cmd, cmdArgs...).CombinedOutput() - if err != nil { - fmt.Fprintf(os.Stderr, "%v\nāŒ FAILED: %v\n%v\n> %v %v\n%v\n%v\n%v\n\n", - strings.Repeat("=", 80), - pkg, - strings.Repeat("-", 80), - cmd, cmdArgs, - err, - string(output), - strings.Repeat("-", 80), - ) - return pkg - } else { - fmt.Printf("āœ… %v\n", pkg) - } - return "" -} diff --git a/.github/workflows/samples-tools/cmd/runAll.go b/.github/workflows/samples-tools/cmd/runAll.go new file mode 100644 index 0000000000..078a929239 --- /dev/null +++ b/.github/workflows/samples-tools/cmd/runAll.go @@ -0,0 +1,75 @@ +/* + Copyright 2024 Google LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package main + +import ( + "fmt" + "os" + "os/exec" + "strings" + "sync" +) + +// runAll runs all the commands in parallel. +func runAll(packages []string, script string, parallel bool) int { + failures := mapFn(parallel, packages, func(pkg string) string { + // TODO(dcavazos): measure time spent on each test and print it along the results + output, err := exec.Command("bash", script, pkg).CombinedOutput() + if err != nil { + fmt.Fprintf(os.Stderr, "%v\nāŒ FAILED: %v\n%v\n%v\n%v\n\n", + strings.Repeat("=", 80), + pkg, + strings.Repeat("-", 80), + err, + string(output), + ) + return pkg + } else { + fmt.Printf("āœ… %v\n", pkg) + } + return "" + }) + + failed := 0 + for _, failure := range failures { + if failure != "" { + failed++ + } + } + return failed +} + +// mapFn applies a function to each item in a slice, it can be done in parallel. +func mapFn[a, b any](parallel bool, items []a, fn func(a) b) []b { + result := make([]b, len(items)) + if parallel { + var wg sync.WaitGroup + for i, item := range items { + wg.Add(1) + go func(i int, item a) { + defer wg.Done() + result[i] = fn(item) + }(i, item) + } + wg.Wait() + } else { + for i, item := range items { + result[i] = fn(item) + } + } + return result +} diff --git a/.github/workflows/samples-tools/pkg/utils/map.go b/.github/workflows/samples-tools/pkg/utils/map.go deleted file mode 100644 index 6b1aacc57a..0000000000 --- a/.github/workflows/samples-tools/pkg/utils/map.go +++ /dev/null @@ -1,40 +0,0 @@ -/* - Copyright 2024 Google LLC - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package utils - -import "sync" - -// Map applies a function to each item in a slice, it can be done in parallel. -func Map[a, b any](parallel bool, items []a, fn func(a) b) []b { - result := make([]b, len(items)) - if parallel { - var wg sync.WaitGroup - for i, item := range items { - wg.Add(1) - go func(i int, item a) { - defer wg.Done() - result[i] = fn(item) - }(i, item) - } - wg.Wait() - } else { - for i, item := range items { - result[i] = fn(item) - } - } - return result -} diff --git a/.github/workflows/scripts/nodejs-test.sh b/.github/workflows/scripts/nodejs-test.sh new file mode 100644 index 0000000000..422f3a5677 --- /dev/null +++ b/.github/workflows/scripts/nodejs-test.sh @@ -0,0 +1,20 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e # exit on error +set -x # print commands as they are executed + +package=$1 + +make test dir=$package From f71163df2a74aa6369d858fd893c7f1ebf4b1302 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 4 Oct 2024 14:01:01 -0700 Subject: [PATCH 095/127] update workflow to new command --- .github/workflows/experimental.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 2cd07cfb61..10a16657a3 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -43,13 +43,13 @@ jobs: - uses: actions/setup-go@v5 with: go-version: ${{ env.GO_VERSION }} - - run: go install ./cmd/affected + - run: go build -o ${{ github.workspace }}/tools ./cmd/... working-directory: .github/workflows/samples-tools - name: Get diffs run: git --no-pager diff --name-only HEAD origin/main | tee diffs.txt - name: Find Node.js affected packages id: nodejs - run: echo "packages=$(affected -config .github/workflows/config/nodejs.jsonc -diffs=diffs.txt)" | tee -a $GITHUB_OUTPUT + run: echo "packages=$(./tools affected .github/workflows/config/nodejs.jsonc diffs.txt)" | tee -a $GITHUB_OUTPUT nodejs-lint: name: Node.js lint @@ -86,7 +86,7 @@ jobs: workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com access_token_lifetime: 600s # 10 minutes - - run: make test dir=${{ matrix.package }} + - run: bash ./github/workflows/scripts/nodejs-test.sh ${{ matrix.package }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 @@ -112,8 +112,8 @@ jobs: # workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider # service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com # access_token_lifetime: 600s # 10 minutes - # - run: go install ./cmd/run-all + # - run: go build -o ${{ github.workspace }}/tools ./cmd/... # working-directory: .github/workflows/samples-tools - # - run: run-all -config=.github/workflows/config/nodejs.jsonc make clean-test dir=%s + # - run: ./tools run-all .github/workflows/config/nodejs.jsonc ./github/workflows/scripts/nodejs-test.sh # env: # GOOGLE_SAMPLES_PROJECT: long-door-651 From 400db0e07de9c7ffcdbf15e8730413d106773041 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 4 Oct 2024 14:02:50 -0700 Subject: [PATCH 096/127] Add comment for cron schedule Co-authored-by: Nim Jayawardena --- .github/workflows/experimental.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 10a16657a3..2955009d1b 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -24,7 +24,7 @@ on: - synchronize - labeled schedule: - - cron: 0 0 * * 0 + - cron: 0 0 * * 0 # Run at midnight (00:00) every Sunday env: GO_VERSION: ^1.22.0 From 26b5d2bf89fb5726772926c8501353df1e8efc85 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 4 Oct 2024 14:08:09 -0700 Subject: [PATCH 097/127] run nightlies each on their own job --- .github/workflows/experimental.yaml | 30 +---------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 2955009d1b..8efe7be5ed 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -72,7 +72,7 @@ jobs: strategy: fail-fast: false matrix: - package: ${{ fromJson(github.event_name == 'pull_request' && needs.affected.outputs.nodejs || '[]') }} + package: ${{ fromJson(needs.affected.outputs.nodejs) }} steps: - uses: actions/checkout@v4 with: @@ -89,31 +89,3 @@ jobs: - run: bash ./github/workflows/scripts/nodejs-test.sh ${{ matrix.package }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 - - # nodejs-nightly: - # name: Node.js 20 - # runs-on: ubuntu-latest - # permissions: - # id-token: write # needed for google-github-actions/auth - # strategy: - # matrix: - # run: ${{ fromJson(github.event_name == 'schedule' && '["nightly tests"]' || '[]') }} - # steps: - # - uses: actions/checkout@v4 - # - uses: actions/setup-node@v4 - # with: - # node-version: 20 - # - uses: actions/setup-go@v5 - # with: - # go-version: ${{ env.GO_VERSION }} - # - uses: google-github-actions/auth@v2 - # with: - # project_id: long-door-651 - # workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider - # service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com - # access_token_lifetime: 600s # 10 minutes - # - run: go build -o ${{ github.workspace }}/tools ./cmd/... - # working-directory: .github/workflows/samples-tools - # - run: ./tools run-all .github/workflows/config/nodejs.jsonc ./github/workflows/scripts/nodejs-test.sh - # env: - # GOOGLE_SAMPLES_PROJECT: long-door-651 From 27841193f63a2be589835b4732d13c6a2777fd7c Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 4 Oct 2024 14:09:15 -0700 Subject: [PATCH 098/127] fix typo on path --- .github/workflows/experimental.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 8efe7be5ed..b185084805 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -86,6 +86,6 @@ jobs: workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com access_token_lifetime: 600s # 10 minutes - - run: bash ./github/workflows/scripts/nodejs-test.sh ${{ matrix.package }} + - run: bash .github/workflows/scripts/nodejs-test.sh ${{ matrix.package }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 From cc7d9a0456873541b51a47fae4875840498ec8c3 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 4 Oct 2024 14:14:11 -0700 Subject: [PATCH 099/127] better messages --- .github/workflows/experimental.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index b185084805..9c7fe9aa29 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -86,6 +86,7 @@ jobs: workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com access_token_lifetime: 600s # 10 minutes - - run: bash .github/workflows/scripts/nodejs-test.sh ${{ matrix.package }} + - name: Test ${{ matrix.package }} + run: bash .github/workflows/scripts/nodejs-test.sh ${{ matrix.package }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 From d944a39992cf3b34ef81e4c450e7c384556421ae Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Fri, 4 Oct 2024 15:20:45 -0700 Subject: [PATCH 100/127] exclude talent package --- .github/workflows/config/nodejs.jsonc | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/config/nodejs.jsonc b/.github/workflows/config/nodejs.jsonc index 28ddb66141..ec77b6ceb5 100644 --- a/.github/workflows/config/nodejs.jsonc +++ b/.github/workflows/config/nodejs.jsonc @@ -93,6 +93,7 @@ "security-center/snippets", // Error: 3 INVALID_ARGUMENT: Fail to resolve resource 'organizations/undefined/locations/global' "speech", // AssertionError: expected 'Transcription: Okay, I\'m here.\n Hi…' to match /Terrific. It's on the way./ "storagetransfer", // CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1 + "talent", // AssertionError: expected '' to match /Job summary/ "translate", // AssertionError: expected 'Languages:\n{ code: \'ab\', name: \'A…' to match /{ code: 'af', name: 'afrikĆ”ans' }/ "video-intelligence", // PERMISSION_DENIED: The caller does not have permission "vision", // REDIS: Error: connect ECONNREFUSED 127.0.0.1:6379 From 51bc759a87e72fc66b22e1b7e9f759ce83f3c58c Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 7 Oct 2024 13:00:39 -0700 Subject: [PATCH 101/127] document affected --- .github/workflows/samples-tools/README.md | 63 +++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/.github/workflows/samples-tools/README.md b/.github/workflows/samples-tools/README.md index a8d68deaad..4edde96c16 100644 --- a/.github/workflows/samples-tools/README.md +++ b/.github/workflows/samples-tools/README.md @@ -2,6 +2,69 @@ This is a collection of tools used for Cloud Samples maintenance and infrastructure. +## Config files + +For this tools, we refer to a **package** as an isolated directory, which contains a "package file". +For example, `package.json` in Node.js, `requirements.txt` in Python, `go.mod` in Go, or `pom.xml` in Java. + +Each language has different configurations. +We define them in config files in the repository, this way the tooling keeps language agnostic and each repository can have different configurations. + +The config file can be a `.json` file, or a `.jsonc` (JSON with comments) file. +For `.jsonc` files, it supports both `// single line comments` and `/* multi-line comments */`. + +For example: + +```jsonc +{ + // The package file where the tests should be run (required). + "package-file": "package.json", + + // Match diffs only on .js and .ts files + // Defaults to match all files. + "match": ["*.js", "*.ts"], + + // Ignore diffs on the README, text files, and anything under node_modules/. + // Defaults to not ignore anything. + "ignore": ["README.md", "*.txt", "node_modules/"], + + // Skip these packages, these could be handled by a different config. + // Defaults to not exclude anything. + "exclude-packages": ["path/to/slow-to-test", "special-config-package"] +} +``` + +For more information, see [`pkg/utils/config.go`](pkg/utils/config.go). + +## Building + +To build the tools, we must change to the directory where the tools package is defined. +We can run it in a subshell using parentheses to keep our working directory from changing. + +```sh +(cd .github/workflows/samples-tools && go build -o /tmp/tools ./cmd/*) +``` + ## Finding affected packages +> This must run at the repository root directory. + +First, generate a file with all the diffs. +This file should be one file per line. + +You can use `git diff` to test on files that have changed in your branch. +You can also create the file manually if you want to test something without commiting changes to your branch. + +```sh +git --no-pager diff --name-only HEAD origin/main | tee /tmp/diffs.txt +``` + +Now we can check which packages have been affected. + +```sh +/tmp/tools affected .github/config/nodejs.jsonc /tmp/diffs.txt +``` + ## Running on all packages + +> This must run at the repository root directory. From da62a12b70db48726d39a1246811c574e139425d Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 7 Oct 2024 13:00:55 -0700 Subject: [PATCH 102/127] reorganize files --- .github/{workflows => }/config/nodejs.jsonc | 2 +- .github/workflows/experimental.yaml | 4 ++-- .github/workflows/samples-tools/cmd/main.go | 12 +++++------ .../samples-tools/pkg/utils/config.go | 11 ++++++---- .github/workflows/scripts/nodejs-test.sh | 20 ------------------- 5 files changed, 16 insertions(+), 33 deletions(-) rename .github/{workflows => }/config/nodejs.jsonc (99%) delete mode 100644 .github/workflows/scripts/nodejs-test.sh diff --git a/.github/workflows/config/nodejs.jsonc b/.github/config/nodejs.jsonc similarity index 99% rename from .github/workflows/config/nodejs.jsonc rename to .github/config/nodejs.jsonc index ec77b6ceb5..6edc1fe3d5 100644 --- a/.github/workflows/config/nodejs.jsonc +++ b/.github/config/nodejs.jsonc @@ -15,6 +15,7 @@ */ { + "package-file": ["package.json"], "ignore": [ ".eslintignore", ".eslintrc.json", @@ -45,7 +46,6 @@ "owlbot.py", "renovate.json" ], - "package": ["package.json"], "exclude-packages": [ "ai-platform/snippets", // PERMISSION_DENIED: Permission denied: Consumer 'projects/undefined' has been suspended. "appengine/analytics", // [ERR_REQUIRE_ESM]: require() of ES Module diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 9c7fe9aa29..9adc56a9bf 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -49,7 +49,7 @@ jobs: run: git --no-pager diff --name-only HEAD origin/main | tee diffs.txt - name: Find Node.js affected packages id: nodejs - run: echo "packages=$(./tools affected .github/workflows/config/nodejs.jsonc diffs.txt)" | tee -a $GITHUB_OUTPUT + run: echo "packages=$(./tools affected .github/config/nodejs.jsonc diffs.txt)" | tee -a $GITHUB_OUTPUT nodejs-lint: name: Node.js lint @@ -87,6 +87,6 @@ jobs: service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com access_token_lifetime: 600s # 10 minutes - name: Test ${{ matrix.package }} - run: bash .github/workflows/scripts/nodejs-test.sh ${{ matrix.package }} + run: make test dir=${{ matrix.package }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 diff --git a/.github/workflows/samples-tools/cmd/main.go b/.github/workflows/samples-tools/cmd/main.go index f3eeccfcc1..50f9655538 100644 --- a/.github/workflows/samples-tools/cmd/main.go +++ b/.github/workflows/samples-tools/cmd/main.go @@ -95,19 +95,19 @@ func printUsage(f *os.File) { func affectedCmd(configFile string, diffsFile string) { config, err := utils.LoadConfig(configFile) if err != nil { - fmt.Fprintf(os.Stderr, "āŒ error loading the config file\n%v\n", err) + fmt.Fprintf(os.Stderr, "āŒ error loading the config file: %v\n%v\n", configFile, err) os.Exit(1) } diffs, err := readDiffs(diffsFile) if err != nil { - fmt.Fprintf(os.Stderr, "āŒ error getting the diffs\n%v\n", err) + fmt.Fprintf(os.Stderr, "āŒ error getting the diffs: %v\n%v\n", diffsFile, err) os.Exit(1) } packages, err := affected(config, diffs) if err != nil { - fmt.Fprintf(os.Stderr, "āŒ error finding the affected packages\n%v\n", err) + fmt.Fprintf(os.Stderr, "āŒ error finding the affected packages.\n%v\n", err) os.Exit(1) } if len(packages) > 256 { @@ -121,7 +121,7 @@ func affectedCmd(configFile string, diffsFile string) { packagesJson, err := json.Marshal(packages) if err != nil { - fmt.Fprintf(os.Stderr, "āŒ error marshaling packages to JSON\n%v\n", err) + fmt.Fprintf(os.Stderr, "āŒ error marshaling packages to JSON.\n%v\n", err) os.Exit(1) } @@ -131,13 +131,13 @@ func affectedCmd(configFile string, diffsFile string) { func runAllCmd(configFile string, script string) { config, err := utils.LoadConfig(configFile) if err != nil { - fmt.Fprintf(os.Stderr, "āŒ error loading the config file\n%v\n", err) + fmt.Fprintf(os.Stderr, "āŒ error loading the config file: %v\n%v\n", configFile, err) os.Exit(1) } packages, err := utils.FindAllPackages(".", config) if err != nil { - fmt.Fprintf(os.Stderr, "āŒ error finding packages\n%v\n", err) + fmt.Fprintf(os.Stderr, "āŒ error finding packages.\n%v\n", err) } parallel := false diff --git a/.github/workflows/samples-tools/pkg/utils/config.go b/.github/workflows/samples-tools/pkg/utils/config.go index 5d4d1672ef..3081825e6b 100644 --- a/.github/workflows/samples-tools/pkg/utils/config.go +++ b/.github/workflows/samples-tools/pkg/utils/config.go @@ -26,15 +26,15 @@ import ( ) type Config struct { + // Filename to look for the root of a package. + PackageFile []string `json:"package-file"` + // Pattern to match filenames or directories. Match []string `json:"match"` // Pattern to ignore filenames or directories. Ignore []string `json:"ignore"` - // Filename to look for the root of a package. - Package []string `json:"package"` - // Packages to always exclude. ExcludePackages []string `json:"exclude-packages"` } @@ -62,6 +62,9 @@ func parseConfig(source []byte) (Config, error) { if err != nil { return Config{}, err } + if config.PackageFile == nil { + return config, errors.New("package-file is required") + } if config.Match == nil { config.Match = []string{"*"} } @@ -95,7 +98,7 @@ func (c Config) Matches(path string) bool { // IsPackageDir returns true if the path is a package directory. func (c Config) IsPackageDir(dir string) bool { - for _, filename := range c.Package { + for _, filename := range c.PackageFile { packageFile := filepath.Join(dir, filename) if fileExists(packageFile) { return true diff --git a/.github/workflows/scripts/nodejs-test.sh b/.github/workflows/scripts/nodejs-test.sh deleted file mode 100644 index 422f3a5677..0000000000 --- a/.github/workflows/scripts/nodejs-test.sh +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright 2024 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -set -e # exit on error -set -x # print commands as they are executed - -package=$1 - -make test dir=$package From 82cb3ba4aabd7b63bbbe0ef79d38059620c971a5 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 7 Oct 2024 13:11:42 -0700 Subject: [PATCH 103/127] use script to run tests --- .github/scripts/nodejs-test.sh | 39 +++++++++++++++++++++++++++++ .github/workflows/experimental.yaml | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 .github/scripts/nodejs-test.sh diff --git a/.github/scripts/nodejs-test.sh b/.github/scripts/nodejs-test.sh new file mode 100644 index 0000000000..9a99ac421b --- /dev/null +++ b/.github/scripts/nodejs-test.sh @@ -0,0 +1,39 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e # exit on error +set -x # print commands as they are executed + +package=$1 + +if [ -z $package ]; then + echo "a package to test is required" + exit 1 +fi + +if [ -z $GOOGLE_SAMPLES_PROJECT ]; then + echo "GOOGLE_SAMPLES_PROJECT environment variable is required" + exit 1 +fi + +# Install the root dependencies used for tests. +npm install + +# Install the package dependencies. +cd $package +npm install +npm run build --if-present + +# Run the tests. +npm test diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 9adc56a9bf..d947cf4d9f 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -87,6 +87,6 @@ jobs: service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com access_token_lifetime: 600s # 10 minutes - name: Test ${{ matrix.package }} - run: make test dir=${{ matrix.package }} + run: bash .github/scripts/nodejs-test.sh ${{ matrix.package }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 From 91f2e62e45ece1320e61de30533ad8c5e26c93ab Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 7 Oct 2024 13:36:37 -0700 Subject: [PATCH 104/127] run all tests --- .github/scripts/nodejs-test.sh | 3 -- .github/workflows/experimental.yaml | 35 ++++++++++++++++++- .github/workflows/samples-tools/cmd/main.go | 4 +-- .github/workflows/samples-tools/cmd/runAll.go | 28 ++++++--------- 4 files changed, 46 insertions(+), 24 deletions(-) diff --git a/.github/scripts/nodejs-test.sh b/.github/scripts/nodejs-test.sh index 9a99ac421b..07d5a6985b 100644 --- a/.github/scripts/nodejs-test.sh +++ b/.github/scripts/nodejs-test.sh @@ -27,9 +27,6 @@ if [ -z $GOOGLE_SAMPLES_PROJECT ]; then exit 1 fi -# Install the root dependencies used for tests. -npm install - # Install the package dependencies. cd $package npm install diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index d947cf4d9f..fe9850e32e 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -72,7 +72,8 @@ jobs: strategy: fail-fast: false matrix: - package: ${{ fromJson(needs.affected.outputs.nodejs) }} + # package: ${{ fromJson(needs.affected.outputs.nodejs) }} + package: [] steps: - uses: actions/checkout@v4 with: @@ -90,3 +91,35 @@ jobs: run: bash .github/scripts/nodejs-test.sh ${{ matrix.package }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 + + nodejs-nightly: + name: Node.js 20 + runs-on: ubuntu-latest + permissions: + id-token: write # needed for google-github-actions/auth + strategy: + fail-fast: false + matrix: + run: ['nightly tests'] + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} + - uses: actions/setup-node@v4 + with: + node-version: 20 + - uses: google-github-actions/auth@v2 + with: + project_id: long-door-651 + workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider + service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com + access_token_lifetime: 600s # 10 minutes + - uses: actions/setup-go@v5 + with: + go-version: ${{ env.GO_VERSION }} + - run: go build -o ${{ github.workspace }}/tools ./cmd/... + working-directory: .github/workflows/samples-tools + - name: Run all tests + run: ./tools run-all .github/config/nodejs.jsonc .github/scripts/nodejs-test.sh + env: + GOOGLE_SAMPLES_PROJECT: long-door-651 diff --git a/.github/workflows/samples-tools/cmd/main.go b/.github/workflows/samples-tools/cmd/main.go index 50f9655538..05e0e2ba08 100644 --- a/.github/workflows/samples-tools/cmd/main.go +++ b/.github/workflows/samples-tools/cmd/main.go @@ -140,8 +140,8 @@ func runAllCmd(configFile string, script string) { fmt.Fprintf(os.Stderr, "āŒ error finding packages.\n%v\n", err) } - parallel := false - failed := runAll(packages, script, parallel) + maxGoroutines := 10 + failed := runAll(packages, script, maxGoroutines) fmt.Printf(strings.Repeat("-", 80) + "\n") fmt.Printf("Total tests: %v\n", len(packages)) diff --git a/.github/workflows/samples-tools/cmd/runAll.go b/.github/workflows/samples-tools/cmd/runAll.go index 078a929239..5c8275f0f1 100644 --- a/.github/workflows/samples-tools/cmd/runAll.go +++ b/.github/workflows/samples-tools/cmd/runAll.go @@ -21,12 +21,11 @@ import ( "os" "os/exec" "strings" - "sync" ) // runAll runs all the commands in parallel. -func runAll(packages []string, script string, parallel bool) int { - failures := mapFn(parallel, packages, func(pkg string) string { +func runAll(packages []string, script string, maxGoroutines int) int { + failures := mapParallel(maxGoroutines, packages, func(pkg string) string { // TODO(dcavazos): measure time spent on each test and print it along the results output, err := exec.Command("bash", script, pkg).CombinedOutput() if err != nil { @@ -53,23 +52,16 @@ func runAll(packages []string, script string, parallel bool) int { return failed } -// mapFn applies a function to each item in a slice, it can be done in parallel. -func mapFn[a, b any](parallel bool, items []a, fn func(a) b) []b { +// mapParallel applies a function in parallel to each item in a slice. +func mapParallel[a, b any](maxGoroutines int, items []a, fn func(a) b) []b { result := make([]b, len(items)) - if parallel { - var wg sync.WaitGroup - for i, item := range items { - wg.Add(1) - go func(i int, item a) { - defer wg.Done() - result[i] = fn(item) - }(i, item) - } - wg.Wait() - } else { - for i, item := range items { + guard := make(chan struct{}, maxGoroutines) + for i, item := range items { + guard <- struct{}{} // block if channel is filled + go func(i int, item a) { result[i] = fn(item) - } + <-guard + }(i, item) } return result } From 4033572eddfc93eedf943bc618f9baa327b18859 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 7 Oct 2024 13:40:53 -0700 Subject: [PATCH 105/127] fix syntax error --- .github/workflows/experimental.yaml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index fe9850e32e..3676a684a3 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -73,7 +73,8 @@ jobs: fail-fast: false matrix: # package: ${{ fromJson(needs.affected.outputs.nodejs) }} - package: [] + # package: ${{ fromJson(github.event_name == 'pull_request' && needs.affected.outputs.nodejs || '[]') }} + package: ${{ fromJson('[]') }} steps: - uses: actions/checkout@v4 with: @@ -100,7 +101,8 @@ jobs: strategy: fail-fast: false matrix: - run: ['nightly tests'] + # run: ${{ fromJson(github.event_name == 'schedule' && '["nightly tests"]' || '[]') }} + run: ${{ fromJson('["nightly tests"]') }} steps: - uses: actions/checkout@v4 with: From 9a7d17278d3e0395937fd6645f04269e9c342e66 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 7 Oct 2024 13:43:47 -0700 Subject: [PATCH 106/127] do not fail on npm install --- .github/scripts/nodejs-test.sh | 2 +- .github/workflows/samples-tools/cmd/main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/scripts/nodejs-test.sh b/.github/scripts/nodejs-test.sh index 07d5a6985b..0a4322a668 100644 --- a/.github/scripts/nodejs-test.sh +++ b/.github/scripts/nodejs-test.sh @@ -29,7 +29,7 @@ fi # Install the package dependencies. cd $package -npm install +npm install || true npm run build --if-present # Run the tests. diff --git a/.github/workflows/samples-tools/cmd/main.go b/.github/workflows/samples-tools/cmd/main.go index 05e0e2ba08..8f81a69eba 100644 --- a/.github/workflows/samples-tools/cmd/main.go +++ b/.github/workflows/samples-tools/cmd/main.go @@ -140,7 +140,7 @@ func runAllCmd(configFile string, script string) { fmt.Fprintf(os.Stderr, "āŒ error finding packages.\n%v\n", err) } - maxGoroutines := 10 + maxGoroutines := 32 failed := runAll(packages, script, maxGoroutines) fmt.Printf(strings.Repeat("-", 80) + "\n") From 34db331dfca289bd5e783bf9fe46edb1aed8a152 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 7 Oct 2024 13:51:20 -0700 Subject: [PATCH 107/127] print outputs immediately --- .github/workflows/samples-tools/cmd/main.go | 2 +- .github/workflows/samples-tools/cmd/runAll.go | 52 ++++++++----------- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/.github/workflows/samples-tools/cmd/main.go b/.github/workflows/samples-tools/cmd/main.go index 8f81a69eba..ea047ce5bb 100644 --- a/.github/workflows/samples-tools/cmd/main.go +++ b/.github/workflows/samples-tools/cmd/main.go @@ -140,7 +140,7 @@ func runAllCmd(configFile string, script string) { fmt.Fprintf(os.Stderr, "āŒ error finding packages.\n%v\n", err) } - maxGoroutines := 32 + maxGoroutines := 16 failed := runAll(packages, script, maxGoroutines) fmt.Printf(strings.Repeat("-", 80) + "\n") diff --git a/.github/workflows/samples-tools/cmd/runAll.go b/.github/workflows/samples-tools/cmd/runAll.go index 5c8275f0f1..5fefd9c341 100644 --- a/.github/workflows/samples-tools/cmd/runAll.go +++ b/.github/workflows/samples-tools/cmd/runAll.go @@ -25,23 +25,21 @@ import ( // runAll runs all the commands in parallel. func runAll(packages []string, script string, maxGoroutines int) int { - failures := mapParallel(maxGoroutines, packages, func(pkg string) string { - // TODO(dcavazos): measure time spent on each test and print it along the results - output, err := exec.Command("bash", script, pkg).CombinedOutput() - if err != nil { - fmt.Fprintf(os.Stderr, "%v\nāŒ FAILED: %v\n%v\n%v\n%v\n\n", - strings.Repeat("=", 80), - pkg, - strings.Repeat("-", 80), - err, - string(output), - ) - return pkg - } else { - fmt.Printf("āœ… %v\n", pkg) - } - return "" - }) + failures := make([]string, len(packages)) + guard := make(chan struct{}, maxGoroutines) + for i, pkg := range packages { + guard <- struct{}{} // block if channel is filled + go func() { + output, err := exec.Command("bash", script, pkg).CombinedOutput() + if err != nil { + printError(os.Stderr, pkg, err, string(output)) + failures[i] = pkg + } else { + fmt.Printf("āœ… %v\n", pkg) + } + <-guard + }() + } failed := 0 for _, failure := range failures { @@ -52,16 +50,12 @@ func runAll(packages []string, script string, maxGoroutines int) int { return failed } -// mapParallel applies a function in parallel to each item in a slice. -func mapParallel[a, b any](maxGoroutines int, items []a, fn func(a) b) []b { - result := make([]b, len(items)) - guard := make(chan struct{}, maxGoroutines) - for i, item := range items { - guard <- struct{}{} // block if channel is filled - go func(i int, item a) { - result[i] = fn(item) - <-guard - }(i, item) - } - return result +func printError(f *os.File, pkg string, err error, output string) { + fmt.Fprintf(f, "%v\nāŒ FAILED: %v\n%v\n%v\n%v\n\n", + strings.Repeat("=", 80), + pkg, + strings.Repeat("-", 80), + err, + string(output), + ) } From 069d376dc20e79d975161cf7fbc26029a82ec0d2 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 7 Oct 2024 14:03:35 -0700 Subject: [PATCH 108/127] increment failures atomically --- .github/workflows/samples-tools/README.md | 12 +++++++++++- .github/workflows/samples-tools/cmd/runAll.go | 18 ++++++------------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/.github/workflows/samples-tools/README.md b/.github/workflows/samples-tools/README.md index 4edde96c16..709c0805c2 100644 --- a/.github/workflows/samples-tools/README.md +++ b/.github/workflows/samples-tools/README.md @@ -30,7 +30,7 @@ For example: // Skip these packages, these could be handled by a different config. // Defaults to not exclude anything. - "exclude-packages": ["path/to/slow-to-test", "special-config-package"] + "exclude-packages": ["path/to/slow-to-test", "special-config-package"], } ``` @@ -60,6 +60,7 @@ git --no-pager diff --name-only HEAD origin/main | tee /tmp/diffs.txt ``` Now we can check which packages have been affected. +We pass the config file and the diffs file as positional arguments. ```sh /tmp/tools affected .github/config/nodejs.jsonc /tmp/diffs.txt @@ -68,3 +69,12 @@ Now we can check which packages have been affected. ## Running on all packages > This must run at the repository root directory. + +We pass the config file and a bash script to run as positional arguments. +The script must receive a single positional argument in `$1` as the package name. +For example, the script should be called like `bash path/to/my-script.sh path/to/package`. + +```sh +# To run all the Node.js tests. +/tmp/tools run-all ./github/config/nodejs.jsonc ./github/scripts/nodejs-test.sh +``` diff --git a/.github/workflows/samples-tools/cmd/runAll.go b/.github/workflows/samples-tools/cmd/runAll.go index 5fefd9c341..211de1c751 100644 --- a/.github/workflows/samples-tools/cmd/runAll.go +++ b/.github/workflows/samples-tools/cmd/runAll.go @@ -21,33 +21,27 @@ import ( "os" "os/exec" "strings" + "sync/atomic" ) // runAll runs all the commands in parallel. -func runAll(packages []string, script string, maxGoroutines int) int { - failures := make([]string, len(packages)) +func runAll(packages []string, script string, maxGoroutines int) int64 { + var failures int64 guard := make(chan struct{}, maxGoroutines) - for i, pkg := range packages { + for _, pkg := range packages { guard <- struct{}{} // block if channel is filled go func() { output, err := exec.Command("bash", script, pkg).CombinedOutput() if err != nil { printError(os.Stderr, pkg, err, string(output)) - failures[i] = pkg + atomic.AddInt64(&failures, 1) } else { fmt.Printf("āœ… %v\n", pkg) } <-guard }() } - - failed := 0 - for _, failure := range failures { - if failure != "" { - failed++ - } - } - return failed + return failures } func printError(f *os.File, pkg string, err error, output string) { From 8732c071feb9d85e631113c8b19c4aa67f72f16a Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 7 Oct 2024 14:07:16 -0700 Subject: [PATCH 109/127] disable nightly batch job --- .github/workflows/experimental.yaml | 71 ++++++++++++++--------------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 3676a684a3..3a93a9a178 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -24,7 +24,8 @@ on: - synchronize - labeled schedule: - - cron: 0 0 * * 0 # Run at midnight (00:00) every Sunday + # https://crontab.guru/#0_12_*_*_0 + - cron: 0 12 * * 0 # At 12:00 on Sunday env: GO_VERSION: ^1.22.0 @@ -72,9 +73,7 @@ jobs: strategy: fail-fast: false matrix: - # package: ${{ fromJson(needs.affected.outputs.nodejs) }} - # package: ${{ fromJson(github.event_name == 'pull_request' && needs.affected.outputs.nodejs || '[]') }} - package: ${{ fromJson('[]') }} + package: ${{ fromJson(github.event_name == 'pull_request' && needs.affected.outputs.nodejs || '[]') }} steps: - uses: actions/checkout@v4 with: @@ -93,35 +92,35 @@ jobs: env: GOOGLE_SAMPLES_PROJECT: long-door-651 - nodejs-nightly: - name: Node.js 20 - runs-on: ubuntu-latest - permissions: - id-token: write # needed for google-github-actions/auth - strategy: - fail-fast: false - matrix: - # run: ${{ fromJson(github.event_name == 'schedule' && '["nightly tests"]' || '[]') }} - run: ${{ fromJson('["nightly tests"]') }} - steps: - - uses: actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.sha }} - - uses: actions/setup-node@v4 - with: - node-version: 20 - - uses: google-github-actions/auth@v2 - with: - project_id: long-door-651 - workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider - service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com - access_token_lifetime: 600s # 10 minutes - - uses: actions/setup-go@v5 - with: - go-version: ${{ env.GO_VERSION }} - - run: go build -o ${{ github.workspace }}/tools ./cmd/... - working-directory: .github/workflows/samples-tools - - name: Run all tests - run: ./tools run-all .github/config/nodejs.jsonc .github/scripts/nodejs-test.sh - env: - GOOGLE_SAMPLES_PROJECT: long-door-651 + # nodejs-nightly: + # name: Node.js 20 + # runs-on: ubuntu-latest + # permissions: + # id-token: write # needed for google-github-actions/auth + # strategy: + # fail-fast: false + # matrix: + # # run: ${{ fromJson(github.event_name == 'schedule' && '["nightly tests"]' || '[]') }} + # run: ${{ fromJson('["nightly tests"]') }} + # steps: + # - uses: actions/checkout@v4 + # with: + # ref: ${{ github.event.pull_request.head.sha }} + # - uses: actions/setup-node@v4 + # with: + # node-version: 20 + # - uses: google-github-actions/auth@v2 + # with: + # project_id: long-door-651 + # workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider + # service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com + # access_token_lifetime: 600s # 10 minutes + # - uses: actions/setup-go@v5 + # with: + # go-version: ${{ env.GO_VERSION }} + # - run: go build -o ${{ github.workspace }}/tools ./cmd/... + # working-directory: .github/workflows/samples-tools + # - name: Run all tests + # run: ./tools run-all .github/config/nodejs.jsonc .github/scripts/nodejs-test.sh + # env: + # GOOGLE_SAMPLES_PROJECT: long-door-651 From fba5bc6081e5ed023e5ccc2f0c13f2325d3b277f Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 7 Oct 2024 14:12:37 -0700 Subject: [PATCH 110/127] install repo root package --- .github/workflows/experimental.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 3a93a9a178..c4b1baf8dc 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -88,7 +88,9 @@ jobs: service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com access_token_lifetime: 600s # 10 minutes - name: Test ${{ matrix.package }} - run: bash .github/scripts/nodejs-test.sh ${{ matrix.package }} + run: | + npm install + bash .github/scripts/nodejs-test.sh ${{ matrix.package }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 From d138552d622e845adc3ed65d481be78f5a6fbf10 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 8 Oct 2024 13:28:55 -0700 Subject: [PATCH 111/127] use log.Fatalf --- .github/workflows/samples-tools/cmd/main.go | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/.github/workflows/samples-tools/cmd/main.go b/.github/workflows/samples-tools/cmd/main.go index ea047ce5bb..439ce5fa22 100644 --- a/.github/workflows/samples-tools/cmd/main.go +++ b/.github/workflows/samples-tools/cmd/main.go @@ -19,6 +19,7 @@ package main import ( "encoding/json" "fmt" + "log" "os" "samples-tools/pkg/utils" "strings" @@ -95,34 +96,29 @@ func printUsage(f *os.File) { func affectedCmd(configFile string, diffsFile string) { config, err := utils.LoadConfig(configFile) if err != nil { - fmt.Fprintf(os.Stderr, "āŒ error loading the config file: %v\n%v\n", configFile, err) - os.Exit(1) + log.Fatalf("āŒ error loading the config file: %v\n%v\n", configFile, err) } diffs, err := readDiffs(diffsFile) if err != nil { - fmt.Fprintf(os.Stderr, "āŒ error getting the diffs: %v\n%v\n", diffsFile, err) - os.Exit(1) + log.Fatalf("āŒ error getting the diffs: %v\n%v\n", diffsFile, err) } packages, err := affected(config, diffs) if err != nil { - fmt.Fprintf(os.Stderr, "āŒ error finding the affected packages.\n%v\n", err) - os.Exit(1) + log.Fatalf("āŒ error finding the affected packages.\n%v\n", err) } if len(packages) > 256 { - fmt.Fprintf(os.Stderr, + log.Fatalf( "āŒ Error: GitHub Actions only supports up to 256 packages, got %v packages, for more details see:\n%v\n", len(packages), "https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow", ) - os.Exit(1) } packagesJson, err := json.Marshal(packages) if err != nil { - fmt.Fprintf(os.Stderr, "āŒ error marshaling packages to JSON.\n%v\n", err) - os.Exit(1) + log.Fatalf("āŒ error marshaling packages to JSON.\n%v\n", err) } fmt.Println(string(packagesJson)) @@ -131,8 +127,7 @@ func affectedCmd(configFile string, diffsFile string) { func runAllCmd(configFile string, script string) { config, err := utils.LoadConfig(configFile) if err != nil { - fmt.Fprintf(os.Stderr, "āŒ error loading the config file: %v\n%v\n", configFile, err) - os.Exit(1) + log.Fatalf("āŒ error loading the config file: %v\n%v\n", configFile, err) } packages, err := utils.FindAllPackages(".", config) @@ -148,6 +143,6 @@ func runAllCmd(configFile string, script string) { fmt.Printf("Failed tests: %v\n", failed) if failed > 0 { - os.Exit(1) + log.Fatalf("āŒ some tests failed, exit with code 1.") } } From 9ba7912bec3e693fb2a5d24c5574d6a725669016 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 8 Oct 2024 16:53:34 -0700 Subject: [PATCH 112/127] add unit tests and testing instructions --- .github/workflows/samples-tools/README.md | 9 ++ .github/workflows/samples-tools/cmd/main.go | 7 +- .../{cmd => pkg/utils}/affected.go | 34 +++---- .../samples-tools/pkg/utils/config.go | 44 ++++++--- .../{cmd => pkg/utils}/runAll.go | 6 +- .../samples-tools/test/affected_test.go | 43 ++++++++ .../samples-tools/test/config_test.go | 98 +++++++++++++++++++ .../test/testdata/config/comments.jsonc | 8 ++ .../test/testdata/config/default-values.json | 3 + .../test/testdata/config/empty.json | 1 + .../test/testdata/my-package/package.json | 0 .../my-package/subpackage/package.json | 0 .github/workflows/samples-tools/test/utils.go | 45 +++++++++ 13 files changed, 257 insertions(+), 41 deletions(-) rename .github/workflows/samples-tools/{cmd => pkg/utils}/affected.go (75%) rename .github/workflows/samples-tools/{cmd => pkg/utils}/runAll.go (91%) create mode 100644 .github/workflows/samples-tools/test/affected_test.go create mode 100644 .github/workflows/samples-tools/test/config_test.go create mode 100644 .github/workflows/samples-tools/test/testdata/config/comments.jsonc create mode 100644 .github/workflows/samples-tools/test/testdata/config/default-values.json create mode 100644 .github/workflows/samples-tools/test/testdata/config/empty.json create mode 100644 .github/workflows/samples-tools/test/testdata/my-package/package.json create mode 100644 .github/workflows/samples-tools/test/testdata/my-package/subpackage/package.json create mode 100644 .github/workflows/samples-tools/test/utils.go diff --git a/.github/workflows/samples-tools/README.md b/.github/workflows/samples-tools/README.md index 709c0805c2..6eb89a10a9 100644 --- a/.github/workflows/samples-tools/README.md +++ b/.github/workflows/samples-tools/README.md @@ -45,6 +45,15 @@ We can run it in a subshell using parentheses to keep our working directory from (cd .github/workflows/samples-tools && go build -o /tmp/tools ./cmd/*) ``` +## Running the tools unit tests + +To the tools tests, we must change to the directory where the tools package is defined. +We can run it in a subshell using parentheses to keep our working directory from changing. + +```sh +(cd .github/workflows/samples-tools && go test ./test) +``` + ## Finding affected packages > This must run at the repository root directory. diff --git a/.github/workflows/samples-tools/cmd/main.go b/.github/workflows/samples-tools/cmd/main.go index 439ce5fa22..487479a078 100644 --- a/.github/workflows/samples-tools/cmd/main.go +++ b/.github/workflows/samples-tools/cmd/main.go @@ -99,12 +99,13 @@ func affectedCmd(configFile string, diffsFile string) { log.Fatalf("āŒ error loading the config file: %v\n%v\n", configFile, err) } - diffs, err := readDiffs(diffsFile) + diffsBytes, err := os.ReadFile(diffsFile) if err != nil { log.Fatalf("āŒ error getting the diffs: %v\n%v\n", diffsFile, err) } + diffs := strings.Split(string(diffsBytes), "\n") - packages, err := affected(config, diffs) + packages, err := utils.Affected(config, diffs) if err != nil { log.Fatalf("āŒ error finding the affected packages.\n%v\n", err) } @@ -136,7 +137,7 @@ func runAllCmd(configFile string, script string) { } maxGoroutines := 16 - failed := runAll(packages, script, maxGoroutines) + failed := utils.RunAll(packages, script, maxGoroutines) fmt.Printf(strings.Repeat("-", 80) + "\n") fmt.Printf("Total tests: %v\n", len(packages)) diff --git a/.github/workflows/samples-tools/cmd/affected.go b/.github/workflows/samples-tools/pkg/utils/affected.go similarity index 75% rename from .github/workflows/samples-tools/cmd/affected.go rename to .github/workflows/samples-tools/pkg/utils/affected.go index 1b7767c69b..cf47fab1ed 100644 --- a/.github/workflows/samples-tools/cmd/affected.go +++ b/.github/workflows/samples-tools/pkg/utils/affected.go @@ -14,17 +14,21 @@ limitations under the License. */ -package main +package utils import ( - "os" "slices" - "strings" - - "samples-tools/pkg/utils" ) -func affected(config utils.Config, diffs []string) ([]string, error) { +func Affected(config Config, diffs []string) ([]string, error) { + changed := Changed(config, diffs) + if slices.Contains(changed, ".") { + return FindAllPackages(".", config) + } + return changed, nil +} + +func Changed(config Config, diffs []string) []string { changedUnique := make(map[string]bool) for _, diff := range diffs { if !config.Matches(diff) { @@ -37,21 +41,13 @@ func affected(config utils.Config, diffs []string) ([]string, error) { changedUnique[pkg] = true } + if len(changedUnique) == 0 { + return []string{"."} + } + changed := make([]string, 0, len(changedUnique)) for pkg := range changedUnique { changed = append(changed, pkg) } - - if slices.Contains(changed, ".") { - return utils.FindAllPackages(".", config) - } - return changed, nil -} - -func readDiffs(path string) ([]string, error) { - bytes, err := os.ReadFile(path) - if err != nil { - return []string{}, err - } - return strings.Split(string(bytes), "\n"), nil + return changed } diff --git a/.github/workflows/samples-tools/pkg/utils/config.go b/.github/workflows/samples-tools/pkg/utils/config.go index 3081825e6b..6780183495 100644 --- a/.github/workflows/samples-tools/pkg/utils/config.go +++ b/.github/workflows/samples-tools/pkg/utils/config.go @@ -39,35 +39,47 @@ type Config struct { ExcludePackages []string `json:"exclude-packages"` } -var multiLineCommentsRegex = regexp.MustCompile(`(?s)\s*/\*.*?\*/\s*`) +var multiLineCommentsRegex = regexp.MustCompile(`(?s)\s*/\*.*?\*/`) var singleLineCommentsRegex = regexp.MustCompile(`\s*//.*\s*`) -// LoadConfig loads the config from the given path. -func LoadConfig(path string) (Config, error) { - bytes, err := os.ReadFile(path) +// SaveConfig saves the config to the given file. +func SaveConfig(config Config, file *os.File) error { + bytes, err := json.MarshalIndent(config, "", " ") if err != nil { - return Config{}, err + return err } - config, err := parseConfig(bytes) + _, err = file.Write(bytes) if err != nil { - return Config{}, err + return err } - return config, nil + return nil } -// parseConfig parses the config from the given source. -func parseConfig(source []byte) (Config, error) { - var config Config - err := json.Unmarshal(StripComments(source), &config) +// LoadConfig loads the config from the given path. +func LoadConfig(path string) (Config, error) { + config := Config{} + + // Read the JSONC file. + sourceJsonc, err := os.ReadFile(path) if err != nil { - return Config{}, err + return config, err } + + // Strip the comments and load the JSON. + sourceJson := StripComments(sourceJsonc) + err = json.Unmarshal(sourceJson, &config) + if err != nil { + return config, err + } + + // Set default values if they are not set. if config.PackageFile == nil { return config, errors.New("package-file is required") } if config.Match == nil { config.Match = []string{"*"} } + return config, nil } @@ -77,8 +89,8 @@ func StripComments(src []byte) []byte { return singleLineCommentsRegex.ReplaceAll(src, []byte{}) } -// match returns true if the path matches any of the patterns. -func match(patterns []string, path string) bool { +// Match returns true if the path matches any of the patterns. +func Match(patterns []string, path string) bool { filename := filepath.Base(path) for _, pattern := range patterns { if match, _ := filepath.Match(pattern, filename); match { @@ -93,7 +105,7 @@ func match(patterns []string, path string) bool { // Matches returns true if the path matches the config. func (c Config) Matches(path string) bool { - return match(c.Match, path) && !match(c.Ignore, path) + return Match(c.Match, path) && !Match(c.Ignore, path) } // IsPackageDir returns true if the path is a package directory. diff --git a/.github/workflows/samples-tools/cmd/runAll.go b/.github/workflows/samples-tools/pkg/utils/runAll.go similarity index 91% rename from .github/workflows/samples-tools/cmd/runAll.go rename to .github/workflows/samples-tools/pkg/utils/runAll.go index 211de1c751..950fad411e 100644 --- a/.github/workflows/samples-tools/cmd/runAll.go +++ b/.github/workflows/samples-tools/pkg/utils/runAll.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package utils import ( "fmt" @@ -24,8 +24,8 @@ import ( "sync/atomic" ) -// runAll runs all the commands in parallel. -func runAll(packages []string, script string, maxGoroutines int) int64 { +// RunAll runs all the commands in parallel. +func RunAll(packages []string, script string, maxGoroutines int) int64 { var failures int64 guard := make(chan struct{}, maxGoroutines) for _, pkg := range packages { diff --git a/.github/workflows/samples-tools/test/affected_test.go b/.github/workflows/samples-tools/test/affected_test.go new file mode 100644 index 0000000000..0414a03cf8 --- /dev/null +++ b/.github/workflows/samples-tools/test/affected_test.go @@ -0,0 +1,43 @@ +package test + +import ( + "path/filepath" + "samples-tools/pkg/utils" + "testing" +) + +func TestChangedRoot(t *testing.T) { + config := utils.Config{PackageFile: []string{"package.json"}} + diffs := []string{ + filepath.Join("testdata", "file.txt"), + } + got := utils.Changed(config, diffs) + expected := []string{"."} + equals(t, expected, got) +} + +func TestChangedPackage(t *testing.T) { + config := utils.Config{ + PackageFile: []string{"package.json"}, + Match: []string{"*"}, + } + diffs := []string{ + filepath.Join("testdata", "my-package", "file.txt"), + } + got := utils.Changed(config, diffs) + expected := []string{"testdata/my-package"} + equals(t, expected, got) +} + +func TestChangedSubpackage(t *testing.T) { + config := utils.Config{ + PackageFile: []string{"package.json"}, + Match: []string{"*"}, + } + diffs := []string{ + filepath.Join("testdata", "my-package", "subpackage", "file.txt"), + } + got := utils.Changed(config, diffs) + expected := []string{"testdata/my-package/subpackage"} + equals(t, expected, got) +} diff --git a/.github/workflows/samples-tools/test/config_test.go b/.github/workflows/samples-tools/test/config_test.go new file mode 100644 index 0000000000..89770671b5 --- /dev/null +++ b/.github/workflows/samples-tools/test/config_test.go @@ -0,0 +1,98 @@ +package test + +import ( + "os" + "path/filepath" + "samples-tools/pkg/utils" + "testing" +) + +func TestLoadConfigEmpty(t *testing.T) { + path := filepath.Join("testdata", "config", "empty.json") + _, err := utils.LoadConfig(path) + expectError(t, err) +} + +func TestLoadConfigDefaultValues(t *testing.T) { + path := filepath.Join("testdata", "config", "default-values.json") + config, err := utils.LoadConfig(path) + ok(t, err) + expected := utils.Config{ + PackageFile: []string{"package.json"}, + Match: []string{"*"}, + } + equals(t, expected, config) +} + +func TestLoadConfigComments(t *testing.T) { + path := filepath.Join("testdata", "config", "comments.jsonc") + config, err := utils.LoadConfig(path) + ok(t, err) + expected := utils.Config{ + PackageFile: []string{"package.json"}, + Match: []string{"*"}, + } + equals(t, expected, config) +} + +func TestSaveLoadConfig(t *testing.T) { + file, err := os.CreateTemp("", "config-*.json") + ok(t, err) + defer os.Remove(file.Name()) + + config := utils.Config{ + PackageFile: []string{"package.json"}, + Ignore: []string{"node_modules/", "*.md"}, + Match: []string{"*.js"}, + ExcludePackages: []string{"excluded"}, + } + err = utils.SaveConfig(config, file) + ok(t, err) + + loadedConfig, err := utils.LoadConfig(file.Name()) + ok(t, err) + + equals(t, config, loadedConfig) +} + +func TestMatchNoPatterns(t *testing.T) { + patterns := []string{} + path := "path/to/file.js" + assert(t, !utils.Match(patterns, path), "match(%v, %v) = false") +} + +func TestMatchFilePattern(t *testing.T) { + patterns := []string{"*.js"} + path := "path/to/file.js" + assert(t, utils.Match(patterns, path), "match(%v, %v) = true") +} + +func TestMatchFilePath(t *testing.T) { + patterns := []string{"path/to/"} + path := "path/to/file.js" + assert(t, utils.Match(patterns, path), "match(%v, %v) = true") +} + +func TestIsPackageDirNotExists(t *testing.T) { + config := utils.Config{PackageFile: []string{"package.json"}} + dir := "path-does-not-exist" + assert(t, !config.IsPackageDir(dir), "config.IsPackageDir(%v) = false") +} + +func TestIsPackageDirExists(t *testing.T) { + config := utils.Config{PackageFile: []string{"package.json"}} + dir := filepath.Join("testdata", "my-package") + assert(t, config.IsPackageDir(dir), "config.IsPackageDir(%v) = true") +} + +func TestFindPackageRoot(t *testing.T) { + config := utils.Config{PackageFile: []string{"package.json"}} + path := filepath.Join("testdata", "my-file.txt") + equals(t, ".", config.FindPackage(path)) +} + +func TestFindPackageSubpackage(t *testing.T) { + config := utils.Config{PackageFile: []string{"package.json"}} + path := filepath.Join("testdata", "my-package", "subpackage", "my-file.txt") + equals(t, "testdata/my-package/subpackage", config.FindPackage(path)) +} diff --git a/.github/workflows/samples-tools/test/testdata/config/comments.jsonc b/.github/workflows/samples-tools/test/testdata/config/comments.jsonc new file mode 100644 index 0000000000..4f9f442fdb --- /dev/null +++ b/.github/workflows/samples-tools/test/testdata/config/comments.jsonc @@ -0,0 +1,8 @@ +/* + * Multi-line + * comment + */ +{ + // Single line comment. + "package-file": /* inline comment */ ["package.json"] +} diff --git a/.github/workflows/samples-tools/test/testdata/config/default-values.json b/.github/workflows/samples-tools/test/testdata/config/default-values.json new file mode 100644 index 0000000000..fd5eb9b8ea --- /dev/null +++ b/.github/workflows/samples-tools/test/testdata/config/default-values.json @@ -0,0 +1,3 @@ +{ + "package-file": ["package.json"] +} diff --git a/.github/workflows/samples-tools/test/testdata/config/empty.json b/.github/workflows/samples-tools/test/testdata/config/empty.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/.github/workflows/samples-tools/test/testdata/config/empty.json @@ -0,0 +1 @@ +{} diff --git a/.github/workflows/samples-tools/test/testdata/my-package/package.json b/.github/workflows/samples-tools/test/testdata/my-package/package.json new file mode 100644 index 0000000000..e69de29bb2 diff --git a/.github/workflows/samples-tools/test/testdata/my-package/subpackage/package.json b/.github/workflows/samples-tools/test/testdata/my-package/subpackage/package.json new file mode 100644 index 0000000000..e69de29bb2 diff --git a/.github/workflows/samples-tools/test/utils.go b/.github/workflows/samples-tools/test/utils.go new file mode 100644 index 0000000000..5ffbc2281f --- /dev/null +++ b/.github/workflows/samples-tools/test/utils.go @@ -0,0 +1,45 @@ +package test + +import ( + "fmt" + "path/filepath" + "reflect" + "runtime" + "testing" +) + +// assert fails the test if the condition is false. (https://github.com/benbjohnson/testing) +func assert(tb testing.TB, condition bool, msg string, v ...interface{}) { + if !condition { + _, file, line, _ := runtime.Caller(1) + fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...) + tb.FailNow() + } +} + +// equals fails the test if exp is not equal to act. (https://github.com/benbjohnson/testing) +func equals(tb testing.TB, exp, act interface{}) { + if !reflect.DeepEqual(exp, act) { + _, file, line, _ := runtime.Caller(1) + fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act) + tb.FailNow() + } +} + +// ok fails the test if an err is not nil. (https://github.com/benbjohnson/testing) +func ok(tb testing.TB, err error) { + if err != nil { + _, file, line, _ := runtime.Caller(1) + fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error()) + tb.FailNow() + } +} + +// expectError fails the test if an err is nil. +func expectError(tb testing.TB, err error) { + if err == nil { + _, file, line, _ := runtime.Caller(1) + fmt.Printf("\033[31m%s:%d: expected error\033[39m\n\n", filepath.Base(file), line) + tb.FailNow() + } +} From 35c9c3dd703927e64cbaab483c25f2b5cf14eca2 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 9 Oct 2024 14:36:36 -0700 Subject: [PATCH 113/127] move tools directory --- .../samples-tools => cloud-samples-tools}/.gitignore | 0 .../samples-tools => cloud-samples-tools}/README.md | 0 .../samples-tools => cloud-samples-tools}/cmd/main.go | 2 +- .github/cloud-samples-tools/go.mod | 3 +++ .../pkg/utils/affected.go | 0 .../samples-tools => cloud-samples-tools}/pkg/utils/config.go | 0 .../pkg/utils/packages.go | 0 .../samples-tools => cloud-samples-tools}/pkg/utils/runAll.go | 0 .../test/affected_test.go | 0 .../samples-tools => cloud-samples-tools}/test/config_test.go | 0 .../test/testdata/config/comments.jsonc | 0 .../test/testdata/config/default-values.json | 0 .../test/testdata/config/empty.json | 0 .../test/testdata/my-package/package.json | 0 .../test/testdata/my-package/subpackage/package.json | 0 .../samples-tools => cloud-samples-tools}/test/utils.go | 0 .github/config/nodejs.jsonc | 1 + .github/workflows/experimental.yaml | 4 ++-- .github/workflows/samples-tools/go.mod | 3 --- 19 files changed, 7 insertions(+), 6 deletions(-) rename .github/{workflows/samples-tools => cloud-samples-tools}/.gitignore (100%) rename .github/{workflows/samples-tools => cloud-samples-tools}/README.md (100%) rename .github/{workflows/samples-tools => cloud-samples-tools}/cmd/main.go (99%) create mode 100644 .github/cloud-samples-tools/go.mod rename .github/{workflows/samples-tools => cloud-samples-tools}/pkg/utils/affected.go (100%) rename .github/{workflows/samples-tools => cloud-samples-tools}/pkg/utils/config.go (100%) rename .github/{workflows/samples-tools => cloud-samples-tools}/pkg/utils/packages.go (100%) rename .github/{workflows/samples-tools => cloud-samples-tools}/pkg/utils/runAll.go (100%) rename .github/{workflows/samples-tools => cloud-samples-tools}/test/affected_test.go (100%) rename .github/{workflows/samples-tools => cloud-samples-tools}/test/config_test.go (100%) rename .github/{workflows/samples-tools => cloud-samples-tools}/test/testdata/config/comments.jsonc (100%) rename .github/{workflows/samples-tools => cloud-samples-tools}/test/testdata/config/default-values.json (100%) rename .github/{workflows/samples-tools => cloud-samples-tools}/test/testdata/config/empty.json (100%) rename .github/{workflows/samples-tools => cloud-samples-tools}/test/testdata/my-package/package.json (100%) rename .github/{workflows/samples-tools => cloud-samples-tools}/test/testdata/my-package/subpackage/package.json (100%) rename .github/{workflows/samples-tools => cloud-samples-tools}/test/utils.go (100%) delete mode 100644 .github/workflows/samples-tools/go.mod diff --git a/.github/workflows/samples-tools/.gitignore b/.github/cloud-samples-tools/.gitignore similarity index 100% rename from .github/workflows/samples-tools/.gitignore rename to .github/cloud-samples-tools/.gitignore diff --git a/.github/workflows/samples-tools/README.md b/.github/cloud-samples-tools/README.md similarity index 100% rename from .github/workflows/samples-tools/README.md rename to .github/cloud-samples-tools/README.md diff --git a/.github/workflows/samples-tools/cmd/main.go b/.github/cloud-samples-tools/cmd/main.go similarity index 99% rename from .github/workflows/samples-tools/cmd/main.go rename to .github/cloud-samples-tools/cmd/main.go index 487479a078..b8e0de779a 100644 --- a/.github/workflows/samples-tools/cmd/main.go +++ b/.github/cloud-samples-tools/cmd/main.go @@ -17,11 +17,11 @@ package main import ( + "cloud-samples-tools/pkg/utils" "encoding/json" "fmt" "log" "os" - "samples-tools/pkg/utils" "strings" ) diff --git a/.github/cloud-samples-tools/go.mod b/.github/cloud-samples-tools/go.mod new file mode 100644 index 0000000000..4375f164d4 --- /dev/null +++ b/.github/cloud-samples-tools/go.mod @@ -0,0 +1,3 @@ +module cloud-samples-tools + +go 1.22.0 diff --git a/.github/workflows/samples-tools/pkg/utils/affected.go b/.github/cloud-samples-tools/pkg/utils/affected.go similarity index 100% rename from .github/workflows/samples-tools/pkg/utils/affected.go rename to .github/cloud-samples-tools/pkg/utils/affected.go diff --git a/.github/workflows/samples-tools/pkg/utils/config.go b/.github/cloud-samples-tools/pkg/utils/config.go similarity index 100% rename from .github/workflows/samples-tools/pkg/utils/config.go rename to .github/cloud-samples-tools/pkg/utils/config.go diff --git a/.github/workflows/samples-tools/pkg/utils/packages.go b/.github/cloud-samples-tools/pkg/utils/packages.go similarity index 100% rename from .github/workflows/samples-tools/pkg/utils/packages.go rename to .github/cloud-samples-tools/pkg/utils/packages.go diff --git a/.github/workflows/samples-tools/pkg/utils/runAll.go b/.github/cloud-samples-tools/pkg/utils/runAll.go similarity index 100% rename from .github/workflows/samples-tools/pkg/utils/runAll.go rename to .github/cloud-samples-tools/pkg/utils/runAll.go diff --git a/.github/workflows/samples-tools/test/affected_test.go b/.github/cloud-samples-tools/test/affected_test.go similarity index 100% rename from .github/workflows/samples-tools/test/affected_test.go rename to .github/cloud-samples-tools/test/affected_test.go diff --git a/.github/workflows/samples-tools/test/config_test.go b/.github/cloud-samples-tools/test/config_test.go similarity index 100% rename from .github/workflows/samples-tools/test/config_test.go rename to .github/cloud-samples-tools/test/config_test.go diff --git a/.github/workflows/samples-tools/test/testdata/config/comments.jsonc b/.github/cloud-samples-tools/test/testdata/config/comments.jsonc similarity index 100% rename from .github/workflows/samples-tools/test/testdata/config/comments.jsonc rename to .github/cloud-samples-tools/test/testdata/config/comments.jsonc diff --git a/.github/workflows/samples-tools/test/testdata/config/default-values.json b/.github/cloud-samples-tools/test/testdata/config/default-values.json similarity index 100% rename from .github/workflows/samples-tools/test/testdata/config/default-values.json rename to .github/cloud-samples-tools/test/testdata/config/default-values.json diff --git a/.github/workflows/samples-tools/test/testdata/config/empty.json b/.github/cloud-samples-tools/test/testdata/config/empty.json similarity index 100% rename from .github/workflows/samples-tools/test/testdata/config/empty.json rename to .github/cloud-samples-tools/test/testdata/config/empty.json diff --git a/.github/workflows/samples-tools/test/testdata/my-package/package.json b/.github/cloud-samples-tools/test/testdata/my-package/package.json similarity index 100% rename from .github/workflows/samples-tools/test/testdata/my-package/package.json rename to .github/cloud-samples-tools/test/testdata/my-package/package.json diff --git a/.github/workflows/samples-tools/test/testdata/my-package/subpackage/package.json b/.github/cloud-samples-tools/test/testdata/my-package/subpackage/package.json similarity index 100% rename from .github/workflows/samples-tools/test/testdata/my-package/subpackage/package.json rename to .github/cloud-samples-tools/test/testdata/my-package/subpackage/package.json diff --git a/.github/workflows/samples-tools/test/utils.go b/.github/cloud-samples-tools/test/utils.go similarity index 100% rename from .github/workflows/samples-tools/test/utils.go rename to .github/cloud-samples-tools/test/utils.go diff --git a/.github/config/nodejs.jsonc b/.github/config/nodejs.jsonc index 6edc1fe3d5..47d2dd6fe8 100644 --- a/.github/config/nodejs.jsonc +++ b/.github/config/nodejs.jsonc @@ -25,6 +25,7 @@ ".github/PULL_REQUEST_TEMPLATE.md", ".github/auto-label.yaml", ".github/blunderbuss.yaml", + ".github/cloud-samples-tools/", ".github/flakybot.yaml", ".github/header-checker-lint.yaml", ".github/snippet-bot.yml", diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index c4b1baf8dc..dc30444160 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -45,7 +45,7 @@ jobs: with: go-version: ${{ env.GO_VERSION }} - run: go build -o ${{ github.workspace }}/tools ./cmd/... - working-directory: .github/workflows/samples-tools + working-directory: .github/cloud-samples-tools - name: Get diffs run: git --no-pager diff --name-only HEAD origin/main | tee diffs.txt - name: Find Node.js affected packages @@ -121,7 +121,7 @@ jobs: # with: # go-version: ${{ env.GO_VERSION }} # - run: go build -o ${{ github.workspace }}/tools ./cmd/... - # working-directory: .github/workflows/samples-tools + # working-directory: .github/cloud-samples-tools # - name: Run all tests # run: ./tools run-all .github/config/nodejs.jsonc .github/scripts/nodejs-test.sh # env: diff --git a/.github/workflows/samples-tools/go.mod b/.github/workflows/samples-tools/go.mod deleted file mode 100644 index 8da07aceac..0000000000 --- a/.github/workflows/samples-tools/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module samples-tools - -go 1.22.0 From 4f58bcde0f3fd6fb7fc5e9036c3828620ca61e05 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 9 Oct 2024 14:52:24 -0700 Subject: [PATCH 114/127] fix tests pkg path --- .github/cloud-samples-tools/test/affected_test.go | 2 +- .github/cloud-samples-tools/test/config_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/cloud-samples-tools/test/affected_test.go b/.github/cloud-samples-tools/test/affected_test.go index 0414a03cf8..5dc7172c23 100644 --- a/.github/cloud-samples-tools/test/affected_test.go +++ b/.github/cloud-samples-tools/test/affected_test.go @@ -1,8 +1,8 @@ package test import ( + "cloud-samples-tools/pkg/utils" "path/filepath" - "samples-tools/pkg/utils" "testing" ) diff --git a/.github/cloud-samples-tools/test/config_test.go b/.github/cloud-samples-tools/test/config_test.go index 89770671b5..f8ad00f497 100644 --- a/.github/cloud-samples-tools/test/config_test.go +++ b/.github/cloud-samples-tools/test/config_test.go @@ -1,9 +1,9 @@ package test import ( + "cloud-samples-tools/pkg/utils" "os" "path/filepath" - "samples-tools/pkg/utils" "testing" ) From f965b2d07d5a635ce17756a3ac76c925d1a083b0 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 9 Oct 2024 14:56:03 -0700 Subject: [PATCH 115/127] run tests on action --- .../workflows/cloud-samples-tools-test.yaml | 40 +++++++++++++++++++ .github/workflows/experimental.yaml | 5 --- 2 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/cloud-samples-tools-test.yaml diff --git a/.github/workflows/cloud-samples-tools-test.yaml b/.github/workflows/cloud-samples-tools-test.yaml new file mode 100644 index 0000000000..86623cab3e --- /dev/null +++ b/.github/workflows/cloud-samples-tools-test.yaml @@ -0,0 +1,40 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: Cloud Samples Tools +on: + push: + branches: + - main + paths: + - .github/cloud-samples-tools/ + pull_request: + paths: + - .github/cloud-samples-tools/ + +env: + GO_VERSION: ^1.22.0 + +jobs: + test: + name: Test + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: ${{ env.GO_VERSION }} + - run: go test ./test + working-directory: .github/cloud-samples-tools diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index dc30444160..bdc1c8f2fe 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -18,11 +18,6 @@ on: branches: - main pull_request: - types: - - opened - - reopened - - synchronize - - labeled schedule: # https://crontab.guru/#0_12_*_*_0 - cron: 0 12 * * 0 # At 12:00 on Sunday From 932c00a0b6dad258fbc23e5f899d96419ebf93bd Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 9 Oct 2024 15:03:05 -0700 Subject: [PATCH 116/127] add go linting --- .../workflows/cloud-samples-tools-test.yaml | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cloud-samples-tools-test.yaml b/.github/workflows/cloud-samples-tools-test.yaml index 86623cab3e..6fc647cdd2 100644 --- a/.github/workflows/cloud-samples-tools-test.yaml +++ b/.github/workflows/cloud-samples-tools-test.yaml @@ -27,14 +27,34 @@ env: GO_VERSION: ^1.22.0 jobs: + lint: + name: Lint + runs-on: ubuntu-latest + timeout-minutes: 5 + defaults: + run: + working-directory: .github/cloud-samples-tools + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: ${{ env.GO_VERSION }} + - name: Go vet + run: go vet ./... + - name: Check format + run: test -z $(gofmt -l .) + test: name: Test runs-on: ubuntu-latest timeout-minutes: 5 + defaults: + run: + working-directory: .github/cloud-samples-tools steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version: ${{ env.GO_VERSION }} - - run: go test ./test - working-directory: .github/cloud-samples-tools + - name: Run tests + run: go test ./test From 5012283d6d2b96469210d6a8b1cf90e22fa58e73 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Wed, 9 Oct 2024 15:07:35 -0700 Subject: [PATCH 117/127] use double star on paths --- .github/workflows/cloud-samples-tools-test.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/cloud-samples-tools-test.yaml b/.github/workflows/cloud-samples-tools-test.yaml index 6fc647cdd2..0a11649e24 100644 --- a/.github/workflows/cloud-samples-tools-test.yaml +++ b/.github/workflows/cloud-samples-tools-test.yaml @@ -18,10 +18,10 @@ on: branches: - main paths: - - .github/cloud-samples-tools/ + - .github/cloud-samples-tools/** pull_request: paths: - - .github/cloud-samples-tools/ + - .github/cloud-samples-tools/** env: GO_VERSION: ^1.22.0 From 22f436180321ad9a4f496cf76cfb008459e8b5b2 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 10 Oct 2024 13:58:59 -0700 Subject: [PATCH 118/127] use variadic argument prints --- .github/cloud-samples-tools/cmd/main.go | 91 ++++++++++--------------- 1 file changed, 36 insertions(+), 55 deletions(-) diff --git a/.github/cloud-samples-tools/cmd/main.go b/.github/cloud-samples-tools/cmd/main.go index b8e0de779a..d33139c291 100644 --- a/.github/cloud-samples-tools/cmd/main.go +++ b/.github/cloud-samples-tools/cmd/main.go @@ -19,107 +19,88 @@ package main import ( "cloud-samples-tools/pkg/utils" "encoding/json" + "flag" "fmt" "log" "os" "strings" ) +var usage = `usage: tools ... + +commands: + affected path/to/config.jsonc path/to/diffs.txt + run-all path/to/config.jsonc path/to/script.sh +` + func main() { - command := "" - if len(os.Args) > 1 { - command = os.Args[1] - } else { - fmt.Fprintf(os.Stderr, "āŒ no command specified\n") - printUsage(os.Stderr) - os.Exit(1) + flag.Parse() + + command := flag.Arg(0) + if command == "" { + log.Fatalln("āŒ no command specified\n", usage) } switch command { case "affected": - configFile := "" - if len(os.Args) > 2 { - configFile = os.Args[2] - } else { - fmt.Fprintf(os.Stderr, "āŒ no config file specified\n") - printUsage(os.Stderr) - os.Exit(1) + configFile := flag.Arg(1) + if configFile == "" { + log.Fatalln("āŒ no config file specified\n", usage) } - diffsFile := "" - if len(os.Args) > 3 { - diffsFile = os.Args[3] - } else { - fmt.Fprintf(os.Stderr, "āŒ no diffs file specified\n") - printUsage(os.Stderr) - os.Exit(1) + diffsFile := flag.Arg(2) + if diffsFile == "" { + log.Fatalln("āŒ no diffs file specified\n", usage) } affectedCmd(configFile, diffsFile) case "run-all": - configFile := "" - if len(os.Args) > 2 { - configFile = os.Args[2] - } else { - fmt.Fprintf(os.Stderr, "āŒ no config file specified\n") - printUsage(os.Stderr) - os.Exit(1) + configFile := flag.Arg(1) + if configFile == "" { + log.Fatalln("āŒ no config file specified\n", usage) } - script := "" - if len(os.Args) > 3 { - script = os.Args[3] - } else { - fmt.Fprintf(os.Stderr, "āŒ no script file specified\n") - printUsage(os.Stderr) - os.Exit(1) + script := flag.Arg(2) + if script == "" { + log.Fatalln("āŒ no script file specified\n", usage) } runAllCmd(configFile, script) default: - fmt.Fprintf(os.Stderr, "āŒ unknown command: %s\n", command) - printUsage(os.Stderr) - os.Exit(1) + log.Fatalln("āŒ unknown command: ", command, "\n", usage) } } -func printUsage(f *os.File) { - fmt.Fprintf(f, "usage: tools ...\n") - fmt.Fprintf(f, "\n") - fmt.Fprintf(f, "commands:\n") - fmt.Fprintf(f, " affected path/to/config.jsonc path/to/diffs.txt\n") - fmt.Fprintf(f, " run-all path/to/config.jsonc path/to/script.sh\n") -} - func affectedCmd(configFile string, diffsFile string) { config, err := utils.LoadConfig(configFile) if err != nil { - log.Fatalf("āŒ error loading the config file: %v\n%v\n", configFile, err) + log.Fatalln("āŒ error loading the config file: ", configFile, "\n", err) } diffsBytes, err := os.ReadFile(diffsFile) if err != nil { - log.Fatalf("āŒ error getting the diffs: %v\n%v\n", diffsFile, err) + log.Fatalln("āŒ error getting the diffs: ", diffsFile, "\n", err) } diffs := strings.Split(string(diffsBytes), "\n") packages, err := utils.Affected(config, diffs) if err != nil { - log.Fatalf("āŒ error finding the affected packages.\n%v\n", err) + log.Fatalln("āŒ error finding the affected packages.\n", err) } if len(packages) > 256 { - log.Fatalf( - "āŒ Error: GitHub Actions only supports up to 256 packages, got %v packages, for more details see:\n%v\n", + log.Fatalln( + "āŒ Error: GitHub Actions only supports up to 256 packages, got ", len(packages), + " packages, for more details see:\n", "https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/running-variations-of-jobs-in-a-workflow", ) } packagesJson, err := json.Marshal(packages) if err != nil { - log.Fatalf("āŒ error marshaling packages to JSON.\n%v\n", err) + log.Fatalln("āŒ error marshaling packages to JSON.\n", err) } fmt.Println(string(packagesJson)) @@ -128,12 +109,12 @@ func affectedCmd(configFile string, diffsFile string) { func runAllCmd(configFile string, script string) { config, err := utils.LoadConfig(configFile) if err != nil { - log.Fatalf("āŒ error loading the config file: %v\n%v\n", configFile, err) + log.Fatalln("āŒ error loading the config file: ", configFile, "\n", err) } packages, err := utils.FindAllPackages(".", config) if err != nil { - fmt.Fprintf(os.Stderr, "āŒ error finding packages.\n%v\n", err) + log.Fatalln("āŒ error finding packages.\n", err) } maxGoroutines := 16 @@ -144,6 +125,6 @@ func runAllCmd(configFile string, script string) { fmt.Printf("Failed tests: %v\n", failed) if failed > 0 { - log.Fatalf("āŒ some tests failed, exit with code 1.") + log.Fatalln("āŒ some tests failed, exit with code 1.") } } From b7e6c59dde03946b1a7408bfdd780c0773b58b1a Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 10 Oct 2024 15:21:59 -0700 Subject: [PATCH 119/127] remove everything related to run-all --- .github/cloud-samples-tools/cmd/main.go | 38 +------------ .../cloud-samples-tools/pkg/utils/runAll.go | 55 ------------------- .github/workflows/experimental.yaml | 33 ----------- 3 files changed, 2 insertions(+), 124 deletions(-) delete mode 100644 .github/cloud-samples-tools/pkg/utils/runAll.go diff --git a/.github/cloud-samples-tools/cmd/main.go b/.github/cloud-samples-tools/cmd/main.go index d33139c291..d5dde5e5f8 100644 --- a/.github/cloud-samples-tools/cmd/main.go +++ b/.github/cloud-samples-tools/cmd/main.go @@ -33,6 +33,7 @@ commands: run-all path/to/config.jsonc path/to/script.sh ` +// Entry point to validate command line arguments. func main() { flag.Parse() @@ -55,24 +56,12 @@ func main() { affectedCmd(configFile, diffsFile) - case "run-all": - configFile := flag.Arg(1) - if configFile == "" { - log.Fatalln("āŒ no config file specified\n", usage) - } - - script := flag.Arg(2) - if script == "" { - log.Fatalln("āŒ no script file specified\n", usage) - } - - runAllCmd(configFile, script) - default: log.Fatalln("āŒ unknown command: ", command, "\n", usage) } } +// affected command entry point to validate inputs. func affectedCmd(configFile string, diffsFile string) { config, err := utils.LoadConfig(configFile) if err != nil { @@ -105,26 +94,3 @@ func affectedCmd(configFile string, diffsFile string) { fmt.Println(string(packagesJson)) } - -func runAllCmd(configFile string, script string) { - config, err := utils.LoadConfig(configFile) - if err != nil { - log.Fatalln("āŒ error loading the config file: ", configFile, "\n", err) - } - - packages, err := utils.FindAllPackages(".", config) - if err != nil { - log.Fatalln("āŒ error finding packages.\n", err) - } - - maxGoroutines := 16 - failed := utils.RunAll(packages, script, maxGoroutines) - - fmt.Printf(strings.Repeat("-", 80) + "\n") - fmt.Printf("Total tests: %v\n", len(packages)) - fmt.Printf("Failed tests: %v\n", failed) - - if failed > 0 { - log.Fatalln("āŒ some tests failed, exit with code 1.") - } -} diff --git a/.github/cloud-samples-tools/pkg/utils/runAll.go b/.github/cloud-samples-tools/pkg/utils/runAll.go deleted file mode 100644 index 950fad411e..0000000000 --- a/.github/cloud-samples-tools/pkg/utils/runAll.go +++ /dev/null @@ -1,55 +0,0 @@ -/* - Copyright 2024 Google LLC - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package utils - -import ( - "fmt" - "os" - "os/exec" - "strings" - "sync/atomic" -) - -// RunAll runs all the commands in parallel. -func RunAll(packages []string, script string, maxGoroutines int) int64 { - var failures int64 - guard := make(chan struct{}, maxGoroutines) - for _, pkg := range packages { - guard <- struct{}{} // block if channel is filled - go func() { - output, err := exec.Command("bash", script, pkg).CombinedOutput() - if err != nil { - printError(os.Stderr, pkg, err, string(output)) - atomic.AddInt64(&failures, 1) - } else { - fmt.Printf("āœ… %v\n", pkg) - } - <-guard - }() - } - return failures -} - -func printError(f *os.File, pkg string, err error, output string) { - fmt.Fprintf(f, "%v\nāŒ FAILED: %v\n%v\n%v\n%v\n\n", - strings.Repeat("=", 80), - pkg, - strings.Repeat("-", 80), - err, - string(output), - ) -} diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index bdc1c8f2fe..5947d64fbd 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -88,36 +88,3 @@ jobs: bash .github/scripts/nodejs-test.sh ${{ matrix.package }} env: GOOGLE_SAMPLES_PROJECT: long-door-651 - - # nodejs-nightly: - # name: Node.js 20 - # runs-on: ubuntu-latest - # permissions: - # id-token: write # needed for google-github-actions/auth - # strategy: - # fail-fast: false - # matrix: - # # run: ${{ fromJson(github.event_name == 'schedule' && '["nightly tests"]' || '[]') }} - # run: ${{ fromJson('["nightly tests"]') }} - # steps: - # - uses: actions/checkout@v4 - # with: - # ref: ${{ github.event.pull_request.head.sha }} - # - uses: actions/setup-node@v4 - # with: - # node-version: 20 - # - uses: google-github-actions/auth@v2 - # with: - # project_id: long-door-651 - # workload_identity_provider: projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider - # service_account: kokoro-system-test@long-door-651.iam.gserviceaccount.com - # access_token_lifetime: 600s # 10 minutes - # - uses: actions/setup-go@v5 - # with: - # go-version: ${{ env.GO_VERSION }} - # - run: go build -o ${{ github.workspace }}/tools ./cmd/... - # working-directory: .github/cloud-samples-tools - # - name: Run all tests - # run: ./tools run-all .github/config/nodejs.jsonc .github/scripts/nodejs-test.sh - # env: - # GOOGLE_SAMPLES_PROJECT: long-door-651 From de7303679f4b85b47a0bcd7ba1976ecf12dcba14 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 10 Oct 2024 15:23:32 -0700 Subject: [PATCH 120/127] add tool summary --- .github/cloud-samples-tools/README.md | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/.github/cloud-samples-tools/README.md b/.github/cloud-samples-tools/README.md index 6eb89a10a9..c59a0177ed 100644 --- a/.github/cloud-samples-tools/README.md +++ b/.github/cloud-samples-tools/README.md @@ -2,6 +2,10 @@ This is a collection of tools used for Cloud Samples maintenance and infrastructure. +This tool has one function: + +- `affected` finds the affected packages given a list of diffs. + ## Config files For this tools, we refer to a **package** as an isolated directory, which contains a "package file". @@ -74,16 +78,3 @@ We pass the config file and the diffs file as positional arguments. ```sh /tmp/tools affected .github/config/nodejs.jsonc /tmp/diffs.txt ``` - -## Running on all packages - -> This must run at the repository root directory. - -We pass the config file and a bash script to run as positional arguments. -The script must receive a single positional argument in `$1` as the package name. -For example, the script should be called like `bash path/to/my-script.sh path/to/package`. - -```sh -# To run all the Node.js tests. -/tmp/tools run-all ./github/config/nodejs.jsonc ./github/scripts/nodejs-test.sh -``` From c74db7839ac9da00d0dac698103f022645b8c805 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 10 Oct 2024 15:36:00 -0700 Subject: [PATCH 121/127] use pointer for methods --- .github/cloud-samples-tools/cmd/main.go | 6 +- .../pkg/{utils => config}/config.go | 87 +++++++++++++++---- .../cloud-samples-tools/pkg/config/utils.go | 30 +++++++ .../cloud-samples-tools/pkg/utils/affected.go | 53 ----------- .../cloud-samples-tools/pkg/utils/packages.go | 49 ----------- .../cloud-samples-tools/test/affected_test.go | 43 --------- .../cloud-samples-tools/test/config_test.go | 68 +++++++++++---- 7 files changed, 153 insertions(+), 183 deletions(-) rename .github/cloud-samples-tools/pkg/{utils => config}/config.go (59%) create mode 100644 .github/cloud-samples-tools/pkg/config/utils.go delete mode 100644 .github/cloud-samples-tools/pkg/utils/affected.go delete mode 100644 .github/cloud-samples-tools/pkg/utils/packages.go delete mode 100644 .github/cloud-samples-tools/test/affected_test.go diff --git a/.github/cloud-samples-tools/cmd/main.go b/.github/cloud-samples-tools/cmd/main.go index d5dde5e5f8..b79fdd2fab 100644 --- a/.github/cloud-samples-tools/cmd/main.go +++ b/.github/cloud-samples-tools/cmd/main.go @@ -17,7 +17,7 @@ package main import ( - "cloud-samples-tools/pkg/utils" + c "cloud-samples-tools/pkg/config" "encoding/json" "flag" "fmt" @@ -63,7 +63,7 @@ func main() { // affected command entry point to validate inputs. func affectedCmd(configFile string, diffsFile string) { - config, err := utils.LoadConfig(configFile) + config, err := c.LoadConfig(configFile) if err != nil { log.Fatalln("āŒ error loading the config file: ", configFile, "\n", err) } @@ -74,7 +74,7 @@ func affectedCmd(configFile string, diffsFile string) { } diffs := strings.Split(string(diffsBytes), "\n") - packages, err := utils.Affected(config, diffs) + packages, err := config.Affected(diffs) if err != nil { log.Fatalln("āŒ error finding the affected packages.\n", err) } diff --git a/.github/cloud-samples-tools/pkg/utils/config.go b/.github/cloud-samples-tools/pkg/config/config.go similarity index 59% rename from .github/cloud-samples-tools/pkg/utils/config.go rename to .github/cloud-samples-tools/pkg/config/config.go index 6780183495..de2886c514 100644 --- a/.github/cloud-samples-tools/pkg/utils/config.go +++ b/.github/cloud-samples-tools/pkg/config/config.go @@ -14,14 +14,16 @@ limitations under the License. */ -package utils +package config import ( "encoding/json" "errors" + "io/fs" "os" "path/filepath" "regexp" + "slices" "strings" ) @@ -42,9 +44,9 @@ type Config struct { var multiLineCommentsRegex = regexp.MustCompile(`(?s)\s*/\*.*?\*/`) var singleLineCommentsRegex = regexp.MustCompile(`\s*//.*\s*`) -// SaveConfig saves the config to the given file. -func SaveConfig(config Config, file *os.File) error { - bytes, err := json.MarshalIndent(config, "", " ") +// Saves the config to the given file. +func (c *Config) Save(file *os.File) error { + bytes, err := json.MarshalIndent(c, "", " ") if err != nil { return err } @@ -66,7 +68,8 @@ func LoadConfig(path string) (Config, error) { } // Strip the comments and load the JSON. - sourceJson := StripComments(sourceJsonc) + sourceJson := multiLineCommentsRegex.ReplaceAll(sourceJsonc, []byte{}) + sourceJson = singleLineCommentsRegex.ReplaceAll(sourceJson, []byte{}) err = json.Unmarshal(sourceJson, &config) if err != nil { return config, err @@ -83,12 +86,6 @@ func LoadConfig(path string) (Config, error) { return config, nil } -// StripComments removes comments from the given source. -func StripComments(src []byte) []byte { - src = multiLineCommentsRegex.ReplaceAll(src, []byte{}) - return singleLineCommentsRegex.ReplaceAll(src, []byte{}) -} - // Match returns true if the path matches any of the patterns. func Match(patterns []string, path string) bool { filename := filepath.Base(path) @@ -104,12 +101,12 @@ func Match(patterns []string, path string) bool { } // Matches returns true if the path matches the config. -func (c Config) Matches(path string) bool { +func (c *Config) Matches(path string) bool { return Match(c.Match, path) && !Match(c.Ignore, path) } // IsPackageDir returns true if the path is a package directory. -func (c Config) IsPackageDir(dir string) bool { +func (c *Config) IsPackageDir(dir string) bool { for _, filename := range c.PackageFile { packageFile := filepath.Join(dir, filename) if fileExists(packageFile) { @@ -120,7 +117,7 @@ func (c Config) IsPackageDir(dir string) bool { } // FindPackage returns the package name for the given path. -func (c Config) FindPackage(path string) string { +func (c *Config) FindPackage(path string) string { dir := filepath.Dir(path) if dir == "." || c.IsPackageDir(dir) { return dir @@ -128,10 +125,62 @@ func (c Config) FindPackage(path string) string { return c.FindPackage(dir) } -// fileExists returns true if the file exists. -func fileExists(path string) bool { - if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { - return false +// FindAllPackages finds all the packages in the given root directory. +func (c *Config) FindAllPackages(root string) ([]string, error) { + var packages []string + err := fs.WalkDir(os.DirFS(root), ".", + func(path string, d os.DirEntry, err error) error { + if err != nil { + return err + } + if path == "." { + return nil + } + if slices.Contains(c.ExcludePackages, path) { + return nil + } + if d.IsDir() && c.Matches(path) && c.IsPackageDir(path) { + packages = append(packages, path) + return nil + } + return nil + }) + if err != nil { + return []string{}, err + } + return packages, nil +} + +// Affected returns the packages that have been affected from diffs. +func (c *Config) Affected(diffs []string) ([]string, error) { + changed := c.Changed(diffs) + if slices.Contains(changed, ".") { + return c.FindAllPackages(".") + } + return changed, nil +} + +// Changed returns the packages that have changed. +func (c *Config) Changed(diffs []string) []string { + changedUnique := make(map[string]bool) + for _, diff := range diffs { + if !c.Matches(diff) { + continue + } + pkg := c.FindPackage(diff) + if slices.Contains(c.ExcludePackages, pkg) { + continue + } + changedUnique[pkg] = true + } + + if len(changedUnique) == 0 { + return []string{"."} + } + + changed := make([]string, 0, len(changedUnique)) + for pkg := range changedUnique { + changed = append(changed, pkg) } - return true + return changed } diff --git a/.github/cloud-samples-tools/pkg/config/utils.go b/.github/cloud-samples-tools/pkg/config/utils.go new file mode 100644 index 0000000000..b227f9cd99 --- /dev/null +++ b/.github/cloud-samples-tools/pkg/config/utils.go @@ -0,0 +1,30 @@ +/* + Copyright 2024 Google LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package config + +import ( + "errors" + "os" +) + +// fileExists returns true if the file exists. +func fileExists(path string) bool { + if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { + return false + } + return true +} diff --git a/.github/cloud-samples-tools/pkg/utils/affected.go b/.github/cloud-samples-tools/pkg/utils/affected.go deleted file mode 100644 index cf47fab1ed..0000000000 --- a/.github/cloud-samples-tools/pkg/utils/affected.go +++ /dev/null @@ -1,53 +0,0 @@ -/* - Copyright 2024 Google LLC - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package utils - -import ( - "slices" -) - -func Affected(config Config, diffs []string) ([]string, error) { - changed := Changed(config, diffs) - if slices.Contains(changed, ".") { - return FindAllPackages(".", config) - } - return changed, nil -} - -func Changed(config Config, diffs []string) []string { - changedUnique := make(map[string]bool) - for _, diff := range diffs { - if !config.Matches(diff) { - continue - } - pkg := config.FindPackage(diff) - if slices.Contains(config.ExcludePackages, pkg) { - continue - } - changedUnique[pkg] = true - } - - if len(changedUnique) == 0 { - return []string{"."} - } - - changed := make([]string, 0, len(changedUnique)) - for pkg := range changedUnique { - changed = append(changed, pkg) - } - return changed -} diff --git a/.github/cloud-samples-tools/pkg/utils/packages.go b/.github/cloud-samples-tools/pkg/utils/packages.go deleted file mode 100644 index 60602a564e..0000000000 --- a/.github/cloud-samples-tools/pkg/utils/packages.go +++ /dev/null @@ -1,49 +0,0 @@ -/* - Copyright 2024 Google LLC - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package utils - -import ( - "io/fs" - "os" - "slices" -) - -// FindAllPackages finds all the packages in the given root directory. -func FindAllPackages(root string, config Config) ([]string, error) { - var packages []string - err := fs.WalkDir(os.DirFS(root), ".", - func(path string, d os.DirEntry, err error) error { - if err != nil { - return err - } - if path == "." { - return nil - } - if slices.Contains(config.ExcludePackages, path) { - return nil - } - if d.IsDir() && config.Matches(path) && config.IsPackageDir(path) { - packages = append(packages, path) - return nil - } - return nil - }) - if err != nil { - return []string{}, err - } - return packages, nil -} diff --git a/.github/cloud-samples-tools/test/affected_test.go b/.github/cloud-samples-tools/test/affected_test.go deleted file mode 100644 index 5dc7172c23..0000000000 --- a/.github/cloud-samples-tools/test/affected_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package test - -import ( - "cloud-samples-tools/pkg/utils" - "path/filepath" - "testing" -) - -func TestChangedRoot(t *testing.T) { - config := utils.Config{PackageFile: []string{"package.json"}} - diffs := []string{ - filepath.Join("testdata", "file.txt"), - } - got := utils.Changed(config, diffs) - expected := []string{"."} - equals(t, expected, got) -} - -func TestChangedPackage(t *testing.T) { - config := utils.Config{ - PackageFile: []string{"package.json"}, - Match: []string{"*"}, - } - diffs := []string{ - filepath.Join("testdata", "my-package", "file.txt"), - } - got := utils.Changed(config, diffs) - expected := []string{"testdata/my-package"} - equals(t, expected, got) -} - -func TestChangedSubpackage(t *testing.T) { - config := utils.Config{ - PackageFile: []string{"package.json"}, - Match: []string{"*"}, - } - diffs := []string{ - filepath.Join("testdata", "my-package", "subpackage", "file.txt"), - } - got := utils.Changed(config, diffs) - expected := []string{"testdata/my-package/subpackage"} - equals(t, expected, got) -} diff --git a/.github/cloud-samples-tools/test/config_test.go b/.github/cloud-samples-tools/test/config_test.go index f8ad00f497..815cae1a12 100644 --- a/.github/cloud-samples-tools/test/config_test.go +++ b/.github/cloud-samples-tools/test/config_test.go @@ -1,7 +1,7 @@ package test import ( - "cloud-samples-tools/pkg/utils" + c "cloud-samples-tools/pkg/config" "os" "path/filepath" "testing" @@ -9,15 +9,15 @@ import ( func TestLoadConfigEmpty(t *testing.T) { path := filepath.Join("testdata", "config", "empty.json") - _, err := utils.LoadConfig(path) + _, err := c.LoadConfig(path) expectError(t, err) } func TestLoadConfigDefaultValues(t *testing.T) { path := filepath.Join("testdata", "config", "default-values.json") - config, err := utils.LoadConfig(path) + config, err := c.LoadConfig(path) ok(t, err) - expected := utils.Config{ + expected := c.Config{ PackageFile: []string{"package.json"}, Match: []string{"*"}, } @@ -26,9 +26,9 @@ func TestLoadConfigDefaultValues(t *testing.T) { func TestLoadConfigComments(t *testing.T) { path := filepath.Join("testdata", "config", "comments.jsonc") - config, err := utils.LoadConfig(path) + config, err := c.LoadConfig(path) ok(t, err) - expected := utils.Config{ + expected := c.Config{ PackageFile: []string{"package.json"}, Match: []string{"*"}, } @@ -40,16 +40,16 @@ func TestSaveLoadConfig(t *testing.T) { ok(t, err) defer os.Remove(file.Name()) - config := utils.Config{ + config := c.Config{ PackageFile: []string{"package.json"}, Ignore: []string{"node_modules/", "*.md"}, Match: []string{"*.js"}, ExcludePackages: []string{"excluded"}, } - err = utils.SaveConfig(config, file) + err = config.Save(file) ok(t, err) - loadedConfig, err := utils.LoadConfig(file.Name()) + loadedConfig, err := c.LoadConfig(file.Name()) ok(t, err) equals(t, config, loadedConfig) @@ -58,41 +58,77 @@ func TestSaveLoadConfig(t *testing.T) { func TestMatchNoPatterns(t *testing.T) { patterns := []string{} path := "path/to/file.js" - assert(t, !utils.Match(patterns, path), "match(%v, %v) = false") + assert(t, !c.Match(patterns, path), "match(%v, %v) = false") } func TestMatchFilePattern(t *testing.T) { patterns := []string{"*.js"} path := "path/to/file.js" - assert(t, utils.Match(patterns, path), "match(%v, %v) = true") + assert(t, c.Match(patterns, path), "match(%v, %v) = true") } func TestMatchFilePath(t *testing.T) { patterns := []string{"path/to/"} path := "path/to/file.js" - assert(t, utils.Match(patterns, path), "match(%v, %v) = true") + assert(t, c.Match(patterns, path), "match(%v, %v) = true") } func TestIsPackageDirNotExists(t *testing.T) { - config := utils.Config{PackageFile: []string{"package.json"}} + config := c.Config{PackageFile: []string{"package.json"}} dir := "path-does-not-exist" assert(t, !config.IsPackageDir(dir), "config.IsPackageDir(%v) = false") } func TestIsPackageDirExists(t *testing.T) { - config := utils.Config{PackageFile: []string{"package.json"}} + config := c.Config{PackageFile: []string{"package.json"}} dir := filepath.Join("testdata", "my-package") assert(t, config.IsPackageDir(dir), "config.IsPackageDir(%v) = true") } func TestFindPackageRoot(t *testing.T) { - config := utils.Config{PackageFile: []string{"package.json"}} + config := c.Config{PackageFile: []string{"package.json"}} path := filepath.Join("testdata", "my-file.txt") equals(t, ".", config.FindPackage(path)) } func TestFindPackageSubpackage(t *testing.T) { - config := utils.Config{PackageFile: []string{"package.json"}} + config := c.Config{PackageFile: []string{"package.json"}} path := filepath.Join("testdata", "my-package", "subpackage", "my-file.txt") equals(t, "testdata/my-package/subpackage", config.FindPackage(path)) } + +func TestChangedRoot(t *testing.T) { + config := c.Config{PackageFile: []string{"package.json"}} + diffs := []string{ + filepath.Join("testdata", "file.txt"), + } + got := config.Changed(diffs) + expected := []string{"."} + equals(t, expected, got) +} + +func TestChangedPackage(t *testing.T) { + config := c.Config{ + PackageFile: []string{"package.json"}, + Match: []string{"*"}, + } + diffs := []string{ + filepath.Join("testdata", "my-package", "file.txt"), + } + got := config.Changed(diffs) + expected := []string{"testdata/my-package"} + equals(t, expected, got) +} + +func TestChangedSubpackage(t *testing.T) { + config := c.Config{ + PackageFile: []string{"package.json"}, + Match: []string{"*"}, + } + diffs := []string{ + filepath.Join("testdata", "my-package", "subpackage", "file.txt"), + } + got := config.Changed(diffs) + expected := []string{"testdata/my-package/subpackage"} + equals(t, expected, got) +} From 25b6929f768867c7a2526d1be2ec3d0421b2c706 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 10 Oct 2024 16:40:25 -0700 Subject: [PATCH 122/127] use table based testing --- .../cloud-samples-tools/pkg/config/config.go | 18 +- .../cloud-samples-tools/test/config_test.go | 201 +++++++++++------- 2 files changed, 131 insertions(+), 88 deletions(-) diff --git a/.github/cloud-samples-tools/pkg/config/config.go b/.github/cloud-samples-tools/pkg/config/config.go index de2886c514..23cc7607c6 100644 --- a/.github/cloud-samples-tools/pkg/config/config.go +++ b/.github/cloud-samples-tools/pkg/config/config.go @@ -58,32 +58,32 @@ func (c *Config) Save(file *os.File) error { } // LoadConfig loads the config from the given path. -func LoadConfig(path string) (Config, error) { - config := Config{} - +func LoadConfig(path string) (*Config, error) { // Read the JSONC file. sourceJsonc, err := os.ReadFile(path) if err != nil { - return config, err + return nil, err } // Strip the comments and load the JSON. sourceJson := multiLineCommentsRegex.ReplaceAll(sourceJsonc, []byte{}) sourceJson = singleLineCommentsRegex.ReplaceAll(sourceJson, []byte{}) + + var config Config err = json.Unmarshal(sourceJson, &config) if err != nil { - return config, err + return &config, err } // Set default values if they are not set. if config.PackageFile == nil { - return config, errors.New("package-file is required") + return &config, errors.New("package-file is required") } if config.Match == nil { config.Match = []string{"*"} } - return config, nil + return &config, nil } // Match returns true if the path matches any of the patterns. @@ -152,6 +152,8 @@ func (c *Config) FindAllPackages(root string) ([]string, error) { } // Affected returns the packages that have been affected from diffs. +// If there are diffs on at leat one global file affecting all packages, +// then this returns all packages matched by the config. func (c *Config) Affected(diffs []string) ([]string, error) { changed := c.Changed(diffs) if slices.Contains(changed, ".") { @@ -161,6 +163,8 @@ func (c *Config) Affected(diffs []string) ([]string, error) { } // Changed returns the packages that have changed. +// It only returns packages that are matched by the config, +// and are not excluded by the config. func (c *Config) Changed(diffs []string) []string { changedUnique := make(map[string]bool) for _, diff := range diffs { diff --git a/.github/cloud-samples-tools/test/config_test.go b/.github/cloud-samples-tools/test/config_test.go index 815cae1a12..bb5d3b921b 100644 --- a/.github/cloud-samples-tools/test/config_test.go +++ b/.github/cloud-samples-tools/test/config_test.go @@ -7,32 +7,42 @@ import ( "testing" ) -func TestLoadConfigEmpty(t *testing.T) { - path := filepath.Join("testdata", "config", "empty.json") - _, err := c.LoadConfig(path) - expectError(t, err) -} - -func TestLoadConfigDefaultValues(t *testing.T) { - path := filepath.Join("testdata", "config", "default-values.json") - config, err := c.LoadConfig(path) - ok(t, err) - expected := c.Config{ - PackageFile: []string{"package.json"}, - Match: []string{"*"}, +func TestLoadConfig(t *testing.T) { + tests := []struct { + filename string + config *c.Config + fails bool + }{ + { + filename: "empty.json", + fails: true, + }, + { + filename: "default-values.json", + config: &c.Config{ + PackageFile: []string{"package.json"}, + Match: []string{"*"}, + }, + }, + { + filename: "comments.jsonc", + config: &c.Config{ + PackageFile: []string{"package.json"}, + Match: []string{"*"}, + }, + }, } - equals(t, expected, config) -} -func TestLoadConfigComments(t *testing.T) { - path := filepath.Join("testdata", "config", "comments.jsonc") - config, err := c.LoadConfig(path) - ok(t, err) - expected := c.Config{ - PackageFile: []string{"package.json"}, - Match: []string{"*"}, + for _, test := range tests { + path := filepath.Join("testdata", "config", test.filename) + got, err := c.LoadConfig(path) + if test.fails { + expectError(t, err) + continue + } + ok(t, err) + equals(t, test.config, got) } - equals(t, expected, config) } func TestSaveLoadConfig(t *testing.T) { @@ -52,83 +62,112 @@ func TestSaveLoadConfig(t *testing.T) { loadedConfig, err := c.LoadConfig(file.Name()) ok(t, err) - equals(t, config, loadedConfig) -} - -func TestMatchNoPatterns(t *testing.T) { - patterns := []string{} - path := "path/to/file.js" - assert(t, !c.Match(patterns, path), "match(%v, %v) = false") + equals(t, &config, loadedConfig) } -func TestMatchFilePattern(t *testing.T) { - patterns := []string{"*.js"} - path := "path/to/file.js" - assert(t, c.Match(patterns, path), "match(%v, %v) = true") -} - -func TestMatchFilePath(t *testing.T) { - patterns := []string{"path/to/"} - path := "path/to/file.js" - assert(t, c.Match(patterns, path), "match(%v, %v) = true") -} +func TestMatch(t *testing.T) { + tests := []struct { + patterns []string + path string + expected bool + }{ + { + patterns: []string{}, + path: "path/to/file.js", + expected: false, + }, + { + patterns: []string{"*.js"}, + path: "path/to/file.js", + expected: true, + }, + { + patterns: []string{"path/to/"}, + path: "path/to/file.js", + expected: true, + }, + } -func TestIsPackageDirNotExists(t *testing.T) { - config := c.Config{PackageFile: []string{"package.json"}} - dir := "path-does-not-exist" - assert(t, !config.IsPackageDir(dir), "config.IsPackageDir(%v) = false") + for _, test := range tests { + got := c.Match(test.patterns, test.path) + equals(t, test.expected, got) + } } -func TestIsPackageDirExists(t *testing.T) { +func TestIsPackage(t *testing.T) { config := c.Config{PackageFile: []string{"package.json"}} - dir := filepath.Join("testdata", "my-package") - assert(t, config.IsPackageDir(dir), "config.IsPackageDir(%v) = true") -} + tests := []struct { + path string + expected bool + }{ + { + path: filepath.Join("testdata", "path-does-not-exist"), + expected: false, + }, + { + path: filepath.Join("testdata", "my-package"), + expected: true, + }, + } -func TestFindPackageRoot(t *testing.T) { - config := c.Config{PackageFile: []string{"package.json"}} - path := filepath.Join("testdata", "my-file.txt") - equals(t, ".", config.FindPackage(path)) + for _, test := range tests { + got := config.IsPackageDir(test.path) + equals(t, test.expected, got) + } } -func TestFindPackageSubpackage(t *testing.T) { +func TestFindPackage(t *testing.T) { config := c.Config{PackageFile: []string{"package.json"}} - path := filepath.Join("testdata", "my-package", "subpackage", "my-file.txt") - equals(t, "testdata/my-package/subpackage", config.FindPackage(path)) -} + tests := []struct { + path string + expected string + }{ + { + path: filepath.Join("testdata", "my-file.txt"), + expected: ".", + }, + { + path: filepath.Join("testdata", "my-package", "my-file.txt"), + expected: filepath.Join("testdata", "my-package"), + }, + { + path: filepath.Join("testdata", "my-package", "subpackage", "my-file.txt"), + expected: filepath.Join("testdata", "my-package", "subpackage"), + }, + } -func TestChangedRoot(t *testing.T) { - config := c.Config{PackageFile: []string{"package.json"}} - diffs := []string{ - filepath.Join("testdata", "file.txt"), + for _, test := range tests { + got := config.FindPackage(test.path) + equals(t, test.expected, got) } - got := config.Changed(diffs) - expected := []string{"."} - equals(t, expected, got) } -func TestChangedPackage(t *testing.T) { +func TestChanged(t *testing.T) { config := c.Config{ PackageFile: []string{"package.json"}, Match: []string{"*"}, } - diffs := []string{ - filepath.Join("testdata", "my-package", "file.txt"), - } - got := config.Changed(diffs) - expected := []string{"testdata/my-package"} - equals(t, expected, got) -} -func TestChangedSubpackage(t *testing.T) { - config := c.Config{ - PackageFile: []string{"package.json"}, - Match: []string{"*"}, + tests := []struct { + diffs []string + expected []string + }{ + { + diffs: []string{filepath.Join("testdata", "file.txt")}, + expected: []string{"."}, + }, + { + diffs: []string{filepath.Join("testdata", "my-package", "file.txt")}, + expected: []string{filepath.Join("testdata", "my-package")}, + }, + { + diffs: []string{filepath.Join("testdata", "my-package", "subpackage", "file.txt")}, + expected: []string{filepath.Join("testdata", "my-package", "subpackage")}, + }, } - diffs := []string{ - filepath.Join("testdata", "my-package", "subpackage", "file.txt"), + + for _, test := range tests { + got := config.Changed(test.diffs) + equals(t, test.expected, got) } - got := config.Changed(diffs) - expected := []string{"testdata/my-package/subpackage"} - equals(t, expected, got) } From cc7fd6b040e13d3ac7b05656f6b2fa14db790417 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Thu, 10 Oct 2024 16:42:58 -0700 Subject: [PATCH 123/127] return nil if error --- .github/cloud-samples-tools/pkg/config/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/cloud-samples-tools/pkg/config/config.go b/.github/cloud-samples-tools/pkg/config/config.go index 23cc7607c6..b3f959d78f 100644 --- a/.github/cloud-samples-tools/pkg/config/config.go +++ b/.github/cloud-samples-tools/pkg/config/config.go @@ -72,12 +72,12 @@ func LoadConfig(path string) (*Config, error) { var config Config err = json.Unmarshal(sourceJson, &config) if err != nil { - return &config, err + return nil, err } // Set default values if they are not set. if config.PackageFile == nil { - return &config, errors.New("package-file is required") + return nil, errors.New("package-file is required") } if config.Match == nil { config.Match = []string{"*"} From 687262b6c4641f71da6c3a38f232dead3ad590af Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Tue, 15 Oct 2024 13:08:25 -0700 Subject: [PATCH 124/127] use manual test checks --- .../cloud-samples-tools/test/config_test.go | 51 ++++++++++++++----- .github/cloud-samples-tools/test/utils.go | 45 ---------------- 2 files changed, 38 insertions(+), 58 deletions(-) delete mode 100644 .github/cloud-samples-tools/test/utils.go diff --git a/.github/cloud-samples-tools/test/config_test.go b/.github/cloud-samples-tools/test/config_test.go index bb5d3b921b..a4a112d40f 100644 --- a/.github/cloud-samples-tools/test/config_test.go +++ b/.github/cloud-samples-tools/test/config_test.go @@ -4,6 +4,7 @@ import ( c "cloud-samples-tools/pkg/config" "os" "path/filepath" + "reflect" "testing" ) @@ -36,18 +37,23 @@ func TestLoadConfig(t *testing.T) { for _, test := range tests { path := filepath.Join("testdata", "config", test.filename) got, err := c.LoadConfig(path) - if test.fails { - expectError(t, err) - continue + if test.fails && err == nil { + t.Fatal("expected failure\n", got) + } + if !test.fails && err != nil { + t.Fatal("error loading config\n", err) + } + if !reflect.DeepEqual(test.config, got) { + t.Fatal("expected equal\n", test.config, got) } - ok(t, err) - equals(t, test.config, got) } } func TestSaveLoadConfig(t *testing.T) { file, err := os.CreateTemp("", "config-*.json") - ok(t, err) + if err != nil { + t.Fatal("error creating temp file\n", err) + } defer os.Remove(file.Name()) config := c.Config{ @@ -57,12 +63,23 @@ func TestSaveLoadConfig(t *testing.T) { ExcludePackages: []string{"excluded"}, } err = config.Save(file) - ok(t, err) + if err != nil { + t.Fatal("error saving config\n", err) + } + + err = file.Close() + if err != nil { + t.Fatal("error closing file\n", err) + } loadedConfig, err := c.LoadConfig(file.Name()) - ok(t, err) + if err != nil { + t.Fatal("error loading config\n", err) + } - equals(t, &config, loadedConfig) + if !reflect.DeepEqual(&config, loadedConfig) { + t.Fatal("expected equal\n", &config, loadedConfig) + } } func TestMatch(t *testing.T) { @@ -90,7 +107,9 @@ func TestMatch(t *testing.T) { for _, test := range tests { got := c.Match(test.patterns, test.path) - equals(t, test.expected, got) + if got != test.expected { + t.Fatal("expected equal\n", test.expected, got) + } } } @@ -112,7 +131,9 @@ func TestIsPackage(t *testing.T) { for _, test := range tests { got := config.IsPackageDir(test.path) - equals(t, test.expected, got) + if test.expected != got { + t.Fatal("expected equal\n", test.expected, got) + } } } @@ -138,7 +159,9 @@ func TestFindPackage(t *testing.T) { for _, test := range tests { got := config.FindPackage(test.path) - equals(t, test.expected, got) + if test.expected != got { + t.Fatal("expected equal\n", test.expected, got) + } } } @@ -168,6 +191,8 @@ func TestChanged(t *testing.T) { for _, test := range tests { got := config.Changed(test.diffs) - equals(t, test.expected, got) + if !reflect.DeepEqual(test.expected, got) { + t.Fatal("expected equal\n", test.expected, got) + } } } diff --git a/.github/cloud-samples-tools/test/utils.go b/.github/cloud-samples-tools/test/utils.go deleted file mode 100644 index 5ffbc2281f..0000000000 --- a/.github/cloud-samples-tools/test/utils.go +++ /dev/null @@ -1,45 +0,0 @@ -package test - -import ( - "fmt" - "path/filepath" - "reflect" - "runtime" - "testing" -) - -// assert fails the test if the condition is false. (https://github.com/benbjohnson/testing) -func assert(tb testing.TB, condition bool, msg string, v ...interface{}) { - if !condition { - _, file, line, _ := runtime.Caller(1) - fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...) - tb.FailNow() - } -} - -// equals fails the test if exp is not equal to act. (https://github.com/benbjohnson/testing) -func equals(tb testing.TB, exp, act interface{}) { - if !reflect.DeepEqual(exp, act) { - _, file, line, _ := runtime.Caller(1) - fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act) - tb.FailNow() - } -} - -// ok fails the test if an err is not nil. (https://github.com/benbjohnson/testing) -func ok(tb testing.TB, err error) { - if err != nil { - _, file, line, _ := runtime.Caller(1) - fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error()) - tb.FailNow() - } -} - -// expectError fails the test if an err is nil. -func expectError(tb testing.TB, err error) { - if err == nil { - _, file, line, _ := runtime.Caller(1) - fmt.Printf("\033[31m%s:%d: expected error\033[39m\n\n", filepath.Base(file), line) - tb.FailNow() - } -} From ed6906f393daf412ae75258a644ac79478bbab3f Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 21 Oct 2024 16:24:19 -0700 Subject: [PATCH 125/127] moved tests to source directory --- .github/cloud-samples-tools/README.md | 2 +- .../{test => pkg/config}/config_test.go | 2 +- .../pkg/config/testdata/config/comments.jsonc | 8 ++++++++ .../config}/testdata/config/default-values.json | 0 .../{test => pkg/config}/testdata/config/empty.json | 0 .../{test => pkg/config}/testdata/my-package/package.json | 0 .../config}/testdata/my-package/subpackage/package.json | 0 .../test/testdata/config/comments.jsonc | 8 -------- 8 files changed, 10 insertions(+), 10 deletions(-) rename .github/cloud-samples-tools/{test => pkg/config}/config_test.go (99%) create mode 100644 .github/cloud-samples-tools/pkg/config/testdata/config/comments.jsonc rename .github/cloud-samples-tools/{test => pkg/config}/testdata/config/default-values.json (100%) rename .github/cloud-samples-tools/{test => pkg/config}/testdata/config/empty.json (100%) rename .github/cloud-samples-tools/{test => pkg/config}/testdata/my-package/package.json (100%) rename .github/cloud-samples-tools/{test => pkg/config}/testdata/my-package/subpackage/package.json (100%) delete mode 100644 .github/cloud-samples-tools/test/testdata/config/comments.jsonc diff --git a/.github/cloud-samples-tools/README.md b/.github/cloud-samples-tools/README.md index c59a0177ed..b3b247923c 100644 --- a/.github/cloud-samples-tools/README.md +++ b/.github/cloud-samples-tools/README.md @@ -55,7 +55,7 @@ To the tools tests, we must change to the directory where the tools package is d We can run it in a subshell using parentheses to keep our working directory from changing. ```sh -(cd .github/workflows/samples-tools && go test ./test) +(cd .github/workflows/samples-tools && go test ./...) ``` ## Finding affected packages diff --git a/.github/cloud-samples-tools/test/config_test.go b/.github/cloud-samples-tools/pkg/config/config_test.go similarity index 99% rename from .github/cloud-samples-tools/test/config_test.go rename to .github/cloud-samples-tools/pkg/config/config_test.go index a4a112d40f..fc3cdd92e7 100644 --- a/.github/cloud-samples-tools/test/config_test.go +++ b/.github/cloud-samples-tools/pkg/config/config_test.go @@ -1,4 +1,4 @@ -package test +package config_test import ( c "cloud-samples-tools/pkg/config" diff --git a/.github/cloud-samples-tools/pkg/config/testdata/config/comments.jsonc b/.github/cloud-samples-tools/pkg/config/testdata/config/comments.jsonc new file mode 100644 index 0000000000..193ff7e88d --- /dev/null +++ b/.github/cloud-samples-tools/pkg/config/testdata/config/comments.jsonc @@ -0,0 +1,8 @@ +/* + * Multi-line + * comment + */ +{ + // Single line comment. + "package-file": /* inline comment */ ["package.json"] // trailing comment +} diff --git a/.github/cloud-samples-tools/test/testdata/config/default-values.json b/.github/cloud-samples-tools/pkg/config/testdata/config/default-values.json similarity index 100% rename from .github/cloud-samples-tools/test/testdata/config/default-values.json rename to .github/cloud-samples-tools/pkg/config/testdata/config/default-values.json diff --git a/.github/cloud-samples-tools/test/testdata/config/empty.json b/.github/cloud-samples-tools/pkg/config/testdata/config/empty.json similarity index 100% rename from .github/cloud-samples-tools/test/testdata/config/empty.json rename to .github/cloud-samples-tools/pkg/config/testdata/config/empty.json diff --git a/.github/cloud-samples-tools/test/testdata/my-package/package.json b/.github/cloud-samples-tools/pkg/config/testdata/my-package/package.json similarity index 100% rename from .github/cloud-samples-tools/test/testdata/my-package/package.json rename to .github/cloud-samples-tools/pkg/config/testdata/my-package/package.json diff --git a/.github/cloud-samples-tools/test/testdata/my-package/subpackage/package.json b/.github/cloud-samples-tools/pkg/config/testdata/my-package/subpackage/package.json similarity index 100% rename from .github/cloud-samples-tools/test/testdata/my-package/subpackage/package.json rename to .github/cloud-samples-tools/pkg/config/testdata/my-package/subpackage/package.json diff --git a/.github/cloud-samples-tools/test/testdata/config/comments.jsonc b/.github/cloud-samples-tools/test/testdata/config/comments.jsonc deleted file mode 100644 index 4f9f442fdb..0000000000 --- a/.github/cloud-samples-tools/test/testdata/config/comments.jsonc +++ /dev/null @@ -1,8 +0,0 @@ -/* - * Multi-line - * comment - */ -{ - // Single line comment. - "package-file": /* inline comment */ ["package.json"] -} From f3f88e635d7d738c8dd686a557ca76442eb140f8 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 21 Oct 2024 16:26:14 -0700 Subject: [PATCH 126/127] update test command --- .github/workflows/cloud-samples-tools-test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cloud-samples-tools-test.yaml b/.github/workflows/cloud-samples-tools-test.yaml index 0a11649e24..b6f9508c53 100644 --- a/.github/workflows/cloud-samples-tools-test.yaml +++ b/.github/workflows/cloud-samples-tools-test.yaml @@ -57,4 +57,4 @@ jobs: with: go-version: ${{ env.GO_VERSION }} - name: Run tests - run: go test ./test + run: go test ./... From a3cb72dc4ecf1172f666cdeafe5daa0b49942922 Mon Sep 17 00:00:00 2001 From: David Cavazos Date: Mon, 18 Nov 2024 14:36:59 -0800 Subject: [PATCH 127/127] comment schedule trigger --- .github/workflows/experimental.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/experimental.yaml b/.github/workflows/experimental.yaml index 5947d64fbd..5484a90463 100644 --- a/.github/workflows/experimental.yaml +++ b/.github/workflows/experimental.yaml @@ -18,9 +18,9 @@ on: branches: - main pull_request: - schedule: - # https://crontab.guru/#0_12_*_*_0 - - cron: 0 12 * * 0 # At 12:00 on Sunday + # schedule: + # # https://crontab.guru/#0_12_*_*_0 + # - cron: 0 12 * * 0 # At 12:00 on Sunday env: GO_VERSION: ^1.22.0