Skip to content

feat: add TypeScript samples support #3029

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

Merged
merged 77 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
1c6785f
chore: add owlbot
kweinmeister Feb 10, 2023
9146d04
add new lines
kweinmeister Feb 10, 2023
3f8f03b
Add owlbot.py
kweinmeister Feb 11, 2023
f79385c
Merge branch 'main' into owlbot
kweinmeister Feb 13, 2023
bf97dd6
Rename .Owlbot.yaml to .OwlBot.yaml
kweinmeister Feb 15, 2023
5a2bd95
Rename .Owlbot.lock.yaml to .OwlBot.lock.yaml
kweinmeister Feb 15, 2023
1ccd206
updates
kweinmeister Feb 15, 2023
76805b9
test typeless bot
kweinmeister Feb 15, 2023
8894f66
cleanup owlbot.py
kweinmeister Feb 15, 2023
1584b87
add typescript support for tests
kweinmeister Feb 15, 2023
d966485
update copyright year
kweinmeister Feb 15, 2023
264762e
update copyright year
kweinmeister Feb 15, 2023
c9f5e20
add ts-node dependency
kweinmeister Feb 15, 2023
369b892
update ts support
kweinmeister Feb 15, 2023
47b39c7
linting
kweinmeister Feb 15, 2023
ee4c11d
remove comment
kweinmeister Feb 15, 2023
56c1781
add npm build step
kweinmeister Feb 15, 2023
d99e884
change build cmd
kweinmeister Feb 15, 2023
84402f2
test updating pr
kweinmeister Feb 15, 2023
40d3af9
update comments
kweinmeister Feb 15, 2023
009643d
updated owlbot yaml
kweinmeister Feb 16, 2023
436d73d
Update .github/.OwlBot.lock.yaml
kweinmeister Feb 16, 2023
1283f94
Update .OwlBot.lock.yaml
kweinmeister Feb 16, 2023
7572152
update owlbot yaml
kweinmeister Feb 16, 2023
8e9a978
Merge branch 'owlbot' of https://github.com/GoogleCloudPlatform/nodej…
kweinmeister Feb 16, 2023
b0adc93
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 16, 2023
bc3ce86
remove extra comment
kweinmeister Feb 16, 2023
a5f4b43
Merge branch 'owlbot' of https://github.com/GoogleCloudPlatform/nodej…
kweinmeister Feb 16, 2023
bb7620f
Update createJob.js
kweinmeister Feb 16, 2023
4376bd7
update tsconfig
kweinmeister Feb 16, 2023
3cd9308
Merge branch 'owlbot' of https://github.com/GoogleCloudPlatform/nodej…
kweinmeister Feb 16, 2023
e8a0f70
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 16, 2023
eaaf2dc
add export filter
kweinmeister Feb 16, 2023
a243bfb
update filter
kweinmeister Feb 16, 2023
b72636d
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 16, 2023
98aeea7
owlbot update
kweinmeister Feb 17, 2023
3f31f87
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 17, 2023
c76278f
owlbot update
kweinmeister Feb 17, 2023
ed3e3d1
Merge branch 'owlbot' of https://github.com/GoogleCloudPlatform/nodej…
kweinmeister Feb 17, 2023
691d6e3
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 17, 2023
daa6f0b
final updates
kweinmeister Feb 17, 2023
701ed63
license
kweinmeister Feb 17, 2023
26a48aa
update linter
kweinmeister Feb 17, 2023
acf9c9a
add build step
kweinmeister Feb 17, 2023
670b4aa
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 17, 2023
608e912
Merge branch 'main' into owlbot
kweinmeister Feb 17, 2023
950a2df
more updates
kweinmeister Feb 17, 2023
67c815d
updates
kweinmeister Feb 17, 2023
2a6ec44
eslint
kweinmeister Feb 17, 2023
629c488
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 17, 2023
e0e6709
fix gts check
kweinmeister Feb 17, 2023
79008da
Merge branches 'owlbot' and 'owlbot' of https://github.com/GoogleClou…
kweinmeister Feb 17, 2023
550d1e9
reduce ecmascript version
kweinmeister Feb 17, 2023
002a9bc
fixes
kweinmeister Feb 17, 2023
059cb4c
add express types
kweinmeister Feb 17, 2023
f324630
skiplibcheck
kweinmeister Feb 17, 2023
179febb
remove global tsconfig
kweinmeister Feb 17, 2023
8a531d6
use root gts rules
kweinmeister Feb 17, 2023
82f4b0b
add root=true
kweinmeister Feb 17, 2023
9a7ea47
add extends to individual dirs
kweinmeister Feb 17, 2023
1ab5bba
update workflow
kweinmeister Feb 17, 2023
798e752
reorganize
kweinmeister Feb 17, 2023
e2422fd
update ts workflow
kweinmeister Feb 17, 2023
870e716
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 17, 2023
9512d97
revert ccai change
kweinmeister Feb 17, 2023
d2ddeb7
Merge branch 'owlbot' of https://github.com/GoogleCloudPlatform/nodej…
kweinmeister Feb 17, 2023
2876193
revert ccai change part deux
kweinmeister Feb 17, 2023
a543a43
fix spacing
kweinmeister Feb 17, 2023
f26148f
appengine include ts only
kweinmeister Feb 17, 2023
283462e
eslintignore
kweinmeister Feb 17, 2023
7dfaee8
update package json
kweinmeister Feb 17, 2023
d28c73f
exclude test files
kweinmeister Feb 17, 2023
8ade82e
Merge branch 'main' into owlbot
kweinmeister Feb 17, 2023
5fb2233
fix newline
kweinmeister Feb 17, 2023
61b0563
updates based on feedback
kweinmeister Feb 17, 2023
9ab4f3d
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 17, 2023
a85d7b8
Merge branch 'main' into owlbot
kweinmeister Feb 23, 2023
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
17 changes: 17 additions & 0 deletions .github/.OwlBot.lock.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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.
docker:
image: gcr.io/cloud-devrel-public-resources/owlbot-nodejs-mono-repo:latest
digest: sha256:85bf089b45ad70d1ec02d9d628a0051a06bb63fb79f6b740f787d1a34347f732

22 changes: 22 additions & 0 deletions .github/.OwlBot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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.
docker:
image: gcr.io/cloud-devrel-public-resources/owlbot-nodejs-mono-repo:latest

deep-remove-regex:
- /owl-bot-staging

deep-copy-regex:
- source: (.*)/(.*).js
dest: /owl-bot-staging/$1/$2
16 changes: 16 additions & 0 deletions .github/workflows/scheduler.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# 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: scheduler
on:
push:
Expand Down Expand Up @@ -41,6 +55,8 @@ jobs:
node-version: 16
- run: npm install
working-directory: scheduler
- run: npm run build
working-directory: scheduler
- run: npm test
working-directory: scheduler
env:
Expand Down
104 changes: 104 additions & 0 deletions owlbot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# 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.

import re
import subprocess
from pathlib import Path

from synthtool import shell
from synthtool.log import logger

_TOOLS_DIRECTORY = "/synthtool"
_EXCLUDED_DIRS = [r"node_modules", r"^\."]


def walk_through_owlbot_dirs(dir: Path, search_for_changed_files: bool) -> list[str]:
"""
Walks through all sample directories
Returns:
A list of directories
"""
owlbot_dirs: list[str] = []
packages_to_exclude = _EXCLUDED_DIRS
if search_for_changed_files:
try:
# Need to run this step first in the post processor since we only clone
# the branch the PR is on in the Docker container
output = subprocess.run(
["git", "fetch", "origin", "main:main", "--deepen=200"]
)
output.check_returncode()
except subprocess.CalledProcessError as e:
if e.returncode == 128:
logger.info(f"Error: ${e.output}; skipping fetching main")
else:
raise e
for path_object in dir.glob("**/package.json"):
object_dir = str(Path(path_object).parents[0])
if path_object.is_file() and object_dir != str(dir) and not re.search(
"(?:% s)" % "|".join(packages_to_exclude), str(Path(path_object))
):
if search_for_changed_files:
if (
subprocess.run(
[
"git",
"diff",
"--quiet",
"main...",
object_dir
]
).returncode
== 1
):
owlbot_dirs.append(object_dir)
else:
owlbot_dirs.append(object_dir)
for path_object in dir.glob("owl-bot-staging/*"):
owlbot_dirs.append(
f"{Path(path_object).parents[1]}/packages/{Path(path_object).name}"
)
return owlbot_dirs


def typeless_samples_hermetic(output_path: str, targets: str, hide_output: bool=False) -> None:
"""
Converts TypeScript samples in the current Node.js library
to JavaScript samples. Run this step before fix() and friends.
Assumes that typeless-sample-bot is already installed in a well
known location on disk (node_modules/.bin).
This is currently an optional, opt-in part of an individual repo's
OwlBot.py, and must be called from there before calling owlbot_main.
"""
logger.debug("Run typeless sample bot")
shell.run(
[
f"{_TOOLS_DIRECTORY}/node_modules/.bin/typeless-sample-bot",
"--outputpath",
output_path,
"--targets",
targets,
"--recursive",
],
check=False,
hide_output=hide_output,
)


# Retrieve list of directories
dirs: list[str] = walk_through_owlbot_dirs(Path.cwd(), search_for_changed_files=True)
for d in dirs:
logger.debug(f"Directory: {d}")
# Run typeless bot to convert from TS -> JS
typeless_samples_hermetic(output_path=d, targets=d)
21 changes: 14 additions & 7 deletions scheduler/createJob.js → scheduler/createJob.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 Google LLC
// 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.
Expand All @@ -17,26 +17,32 @@
// description: Create a job that posts to /log_payload on an App Engine service.
// usage: node createJob.js [project-id] [location-id] [app-engine-service-id]

import {protos, CloudSchedulerClient} from '@google-cloud/scheduler';

/**
* Create a job with an App Engine target via the Cloud Scheduler API
*/
async function createJob(projectId, locationId, serviceId) {
async function createJob(
projectId: string,
locationId: string,
serviceId: string
) {
// [START cloudscheduler_create_job]
const scheduler = require('@google-cloud/scheduler');

// Create a client.
const client = new scheduler.CloudSchedulerClient();
const client: CloudSchedulerClient = new scheduler.CloudSchedulerClient();

// TODO(developer): Uncomment and set the following variables
// const projectId = "PROJECT_ID"
// const locationId = "LOCATION_ID"
// const serviceId = "my-serivce"

// Construct the fully qualified location path.
const parent = client.locationPath(projectId, locationId);
const parent: string = client.locationPath(projectId, locationId);

// Construct the request body.
const job = {
const job: protos.google.cloud.scheduler.v1.IJob = {
appEngineHttpTarget: {
appEngineRouting: {
service: serviceId,
Expand All @@ -49,19 +55,20 @@ async function createJob(projectId, locationId, serviceId) {
timeZone: 'America/Los_Angeles',
};

const request = {
const request: protos.google.cloud.scheduler.v1.ICreateJobRequest = {
parent: parent,
job: job,
};

// Use the client to send the job creation request.
const [response] = await client.createJob(request);
// await client.createJob(request);
console.log(`Created job: ${response.name}`);
// [END cloudscheduler_create_job]
}

const args = process.argv.slice(2);
createJob(...args).catch(err => {
createJob(args[0], args[1], args[2]).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
9 changes: 6 additions & 3 deletions scheduler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@
"node": ">=12.0.0"
},
"files": [
"*.js"
"*.js", "*.ts"
],
"license": "Apache-2.0",
"scripts": {
"build": "tsc",
"start": "node app.js",
"test": "mocha --timeout 10000 --exit"
"test": "mocha --require ts-node/register test/*.ts --timeout 10000 --exit"
},
"dependencies": {
"@google-cloud/scheduler": "^3.0.5",
"body-parser": "^1.18.3",
"express": "^4.16.4"
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"chai": "^4.2.0",
"mocha": "^10.0.0"
"mocha": "^10.0.0",
"ts-node": "^10.9.1"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 Google LLC
// 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.
Expand All @@ -14,33 +14,33 @@

'use strict';

const {CloudSchedulerClient} = require('@google-cloud/scheduler');
import {CloudSchedulerClient} from '@google-cloud/scheduler';

const {assert} = require('chai');
const {describe, it, before} = require('mocha');
const cp = require('child_process');

const client = new CloudSchedulerClient();
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
const client: CloudSchedulerClient = new CloudSchedulerClient();
const execSync = (cmd: String) => cp.execSync(cmd, {encoding: 'utf-8'});
const LOCATION_ID = process.env.LOCATION_ID || 'us-central1';
const SERVICE_ID = 'my-service';

describe('Cloud Scheduler Sample Tests', () => {
let PROJECT_ID, stdout;
let PROJECT_ID: String, stdout: String;

before(async () => {
PROJECT_ID = await client.getProjectId();
});

after(async () => {
const jobName = stdout && stdout.trim().split(' ').slice(-1);
const jobName = (stdout && stdout.trim().split(' ').slice(-1))[0];
if (jobName) {
await client.deleteJob({name: jobName});
}
});

it('should create a scheduler job', async () => {
stdout = execSync(
`node createJob.js ${PROJECT_ID} ${LOCATION_ID} ${SERVICE_ID}`
`node --loader ts-node/esm createJob.ts ${PROJECT_ID} ${LOCATION_ID} ${SERVICE_ID}`
);
assert.match(stdout, /Created job/);
});
Expand Down
8 changes: 8 additions & 0 deletions scheduler/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "ESNext",
"strict": true,
"noImplicitAny": false,
"moduleResolution": "node",
}
}