Skip to content

Commit

Permalink
ci: move away from external action to create PR
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarowolfx committed Jun 3, 2024
1 parent 8ddc849 commit 0ce8210
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 10 deletions.
15 changes: 5 additions & 10 deletions .github/workflows/discovery.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
on:
schedule:
- cron: '0 12 * * *'
workflow_dispatch:
name: Update Discovery Generated Types
jobs:
sync:
Expand All @@ -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 }}

1 change: 1 addition & 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 Down
83 changes: 83 additions & 0 deletions scripts/submit-discovery-pr.js
Original file line number Diff line number Diff line change
@@ -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');

Check failure on line 15 in scripts/submit-discovery-pr.js

View workflow job for this annotation

GitHub Actions / lint

"execa" is extraneous
const path = require('path');

Check failure on line 16 in scripts/submit-discovery-pr.js

View workflow job for this annotation

GitHub Actions / lint

'path' is assigned a value but never used
const fs = require('fs');

Check failure on line 17 in scripts/submit-discovery-pr.js

View workflow job for this annotation

GitHub Actions / lint

'fs' is assigned a value but never used
const gaxios = require('gaxios');

Check failure on line 18 in scripts/submit-discovery-pr.js

View workflow job for this annotation

GitHub Actions / lint

"gaxios" is extraneous

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']);


Check failure on line 50 in scripts/submit-discovery-pr.js

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
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', '[email protected]']);
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();

0 comments on commit 0ce8210

Please sign in to comment.