generated from codacy/codacy-public-template
-
Notifications
You must be signed in to change notification settings - Fork 20
Feature/cli cy 7427 #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3c89453
feature: adds cli support, installation, eslint analysis CY-7427 CY-7428
pedrobpereira 04c5765
feat: Improved error handling and checks
og-pixel 8a0ec5b
feat: Codacy CLI downloads and installs CY-7427
og-pixel 430ff59
feat: Fix rebase, rename file CY-7427
og-pixel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { exec } from 'child_process'; | ||
| import { getCodacyCliPath } from './installCLI.js'; | ||
|
|
||
| export async function cliAnalysisHandler(args: { | ||
| tool: string; | ||
| format: string; | ||
| output: string; | ||
| }): Promise<{ | ||
| message: string; | ||
| }> { | ||
| const codacyCliPath = await getCodacyCliPath(); | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| const command = `${codacyCliPath} analyze --tool ${args.tool} --format ${args.format} -o ${args.output}`; | ||
|
|
||
| exec(command, (err, stdout) => { | ||
|
Check failure on line 16 in src/handlers/cliAnalysis.ts
|
||
| if (err) { | ||
| console.error(`Analysis error: ${err}`); | ||
| reject({ message: `Analysis failed: ${err.message}` }); | ||
| return; | ||
| } | ||
|
|
||
| console.log(`Analysis completed: ${stdout}`); | ||
| resolve({ message: `Analysis completed successfully. Output saved to ${args.output}` }); | ||
| }); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { exec } from 'child_process'; | ||
| import https from 'https'; | ||
| import os from 'os'; | ||
| import path from 'node:path'; | ||
| import { promisify } from 'node:util'; | ||
| import * as fs from 'node:fs'; | ||
|
|
||
| const MAC_OS_PATH = path.join(os.homedir(), 'Library/Caches/Codacy/codacy-cli-v2/'); | ||
| const LINUX_PATH = path.join(os.homedir(), '.cache/Codacy/codacy-cli-v2/'); | ||
| const GITHUB_LATEST_RELEASE_URL = | ||
| 'https://api.github.com/repos/codacy/codacy-cli-v2/releases/latest'; | ||
| const GITHUB_INSTALL_SCRIPT_URL = | ||
| 'https://raw.githubusercontent.com/codacy/codacy-cli-v2/main/codacy-cli.sh'; | ||
|
|
||
| export const installCliHandler = async (): Promise<{ message: string }> => { | ||
| const isCliInstalled = await isCodacyCliInstalled(); | ||
| const isConfigPresent = isCodacyConfigPresent(); | ||
|
|
||
| if (!isCliInstalled) { | ||
| const downloadSuccessful = await downloadCliTool(); | ||
| if (!downloadSuccessful) { | ||
| return { | ||
| message: 'Failed to download Codacy CLI', | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| const cliPath = await getCodacyCliPath(); | ||
|
|
||
| if (isConfigPresent) { | ||
| return { | ||
| message: 'Codacy CLI is already installed and configured', | ||
| }; | ||
| } | ||
|
|
||
| const initSuccessful = await execPromise(`${cliPath} init`); | ||
| if (!initSuccessful) { | ||
| return { | ||
| message: 'Failed to initialize Codacy CLI', | ||
| }; | ||
| } | ||
|
|
||
| const installSuccessful = await execPromise(`${cliPath} install`); | ||
| if (!installSuccessful) { | ||
| return { | ||
| message: 'Failed to install Codacy CLI', | ||
| }; | ||
| } | ||
| return { | ||
| message: 'Codacy CLI installed successfully', | ||
| }; | ||
| }; | ||
|
|
||
| export const getCodacyCliPath: () => Promise<string> = async () => { | ||
| const latestReleaseTag = await getLatestReleaseTag(); | ||
|
|
||
| if (os.platform() === 'darwin') { | ||
| return path.join(MAC_OS_PATH, latestReleaseTag, 'codacy-cli-v2'); | ||
| } else if (os.platform() === 'linux') { | ||
| return path.join(LINUX_PATH, latestReleaseTag, 'codacy-cli-v2'); | ||
| } else { | ||
| throw new Error('Unsupported OS'); | ||
| } | ||
| }; | ||
|
|
||
| const getLatestReleaseTag = (): Promise<string> => { | ||
| return new Promise((resolve, reject) => { | ||
| https | ||
| .get( | ||
| GITHUB_LATEST_RELEASE_URL, | ||
| { | ||
| headers: { 'User-Agent': 'node.js' }, | ||
| }, | ||
| res => { | ||
| let data = ''; | ||
|
|
||
| res.on('data', chunk => { | ||
| data += chunk; | ||
| }); | ||
|
|
||
| res.on('end', () => { | ||
| try { | ||
| const json = JSON.parse(data); | ||
| const tagName = json.tag_name; | ||
| resolve(tagName); | ||
| } catch (error) { | ||
| reject(new Error('Failed to parse response')); | ||
| } | ||
| }); | ||
| } | ||
| ) | ||
| .on('error', error => { | ||
| reject(new Error(`Request failed: ${error.message}`)); | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| const execAsync = promisify(exec); | ||
|
|
||
| const execPromise = async (command: string): Promise<boolean> => { | ||
| try { | ||
| await execAsync(command); | ||
| return true; | ||
| } catch (error) { | ||
| console.error(`Error executing command: ${command}, reason: ${error}`); | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| const downloadCliTool = (): Promise<boolean> => { | ||
| return new Promise((resolve, _reject) => { | ||
| exec( | ||
| `bash <(curl -Ls ${GITHUB_INSTALL_SCRIPT_URL})`, | ||
| { shell: '/bin/bash' }, | ||
| (error, _stdout, _stderr) => { | ||
| if (error == null) { | ||
| resolve(true); | ||
| return; | ||
| } else { | ||
| resolve(false); | ||
| return; | ||
| } | ||
| } | ||
| ); | ||
| }); | ||
| }; | ||
|
|
||
| const isCodacyCliInstalled: () => Promise<boolean> = async () => { | ||
| const codacyCliPath = await getCodacyCliPath(); | ||
| return fs.existsSync(codacyCliPath); | ||
| }; | ||
|
|
||
| const isCodacyConfigPresent = () => { | ||
| return ( | ||
| fs.existsSync(path.join(process.cwd(), '.codacy', 'codacy.yml')) || | ||
| fs.existsSync(path.join(process.cwd(), '.codacy', 'codacy.yaml')) | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { Tool } from '@modelcontextprotocol/sdk/types.js'; | ||
|
|
||
| export const cliAnalysisTool: Tool = { | ||
| name: 'codacy_cli_analysis', | ||
| description: 'Run analysis using Codacy CLI', | ||
| inputSchema: { | ||
| type: 'object', | ||
| properties: { | ||
| tool: { | ||
| type: 'string', | ||
| description: 'Tool to use for analysis (e.g., eslint)', | ||
| default: 'eslint', | ||
| }, | ||
| format: { | ||
| type: 'string', | ||
| description: 'Output format (e.g., sarif)', | ||
| default: 'sarif', | ||
| }, | ||
| output: { | ||
| type: 'string', | ||
| description: 'Output file path', | ||
| default: 'results.sarif', | ||
| }, | ||
| }, | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { Tool } from '@modelcontextprotocol/sdk/types.js'; | ||
|
|
||
| export const installCLITool: Tool = { | ||
| name: 'codacy_install_cli', | ||
| description: 'Install and configure the Codacy CLI', | ||
| inputSchema: { | ||
| type: 'object', | ||
| properties: { | ||
| token: { | ||
| type: 'string', | ||
| description: 'The Codacy account token', | ||
| }, | ||
| }, | ||
| }, | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codacy has a fix for the issue: Unexpected console statement.