Skip to content

Commit

Permalink
Don't log output from highly verbose components that are not useful t…
Browse files Browse the repository at this point in the history
…o the user, unless debugging
  • Loading branch information
rhyskoedijk committed Dec 3, 2024
1 parent a0d7902 commit fbcba56
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions extension/tasks/dependabotV2/utils/dependabot-cli/DependabotCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand All @@ -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 = this.logOutput;
this.debug = debug;
this.ensureJobsPathExists();
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -195,6 +200,31 @@ export class DependabotCli {
}
}

// Log output from Dependabot based on the sub-component it originates from
private logOutput(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':
debug(line);
break;

// Log output from all other components
default:
console.log(line);
break;
}
});
callback();
}

// Clean up the jobs directory and its contents
public cleanup(): void {
if (fs.existsSync(this.jobsPath)) {
Expand Down

0 comments on commit fbcba56

Please sign in to comment.