-
Notifications
You must be signed in to change notification settings - Fork 183
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
Fix #8305: failed to generate the changelog between HLC and Modular #8332
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8925081
fix #8305
wanlwanl af6769f
update version
wanlwanl 2f37ada
changelog-temp should keeps for gen changelog.md
wanlwanl 2b44dbf
Revert "changelog-temp should keeps for gen changelog.md"
wanlwanl 4ec8a30
cleanup
wanlwanl 3109bac
cleanup
wanlwanl 44a4abf
WIP on wanl/version-extraction
wanlwanl 2816a82
part 1
wanlwanl 6bfe0bf
part 1
wanlwanl cf87f03
fix no feature bug: specify wrong api.md path for MLC
wanlwanl cba9564
cleanup
wanlwanl 9e5c96b
support mix api versions in operations
wanlwanl 32aa425
get api.md in a better way
albertxavier100 8d8a445
rename sdktype
albertxavier100 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ApiVersionType } from "./types"; | ||
|
||
export interface IApiVersionTypeExtractor { | ||
(packageRoot: string): ApiVersionType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export enum SDKType { | ||
HighLevelClient = 'HighLevelClient', | ||
RestLevelClient = 'RestLevelClient', | ||
ModularClient = 'ModularClient', | ||
}; | ||
|
||
export enum ApiVersionType { | ||
None = 'None', | ||
Stable = 'Stable', | ||
Preview = 'Preview', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import shell from 'shelljs'; | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
|
||
import { SDKType } from './types' | ||
import { logger } from "../utils/logger"; | ||
import { Project, ScriptTarget, SourceFile } from 'ts-morph'; | ||
|
||
export function getClassicClientParametersPath(packageRoot: string): string { | ||
return path.join(packageRoot, 'src', 'models', 'parameters.ts'); | ||
} | ||
|
||
export function getSDKType(packageRoot: string): SDKType { | ||
const paraPath = getClassicClientParametersPath(packageRoot); | ||
const exist = shell.test('-e', paraPath); | ||
const type = exist ? SDKType.HighLevelClient : SDKType.ModularClient; | ||
logger.logInfo(`SDK type: ${type} detected in ${packageRoot}`); | ||
return type; | ||
} | ||
|
||
export function getNpmPackageName(packageRoot: string): string { | ||
const packageJsonPath = path.join(packageRoot, 'package.json'); | ||
const packageJson = fs.readFileSync(packageJsonPath, { encoding: 'utf-8' }); | ||
const packageName = JSON.parse(packageJson).name; | ||
return packageName; | ||
} | ||
|
||
export function getApiReviewPath(packageRoot: string): string { | ||
const sdkType = getSDKType(packageRoot); | ||
const reviewDir = path.join(packageRoot, 'review'); | ||
switch (sdkType) { | ||
case SDKType.ModularClient: | ||
const npmPackageName = getNpmPackageName(packageRoot); | ||
const packageName = npmPackageName.substring("@azure/".length); | ||
const apiViewFileName = `${packageName}.api.md`; | ||
return path.join(packageRoot, 'review', apiViewFileName); | ||
case SDKType.HighLevelClient: | ||
case SDKType.RestLevelClient: | ||
default: | ||
// only one xxx.api.md | ||
return path.join(packageRoot, 'review', fs.readdirSync(reviewDir)[0]); | ||
} | ||
} | ||
|
||
export function getTsSourceFile(filePath: string): SourceFile | undefined { | ||
const target = ScriptTarget.ES2015; | ||
const compilerOptions = { target }; | ||
const project = new Project({ compilerOptions }); | ||
project.addSourceFileAtPath(filePath); | ||
return project.getSourceFile(filePath); | ||
} |
24 changes: 24 additions & 0 deletions
24
tools/js-sdk-release-tools/src/hlc/apiVersion/apiVersionTypeExtractor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ApiVersionType } from "../../common/types" | ||
import { IApiVersionTypeExtractor } from "../../common/interfaces"; | ||
import { getClassicClientParametersPath, getTsSourceFile } from "../../common/utils"; | ||
|
||
// TODO: add unit test | ||
export const getApiVersionType: IApiVersionTypeExtractor = (packageRoot: string): ApiVersionType => { | ||
const paraPath = getClassicClientParametersPath(packageRoot); | ||
const source = getTsSourceFile(paraPath); | ||
const variableDeclarations = source?.getVariableDeclarations(); | ||
if (!variableDeclarations) return ApiVersionType.Stable; | ||
for (const variableDeclaration of variableDeclarations) { | ||
const fullText = variableDeclaration.getFullText(); | ||
if (fullText.toLowerCase().includes('apiversion')) { | ||
const match = fullText.match(/defaultValue: "([0-9a-z-]+)"/); | ||
if (!match || match.length !== 2) { | ||
continue; | ||
} | ||
if (match[1].includes('preview')) { | ||
return ApiVersionType.Preview; | ||
} | ||
} | ||
} | ||
return ApiVersionType.Stable; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
tools/js-sdk-release-tools/src/hlc/utils/isGeneratedCodeStable.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
tools/js-sdk-release-tools/src/mlc/apiVersion/apiVersionTypeExtractor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { SourceFile, SyntaxKind } from "ts-morph"; | ||
import shell from 'shelljs'; | ||
import path from 'path'; | ||
|
||
import { ApiVersionType } from "../../common/types" | ||
import { IApiVersionTypeExtractor } from "../../common/interfaces"; | ||
import { getTsSourceFile } from "../../common/utils"; | ||
|
||
const findRestClientPath = (packageRoot: string): string => { | ||
const restPath = path.join(packageRoot, 'src/rest/'); | ||
const fileNames = shell.ls(restPath); | ||
const clientFiles = fileNames.filter(f => f.endsWith("Client.ts")); | ||
if (clientFiles.length !== 1) throw new Error(`Single client is supported, but found ${clientFiles}`); | ||
|
||
const clientPath = path.join(restPath, clientFiles[0]); | ||
return clientPath; | ||
}; | ||
|
||
const matchPattern = (text: string, pattern: RegExp): string | undefined => { | ||
const match = text.match(pattern); | ||
const found = match != null && match.length === 2; | ||
return found ? match?.at(1) : undefined; | ||
} | ||
|
||
const findApiVersionInRestClient = (clientPath: string): string | undefined => { | ||
const sourceFile = getTsSourceFile(clientPath); | ||
const createClientFunction = sourceFile?.getFunction("createClient"); | ||
if (!createClientFunction) throw new Error("Function 'createClient' not found."); | ||
|
||
const apiVersionStatements = createClientFunction.getStatements() | ||
.filter(s => | ||
s.getKind() === SyntaxKind.ExpressionStatement && | ||
s.getText().indexOf("options.apiVersion") > -1); | ||
if (apiVersionStatements.length === 0) return undefined; | ||
|
||
const text = apiVersionStatements[apiVersionStatements.length - 1].getText(); | ||
const pattern = /(\d{4}-\d{2}-\d{2}(?:-preview)?)/; | ||
const apiVersion = matchPattern(text, pattern); | ||
return apiVersion; | ||
}; | ||
|
||
const getApiVersionTypeFromRestClient: IApiVersionTypeExtractor = (packageRoot: string): ApiVersionType => { | ||
const clientPath = findRestClientPath(packageRoot); | ||
const apiVersion = findApiVersionInRestClient(clientPath); | ||
if (apiVersion && apiVersion.indexOf("-preview") >= 0) return ApiVersionType.Preview; | ||
if (apiVersion && apiVersion.indexOf("-preview") < 0) return ApiVersionType.Stable; | ||
return ApiVersionType.None; | ||
}; | ||
|
||
const findApiVersionsInOperations = (sourceFile: SourceFile | undefined): Array<string> | undefined => { | ||
const interfaces = sourceFile?.getInterfaces(); | ||
const interfacesWithApiVersion = interfaces?.filter(itf => itf.getProperty('"api-version"')); | ||
const apiVersions = interfacesWithApiVersion?.map(itf => { | ||
const property = itf.getMembers() | ||
.filter(m => { | ||
const defaultValue = m.getChildrenOfKind(SyntaxKind.StringLiteral)[0]; | ||
return defaultValue && defaultValue.getText() === '"api-version"'; | ||
})[0]; | ||
const apiVersion = property.getChildrenOfKind(SyntaxKind.LiteralType)[0].getText(); | ||
return apiVersion; | ||
}); | ||
return apiVersions; | ||
} | ||
|
||
const getApiVersionTypeFromOperations: IApiVersionTypeExtractor = (packageRoot: string): ApiVersionType => { | ||
const paraPath = path.join(packageRoot, 'src/rest/parameters.ts'); | ||
const sourceFile = getTsSourceFile(paraPath); | ||
const apiVersions = findApiVersionsInOperations(sourceFile); | ||
if (!apiVersions) return ApiVersionType.None; | ||
const previewVersions = apiVersions.filter(v => v.indexOf("-preview") >= 0); | ||
return previewVersions.length > 0 ? ApiVersionType.Preview : ApiVersionType.Stable; | ||
}; | ||
|
||
// TODO: add unit test | ||
export const getApiVersionType: IApiVersionTypeExtractor = (packageRoot: string): ApiVersionType => { | ||
const typeFromClient = getApiVersionTypeFromRestClient(packageRoot); | ||
if (typeFromClient !== ApiVersionType.None) return typeFromClient; | ||
const typeFromOperations = getApiVersionTypeFromOperations(packageRoot); | ||
if (typeFromOperations !== ApiVersionType.None) return typeFromOperations; | ||
return ApiVersionType.Stable; | ||
} |
19 changes: 19 additions & 0 deletions
19
tools/js-sdk-release-tools/src/xlc/apiVersion/apiVersionTypeExtractor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { getSDKType } from "../../common/utils"; | ||
import { ApiVersionType, SDKType } from "../../common/types"; | ||
import { IApiVersionTypeExtractor } from "../../common/interfaces"; | ||
import * as mlcApi from '../../mlc/apiVersion/apiVersionTypeExtractor' | ||
import * as hlcApi from '../../hlc/apiVersion/apiVersionTypeExtractor' | ||
|
||
// TODO: move to x-level-client folder | ||
export const getApiVersionType: IApiVersionTypeExtractor = (packageRoot: string): ApiVersionType => { | ||
const sdkType = getSDKType(packageRoot); | ||
switch (sdkType) { | ||
case SDKType.ModularClient: | ||
return mlcApi.getApiVersionType(packageRoot); | ||
case SDKType.HighLevelClient: | ||
return hlcApi.getApiVersionType(packageRoot); | ||
default: | ||
console.warn(`Unsupported SDK type ${sdkType} to get detact api version`); | ||
return ApiVersionType.None; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe not a high priority for now but should we error out if it's comparing between RLC and HLC? or Modular api layer and HLC ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like all sdk type should error out in a general way. plan to start a new pr to do it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
leave a TODO in code