Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ dist/
api-v3/

# Ignore .codacy
.codacy/
.codacy/

#Ignore vscode AI rules
.github/instructions/codacy.instructions.md
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"prepublishOnly": "npm run update-api && npm run build",
"start": "npx ts-node src/index.ts",
"start:dist": "node dist/index.js",
"fetch-api": "curl https://artifacts.codacy.com/api/codacy-api/50.7.17/apiv3-bundled.yaml -o ./api-v3/api-swagger.yaml --create-dirs",
"fetch-api": "curl https://artifacts.codacy.com/api/codacy-api/52.1.31/apiv3-bundled.yaml -o ./api-v3/api-swagger.yaml --create-dirs",
"generate-api": "rm -rf ./src/api/client && openapi --input ./api-v3/api-swagger.yaml --output ./src/api/client --useUnionTypes --indent 2 --client fetch",
"update-api": "npm run fetch-api && npm run generate-api",
"check-types": "tsc --noEmit"
Expand Down
173 changes: 173 additions & 0 deletions src/commands/issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { describe, it, expect, vi, beforeEach } from "vitest";
import { Command } from "commander";
import { registerIssuesCommand } from "./issues";
import { AnalysisService } from "../api/client/services/AnalysisService";
import { ToolsService } from "../api/client/services/ToolsService";

vi.mock("../api/client/services/AnalysisService");
vi.mock("../api/client/services/ToolsService");
vi.mock("../utils/credentials", () => ({ loadCredentials: vi.fn(() => null) }));
vi.spyOn(console, "log").mockImplementation(() => {});

Expand Down Expand Up @@ -612,6 +614,177 @@ describe("issues command", () => {
);
});

describe("--tools filter", () => {
const mockToolList = {
data: [
{ uuid: "uuid-eslint", name: "ESLint", shortName: "eslint", prefix: "ESLint_" },
{ uuid: "uuid-eslint9", name: "ESLint 9", shortName: "eslint9", prefix: "ESLint9_" },
{ uuid: "uuid-semgrep", name: "Semgrep", shortName: "semgrep", prefix: "Semgrep_" },
{ uuid: "uuid-markdownlint", name: "Markdownlint", shortName: "markdownlint", prefix: "Markdownlint_" },
{ uuid: "uuid-remarklint", name: "Remarklint", shortName: "remarklint", prefix: "Remarklint_" },
],
pagination: undefined,
};

it("should pass a UUID directly to body.toolUuids", async () => {
vi.mocked(AnalysisService.searchRepositoryIssues).mockResolvedValue({
data: [],
} as any);

const program = createProgram();
await program.parseAsync([
"node", "test", "issues", "gh", "test-org", "test-repo",
"--tools", "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
]);

expect(ToolsService.listTools).not.toHaveBeenCalled();
expect(AnalysisService.searchRepositoryIssues).toHaveBeenCalledWith(
"gh", "test-org", "test-repo", undefined, 100,
{ toolUuids: ["a1b2c3d4-e5f6-7890-abcd-ef1234567890"] },
);
});

it("should resolve an exact tool name to its UUID", async () => {
vi.mocked(ToolsService.listTools).mockResolvedValue(mockToolList as any);
vi.mocked(AnalysisService.searchRepositoryIssues).mockResolvedValue({
data: [],
} as any);

const program = createProgram();
await program.parseAsync([
"node", "test", "issues", "gh", "test-org", "test-repo",
"--tools", "eslint",
]);

expect(ToolsService.listTools).toHaveBeenCalled();
expect(AnalysisService.searchRepositoryIssues).toHaveBeenCalledWith(
"gh", "test-org", "test-repo", undefined, 100,
{ toolUuids: ["uuid-eslint"] },
);
});

it("should resolve a shortName match to its UUID", async () => {
vi.mocked(ToolsService.listTools).mockResolvedValue(mockToolList as any);
vi.mocked(AnalysisService.searchRepositoryIssues).mockResolvedValue({
data: [],
} as any);

const program = createProgram();
await program.parseAsync([
"node", "test", "issues", "gh", "test-org", "test-repo",
"--tools", "semgrep",
]);

expect(AnalysisService.searchRepositoryIssues).toHaveBeenCalledWith(
"gh", "test-org", "test-repo", undefined, 100,
{ toolUuids: ["uuid-semgrep"] },
);
});

it("should resolve an exact shortName match (eslint9)", async () => {
vi.mocked(ToolsService.listTools).mockResolvedValue(mockToolList as any);
vi.mocked(AnalysisService.searchRepositoryIssues).mockResolvedValue({
data: [],
} as any);

const program = createProgram();
await program.parseAsync([
"node", "test", "issues", "gh", "test-org", "test-repo",
"--tools", "eslint9",
]);

expect(AnalysisService.searchRepositoryIssues).toHaveBeenCalledWith(
"gh", "test-org", "test-repo", undefined, 100,
{ toolUuids: ["uuid-eslint9"] },
);
});

it("should resolve a unique substring match via prefix", async () => {
vi.mocked(ToolsService.listTools).mockResolvedValue(mockToolList as any);
vi.mocked(AnalysisService.searchRepositoryIssues).mockResolvedValue({
data: [],
} as any);

const program = createProgram();
// "semgr" is not an exact name or shortName, but substring-matches only Semgrep
await program.parseAsync([
"node", "test", "issues", "gh", "test-org", "test-repo",
"--tools", "semgr",
]);

expect(AnalysisService.searchRepositoryIssues).toHaveBeenCalledWith(
"gh", "test-org", "test-repo", undefined, 100,
{ toolUuids: ["uuid-semgrep"] },
);
});

it("should error when tool name is ambiguous", async () => {
vi.mocked(ToolsService.listTools).mockResolvedValue(mockToolList as any);

const mockExit = vi.spyOn(process, "exit").mockImplementation(() => {
throw new Error("process.exit called");
});
const mockStderr = vi.spyOn(console, "error").mockImplementation(() => {});

const program = createProgram();
await expect(
program.parseAsync([
"node", "test", "issues", "gh", "test-org", "test-repo",
"--tools", "mark",
Comment on lines +721 to +733
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ LOW RISK

Nitpick: The new test cases for the --tools filter introduce significant duplication. Extracting a helper function to handle the common CLI setup and service mocking would improve maintainability and simplify the test suite.

Try running the following prompt in your IDE agent:

In src/commands/issues.test.ts, extract a helper function for the --tools filter tests to reduce duplication in command invocation and error handling assertions.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing test suite in this file follows the same inline pattern. Introducing abstractions in tests reduces readability — keeping it as-is for consistency.

]),
).rejects.toThrow("process.exit called");

expect(mockStderr).toHaveBeenCalledWith(
expect.stringContaining("ambiguous"),
);

mockExit.mockRestore();
mockStderr.mockRestore();
});

it("should error when tool name is not found", async () => {
vi.mocked(ToolsService.listTools).mockResolvedValue(mockToolList as any);

const mockExit = vi.spyOn(process, "exit").mockImplementation(() => {
throw new Error("process.exit called");
});
const mockStderr = vi.spyOn(console, "error").mockImplementation(() => {});

const program = createProgram();
await expect(
program.parseAsync([
"node", "test", "issues", "gh", "test-org", "test-repo",
"--tools", "nonexistent",
]),
).rejects.toThrow("process.exit called");

expect(mockStderr).toHaveBeenCalledWith(
expect.stringContaining("not found"),
);

mockExit.mockRestore();
mockStderr.mockRestore();
});

it("should handle mixed UUIDs and tool names", async () => {
vi.mocked(ToolsService.listTools).mockResolvedValue(mockToolList as any);
vi.mocked(AnalysisService.searchRepositoryIssues).mockResolvedValue({
data: [],
} as any);

const program = createProgram();
await program.parseAsync([
"node", "test", "issues", "gh", "test-org", "test-repo",
"--tools", "a1b2c3d4-e5f6-7890-abcd-ef1234567890,semgrep",
]);

expect(AnalysisService.searchRepositoryIssues).toHaveBeenCalledWith(
"gh", "test-org", "test-repo", undefined, 100,
{ toolUuids: ["a1b2c3d4-e5f6-7890-abcd-ef1234567890", "uuid-semgrep"] },
);
});
});

it("should fail when CODACY_API_TOKEN is not set", async () => {
delete process.env.CODACY_API_TOKEN;

Expand Down
20 changes: 19 additions & 1 deletion src/commands/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import {
printJson,
printPaginationWarning,
} from "../utils/output";
import { printSection, printIssueCard } from "../utils/formatting";
import { printSection, printIssueCard, resolveToolUuids } from "../utils/formatting";
import { AnalysisService } from "../api/client/services/AnalysisService";
import { ToolsService } from "../api/client/services/ToolsService";
import { Tool } from "../api/client/models/Tool";
import { CommitIssue } from "../api/client/models/CommitIssue";
import { SeverityLevel } from "../api/client/models/SeverityLevel";
import { SearchRepositoryIssuesBody } from "../api/client/models/SearchRepositoryIssuesBody";
Expand Down Expand Up @@ -169,6 +171,7 @@ export function registerIssuesCommand(program: Command) {
.argument("<repository>", "repository name")
.option("-b, --branch <branch>", "branch name (defaults to the main branch)")
.option("-p, --patterns <patterns>", "comma-separated list of pattern IDs")
.option("-T, --tools <tools>", "comma-separated tool UUIDs or names to filter by")
.option(
"-s, --severities <severities>",
"comma-separated severity levels: Critical, High, Medium, Minor (or Error, Warning, Info)",
Expand All @@ -189,6 +192,7 @@ Examples:
$ codacy issues gh my-org my-repo
$ codacy issues gh my-org my-repo --branch main --severities Critical,Medium
$ codacy issues gh my-org my-repo --categories Security --overview
$ codacy issues gh my-org my-repo --tools eslint,semgrep
$ codacy issues gh my-org my-repo --limit 500
$ codacy issues gh my-org my-repo --output json`,
)
Expand Down Expand Up @@ -220,6 +224,20 @@ Examples:
const author = parseCommaList(opts.authors);
if (author) body.authorEmails = author;

const toolInputs = parseCommaList(opts.tools);
if (toolInputs) {
body.toolUuids = await resolveToolUuids(toolInputs, async () => {
const tools: Tool[] = [];
let cursor: string | undefined;
do {
const resp = await ToolsService.listTools(cursor, 100);
tools.push(...resp.data);
cursor = resp.pagination?.cursor;
} while (cursor);
return tools;
});
}

const limit = Math.min(Math.max(parseInt(opts.limit, 10) || 100, 1), 1000);

const spinner = ora(
Expand Down
72 changes: 71 additions & 1 deletion src/utils/formatting.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { formatAnalysisStatus } from "./formatting";
import { formatAnalysisStatus, resolveToolUuids } from "./formatting";

// Mock ansis to return raw text for easier testing
vi.mock("ansis", () => ({
Expand Down Expand Up @@ -92,3 +92,73 @@ describe("formatAnalysisStatus", () => {
expect(result).toBe("Never");
});
});

describe("resolveToolUuids", () => {
const mockTools = [
{ uuid: "uuid-eslint", name: "ESLint", shortName: "eslint", prefix: "ESLint_" },
{ uuid: "uuid-eslint9", name: "ESLint 9", shortName: "eslint9", prefix: "ESLint9_" },
{ uuid: "uuid-semgrep", name: "Semgrep", shortName: "semgrep", prefix: "Semgrep_" },
{ uuid: "uuid-markdownlint", name: "Markdownlint", shortName: "markdownlint", prefix: "Markdownlint_" },
{ uuid: "uuid-remarklint", name: "Remarklint", shortName: "remarklint", prefix: "Remarklint_" },
] as any[];

const fetchTools = vi.fn(async () => mockTools);

beforeEach(() => {
fetchTools.mockClear();
});

it("should pass UUIDs through without fetching tools", async () => {
const result = await resolveToolUuids(
["a1b2c3d4-e5f6-7890-abcd-ef1234567890"],
fetchTools,
);
expect(result).toEqual(["a1b2c3d4-e5f6-7890-abcd-ef1234567890"]);
expect(fetchTools).not.toHaveBeenCalled();
});

it("should resolve exact name match (case-insensitive)", async () => {
const result = await resolveToolUuids(["eslint"], fetchTools);
expect(result).toEqual(["uuid-eslint"]);
});

it("should resolve exact shortName match (case-insensitive)", async () => {
const result = await resolveToolUuids(["eslint9"], fetchTools);
expect(result).toEqual(["uuid-eslint9"]);
});

it("should resolve a unique substring match via name", async () => {
const result = await resolveToolUuids(["semgr"], fetchTools);
expect(result).toEqual(["uuid-semgrep"]);
});

it("should error on ambiguous substring match", async () => {
await expect(resolveToolUuids(["mark"], fetchTools)).rejects.toThrow(
/ambiguous.*Markdownlint.*Remarklint/,
);
});
Comment on lines +135 to +139
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — fixing the spy references.


it("should error when tool is not found", async () => {
await expect(resolveToolUuids(["zzz"], fetchTools)).rejects.toThrow(
'Tool "zzz" not found',
);
});

it("should deduplicate resolved UUIDs", async () => {
const result = await resolveToolUuids(["eslint", "eslint"], fetchTools);
expect(result).toEqual(["uuid-eslint"]);
});

it("should handle mixed UUIDs and names, fetching tools only once", async () => {
const result = await resolveToolUuids(
["a1b2c3d4-e5f6-7890-abcd-ef1234567890", "semgrep", "eslint"],
fetchTools,
);
expect(result).toEqual([
"a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"uuid-semgrep",
"uuid-eslint",
]);
expect(fetchTools).toHaveBeenCalledTimes(1);
});
});
Loading
Loading