@codacy/codacy-mcp 0.4.9 → 0.5.0
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.
|
@@ -1,25 +1,98 @@
|
|
|
1
1
|
import util from 'util';
|
|
2
2
|
import { exec as execChildProcess } from 'child_process';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import path from 'path';
|
|
3
5
|
const exec = util.promisify(execChildProcess);
|
|
6
|
+
const CODACY_ACCOUNT_TOKEN = process.env.CODACY_ACCOUNT_TOKEN;
|
|
4
7
|
// Safeguard: Validate and sanitize command inputs
|
|
5
8
|
const sanitizeCommand = (command) => {
|
|
6
9
|
// Remove any shell metacharacters
|
|
7
10
|
return command?.replace(/[;&|`$]/g, '') || '';
|
|
8
11
|
};
|
|
9
|
-
|
|
12
|
+
// Check if codacy-cli is installed and install if needed
|
|
13
|
+
const ensureCodacyCLI = async () => {
|
|
14
|
+
try {
|
|
15
|
+
await exec('which codacy-cli');
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
try {
|
|
20
|
+
// Install codacy-cli using brew
|
|
21
|
+
await exec('brew install codacy/codacy-cli-v2/codacy-cli-v2');
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
console.error('Failed to install codacy-cli:', error);
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
// Check if .codacy/codacy.yaml exists and initialize if needed
|
|
31
|
+
const ensureCodacyConfig = async (rootPath, provider, organization, repository) => {
|
|
32
|
+
const configPath = path.join(rootPath, '.codacy', 'codacy.yaml');
|
|
33
|
+
if (fs.existsSync(configPath)) {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const command = `codacy-cli init --api-token ${CODACY_ACCOUNT_TOKEN} --provider ${provider} --organization ${organization} --repository ${repository}`;
|
|
38
|
+
const options = { cwd: rootPath };
|
|
39
|
+
await exec(command, options);
|
|
40
|
+
await exec('codacy-cli install', options);
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
console.error('Failed to initialize Codacy configuration:', error);
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
// Only treat stderr as an error if:
|
|
49
|
+
// 1. There's no stdout (meaning only errors occurred)
|
|
50
|
+
// 2. stderr contains actual error messages (not just progress logs)
|
|
51
|
+
const falseSuccess = (stdout, stderr) => stderr && (!stdout || /error|fail|exception/i.test(stderr));
|
|
52
|
+
// Run analysis with potential retry after installation
|
|
53
|
+
const runAnalysis = async (args) => {
|
|
54
|
+
const safeFile = sanitizeCommand(args.file);
|
|
55
|
+
const command = `codacy-cli analyze --format sarif ${safeFile}`;
|
|
56
|
+
// Add options object with cwd and increased maxBuffer
|
|
57
|
+
const options = {
|
|
58
|
+
cwd: args.rootPath,
|
|
59
|
+
maxBuffer: 1024 * 1024 * 10, // 10MB buffer size
|
|
60
|
+
};
|
|
10
61
|
try {
|
|
11
|
-
const safeFile = sanitizeCommand(args.file);
|
|
12
|
-
const command = `codacy-cli analyze --format sarif ${safeFile}`;
|
|
13
|
-
// Add options object with cwd and increased maxBuffer
|
|
14
|
-
const options = {
|
|
15
|
-
cwd: args.rootPath,
|
|
16
|
-
maxBuffer: 1024 * 1024 * 10, // 10MB buffer size
|
|
17
|
-
};
|
|
18
62
|
const { stdout, stderr } = await exec(command, options);
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
63
|
+
if (falseSuccess(stdout, stderr)) {
|
|
64
|
+
throw new Error('Failed to run analysis. Trying to install codacy-cli...');
|
|
65
|
+
}
|
|
66
|
+
return { stdout, stderr };
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
// If first attempt fails, try installing and retry
|
|
70
|
+
await exec('codacy-cli install', options);
|
|
71
|
+
return await exec(command, options);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
export const cliAnalyzeHandler = async (args) => {
|
|
75
|
+
try {
|
|
76
|
+
// Ensure codacy-cli is installed
|
|
77
|
+
const isCLIAvailable = await ensureCodacyCLI();
|
|
78
|
+
if (!isCLIAvailable) {
|
|
79
|
+
return {
|
|
80
|
+
success: false,
|
|
81
|
+
errorType: 'cli-missing',
|
|
82
|
+
output: 'Failed to install codacy-cli. Please install it manually.',
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
// Check for mandatory configuration file and initialize if needed
|
|
86
|
+
const configExists = await ensureCodacyConfig(args.rootPath, args.provider, args.organization, args.repository);
|
|
87
|
+
if (!configExists) {
|
|
88
|
+
return {
|
|
89
|
+
success: false,
|
|
90
|
+
errorType: 'cli-config-missing',
|
|
91
|
+
output: 'Failed to find Codacy CLI configuration.',
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
const { stdout, stderr } = await runAnalysis(args);
|
|
95
|
+
if (falseSuccess(stdout, stderr)) {
|
|
23
96
|
return {
|
|
24
97
|
success: false,
|
|
25
98
|
errorType: 'stderr',
|
|
@@ -1,13 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { generalOrganizationMistakes, generalRepositoryMistakes } from '../rules.js';
|
|
2
|
+
import { repositorySchema, toolNames } from '../schemas.js';
|
|
3
|
+
const rules = `
|
|
4
|
+
Use this tool to analyze the quality of a repository using Codacy command line.
|
|
5
|
+
|
|
6
|
+
Common use cases:
|
|
7
|
+
- When the user asks for an analysis of a repository
|
|
8
|
+
- When the user wants to analyze based on Codacy configuration
|
|
9
|
+
- When the user wants to apply fixes based on Codacy configuration and analysis
|
|
10
|
+
- When the user wants Codacy results immediately without waiting for the next scheduled analysis
|
|
11
|
+
|
|
12
|
+
Common mistakes:
|
|
13
|
+
- Using this tool to know the status of a repository
|
|
14
|
+
- Using this tool without specifying the rootPath
|
|
15
|
+
- Using this tool to know current results from Codacy. Use ${toolNames.CODACY_GET_REPOSITORY_WITH_ANALYSIS} instead.
|
|
16
|
+
${generalOrganizationMistakes}
|
|
17
|
+
${generalRepositoryMistakes}
|
|
18
|
+
`;
|
|
2
19
|
export const cliAnalyzeTool = {
|
|
3
20
|
name: toolNames.CODACY_CLI_ANALYZE,
|
|
4
|
-
description:
|
|
21
|
+
description: `Run quality analysis using Codacy CLI. \n ${rules}`,
|
|
5
22
|
inputSchema: {
|
|
6
23
|
type: 'object',
|
|
7
24
|
properties: {
|
|
25
|
+
...repositorySchema,
|
|
8
26
|
rootPath: {
|
|
9
27
|
type: 'string',
|
|
10
|
-
description: '
|
|
28
|
+
description: 'Root path to the project or repository in the local filesystem. This must be filled to ensure permissions are met for the tool to run.',
|
|
11
29
|
},
|
|
12
30
|
file: {
|
|
13
31
|
type: 'string',
|
|
@@ -15,5 +33,6 @@ export const cliAnalyzeTool = {
|
|
|
15
33
|
default: '',
|
|
16
34
|
},
|
|
17
35
|
},
|
|
36
|
+
required: ['provider', 'organization', 'repository', 'rootPath'],
|
|
18
37
|
},
|
|
19
38
|
};
|