Skip to content

Commit 7efdb89

Browse files
committed
export log issues to ado
1 parent fb5ef39 commit 7efdb89

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

eng/tools/spec-gen-sdk-runner/src/commands.ts

+71-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {
88
getAllTypeSpecPaths,
99
resetGitRepo,
1010
} from "./utils.js";
11-
import { LogLevel, logMessage, vsoAddAttachment } from "./log.js";
12-
import { SpecGenSdkCmdInput } from "./types.js";
11+
import { LogLevel, logMessage, vsoAddAttachment, vsoLogIssue } from "./log.js";
12+
import { CommandLog, SpecGenSdkCmdInput } from "./types.js";
1313
import { detectChangedSpecConfigFiles } from "./change-files.js";
1414

1515
export async function generateSdkForSingleSpec(): Promise<number> {
@@ -43,6 +43,9 @@ export async function generateSdkForSingleSpec(): Promise<number> {
4343
statusCode = 1;
4444
}
4545
logMessage("ending group logging", LogLevel.EndGroup);
46+
47+
logIssuesToADO(commandInput, specConfigPath || '');
48+
4649
return statusCode;
4750
}
4851

@@ -105,6 +108,8 @@ export async function generateSdkForSpecPr(): Promise<number> {
105108
statusCode = 1;
106109
}
107110
logMessage("ending group logging", LogLevel.EndGroup);
111+
112+
logIssuesToADO(commandInput, changedSpecPath || '');
108113
}
109114
return statusCode;
110115
}
@@ -180,6 +185,8 @@ export async function generateSdkForBatchSpecs(runMode: string): Promise<number>
180185
statusCode = 1;
181186
}
182187
logMessage("ending group logging", LogLevel.EndGroup);
188+
189+
logIssuesToADO(commandInput, specConfigPath || '');
183190
}
184191
if (failedCount > 0) {
185192
markdownContent += `${failedContent}\n`;
@@ -325,3 +332,65 @@ function getSpecPaths(runMode: string, specRepoPath: string): string[] {
325332
}
326333
return specConfigPaths;
327334
}
335+
336+
/**
337+
* Extract and format the prefix from tspConfigPath or readmePath.
338+
* @param {string | undefined} tspConfigPath The tspConfigPath to extract the prefix from.
339+
* @param {string | undefined} readmePath The readmePath to extract the prefix from.
340+
* @returns {string} The formatted prefix.
341+
*/
342+
function extractPathFromSpecConfig(tspConfigPath: string | undefined, readmePath: string | undefined): string {
343+
let prefix = '';
344+
if (tspConfigPath) {
345+
const match = tspConfigPath.match(/specification\/(.+)\/tspconfig\.yaml$/);
346+
if (match) {
347+
const segments = match[1].split('/');
348+
prefix = segments.join('-').toLowerCase().replace(/\./g, '-');
349+
}
350+
} else if (readmePath) {
351+
const match = readmePath.match(/specification\/(.+?)\/readme\.md$/i);
352+
if (match) {
353+
const segments = match[1].split('/');
354+
prefix = segments.join('-').toLowerCase().replace(/\./g, '-');
355+
}
356+
}
357+
return prefix;
358+
}
359+
360+
/**
361+
* Generates file paths for different log files based on the given command input.
362+
* @param {SpecGenSdkCmdInput} commandInput
363+
* @returns An object containing the full log file path, filtered log file path, and HTML log file path.
364+
*/
365+
function getLogsPath(commandInput: SpecGenSdkCmdInput): { fullLogFileName: string, filteredLogFileName: string, htmlLogFileName: string } {
366+
const fileNamePrefix = extractPathFromSpecConfig(commandInput.tspConfigPath, commandInput.readmePath)
367+
const logFolder = path.join(commandInput.workingFolder, 'out/logs');
368+
const fullLogFileName = path.join(logFolder, `${fileNamePrefix}-full.log`);
369+
const filteredLogFileName = path.join(logFolder, `${fileNamePrefix}-filtered.log`);
370+
const htmlLogFileName = path.join(logFolder, `${fileNamePrefix}-${commandInput.sdkRepoName.substring("azure-sdk-for-".length)}-gen-result.html`);
371+
return { fullLogFileName, filteredLogFileName, htmlLogFileName };
372+
}
373+
374+
/**
375+
* Logs SDK generation issues to Azure DevOps (ADO)
376+
* @param commandInput
377+
* @param specConfigPath
378+
*/
379+
function logIssuesToADO(commandInput: SpecGenSdkCmdInput, specConfigPath: string): void {
380+
const logPath = getLogsPath(commandInput).filteredLogFileName;
381+
let logIssues:CommandLog[] = []
382+
try {
383+
const log = JSON.parse(fs.readFileSync(logPath, "utf8"));
384+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
385+
logIssues = log.logIssues;
386+
} catch (error) {
387+
logMessage(`Error reading log at ${logPath}:${error}`, LogLevel.Error);
388+
}
389+
390+
if(logIssues.length > 0) {
391+
logMessage(`Error generating SDK from ${specConfigPath}`, LogLevel.Group);
392+
const logIssuesList = logIssues.flatMap(entry => [`Get log issues from script ${entry.command} `, ...entry.logIssues]);;
393+
vsoLogIssue(logIssuesList.join('%0D%0A'));
394+
logMessage("ending group logging", LogLevel.EndGroup);
395+
}
396+
}

eng/tools/spec-gen-sdk-runner/src/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,8 @@ export interface SpecGenSdkCmdInput {
1616
headRepoHttpsUrl?: string;
1717
headBranch?: string;
1818
}
19+
20+
export type CommandLog = {
21+
command: string;
22+
logIssues: string[];
23+
};

0 commit comments

Comments
 (0)