Skip to content

Commit 8fa2ef8

Browse files
authoredMay 20, 2021
Merge pull request #515 from adityasharad/analyze/summary-logging
Analyze: Improve log grouping and log the analysis summary in its own group
2 parents 6a98157 + f84cc5e commit 8fa2ef8

13 files changed

+59
-22
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
## [UNRELEASED]
44

55
- Add this changelog file. [#507](https://github.com/github/codeql-action/pull/507)
6+
- Improve grouping of analysis logs. Add a new log group containing a summary of metrics and diagnostics, if they were produced by CodeQL builtin queries. [#515](https://github.com/github/codeql-action/pull/515)

‎lib/analyze.js

+11-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/analyze.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/analyze.test.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/analyze.test.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/codeql.js

+10-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/codeql.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/upload-lib.js

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/upload-lib.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/analyze.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ test("status report fields and search path setting", async (t) => {
7676
})
7777
);
7878
searchPathsUsed.push(searchPath!);
79+
return "";
7980
},
8081
});
8182

‎src/analyze.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,17 @@ export async function runQueries(
169169
}
170170

171171
try {
172+
let analysisSummary = "";
172173
if (queries["builtin"].length > 0) {
173174
const startTimeBuliltIn = new Date().getTime();
174-
const sarifFile = await runQueryGroup(
175+
const { sarifFile, stdout } = await runQueryGroup(
175176
language,
176177
"builtin",
177178
queries["builtin"],
178179
sarifFolder,
179180
undefined
180181
);
182+
analysisSummary = stdout;
181183
await injectLinesOfCode(sarifFile, language, locPromise);
182184

183185
statusReport[`analyze_builtin_queries_${language}_duration_ms`] =
@@ -188,7 +190,7 @@ export async function runQueries(
188190
const temporarySarifFiles: string[] = [];
189191
for (let i = 0; i < queries["custom"].length; ++i) {
190192
if (queries["custom"][i].queries.length > 0) {
191-
const sarifFile = await runQueryGroup(
193+
const { sarifFile } = await runQueryGroup(
192194
language,
193195
`custom-${i}`,
194196
queries["custom"][i].queries,
@@ -206,8 +208,13 @@ export async function runQueries(
206208
statusReport[`analyze_custom_queries_${language}_duration_ms`] =
207209
new Date().getTime() - startTimeCustom;
208210
}
211+
logger.endGroup();
209212

213+
// Print the LoC baseline and the summary results from database analyze.
214+
logger.startGroup(`Analysis summary for ${language}`);
210215
printLinesOfCodeSummary(logger, language, await locPromise);
216+
logger.info(analysisSummary);
217+
logger.endGroup();
211218
} catch (e) {
212219
logger.info(e);
213220
statusReport.analyze_failure_language = language;
@@ -226,7 +233,7 @@ export async function runQueries(
226233
queries: string[],
227234
destinationFolder: string,
228235
searchPath: string | undefined
229-
): Promise<string> {
236+
): Promise<{ sarifFile: string; stdout: string }> {
230237
const databasePath = util.getCodeQLDatabasePath(config, language);
231238
// Pass the queries to codeql using a file instead of using the command
232239
// line to avoid command line length restrictions, particularly on windows.
@@ -240,7 +247,7 @@ export async function runQueries(
240247
const sarifFile = path.join(destinationFolder, `${language}-${type}.sarif`);
241248

242249
const codeql = getCodeQL(config.codeQLCmd);
243-
await codeql.databaseAnalyze(
250+
const databaseAnalyzeStdout = await codeql.databaseAnalyze(
244251
databasePath,
245252
sarifFile,
246253
searchPath,
@@ -254,9 +261,7 @@ export async function runQueries(
254261
logger.debug(
255262
`SARIF results for database ${language} created at "${sarifFile}"`
256263
);
257-
logger.endGroup();
258-
259-
return sarifFile;
264+
return { sarifFile, stdout: databaseAnalyzeStdout };
260265
}
261266
}
262267

‎src/codeql.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export interface CodeQL {
9696
addSnippetsFlag: string,
9797
threadsFlag: string,
9898
automationDetailsId: string | undefined
99-
): Promise<void>;
99+
): Promise<string>;
100100
}
101101

102102
export interface ResolveQueriesOutput {
@@ -688,7 +688,7 @@ function getCodeQLForCmd(cmd: string): CodeQL {
688688
addSnippetsFlag: string,
689689
threadsFlag: string,
690690
automationDetailsId: string | undefined
691-
) {
691+
): Promise<string> {
692692
const args = [
693693
"database",
694694
"analyze",
@@ -712,7 +712,16 @@ function getCodeQLForCmd(cmd: string): CodeQL {
712712
args.push("--sarif-category", automationDetailsId);
713713
}
714714
args.push(querySuite);
715-
await new toolrunner.ToolRunner(cmd, args).exec();
715+
// capture stdout, which contains analysis summaries
716+
let output = "";
717+
await new toolrunner.ToolRunner(cmd, args, {
718+
listeners: {
719+
stdout: (data: Buffer) => {
720+
output += data.toString("utf8");
721+
},
722+
},
723+
}).exec();
724+
return output;
716725
},
717726
};
718727
}

‎src/upload-lib.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ async function uploadFiles(
342342
mode: util.Mode,
343343
logger: Logger
344344
): Promise<UploadStatusReport> {
345-
logger.info(`Uploading sarif files: ${JSON.stringify(sarifFiles)}`);
345+
logger.startGroup("Uploading results");
346+
logger.info(`Processing sarif files: ${JSON.stringify(sarifFiles)}`);
346347

347348
if (mode === "actions") {
348349
// This check only works on actions as env vars don't persist between calls to the runner
@@ -403,6 +404,8 @@ async function uploadFiles(
403404
// Make the upload
404405
await uploadPayload(payload, repositoryNwo, apiDetails, mode, logger);
405406

407+
logger.endGroup();
408+
406409
return {
407410
raw_upload_size_bytes: rawUploadSizeBytes,
408411
zipped_upload_size_bytes: zippedUploadSizeBytes,

0 commit comments

Comments
 (0)
Please sign in to comment.