Skip to content

Commit

Permalink
Added new command line interfaces (#9537)
Browse files Browse the repository at this point in the history
* Support new CLI

* Cleanup some dependencies and updated params

* Remove octokit dependency and sdk breaking change labeling

* Changelog update

* Added sdkVersion parameter to contract

* Added apiVersion and removed unused fields

* Removed githubtoken parameter
  • Loading branch information
raych1 authored Dec 18, 2024
1 parent 0de65bc commit 31ae982
Show file tree
Hide file tree
Showing 25 changed files with 485 additions and 1,355 deletions.
6 changes: 4 additions & 2 deletions tools/spec-gen-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 2024-11-14 - 0.1.0
# Release

- Initial Release
## 2024-12-17 - 0.1.0

- Initial Release
8 changes: 8 additions & 0 deletions tools/spec-gen-sdk/cmd/spec-gen-sdk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env node
import("../dist/cli/cli.js")
.then(() => {
console.log('CLI module loaded successfully.');
})
.catch(err => {
console.error('Error loading CLI module:', err);
});
878 changes: 191 additions & 687 deletions tools/spec-gen-sdk/package-lock.json

Large diffs are not rendered by default.

46 changes: 18 additions & 28 deletions tools/spec-gen-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,42 @@
"keywords": [
"spec-gen-sdk"
],
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"lib/**/*.js",
"lib/**/*.json",
"lib/**/*.js.map",
"lib/**/*.d.ts",
"lib/**/*.d.ts.map",
"lib/**/*.handlebars",
"dist/**/*.js",
"dist/**/*.json",
"dist/**/*.js.map",
"dist/**/*.d.ts",
"dist/**/*.d.ts.map",
"dist/**/*.handlebars",
"images/**/*",
"src/**/*.ts",
"README.md"
"README.md",
"cmd/spec-gen-sdk.js"
],
"license": "MIT",
"engines": {
"node": ">=20.10.0"
},
"dependencies": {
"@octokit/auth-app": "^2.4.5",
"@octokit/rest": "18.0.3",
"ajv": "^6.12.6",
"commonmark": "0.31.2",
"convict": "^6.2.4",
"difflib": "^0.2.4",
"dotenv": "^8.2.0",
"filehound": "^1.17.5",
"fs-extra": "^9.0.0",
"handlebars": "~4.7.7",
"js-yaml": "^3.14.0",
"jsonc-parser": "^2.3.1",
"lodash": "^4.17.20",
"pkginfo": "^0.4.1",
"prettier": "2.1.2",
"rimraf": "^5.0.10",
"simple-git": "^3.16.0",
"source-map": "0.7.3",
"tslib": "1.14.1",
"winston": "^3.3.3",
"winston-transport": "^4.4.0",
"yargs": "~16.2.0"
},
"overrides": {
"@octokit/rest": "18.0.3",
"rimraf": "^5.0.10"
"yargs": "^17.7.2"
},
"devDependencies": {
"@eslint/js": "^9.14.0",
"@types/convict": "^5.2.0",
"@types/difflib": "^0.2.0",
"@types/fs-extra": "^9.0.1",
"@types/jest": "^29.5.11",
Expand All @@ -71,6 +59,7 @@
"@types/rimraf": "^3.0.0",
"@typescript-eslint/eslint-plugin": "^8.12.2",
"@typescript-eslint/parser": "^8.12.2",
"rimraf": "^5.0.10",
"eslint": "^9.14.0",
"fs-extra": "^9.0.0",
"globals": "^15.12.0",
Expand All @@ -92,19 +81,20 @@
},
"scripts": {
"build": "npm run clean && npm run lint && tsc && npm run copy",
"clean": "rimraf -rf lib",
"clean": "rimraf -rf dist",
"purge": "rimraf -rf node_modules package-lock.json",
"copy": "cd src && find . \\( -name '*.handlebars' -o -name '*.json' \\) -exec cp --parents {} ../lib \\; && cd ..",
"copy": "cd src && find . \\( -name '*.handlebars' -o -name '*.json' \\) -exec cp --parents {} ../dist \\; && cd ..",
"watch": "tsc --watch",
"test": "jest -c jest.config.js --verbose",
"start": "node lib/cli/cli.js",
"start": "node dist/cli/cli.js",
"build-test": "npm run build && npm test",
"lint": "eslint src/**/*.ts",
"lint-fix": "eslint src/**/*.ts --fix",
"test-reportFormat": "jest -c jest.config.js --verbose ./test/reportFormat.test.ts",
"test-utils": "jest -c jest.config.js --verbose ./test/utils.test.ts"
"test-utils": "jest -c jest.config.js --verbose ./test/utils.test.ts",
"spec-gen-sdk": "node cmd/spec-gen-sdk.js"
},
"bin": {
"spec-gen-sdk": "bin/spec-gen-sdk"
"spec-gen-sdk": "cmd/spec-gen-sdk.js"
}
}
72 changes: 10 additions & 62 deletions tools/spec-gen-sdk/src/automation/entrypoint.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { RestEndpointMethodTypes } from '@octokit/rest';
import { Octokit } from '@octokit/rest';
import * as fs from 'fs';
import * as winston from 'winston';
import { getAuthenticatedOctokit, getRepoKey, RepoKey } from '../utils/githubUtils';
import { getRepoKey, RepoKey } from '../utils/repo';
import {
FailureType,
setFailureType,
Expand Down Expand Up @@ -31,36 +29,25 @@ interface SdkAutoOptions {
tspConfigPath?: string;
readmePath?: string;
pullNumber?: number;
specCommitSha?: string;
specRepoHttpsUrl?: string;
apiVersion?: string;
specCommitSha: string;
specRepoHttpsUrl: string;
workingFolder: string;

github: {
token?: string;
commentAuthorName?: string;
};

isTriggeredByPipeline: boolean;
headRepoHttpsUrl?: string;
headBranch?: string;
runEnv: 'local' | 'azureDevOps' | 'test';
}

type SpecPrInfo = {
head: {owner: string; repo: string};
base: {owner: string; repo: string};
}

export type SdkAutoContext = {
config: SdkAutoOptions;
octokit: Octokit;
getGithubAccessToken: (owner: string) => Promise<string>;
logger: winston.Logger;
specPrInfo: SpecPrInfo | undefined;
specPrBaseBranch: string | undefined;
specPrHeadBranch: string | undefined;
fullLogFileName: string;
filterLogFileName: string;
specRepoConfig: SpecConfig;
sdkRepoConfig: SdkRepoConfig;
swaggerToSdkConfig: SwaggerToSdkConfig
isPrivateSpecRepo: boolean;
};


Expand Down Expand Up @@ -96,57 +83,18 @@ export const getSdkAutoContext = async (options: SdkAutoOptions): Promise<SdkAut
const swaggerToSdkConfigContent = loadConfigContent(swaggerToSdkConfigPath, logger);
const swaggerToSdkConfig = getSwaggerToSdkConfig(swaggerToSdkConfigContent);

const [{ octokit, getGithubAccessToken, specPR }] = await Promise.all([
getGithubContext(options, logger),
]);

let specPrInfo: SpecPrInfo | undefined
if (specPR) {
specPrInfo = {
head: {
owner: specPR.head.repo.owner.login,
repo: specPR.head.repo.name
},
base: {
owner: specPR.base.repo.owner.login,
repo: specPR.base.repo.name
}
};
}
return {
config: options,
octokit,
getGithubAccessToken,
logger,
specPrInfo,
specPrBaseBranch: specPR?.base.ref,
specPrHeadBranch: specPR?.head.ref,
fullLogFileName,
filterLogFileName,
specRepoConfig,
sdkRepoConfig,
swaggerToSdkConfig
swaggerToSdkConfig,
isPrivateSpecRepo: options.specRepo.name.endsWith('-pr')
};
};

const getGithubContext = async (options: SdkAutoOptions, logger: winston.Logger) => {
const {octokit, getToken: getGithubAccessToken} = getAuthenticatedOctokit(options.github, logger);

if (!options.pullNumber) {
return { octokit, getGithubAccessToken, specPR: undefined };
}
let specPR: RestEndpointMethodTypes['pulls']['get']['response']['data'];
do {
const rsp = await octokit.pulls.get({
owner: options.specRepo.owner,
repo: options.specRepo.name,
pull_number: options.pullNumber
});
specPR = rsp.data;
} while (specPR.mergeable === null && !specPR.merged);
return { octokit, getGithubAccessToken, specPR };
};

export const sdkAutoMain = async (options: SdkAutoOptions) => {
const sdkContext = await getSdkAutoContext(options);
let workflowContext: WorkflowContext | undefined = undefined;
Expand Down
8 changes: 5 additions & 3 deletions tools/spec-gen-sdk/src/automation/reportStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import * as Handlebars from 'handlebars';
import { getSDKAutomationStateString, SDKAutomationState } from './sdkAutomationState';
import { setSdkAutoStatus } from '../utils/runScript';
import { FailureType, setFailureType, WorkflowContext } from './workflow';
import { updateBreakingChangesLabel } from './updateBreakingChangesLabels';
import { formatSuppressionLine } from '../utils/reportFormat';
import { removeAnsiEscapeCodes } from '../utils/utils';
import { CommentCaptureTransport } from './logging';
Expand All @@ -32,6 +31,7 @@ export const generateReport = (context: WorkflowContext) => {
let hasSuppressions = false
let hasAbsentSuppressions = false;
let areBreakingChangeSuppressed = false;
let shouldLabelBreakingChange = false;
if (context.pendingPackages.length > 0) {
setSdkAutoStatus(context, 'failed');
setFailureType(context, FailureType.PipelineFrameworkFailed);
Expand All @@ -49,6 +49,9 @@ export const generateReport = (context: WorkflowContext) => {
if(pkg.hasBreakingChange && hasSuppressions && !hasAbsentSuppressions) {
areBreakingChangeSuppressed = true;
}
if(pkg.hasBreakingChange && !pkg.isBetaMgmtSdk && !pkg.isDataPlane && !areBreakingChangeSuppressed) {
shouldLabelBreakingChange = true;
}
const packageReport: PackageReport = {
packageName: pkg.name,
result: pkg.status,
Expand All @@ -60,6 +63,7 @@ export const generateReport = (context: WorkflowContext) => {
language: pkg.language,
hasBreakingChange: pkg.hasBreakingChange,
breakingChangeLabel: context.swaggerToSdkConfig.packageOptions.breakingChangesLabel,
shouldLabelBreakingChange,
areBreakingChangeSuppressed,
presentBreakingChangeSuppressions: pkg.presentSuppressionLines,
absentBreakingChangeSuppressions: pkg.absentSuppressionLines,
Expand Down Expand Up @@ -135,8 +139,6 @@ export const sdkAutoReportStatus = async (context: WorkflowContext) => {
sendSuccess();
}

await updateBreakingChangesLabel(context, hasBreakingChange, isDataPlane, isBetaMgmtSdk, hasSuppressions, hasAbsentSuppressions);

const extra = { hasBreakingChange, showLiteInstallInstruction };
let subTitle = renderHandlebarTemplate(commentSubTitleView, context, extra);
let commentBody = renderHandlebarTemplate(commentDetailView, context, extra);
Expand Down
Loading

0 comments on commit 31ae982

Please sign in to comment.