From e6f9d2546a8d5810892fc511ceb390488ca65336 Mon Sep 17 00:00:00 2001 From: Rhys Koedijk Date: Mon, 9 Dec 2024 22:00:38 +1300 Subject: [PATCH] Don't log output from highly verbose components, unless debugging (#1504) * Don't log output from highly verbose components that are not useful to the user, unless debugging * Clean up * Tidy up logging commands * Tidy up logging commands --- .../azure-devops/AzureDevOpsWebApiClient.ts | 14 +++---- .../utils/dependabot-cli/DependabotCli.ts | 41 ++++++++++++++++++- .../DependabotOutputProcessor.ts | 4 +- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/extension/tasks/dependabotV2/utils/azure-devops/AzureDevOpsWebApiClient.ts b/extension/tasks/dependabotV2/utils/azure-devops/AzureDevOpsWebApiClient.ts index e9d250bc..c33177d5 100644 --- a/extension/tasks/dependabotV2/utils/azure-devops/AzureDevOpsWebApiClient.ts +++ b/extension/tasks/dependabotV2/utils/azure-devops/AzureDevOpsWebApiClient.ts @@ -7,7 +7,7 @@ import { PullRequestAsyncStatus, PullRequestStatus, } from 'azure-devops-node-api/interfaces/GitInterfaces'; -import { error, warning } from 'azure-pipelines-task-lib/task'; +import { debug, error, warning } from 'azure-pipelines-task-lib/task'; import { IHttpClientResponse } from 'typed-rest-client/Interfaces'; import { IAbandonPullRequest, @@ -683,10 +683,10 @@ export class AzureDevOpsWebApiClient { requestAsync: () => Promise, ): Promise { // Send the request, ready the response - if (this.debug) console.debug(`🌎 🠊 [${method}] ${url}`); + if (this.debug) debug(`🌎 🠊 [${method}] ${url}`); const response = await requestAsync(); const body = await response.readBody(); - if (this.debug) console.debug(`🌎 🠈 [${response.message.statusCode}] ${response.message.statusMessage}`); + if (this.debug) debug(`🌎 🠈 [${response.message.statusCode}] ${response.message.statusMessage}`); try { // Check that the request was successful @@ -702,14 +702,10 @@ export class AzureDevOpsWebApiClient { // In debug mode, log the error, request, and response for debugging if (this.debug) { if (payload) { - console.debug('REQUEST:', JSON.stringify(payload, null, 2)); + debug(`REQUEST: ${JSON.stringify(payload)}`); } if (body) { - try { - console.debug('RESPONSE:', JSON.stringify(JSON.parse(body), null, 2)); - } catch { - console.debug('RESPONSE:', body); // If the response is not JSON, just log the raw body - } + debug(`RESPONSE: ${body}`); } } diff --git a/extension/tasks/dependabotV2/utils/dependabot-cli/DependabotCli.ts b/extension/tasks/dependabotV2/utils/dependabot-cli/DependabotCli.ts index b884a9fe..ec820604 100644 --- a/extension/tasks/dependabotV2/utils/dependabot-cli/DependabotCli.ts +++ b/extension/tasks/dependabotV2/utils/dependabot-cli/DependabotCli.ts @@ -4,6 +4,7 @@ import * as fs from 'fs'; import * as yaml from 'js-yaml'; import * as os from 'os'; import * as path from 'path'; +import { Writable } from 'stream'; import { endgroup, group, section } from '../azure-devops/formattingCommands'; import { IDependabotUpdateJobConfig } from './interfaces/IDependabotUpdateJobConfig'; import { IDependabotUpdateOperation } from './interfaces/IDependabotUpdateOperation'; @@ -18,7 +19,7 @@ export class DependabotCli { private readonly toolImage: string; private readonly outputProcessor: IDependabotUpdateOutputProcessor; private readonly debug: boolean; - + private readonly outputLogStream: Writable; private toolPath: string; public static readonly CLI_IMAGE_LATEST = 'github.com/dependabot/cli/cmd/dependabot@latest'; @@ -27,6 +28,8 @@ export class DependabotCli { this.jobsPath = path.join(os.tmpdir(), 'dependabot-jobs'); this.toolImage = cliToolImage; this.outputProcessor = outputProcessor; + this.outputLogStream = new Writable(); + this.outputLogStream._write = (chunk, encoding, callback) => logComponentOutput(debug, chunk, encoding, callback); this.debug = debug; this.ensureJobsPathExists(); } @@ -105,8 +108,10 @@ export class DependabotCli { section(`Processing job from '${jobInputPath}'`); const dependabotTool = tool(dependabotPath).arg(dependabotArguments); const dependabotResultCode = await dependabotTool.execAsync({ - failOnStdErr: false, + outStream: this.outputLogStream, + errStream: this.outputLogStream, ignoreReturnCode: true, + failOnStdErr: false, env: { DEPENDABOT_JOB_ID: jobId.replace(/-/g, '_'), // replace hyphens with underscores LOCAL_GITHUB_ACCESS_TOKEN: options?.gitHubAccessToken, // avoid rate-limiting when pulling images from GitHub container registries @@ -233,3 +238,35 @@ function readJobScenarioOutputFile(path: string): any[] { return scenario['output'] || []; } + +// Log output from Dependabot based on the sub-component it originates from +function logComponentOutput( + verbose: boolean, + chunk: any, + encoding: BufferEncoding, + callback: (error?: Error | null) => void, +): void { + chunk + .toString() + .split('\n') + .map((line: string) => line.trim()) + .filter((line: string) => line) + .forEach((line: string) => { + const component = line.split('|')?.[0]?.trim(); + switch (component) { + // Don't log highly verbose components that are not useful to the user, unless debugging + case 'collector': + case 'proxy': + if (verbose) { + debug(line); + } + break; + + // Log output from all other components + default: + console.info(line); + break; + } + }); + callback(); +} diff --git a/extension/tasks/dependabotV2/utils/dependabot-cli/DependabotOutputProcessor.ts b/extension/tasks/dependabotV2/utils/dependabot-cli/DependabotOutputProcessor.ts index 774a9895..8fa9f7ee 100644 --- a/extension/tasks/dependabotV2/utils/dependabot-cli/DependabotOutputProcessor.ts +++ b/extension/tasks/dependabotV2/utils/dependabot-cli/DependabotOutputProcessor.ts @@ -1,5 +1,5 @@ import { GitPullRequestMergeStrategy, VersionControlChangeType } from 'azure-devops-node-api/interfaces/GitInterfaces'; -import { error, warning } from 'azure-pipelines-task-lib/task'; +import { debug, error, warning } from 'azure-pipelines-task-lib/task'; import * as path from 'path'; import { AzureDevOpsWebApiClient } from '../azure-devops/AzureDevOpsWebApiClient'; import { section } from '../azure-devops/formattingCommands'; @@ -65,7 +65,7 @@ export class DependabotOutputProcessor implements IDependabotUpdateOutputProcess section(`Processing '${type}'`); if (this.debug) { - console.debug(JSON.stringify(data, null, 2)); + debug(JSON.stringify(data)); } switch (type) { // Documentation on the 'data' model for each output type can be found here: