Skip to content

Commit

Permalink
Fill report issue template with output logs (#16230)
Browse files Browse the repository at this point in the history
* Add output logs in issue template

* Add log output in report issue template
  • Loading branch information
paulacamargo25 authored May 17, 2021
1 parent 8b8b838 commit 31c41e0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion resources/report_issue_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ You can attach such things **after** you create your issue on GitHub.

<!-- Run the "Python: Show Output" command to see the requested output. --->
```
XXX
{3}
```

</p>
Expand Down
4 changes: 3 additions & 1 deletion src/client/common/application/commands/reportIssueCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ICommandManager, IWorkspaceService } from '../types';
import { EXTENSION_ROOT_DIR } from '../../../constants';
import { IInterpreterService, IInterpreterVersionService } from '../../../interpreter/contracts';
import { identifyEnvironment } from '../../../pythonEnvironments/common/environmentIdentifier';
import { getPythonOutputChannelContent } from '../../../logging';

/**
* Allows the user to report an issue related to the Python extension using our template.
Expand All @@ -31,6 +32,7 @@ export class ReportIssueCommandHandler implements IExtensionSingleActivationServ
private templatePath = path.join(EXTENSION_ROOT_DIR, 'resources', 'report_issue_template.md');

public async openReportIssue(): Promise<void> {
const pythonLogs = await getPythonOutputChannelContent();
const template = await fs.readFile(this.templatePath, 'utf8');
const interpreterPath = (await this.interpreterService.getActiveInterpreter())?.path || 'not-selected';
const pythonVersion = await this.interpreterVersionService.getVersion(interpreterPath, '');
Expand All @@ -40,7 +42,7 @@ export class ReportIssueCommandHandler implements IExtensionSingleActivationServ

this.commandManager.executeCommand('workbench.action.openIssueReporter', {
extensionId: 'ms-python.python',
issueBody: template.format(pythonVersion, virtualEnv, languageServer),
issueBody: template.format(pythonVersion, virtualEnv, languageServer, pythonLogs),
});
}
}
8 changes: 7 additions & 1 deletion src/client/logging/_global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import { getFormatter } from './formatters';
import { LogLevel, resolveLevelName } from './levels';
import { configureLogger, createLogger, getPreDefinedConfiguration, logToAll } from './logger';
import { createTracingDecorator, LogInfo, TraceOptions, tracing as _tracing } from './trace';
import { getPythonOutputChannelTransport } from './transports';
import { getPythonOutputChannelTransport, IPythonOutputChannelContent } from './transports';
import { Arguments } from './util';

const globalLogger = createLogger();
let _globalLoggerContent: IPythonOutputChannelContent;
initialize();

/**
Expand Down Expand Up @@ -61,9 +62,14 @@ export function setLoggingLevel(level: LogLevel | 'off') {
export function addOutputChannelLogging(channel: IOutputChannel) {
const formatter = getFormatter();
const transport = getPythonOutputChannelTransport(channel, formatter);
_globalLoggerContent = transport;
globalLogger.add(transport);
}

export function getPythonOutputChannelContent(): Promise<string> {
return _globalLoggerContent?.getContent() ?? '';
}

// Emit a log message derived from the args to all enabled transports.
function log(logLevel: LogLevel, ...args: Arguments) {
logToAll([globalLogger], logLevel, args);
Expand Down
1 change: 1 addition & 0 deletions src/client/logging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export {
logInfo,
logVerbose,
logWarning,
getPythonOutputChannelContent,
traceDecorators,
} from './_global';
16 changes: 14 additions & 2 deletions src/client/logging/transports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// delete everything in '../client' except for '../client/logging' before running smoke tests.

import * as logform from 'logform';
import { EOL } from 'os';
import * as path from 'path';
import { OutputChannel } from 'vscode';
import * as winston from 'winston';
Expand Down Expand Up @@ -70,18 +71,29 @@ export function getConsoleTransport(formatter: logform.Format): Transport {
});
}

class PythonOutputChannelTransport extends Transport {
export interface IPythonOutputChannelContent {
getContent(): Promise<string>;
}

class PythonOutputChannelTransport extends Transport implements IPythonOutputChannelContent {
private content: string[] = [];
constructor(private readonly channel: OutputChannel, options?: any) {
super(options);
}

public log?(info: { message: string; [formattedMessage]: string }, next: () => void): any {
setImmediate(() => this.emit('logged', info));
this.channel.appendLine(info[formattedMessage] || info.message);
const message = info[formattedMessage] || info.message;
this.channel.appendLine(message);
this.content.push(message);
if (next) {
next();
}
}

public getContent(): Promise<string> {
return Promise.resolve(this.content.join(EOL));
}
}

// Create a Python output channel targeting transport that can be added to a winston logger.
Expand Down
2 changes: 1 addition & 1 deletion src/test/common/application/commands/issueTemplateVenv1.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ You can attach such things **after** you create your issue on GitHub.

<!-- Run the "Python: Show Output" command to see the requested output. --->
```
XXX
Python Output
```

</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as sinon from 'sinon';
import * as fs from 'fs-extra';
import * as path from 'path';
import { anyString, anything, capture, instance, mock, verify, when } from 'ts-mockito';
import { anything, capture, instance, mock, verify, when } from 'ts-mockito';
import { expect } from 'chai';
import { LanguageServerType } from '../../../../client/activation/types';
import { CommandManager } from '../../../../client/common/application/commandManager';
Expand All @@ -20,6 +20,7 @@ import * as EnvIdentifier from '../../../../client/pythonEnvironments/common/env
import { MockWorkspaceConfiguration } from '../../../startPage/mockWorkspaceConfig';
import { EXTENSION_ROOT_DIR_FOR_TESTS } from '../../../constants';
import { InterpreterService } from '../../../../client/interpreter/interpreterService';
import * as Logging from '../../../../client/logging/_global';

suite('Report Issue Command', () => {
let reportIssueCommandHandler: ReportIssueCommandHandler;
Expand All @@ -28,13 +29,15 @@ suite('Report Issue Command', () => {
let interpreterVersionService: IInterpreterVersionService;
let interpreterService: IInterpreterService;
let identifyEnvironmentStub: sinon.SinonStub;
let getPythonOutputContentStub: sinon.SinonStub;

setup(async () => {
interpreterVersionService = mock(InterpreterVersionService);
workspaceService = mock(WorkspaceService);
cmdManager = mock(CommandManager);
interpreterService = mock(InterpreterService);

when(cmdManager.executeCommand('workbench.action.openIssueReporter', anything())).thenResolve();
when(interpreterVersionService.getVersion(anything(), anything())).thenResolve('3.9.0');
when(workspaceService.getConfiguration('python')).thenReturn(
new MockWorkspaceConfiguration({
Expand All @@ -46,19 +49,21 @@ suite('Report Issue Command', () => {
identifyEnvironmentStub.resolves(PythonEnvKind.Venv);

cmdManager = mock(CommandManager);

getPythonOutputContentStub = sinon.stub(Logging, 'getPythonOutputChannelContent');
getPythonOutputContentStub.resolves('Python Output');
reportIssueCommandHandler = new ReportIssueCommandHandler(
instance(cmdManager),
instance(workspaceService),
instance(interpreterService),
instance(interpreterVersionService),
);

when(cmdManager.executeCommand(anyString(), anything())).thenResolve();
await reportIssueCommandHandler.activate();
});

teardown(() => {
identifyEnvironmentStub.restore();
getPythonOutputContentStub.restore();
});

test('Test if issue body is filled', async () => {
Expand Down

0 comments on commit 31c41e0

Please sign in to comment.