@codacy/codacy-mcp 0.5.3 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/handlers/cliAnalyze.js +77 -70
- package/package.json +1 -1
|
@@ -1,84 +1,98 @@
|
|
|
1
|
-
import util from 'util';
|
|
2
|
-
import { exec as execChildProcess } from 'child_process';
|
|
3
1
|
import fs from 'fs';
|
|
4
2
|
import path from 'path';
|
|
5
|
-
|
|
3
|
+
import { exec } from 'child_process';
|
|
6
4
|
const CODACY_ACCOUNT_TOKEN = process.env.CODACY_ACCOUNT_TOKEN;
|
|
7
|
-
const
|
|
5
|
+
const CLI_FILE_NAME = 'cli.sh';
|
|
6
|
+
const CLI_FOLDER_NAME = '.codacy';
|
|
7
|
+
const CLI_LOCAL_COMMAND = `${CLI_FOLDER_NAME}/${CLI_FILE_NAME}`;
|
|
8
|
+
const CLI_GLOBAL_COMMAND = 'codacy-cli';
|
|
9
|
+
// Set a larger buffer size (10MB)
|
|
10
|
+
const MAX_BUFFER_SIZE = 1024 * 1024 * 10;
|
|
11
|
+
const execAsync = (command, options) => {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
exec(`CODACY_CLI_V2_VERSION=1.0.0-main.232.a6a6368 ${command}`, {
|
|
14
|
+
cwd: options.rootPath,
|
|
15
|
+
maxBuffer: MAX_BUFFER_SIZE, // To solve: stdout maxBuffer exceeded
|
|
16
|
+
}, (error, stdout, stderr) => {
|
|
17
|
+
if (error) {
|
|
18
|
+
reject(error);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (stderr && (!stdout || /error|fail|exception/i.test(stderr))) {
|
|
22
|
+
reject(new Error(stderr));
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
resolve({ stdout, stderr });
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
};
|
|
8
29
|
// Safeguard: Validate and sanitize command inputs
|
|
9
30
|
const sanitizeCommand = (command) => {
|
|
10
31
|
// Remove any shell metacharacters
|
|
11
32
|
return command?.replace(/[;&|`$]/g, '') || '';
|
|
12
33
|
};
|
|
13
|
-
// Check if codacy-cli is installed and install if needed
|
|
14
|
-
const ensureCodacyCLI = async () => {
|
|
15
|
-
try {
|
|
16
|
-
await exec('which codacy-cli');
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
catch {
|
|
20
|
-
try {
|
|
21
|
-
// Install codacy-cli using brew
|
|
22
|
-
await exec('brew install codacy/codacy-cli-v2/codacy-cli-v2');
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
catch (error) {
|
|
26
|
-
console.error('Failed to install codacy-cli:', error);
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
// Check if .codacy/codacy.yaml exists and initialize if needed
|
|
32
|
-
const ensureCodacyConfig = async (rootPath, provider, organization, repository) => {
|
|
33
|
-
const extensions = ['yaml', 'yml'];
|
|
34
|
-
const configExists = extensions.some(ext => fs.existsSync(path.join(rootPath, '.codacy', `codacy.${ext}`)));
|
|
35
|
-
if (configExists) {
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
try {
|
|
39
|
-
const command = `${CODACY_CLI_COMMAND} init --api-token ${CODACY_ACCOUNT_TOKEN} --provider ${provider} --organization ${organization} --repository ${repository}`;
|
|
40
|
-
const options = { cwd: rootPath };
|
|
41
|
-
await exec(command, options);
|
|
42
|
-
await exec(`${CODACY_CLI_COMMAND} install`, options);
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
catch (error) {
|
|
46
|
-
console.error('Failed to initialize Codacy configuration:', error);
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
// Only treat stderr as an error if:
|
|
51
|
-
// 1. There's no stdout (meaning only errors occurred)
|
|
52
|
-
// 2. stderr contains actual error messages (not just progress logs)
|
|
53
|
-
const falseSuccess = (stdout, stderr) => stderr && (!stdout || /error|fail|exception/i.test(stderr));
|
|
54
34
|
// Run analysis with potential retry after installation
|
|
55
|
-
const runAnalysis = async (args) => {
|
|
35
|
+
const runAnalysis = async (cliCommand, args) => {
|
|
56
36
|
const safeFile = sanitizeCommand(args.file);
|
|
57
37
|
const tool = args.tool ? `--tool ${args.tool}` : '';
|
|
58
|
-
const command = `${
|
|
59
|
-
// Add options object with cwd and increased maxBuffer
|
|
38
|
+
const command = `${cliCommand} analyze ${tool} --format sarif ${safeFile}`;
|
|
60
39
|
const options = {
|
|
61
|
-
|
|
62
|
-
maxBuffer: 1024 * 1024 * 10, // 10MB buffer size
|
|
40
|
+
rootPath: args.rootPath,
|
|
63
41
|
};
|
|
42
|
+
return await execAsync(command, options);
|
|
43
|
+
};
|
|
44
|
+
const ensureCodacyConfig = async (cliCommand, rootPath, provider, organization, repository) => {
|
|
45
|
+
const codacyConfigPath = path.join(rootPath, CLI_FOLDER_NAME, 'codacy.yaml');
|
|
46
|
+
if (!fs.existsSync(codacyConfigPath)) {
|
|
47
|
+
const apiToken = CODACY_ACCOUNT_TOKEN ? `--api-token ${CODACY_ACCOUNT_TOKEN}` : '';
|
|
48
|
+
const repositoryAccess = repository && provider && organization
|
|
49
|
+
? `--provider ${provider} --organization ${organization} --repository ${repository}`
|
|
50
|
+
: '';
|
|
51
|
+
// initialize codacy-cli
|
|
52
|
+
await execAsync(`${cliCommand} init ${apiToken} ${repositoryAccess}`, { rootPath });
|
|
53
|
+
// install dependencies
|
|
54
|
+
await execAsync(`${cliCommand} install`, { rootPath });
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
57
|
+
};
|
|
58
|
+
const ensureCodacyCLIExists = async (rootPath, provider, organization, repository) => {
|
|
59
|
+
let isCLIAvailable = false;
|
|
64
60
|
try {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
61
|
+
await execAsync(`${CLI_LOCAL_COMMAND} --help`, { rootPath });
|
|
62
|
+
return CLI_LOCAL_COMMAND;
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
isCLIAvailable = false;
|
|
66
|
+
}
|
|
67
|
+
if (!isCLIAvailable) {
|
|
68
|
+
try {
|
|
69
|
+
await execAsync(`${CLI_GLOBAL_COMMAND} --help`, { rootPath });
|
|
70
|
+
return CLI_GLOBAL_COMMAND;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
isCLIAvailable = false;
|
|
68
74
|
}
|
|
69
|
-
return { stdout, stderr };
|
|
70
75
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
// install locally if not available
|
|
77
|
+
const codacyFolder = path.join(rootPath, CLI_FOLDER_NAME);
|
|
78
|
+
if (!fs.existsSync(codacyFolder)) {
|
|
79
|
+
fs.mkdirSync(codacyFolder, { recursive: true });
|
|
75
80
|
}
|
|
81
|
+
// Download cli.sh if it doesn't exist
|
|
82
|
+
const codacyCliPath = path.join(codacyFolder, CLI_FILE_NAME);
|
|
83
|
+
if (!fs.existsSync(codacyCliPath)) {
|
|
84
|
+
await execAsync(`curl -Ls -o "${CLI_LOCAL_COMMAND}" https://raw.githubusercontent.com/codacy/codacy-cli-v2/main/codacy-cli.sh`, { rootPath });
|
|
85
|
+
await execAsync(`chmod +x "${CLI_LOCAL_COMMAND}"`, { rootPath });
|
|
86
|
+
}
|
|
87
|
+
// initialize codacy-cli
|
|
88
|
+
await ensureCodacyConfig(CLI_LOCAL_COMMAND, rootPath, provider, organization, repository);
|
|
89
|
+
return CLI_LOCAL_COMMAND;
|
|
76
90
|
};
|
|
77
91
|
export const cliAnalyzeHandler = async (args) => {
|
|
78
92
|
try {
|
|
79
93
|
// Ensure codacy-cli is installed
|
|
80
|
-
const
|
|
81
|
-
if (!
|
|
94
|
+
const cliCommand = await ensureCodacyCLIExists(args.rootPath, args.provider, args.organization, args.repository);
|
|
95
|
+
if (!cliCommand) {
|
|
82
96
|
return {
|
|
83
97
|
success: false,
|
|
84
98
|
errorType: 'cli-missing',
|
|
@@ -86,7 +100,7 @@ export const cliAnalyzeHandler = async (args) => {
|
|
|
86
100
|
};
|
|
87
101
|
}
|
|
88
102
|
// Check for mandatory configuration file and initialize if needed
|
|
89
|
-
const configExists = await ensureCodacyConfig(args.rootPath, args.provider, args.organization, args.repository);
|
|
103
|
+
const configExists = await ensureCodacyConfig(cliCommand, args.rootPath, args.provider, args.organization, args.repository);
|
|
90
104
|
if (!configExists) {
|
|
91
105
|
return {
|
|
92
106
|
success: false,
|
|
@@ -94,20 +108,13 @@ export const cliAnalyzeHandler = async (args) => {
|
|
|
94
108
|
output: 'Failed to find Codacy CLI configuration.',
|
|
95
109
|
};
|
|
96
110
|
}
|
|
97
|
-
const { stdout, stderr } = await runAnalysis(args);
|
|
98
|
-
if (falseSuccess(stdout, stderr)) {
|
|
99
|
-
return {
|
|
100
|
-
success: false,
|
|
101
|
-
errorType: 'stderr',
|
|
102
|
-
output: stderr,
|
|
103
|
-
};
|
|
104
|
-
}
|
|
111
|
+
const { stdout, stderr } = await runAnalysis(cliCommand, args);
|
|
105
112
|
// Try to extract JSON from the output if it's embedded in other text
|
|
106
113
|
const jsonMatch = /(\{[\s\S]*\}|\[[\s\S]*\])/.exec(stdout);
|
|
107
114
|
return {
|
|
108
115
|
success: true,
|
|
109
|
-
output: stdout,
|
|
110
116
|
result: jsonMatch ? JSON.parse(jsonMatch[0]) : null,
|
|
117
|
+
warnings: stderr,
|
|
111
118
|
};
|
|
112
119
|
}
|
|
113
120
|
catch (error) {
|