From 2757c3c978e92d00cc1a3b365643c5886d885f93 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 22 Jan 2024 10:19:05 -0400 Subject: [PATCH 01/20] ci: add workflow to update discovery types --- .github/workflows/discovery.yaml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .github/workflows/discovery.yaml diff --git a/.github/workflows/discovery.yaml b/.github/workflows/discovery.yaml new file mode 100644 index 00000000..8980ea7d --- /dev/null +++ b/.github/workflows/discovery.yaml @@ -0,0 +1,25 @@ +on: + schedule: + - cron: '0 12 * * TUE' +name: Update Discovery Generated Types +jobs: + sync: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 14 + # Install all deps, including dev dependencies. + - run: npm install + # Generate types + - run: npm run types + - name: Create Pull Request + uses: peter-evans/create-pull-request@v5 + with: + add-paths: src/types.d.ts + commit-message: "chore: update types from Discovery" + title: "chore: update types from Discovery" + body: | + Automated pull-request to keep BigQuery Discovery types up-to-date. + From 3f39f50de77357bddf4dac13f84bd8df09f21a19 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 22 Jan 2024 10:32:42 -0400 Subject: [PATCH 02/20] ci: make discovery workflow temp trigger on PR --- .github/workflows/discovery.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/discovery.yaml b/.github/workflows/discovery.yaml index 8980ea7d..d6c5a31f 100644 --- a/.github/workflows/discovery.yaml +++ b/.github/workflows/discovery.yaml @@ -1,6 +1,7 @@ on: schedule: - cron: '0 12 * * TUE' + pull_request: name: Update Discovery Generated Types jobs: sync: From 02a1154ebfe4ec7002821f73fee60d4a27ff2998 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 22 Jan 2024 10:35:26 -0400 Subject: [PATCH 03/20] fix: use main branch as base --- .github/workflows/discovery.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/discovery.yaml b/.github/workflows/discovery.yaml index d6c5a31f..66d4806b 100644 --- a/.github/workflows/discovery.yaml +++ b/.github/workflows/discovery.yaml @@ -18,6 +18,7 @@ jobs: - name: Create Pull Request uses: peter-evans/create-pull-request@v5 with: + base: main add-paths: src/types.d.ts commit-message: "chore: update types from Discovery" title: "chore: update types from Discovery" From 34cb456ed9ed3c02677cae3c84382f792d5f1e79 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 22 Jan 2024 11:02:23 -0400 Subject: [PATCH 04/20] fix: update types script to keep license header --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5bd1160c..726fa485 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "pretest": "npm run compile", "docs-test": "linkinator docs", "predocs-test": "npm run docs", - "types": "dtsd bigquery v2 > ./src/types.d.ts", + "types": "head -n 14 ./src/types.d.ts > /tmp/license && cat /tmp/license > ./src/types.d.ts && dtsd bigquery v2 >> ./src/types.d.ts", "prelint": "cd samples; npm link ../; npm install", "precompile": "gts clean" }, From 5162b72c69f652a1e890037aa6fca7db1a43effb Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 22 Jan 2024 11:06:39 -0400 Subject: [PATCH 05/20] ci: update branch name --- .github/workflows/discovery.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/discovery.yaml b/.github/workflows/discovery.yaml index 66d4806b..4da3405b 100644 --- a/.github/workflows/discovery.yaml +++ b/.github/workflows/discovery.yaml @@ -19,6 +19,7 @@ jobs: uses: peter-evans/create-pull-request@v5 with: base: main + branch: update-discovery/patch add-paths: src/types.d.ts commit-message: "chore: update types from Discovery" title: "chore: update types from Discovery" From af93438eb878c51464513615fc0fcb6d2ae02b79 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Tue, 14 May 2024 14:30:16 -0400 Subject: [PATCH 06/20] chore: update actions versions --- .github/workflows/discovery.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/discovery.yaml b/.github/workflows/discovery.yaml index 4da3405b..b4c8d03b 100644 --- a/.github/workflows/discovery.yaml +++ b/.github/workflows/discovery.yaml @@ -7,16 +7,16 @@ jobs: sync: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: - node-version: 14 + node-version: 16 # Install all deps, including dev dependencies. - run: npm install # Generate types - run: npm run types - name: Create Pull Request - uses: peter-evans/create-pull-request@v5 + uses: peter-evans/create-pull-request@v6 with: base: main branch: update-discovery/patch From 5edee9bb79cc56803d215df1fe27e8e0a7f44230 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Thu, 23 May 2024 16:34:32 -0400 Subject: [PATCH 07/20] ci: remove pr trigger --- .github/workflows/discovery.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/discovery.yaml b/.github/workflows/discovery.yaml index b4c8d03b..e141a230 100644 --- a/.github/workflows/discovery.yaml +++ b/.github/workflows/discovery.yaml @@ -1,7 +1,6 @@ on: schedule: - cron: '0 12 * * TUE' - pull_request: name: Update Discovery Generated Types jobs: sync: From 94ca295826ae9c655edf2fd52bfe20af859a55ae Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Fri, 24 May 2024 15:14:55 -0400 Subject: [PATCH 08/20] fix: remove package.json change --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bbdc4da9..c75e3130 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "prepare": "npm run compile", "pretest": "npm run compile", "docs-test": "linkinator docs", - "predocs-test": "npm run docs", + "predocs-test": "npm run docs", "types": "node scripts/gen-types.js", "prelint": "cd samples; npm link ../; npm install", "precompile": "gts clean" From 1e989ae92fb9dad36f620e33eec8b0fc4689135c Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Fri, 31 May 2024 15:36:39 -0400 Subject: [PATCH 09/20] ci: use commit hash on gha --- .github/workflows/discovery.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/discovery.yaml b/.github/workflows/discovery.yaml index e141a230..4c8b8811 100644 --- a/.github/workflows/discovery.yaml +++ b/.github/workflows/discovery.yaml @@ -6,8 +6,8 @@ jobs: sync: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 + - uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2 with: node-version: 16 # Install all deps, including dev dependencies. @@ -15,7 +15,7 @@ jobs: # Generate types - run: npm run types - name: Create Pull Request - uses: peter-evans/create-pull-request@v6 + uses: peter-evans/create-pull-request@6d6857d36972b65feb161a90e484f2984215f83e # v6.0.5 with: base: main branch: update-discovery/patch From 8ddc84906d5fb479c7dd571a783d8062274e688e Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 3 Jun 2024 15:25:13 -0400 Subject: [PATCH 10/20] ci: change to run daily --- .github/workflows/discovery.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/discovery.yaml b/.github/workflows/discovery.yaml index 4c8b8811..4b31d1b7 100644 --- a/.github/workflows/discovery.yaml +++ b/.github/workflows/discovery.yaml @@ -1,6 +1,6 @@ on: schedule: - - cron: '0 12 * * TUE' + - cron: '0 12 * * *' name: Update Discovery Generated Types jobs: sync: From 0ce8210a8ccbd887b4ad46411a82eb1e3c9e359d Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 3 Jun 2024 16:14:17 -0400 Subject: [PATCH 11/20] ci: move away from external action to create PR --- .github/workflows/discovery.yaml | 15 ++---- package.json | 1 + scripts/submit-discovery-pr.js | 83 ++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 scripts/submit-discovery-pr.js diff --git a/.github/workflows/discovery.yaml b/.github/workflows/discovery.yaml index 4b31d1b7..c965ce17 100644 --- a/.github/workflows/discovery.yaml +++ b/.github/workflows/discovery.yaml @@ -1,6 +1,7 @@ on: schedule: - cron: '0 12 * * *' + workflow_dispatch: name: Update Discovery Generated Types jobs: sync: @@ -14,14 +15,8 @@ jobs: - run: npm install # Generate types - run: npm run types - - name: Create Pull Request - uses: peter-evans/create-pull-request@6d6857d36972b65feb161a90e484f2984215f83e # v6.0.5 - with: - base: main - branch: update-discovery/patch - add-paths: src/types.d.ts - commit-message: "chore: update types from Discovery" - title: "chore: update types from Discovery" - body: | - Automated pull-request to keep BigQuery Discovery types up-to-date. + # Submit pull request + - run: npm run submit-discovery-pr + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/package.json b/package.json index b5545d1b..db0122d8 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "docs-test": "linkinator docs", "predocs-test": "npm run docs", "types": "node scripts/gen-types.js", + "submit-discovery-pr": "node scripts/submit-discovery-pr.js", "prelint": "cd samples; npm link ../; npm install", "precompile": "gts clean" }, diff --git a/scripts/submit-discovery-pr.js b/scripts/submit-discovery-pr.js new file mode 100644 index 00000000..23e3bff3 --- /dev/null +++ b/scripts/submit-discovery-pr.js @@ -0,0 +1,83 @@ +// 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. + +const execa = require('execa'); +const path = require('path'); +const fs = require('fs'); +const gaxios = require('gaxios'); + +const REPO = 'googleapis/bigquery-nodejs'; +const BRANCH = 'update-discovery/patch'; +const TRACK_PATHS = ['src/types.d.ts']; +const COMMIT_MESSAGE = 'chore: update types from Discovery'; +const COMMIT_BODY = + 'Automated pull-request to keep BigQuery Discovery types up-to-date.'; + +async function submitDiscoveryPR() { + const statusResult = await execa('git', ['status', '--porcelain']); + const status = statusResult.stdout; + const statusFiles = status.split('\n').map(x => x.slice(3)); + + const foundChanges = statusFiles.filter(f => { + return TRACK_PATHS.some(filename => f.startsWith(filename)); + }); + console.log(`Changes found in ${foundChanges.length} files`); + console.log(foundChanges.join('\n')); + + if (foundChanges.length === 0) { + console.log('No changes found'); + return; + } + + await execa('git', ['checkout', '-B', BRANCH]); + for (const filename of foundChanges) { + await execa('git', ['add', filename]); + } + await execa('git', ['commit', '-m', COMMIT_MESSAGE, '-m', COMMIT_BODY]); + await execa('git', ['push', 'origin', BRANCH, '--force']); + + + const githubToken = process.env.GITHUB_TOKEN; + if (!githubToken) { + throw new Error('please include a GITHUB_TOKEN'); + } + + if (process.env.GITHUB_ACTIONS) { + await execa('git', ['config', 'user.email', 'yoshi-automation@google.com']); + await execa('git', ['config', 'user.name', 'Yoshi Automation']); + } + + try { + // Open the pull request with the YOSHI_CODE_BOT_TOKEN + await gaxios.request({ + method: 'POST', + headers: { + Authorization: `token ${githubToken}`, + }, + url: `https://api.github.com/repos/${REPO}/pulls`, + data: { + title: COMMIT_MESSAGE, + head: BRANCH, + base: 'main', + body: COMMIT_BODY, + }, + }); + } catch (err) { + console.error('failed to submit Pull Request', err); + throw err; + } + await execa('git', ['checkout', 'main']); +} + +submitDiscoveryPR(); From 032b51c383f57dd9d8a9d1fe5e5ffde45831bb90 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 3 Jun 2024 16:16:00 -0400 Subject: [PATCH 12/20] fix: lint issues --- scripts/submit-discovery-pr.js | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/submit-discovery-pr.js b/scripts/submit-discovery-pr.js index 23e3bff3..89e55775 100644 --- a/scripts/submit-discovery-pr.js +++ b/scripts/submit-discovery-pr.js @@ -47,7 +47,6 @@ async function submitDiscoveryPR() { await execa('git', ['commit', '-m', COMMIT_MESSAGE, '-m', COMMIT_BODY]); await execa('git', ['push', 'origin', BRANCH, '--force']); - const githubToken = process.env.GITHUB_TOKEN; if (!githubToken) { throw new Error('please include a GITHUB_TOKEN'); From 7966a792584e4570e1eefaa9e369be958d540678 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 3 Jun 2024 16:23:30 -0400 Subject: [PATCH 13/20] ci: add dev deps --- package.json | 2 ++ scripts/submit-discovery-pr.js | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index db0122d8..e257af8d 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,9 @@ "arrify": "^2.0.1", "big.js": "^6.0.0", "duplexify": "^4.0.0", + "execa": "^9.1.0", "extend": "^3.0.2", + "gaxios": "^6.6.0", "is": "^3.3.0", "stream-events": "^1.0.5", "uuid": "^9.0.0" diff --git a/scripts/submit-discovery-pr.js b/scripts/submit-discovery-pr.js index 89e55775..5668380d 100644 --- a/scripts/submit-discovery-pr.js +++ b/scripts/submit-discovery-pr.js @@ -13,8 +13,6 @@ // limitations under the License. const execa = require('execa'); -const path = require('path'); -const fs = require('fs'); const gaxios = require('gaxios'); const REPO = 'googleapis/bigquery-nodejs'; From 96a792c62f26da541c3943b53c019e4cbb7c26ed Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 3 Jun 2024 16:26:03 -0400 Subject: [PATCH 14/20] ci: update deps to older version --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index e257af8d..08d8d1f7 100644 --- a/package.json +++ b/package.json @@ -56,9 +56,9 @@ "arrify": "^2.0.1", "big.js": "^6.0.0", "duplexify": "^4.0.0", - "execa": "^9.1.0", + "execa": "^5.0.0", "extend": "^3.0.2", - "gaxios": "^6.6.0", + "gaxios": "^6.0.3", "is": "^3.3.0", "stream-events": "^1.0.5", "uuid": "^9.0.0" From f6abb6694d70243af48b11ffb37d7af0d39207a5 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 3 Jun 2024 16:34:27 -0400 Subject: [PATCH 15/20] ci: temp enable to run on PR --- .github/workflows/discovery.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/discovery.yaml b/.github/workflows/discovery.yaml index c965ce17..3d5523df 100644 --- a/.github/workflows/discovery.yaml +++ b/.github/workflows/discovery.yaml @@ -1,6 +1,7 @@ on: schedule: - cron: '0 12 * * *' + pull_request: workflow_dispatch: name: Update Discovery Generated Types jobs: From b0ce38cdd98893dadc2a2a8d7932ac91a9c03f5f Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 3 Jun 2024 16:36:49 -0400 Subject: [PATCH 16/20] fix: set up git user ealier --- scripts/submit-discovery-pr.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/submit-discovery-pr.js b/scripts/submit-discovery-pr.js index 5668380d..e228bb37 100644 --- a/scripts/submit-discovery-pr.js +++ b/scripts/submit-discovery-pr.js @@ -38,6 +38,11 @@ async function submitDiscoveryPR() { return; } + if (process.env.GITHUB_ACTIONS) { + await execa('git', ['config', 'user.email', 'yoshi-automation@google.com']); + await execa('git', ['config', 'user.name', 'Yoshi Automation']); + } + await execa('git', ['checkout', '-B', BRANCH]); for (const filename of foundChanges) { await execa('git', ['add', filename]); @@ -50,11 +55,6 @@ async function submitDiscoveryPR() { throw new Error('please include a GITHUB_TOKEN'); } - if (process.env.GITHUB_ACTIONS) { - await execa('git', ['config', 'user.email', 'yoshi-automation@google.com']); - await execa('git', ['config', 'user.name', 'Yoshi Automation']); - } - try { // Open the pull request with the YOSHI_CODE_BOT_TOKEN await gaxios.request({ From b4997bddd1f1aa9d83210d2757ff28b9ce725b2a Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 3 Jun 2024 16:45:04 -0400 Subject: [PATCH 17/20] test: set personal fork --- scripts/submit-discovery-pr.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/submit-discovery-pr.js b/scripts/submit-discovery-pr.js index e228bb37..4dafabed 100644 --- a/scripts/submit-discovery-pr.js +++ b/scripts/submit-discovery-pr.js @@ -15,7 +15,7 @@ const execa = require('execa'); const gaxios = require('gaxios'); -const REPO = 'googleapis/bigquery-nodejs'; +const REPO = 'alvarowolfx/nodejs-bigquery'; const BRANCH = 'update-discovery/patch'; const TRACK_PATHS = ['src/types.d.ts']; const COMMIT_MESSAGE = 'chore: update types from Discovery'; @@ -56,7 +56,7 @@ async function submitDiscoveryPR() { } try { - // Open the pull request with the YOSHI_CODE_BOT_TOKEN + // Open the pull request with the GITHUB_TOKEN await gaxios.request({ method: 'POST', headers: { From d39ccf0b7566b44e9b688277a5a582271ccb1efc Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 3 Jun 2024 16:53:43 -0400 Subject: [PATCH 18/20] ci: validate that pr exists --- scripts/submit-discovery-pr.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/scripts/submit-discovery-pr.js b/scripts/submit-discovery-pr.js index 4dafabed..092224bc 100644 --- a/scripts/submit-discovery-pr.js +++ b/scripts/submit-discovery-pr.js @@ -71,8 +71,20 @@ async function submitDiscoveryPR() { }, }); } catch (err) { - console.error('failed to submit Pull Request', err); - throw err; + console.error('failed to submit Pull Request:', err); + if (err.response && err.response.data) { + if (err.response.data.errors) { + const errors = err.response.data.errors; + const exists = errors.some(err => + err.message.includes('already exists') + ); + if (!exists) { + throw err; + } + } + } else { + throw err; + } } await execa('git', ['checkout', 'main']); } From 53661f972f3423395efc26c71eee1a94d276f45f Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 3 Jun 2024 16:55:26 -0400 Subject: [PATCH 19/20] fix: remove checkout main --- scripts/submit-discovery-pr.js | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/submit-discovery-pr.js b/scripts/submit-discovery-pr.js index 092224bc..d1f52917 100644 --- a/scripts/submit-discovery-pr.js +++ b/scripts/submit-discovery-pr.js @@ -81,6 +81,7 @@ async function submitDiscoveryPR() { if (!exists) { throw err; } + console.log('PR already exists'); } } else { throw err; From b094d54b775febef50496a27f75154176dacd4f6 Mon Sep 17 00:00:00 2001 From: Alvaro Viebrantz Date: Mon, 3 Jun 2024 16:55:49 -0400 Subject: [PATCH 20/20] fix: remove checkout main --- scripts/submit-discovery-pr.js | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/submit-discovery-pr.js b/scripts/submit-discovery-pr.js index d1f52917..4017f53b 100644 --- a/scripts/submit-discovery-pr.js +++ b/scripts/submit-discovery-pr.js @@ -87,7 +87,6 @@ async function submitDiscoveryPR() { throw err; } } - await execa('git', ['checkout', 'main']); } submitDiscoveryPR();