Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: automate discovery types #1366

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2757c3c
ci: add workflow to update discovery types
alvarowolfx Jan 22, 2024
3f39f50
ci: make discovery workflow temp trigger on PR
alvarowolfx Jan 22, 2024
02a1154
fix: use main branch as base
alvarowolfx Jan 22, 2024
34cb456
fix: update types script to keep license header
alvarowolfx Jan 22, 2024
5162b72
ci: update branch name
alvarowolfx Jan 22, 2024
afb4bb9
Merge branch 'main' into ci-automate-discovery-types
alvarowolfx May 14, 2024
af93438
chore: update actions versions
alvarowolfx May 14, 2024
5edee9b
ci: remove pr trigger
alvarowolfx May 23, 2024
cbd9462
Merge branch 'main' into ci-automate-discovery-types
alvarowolfx May 23, 2024
94ca295
fix: remove package.json change
alvarowolfx May 24, 2024
1e989ae
ci: use commit hash on gha
alvarowolfx May 31, 2024
6a5889a
Merge branch 'main' into ci-automate-discovery-types
alvarowolfx May 31, 2024
8ddc849
ci: change to run daily
alvarowolfx Jun 3, 2024
0ce8210
ci: move away from external action to create PR
alvarowolfx Jun 3, 2024
032b51c
fix: lint issues
alvarowolfx Jun 3, 2024
7966a79
ci: add dev deps
alvarowolfx Jun 3, 2024
96a792c
ci: update deps to older version
alvarowolfx Jun 3, 2024
f6abb66
ci: temp enable to run on PR
alvarowolfx Jun 3, 2024
b0ce38c
fix: set up git user ealier
alvarowolfx Jun 3, 2024
b4997bd
test: set personal fork
alvarowolfx Jun 3, 2024
d39ccf0
ci: validate that pr exists
alvarowolfx Jun 3, 2024
53661f9
fix: remove checkout main
alvarowolfx Jun 3, 2024
b094d54
fix: remove checkout main
alvarowolfx Jun 3, 2024
5cf7d65
ci: set main repo
alvarowolfx Jun 3, 2024
7d54344
ci: change to weekly sync
alvarowolfx Jun 4, 2024
ce7b21a
ci: change github token env
alvarowolfx Jun 4, 2024
95d4239
ci: add comment on line to remove on workflow
alvarowolfx Jun 4, 2024
13b1da4
ci: add github token back
alvarowolfx Jun 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/discovery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
on:
schedule:
- cron: '0 12 * * TUE'
pull_request: # REMOVE BEFORE MERGING
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pull_request: # REMOVE BEFORE MERGING
pull_request: # REMOVE BEFORE MERGING

Does this need to be removed now?

workflow_dispatch:
name: Update Discovery Generated Types
jobs:
sync:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
- uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
Comment on lines +11 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these get updated by dependabot or renovate bot? Wanting to make sure we're not adding a manual maintenance step of updating

with:
node-version: 16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for this version about maintenance

# Install all deps, including dev dependencies.
- run: npm install
# Generate types
- run: npm run types
# Submit pull request
- run: npm run submit-discovery-pr
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CODE_BOT_TOKEN: ${{ secrets.APIARY_GEN_SECRET }}

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -55,7 +56,9 @@
"arrify": "^2.0.1",
"big.js": "^6.0.0",
"duplexify": "^4.0.0",
"execa": "^5.0.0",
"extend": "^3.0.2",
"gaxios": "^6.0.3",
Comment on lines +59 to +61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should these be dev dependencies instead of dependencies we're having the user add?

"is": "^3.3.0",
"stream-events": "^1.0.5",
"uuid": "^9.0.0"
Expand Down
96 changes: 96 additions & 0 deletions scripts/submit-discovery-pr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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 gaxios = require('gaxios');

const REPO = 'googleapis/nodejs-bigquery';
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']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL 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;
}

if (process.env.GITHUB_ACTIONS) {
await execa('git', ['config', 'user.email', 'yoshi-automation@google.com']);
await execa('git', ['config', 'user.name', 'Yoshi Automation']);
Comment on lines +42 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sofisl just checking this is our most up to date git username and email

}

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');
}
const codeBotToken = process.env.CODE_BOT_TOKEN;
if (!codeBotToken) {
throw new Error('please include a CODE_BOT_TOKEN');
}

try {
// Open the pull request with the GITHUB_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);
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;
}
console.log('PR already exists');
}
} else {
throw err;
}
}
}

submitDiscoveryPR();
Loading