Skip to content

Commit

Permalink
Don't log output from highly verbose components, unless debugging (#1504
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
rhyskoedijk authored Dec 9, 2024
1 parent 30a8657 commit e6f9d25
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -683,10 +683,10 @@ export class AzureDevOpsWebApiClient {
requestAsync: () => Promise<IHttpClientResponse>,
): Promise<any | undefined> {
// 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
Expand All @@ -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}`);
}
}

Expand Down
41 changes: 39 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 = (chunk, encoding, callback) => logComponentOutput(debug, chunk, encoding, callback);
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 @@ -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();
}
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit e6f9d25

Please sign in to comment.