|
| 1 | +import { config } from 'process'; |
| 2 | +import * as vscode from 'vscode'; |
| 3 | + |
| 4 | +export function activate(context: vscode.ExtensionContext) { |
| 5 | + const diagnosticCollection = vscode.languages.createDiagnosticCollection('codacy-cli'); |
| 6 | + console.log('Congratulations, your extension "codacy-cli-integration" is now active!'); |
| 7 | + |
| 8 | + let disposable = vscode.commands.registerCommand('codacy-cli-integration.runCodacyCli', () => { |
| 9 | + vscode.window.showInformationMessage('Codacy CLI has been launched!'); |
| 10 | + |
| 11 | + |
| 12 | + if (vscode.workspace.workspaceFolders !== undefined && vscode.workspace.workspaceFolders.length > 0) { |
| 13 | + let configuration = vscode.workspace.getConfiguration("codacy-cli"); |
| 14 | + const env: any = { env: { ...process.env } } |
| 15 | + let additionalParameters = [] |
| 16 | + let diagnosticMap: Map<string, vscode.Diagnostic[]> = new Map(); |
| 17 | + |
| 18 | + //if user has api-token, it also needs to have 'provider', 'username' and 'project' |
| 19 | + if (configuration.has('api-token')) { |
| 20 | + if (configuration.has('provider') && configuration.has('username') && configuration.has('project')) { |
| 21 | + env.env['CODACY_API_TOKEN'] = configuration.get('api-token') |
| 22 | + additionalParameters.push(`--provider ${configuration.get('provider')}`); |
| 23 | + additionalParameters.push(`--username ${configuration.get('username')}`); |
| 24 | + additionalParameters.push(`--project ${configuration.get('project')}`); |
| 25 | + } else { |
| 26 | + vscode.window.showErrorMessage('You have an api-token but no provider, username or project so CLI will not consider the token'); |
| 27 | + } |
| 28 | + } else if (configuration.has('project-token')) { |
| 29 | + env.env['CODACY_PROJECT_TOKEN'] = configuration.get('project-token') |
| 30 | + } |
| 31 | + |
| 32 | + if(configuration.has('codacy-api-base-url')){ |
| 33 | + env.env['env.CODACY_API_BASE_URL'] = configuration.get('codacy-api-base-url') |
| 34 | + } |
| 35 | + |
| 36 | + for (let i = 0; i < vscode.workspace.workspaceFolders.length; i++) { |
| 37 | + let workspaceFolder = vscode.workspace.workspaceFolders[i].uri.path; |
| 38 | + env.env['CODACY_CODE'] = workspaceFolder |
| 39 | + const dockerCommand = `docker run --rm=true --env CODACY_CODE="$CODACY_CODE" --volume /var/run/docker.sock:/var/run/docker.sock --volume "$CODACY_CODE":"$CODACY_CODE" --volume /tmp:/tmp codacy/codacy-analysis-cli analyze --tool eslint --format sarif ${additionalParameters.join(' ')}` |
| 40 | + const cp = require('child_process') |
| 41 | + cp.exec( |
| 42 | + dockerCommand, |
| 43 | + env, |
| 44 | + (err: any, stdout: string, stderr: string) => { |
| 45 | + if (err && err.code != 102) { |
| 46 | + vscode.window.showErrorMessage(`Codacy CLI failed due to ${err.message}`); |
| 47 | + console.log('error: ' + err); |
| 48 | + return |
| 49 | + } |
| 50 | + let sarifReport = JSON.parse(stdout); |
| 51 | + |
| 52 | + sarifReport.runs.forEach((run: any) => { |
| 53 | + run.results.forEach((issue: any) => { |
| 54 | + let canonicalFile = workspaceFolder + issue.locations[0].physicalLocation.artifactLocation.uri.replace(run.invocations[0].workingDirectory.uri, ''); |
| 55 | + let diagnostics = diagnosticMap.get(canonicalFile); |
| 56 | + if (!diagnostics) { diagnostics = []; } |
| 57 | + let line = issue.locations[0].physicalLocation.region.startLine - 1; |
| 58 | + let column = issue.locations[0].physicalLocation.region.startColumn; |
| 59 | + const range = new vscode.Range(line, column, line, column + 1); |
| 60 | + const diagnostic = new vscode.Diagnostic(range, issue.message.text, vscode.DiagnosticSeverity.Information); |
| 61 | + diagnostic.code = issue.ruleId; |
| 62 | + diagnostics.push(diagnostic); |
| 63 | + diagnosticMap.set(canonicalFile, diagnostics); |
| 64 | + }); |
| 65 | + |
| 66 | + }); |
| 67 | + diagnosticMap.forEach((diags, file) => { |
| 68 | + diagnosticCollection.set(vscode.Uri.parse(file), diags); |
| 69 | + }); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + }else { |
| 75 | + vscode.window.showErrorMessage('There are no workspaces to analyze!'); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + }); |
| 83 | + |
| 84 | + context.subscriptions.push(disposable); |
| 85 | + context.subscriptions.push(diagnosticCollection); |
| 86 | +} |
| 87 | + |
| 88 | +// this method is called when your extension is deactivated |
| 89 | +export function deactivate() { } |
0 commit comments