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

test: add script to check that all services have corresponding endpoints #145

Merged
merged 2 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ jobs:
steps:
- uses: yandex-cloud/nodejs-sdk/.github/actions/checkout-and-install-node@f69248b52b7991214847e889f28ba0883ed0ca2c
- run: npm run test
# check-endpoints:
# runs-on: ubuntu-20.04
# steps:
# - uses: yandex-cloud/nodejs-sdk/.github/actions/checkout-and-install-node@f69248b52b7991214847e889f28ba0883ed0ca2c
# - run: npm run check-endpoints
lint:
runs-on: ubuntu-20.04
steps:
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"eslint-plugin-prefer-arrow-functions": "^3.1.4",
"eslint-plugin-unicorn": "^39.0.0",
"fast-glob": "^3.2.7",
"grpc-tools": "^1.11.2",
"grpc-tools": "^1.12.4",
"husky": "^7.0.4",
"jest": "^27.4.5",
"semantic-release": "^21.0.1",
Expand All @@ -70,6 +70,7 @@
"lint": "eslint src config",
"build": "cross-env NODE_OPTIONS=\"--max-old-space-size=4096\" tsc -p .",
"generate-code": "ts-node scripts/generate-code.ts",
"check-endpoints": "ts-node scripts/check-endpoints.ts",
"prepare": "husky install",
"prepublishOnly": "npm run build"
},
Expand Down
79 changes: 79 additions & 0 deletions scripts/check-endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import * as fg from 'fast-glob';
import * as path from 'path';
import {
Namespace, NamespaceBase, Root, Service,
} from 'protobufjs';
import { SERVICE_ENDPOINTS_LIST } from '../src/service-endpoints';

const PROTO_DIR = path.resolve('./cloudapi');

const protoFiles = fg.sync('**/*.proto', { cwd: PROTO_DIR });

const pbRoot = new Root();

pbRoot.resolvePath = (origin, target) => {
const targets = target.split('/');

switch (targets[0]) {
case 'google': {
switch (targets[1]) {
case 'protobuf': {
return `./node_modules/protobufjs/${target}`;
}
default: {
return `./cloudapi/third_party/googleapis/${target}`;
}
}
}
case 'third_party': {
return `./cloudapi/${target}`;
}
case 'yandex': {
return `./cloudapi/${target}`;
}
default: {
return target;
}
}
};

const SERVICES: Service[] = [];
const findServices = <T extends NamespaceBase>(node: T) => {
for (const child of Object.values(node.nested ?? {})
.sort((a, b) => (a.name < b.name ? -1 : (a.name === b.name ? 0 : 1)))) {
if (child instanceof Service) {
SERVICES.push(child);
} else if (child instanceof Namespace) {
findServices(child);
}
}
};

// eslint-disable-next-line unicorn/prefer-top-level-await
pbRoot.load(protoFiles, { alternateCommentMode: true }).then((loadedRoot) => {
const SERVICE_IDS = new Set<string>();
let hasMissing = false;

for (const serviceEndpoint of SERVICE_ENDPOINTS_LIST) {
for (const service of serviceEndpoint.serviceIds) {
SERVICE_IDS.add(service);
}
}

findServices(loadedRoot);
for (const s of SERVICES) {
// full name without leading dot
const fullName = s.fullName.slice(1);

if (!SERVICE_IDS.has(fullName)) {
// eslint-disable-next-line no-console
console.log('Missing service', fullName);
hasMissing = true;
}
}

if (hasMissing) {
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}
});
2 changes: 1 addition & 1 deletion src/service-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface ServiceEndpoint {
type ServiceEndpointsList = ServiceEndpoint[];

// @see https://api.cloud.yandex.net/endpoints
const SERVICE_ENDPOINTS_LIST: ServiceEndpointsList = [
export const SERVICE_ENDPOINTS_LIST: ServiceEndpointsList = [
{
serviceIds: ['yandex.cloud.operation.OperationService'],
endpoint: 'operation.api.cloud.yandex.net:443',
Expand Down
Loading