@codacy/codacy-mcp 0.4.8 → 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.
- package/dist/src/handlers/cliAnalyze.js +85 -12
- package/dist/src/tools/cliAnalyzeTool.js +22 -3
- package/dist/src/tools/getFileWithAnalysisTool.js +15 -1
- package/dist/src/tools/getPatternTool.js +10 -1
- package/dist/src/tools/getRepositoryPullRequestFilesCoverageTool.js +11 -1
- package/dist/src/tools/listFileIssuesTool.js +1 -0
- package/package.json +1 -1
|
@@ -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
|
};
|
|
@@ -1,7 +1,21 @@
|
|
|
1
|
+
import { generalOrganizationMistakes, generalRepositoryMistakes } from '../rules.js';
|
|
1
2
|
import { fileSchema, toolNames } from '../schemas.js';
|
|
3
|
+
import { listFilesTool } from './listFilesTool.js';
|
|
4
|
+
const rules = `
|
|
5
|
+
If a file is ignored, it is not analyzed by Codacy.
|
|
6
|
+
Using this tool you'll get results for five metrics: Grade, Issues, Duplication, Complexity and Coverage.
|
|
7
|
+
|
|
8
|
+
Common use cases:
|
|
9
|
+
- When the user asks for the analysis results of a specific file.
|
|
10
|
+
|
|
11
|
+
Common mistakes:
|
|
12
|
+
- Using this tool with the wrong fileId. You can get the fileId by using the ${listFilesTool.name} tool with this file name as the search parameter.
|
|
13
|
+
${generalOrganizationMistakes}
|
|
14
|
+
${generalRepositoryMistakes}
|
|
15
|
+
`;
|
|
2
16
|
export const getFileWithAnalysisTool = {
|
|
3
17
|
name: toolNames.CODACY_GET_FILE_WITH_ANALYSIS,
|
|
4
|
-
description:
|
|
18
|
+
description: `Use this tool to get information for a file and it's analysis information and coverage metrics. \n ${rules}`,
|
|
5
19
|
inputSchema: {
|
|
6
20
|
type: 'object',
|
|
7
21
|
properties: {
|
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
import { toolNames } from '../schemas.js';
|
|
2
|
+
const rules = `
|
|
3
|
+
Common use cases:
|
|
4
|
+
- When the user asks for the definition of a specific pattern
|
|
5
|
+
- When the user asks for more information about an issue found by Codacy
|
|
6
|
+
|
|
7
|
+
Common mistakes:
|
|
8
|
+
- Using this tool with the wrong toolUuid
|
|
9
|
+
- Using this tool with the wrong patternId
|
|
10
|
+
`;
|
|
2
11
|
export const getPatternTool = {
|
|
3
12
|
name: toolNames.CODACY_GET_PATTERN,
|
|
4
|
-
description:
|
|
13
|
+
description: `Get the definition of a specific pattern. \n ${rules}`,
|
|
5
14
|
inputSchema: {
|
|
6
15
|
type: 'object',
|
|
7
16
|
properties: {
|
|
@@ -1,7 +1,17 @@
|
|
|
1
|
+
import { generalOrganizationMistakes, generalRepositoryMistakes } from '../rules.js';
|
|
1
2
|
import { repositorySchema, toolNames } from '../schemas.js';
|
|
3
|
+
const rules = `
|
|
4
|
+
Common use cases:
|
|
5
|
+
- When the user asks for the coverage of the files in a specific pull request
|
|
6
|
+
|
|
7
|
+
Common mistakes:
|
|
8
|
+
- Using this tool for a specific file. For specific file coverage, use the ${toolNames.CODACY_GET_FILE_WITH_ANALYSIS} tool instead.
|
|
9
|
+
${generalOrganizationMistakes}
|
|
10
|
+
${generalRepositoryMistakes}
|
|
11
|
+
`;
|
|
2
12
|
export const getRepositoryPullRequestFilesCoverageTool = {
|
|
3
13
|
name: toolNames.CODACY_GET_REPOSITORY_PULL_REQUEST_FILES_COVERAGE,
|
|
4
|
-
description:
|
|
14
|
+
description: `Use this tool to get coverage information for all files in a pull request. \n ${rules}`,
|
|
5
15
|
inputSchema: {
|
|
6
16
|
type: 'object',
|
|
7
17
|
properties: {
|
|
@@ -7,6 +7,7 @@ const rules = `
|
|
|
7
7
|
Common use cases:
|
|
8
8
|
- When the user asks for the issues for a specific file
|
|
9
9
|
- When the user is in a file context and asks for the issues
|
|
10
|
+
- When the user asks if a specific file has issues
|
|
10
11
|
|
|
11
12
|
Common mistakes:
|
|
12
13
|
- Using this tool for listing the issues for specific pull requests
|