Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use local cloned repo for generation
  • Loading branch information
raych1 committed Dec 7, 2024
commit 4aae48d937b10f46469c02ecc18ae0d279be680a
80 changes: 75 additions & 5 deletions tools/spec-gen-sdk/src/automation/entrypoint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { RestEndpointMethodTypes } from '@octokit/rest';
import { Octokit } from '@octokit/rest';
import * as fs from 'fs';
import * as winston from 'winston';
import { getAuthenticatedOctokit, RepoKey } from '../utils/githubUtils';
import { getAuthenticatedOctokit, getRepoKey, RepoKey } from '../utils/githubUtils';
import {
FailureType,
setFailureType,
Expand All @@ -18,6 +19,8 @@ import {
} from './logging';
import path from 'path';
import { generateReport } from './reportStatus';
import { SpecConfig, SdkRepoConfig, getSpecConfig, specConfigPath } from '../types/SpecConfig';
import { getSwaggerToSdkConfig, SwaggerToSdkConfig } from '../types/SwaggerToSdkConfig';

interface SdkAutoOptions {
specRepo: RepoKey;
Expand All @@ -34,8 +37,6 @@ interface SdkAutoOptions {

github: {
token?: string;
id?: number;
privateKey?: string;
commentAuthorName?: string;
};

Expand All @@ -57,6 +58,9 @@ export type SdkAutoContext = {
specPrHeadBranch: string | undefined;
fullLogFileName: string;
filterLogFileName: string;
specRepoConfig: SpecConfig;
sdkRepoConfig: SdkRepoConfig;
swaggerToSdkConfig: SwaggerToSdkConfig
};


Expand All @@ -75,8 +79,16 @@ export const getSdkAutoContext = async (options: SdkAutoOptions): Promise<SdkAut

const fullLogFileName = path.join(options.workingFolder, 'full.log');
const filterLogFileName = path.join(options.workingFolder, 'filter.log');
logger.info(`Log to ${fullLogFileName}`);
logger.add(loggerFileTransport(fullLogFileName));
logger.info(`Log to ${fullLogFileName}`);
const localSpecConfigPath = path.join(options.localSpecRepoPath, specConfigPath);
const specConfigContent = loadConfigContent(localSpecConfigPath, logger);
const specRepoConfig = getSpecConfig(specConfigContent, options.specRepo);

const sdkRepoConfig = await getSdkRepoConfig(options, specRepoConfig);
const swaggerToSdkConfigPath = path.join(options.localSdkRepoPath, sdkRepoConfig.configFilePath);
const swaggerToSdkConfigContent = loadConfigContent(swaggerToSdkConfigPath, logger);
const swaggerToSdkConfig = getSwaggerToSdkConfig(swaggerToSdkConfigContent);

const [{ octokit, getGithubAccessToken, specPR }] = await Promise.all([
getGithubContext(options, logger),
Expand Down Expand Up @@ -104,7 +116,10 @@ export const getSdkAutoContext = async (options: SdkAutoOptions): Promise<SdkAut
specPrBaseBranch: specPR?.base.ref,
specPrHeadBranch: specPR?.head.ref,
fullLogFileName,
filterLogFileName
filterLogFileName,
specRepoConfig,
sdkRepoConfig,
swaggerToSdkConfig
};
};

Expand Down Expand Up @@ -167,3 +182,58 @@ export const getLanguageByRepoName = (repoName: string) => {
return repoName;
}
};

export const loadConfigContent = (fileName: string, logger: winston.Logger) => {
logger.info(`Load config file: ${specConfigPath}`);
try {
const fileContent = fs.readFileSync(fileName).toString();
const result = JSON.parse(fileContent);
return result;
}
catch (error) {
logger.error(`IOError: Fails to read config [${fileName}]'. Please ensure the spec config exists with the correct path and the content is valid. Error: ${error.message}`);
throw error;
}
};

export const getSdkRepoConfig = async (options: SdkAutoOptions, specRepoConfig: SpecConfig) => {
const specRepo = options.specRepo;
const sdkName = options.sdkName;
const getConfigRepoKey = (repo: RepoKey | string | undefined, fallback: RepoKey): RepoKey => {
if (repo === undefined) {
return fallback;
}
const repoKey = getRepoKey(repo);
if (!repoKey.owner) {
repoKey.owner = fallback.owner;
}
return repoKey;
};
let sdkRepoConfig = specRepoConfig.sdkRepositoryMappings[sdkName];
if (sdkRepoConfig === undefined) {
throw new Error(`ConfigError: SDK ${sdkName} is not defined in SpecConfig. Please add the related config at the 'specificationRepositoryConfiguration.json' file under the root folder of the azure-rest-api-specs(-pr) repository.`);
}

if (typeof sdkRepoConfig === 'string') {
sdkRepoConfig = {
mainRepository: getRepoKey(sdkRepoConfig)
} as SdkRepoConfig;
}

sdkRepoConfig.mainRepository = getConfigRepoKey(sdkRepoConfig.mainRepository, {
owner: specRepo.owner,
name: sdkName
});
sdkRepoConfig.mainBranch =
sdkRepoConfig.mainBranch ?? "main";
sdkRepoConfig.integrationRepository = getConfigRepoKey(
sdkRepoConfig.integrationRepository,
sdkRepoConfig.mainRepository
);
sdkRepoConfig.integrationBranchPrefix = sdkRepoConfig.integrationBranchPrefix ?? 'sdkAutomation';
sdkRepoConfig.secondaryRepository = getConfigRepoKey(sdkRepoConfig.secondaryRepository, sdkRepoConfig.mainRepository);
sdkRepoConfig.secondaryBranch = sdkRepoConfig.secondaryBranch ?? sdkRepoConfig.mainBranch;
sdkRepoConfig.configFilePath = sdkRepoConfig.configFilePath ?? 'swagger_to_sdk_config.json';

return sdkRepoConfig;
};
7 changes: 4 additions & 3 deletions tools/spec-gen-sdk/src/automation/reportStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ const commentLimit = 60;

export const generateReport = (context: WorkflowContext) => {
context.logger.log('section', 'Generate report');
/*
const captureTransport = new CommentCaptureTransport({
extraLevelFilter: ['error', 'warn'],
level: 'debug',
output: context.messages
});
context.logger.add(captureTransport);
});*/
//context.logger.add(captureTransport);

let executionReport: ExecutionReport;
const packageReports: PackageReport[] = [];
Expand Down Expand Up @@ -87,7 +88,7 @@ export const generateReport = (context: WorkflowContext) => {
}

context.logger.log('endsection', 'Generate report');
context.logger.remove(captureTransport);
//context.logger.remove(captureTransport);
}

export const sdkAutoReportStatus = async (context: WorkflowContext) => {
Expand Down
Loading
Loading