Skip to content

Conversation

@franciscoovazevedo
Copy link
Contributor

@franciscoovazevedo franciscoovazevedo commented May 14, 2025

Use Codacy Default Patterns when no Token is provided

Overview

This PR introduces two main improvements:

  1. Use Codacy's default patterns for each tool when no API token is provided
  2. Add new endpoint in codacy-client package to fetch patterns for supported tools

Changes

Default Patterns Support

  • When initializing without an API token, the CLI now fetches and uses Codacy's default patterns for each tool
  • This ensures consistent behavior across all installations, even in local mode
  • Default patterns are fetched from Codacy's API and stored in the local configuration
  • Affects all supported tools: ESLint, PMD, Pylint, Trivy, DartAnalyzer, etc.

New Pattern Fetching Endpoint

  • Added new GetDefaultToolPatternsConfig function in codacy-client package
  • This endpoint handles fetching patterns for any supported tool
  • Implemented generic pagination handling for consistent API response processing
  • Note: This is the first step towards centralizing API requests. Future PRs will move other API interactions to this package.

@codacy-production
Copy link

codacy-production bot commented May 14, 2025

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
Report missing for 237e4271 (target: 50.00%)
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (237e427) Report Missing Report Missing Report Missing
Head commit (9b41044) 4637 1424 30.71%

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#119) 0 0 ∅ (not applicable)

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Footnotes

  1. Codacy didn't receive coverage data for the commit, or there was an error processing the received data. Check your integration for errors and validate that your coverage setup is correct.

@franciscoovazevedo franciscoovazevedo marked this pull request as ready for review May 14, 2025 14:39
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enables fetching and using Codacy’s default patterns when no API token is provided and centralizes pattern-fetching logic in the codacy-client package.

  • Support default tool patterns in CLI init when no token is set
  • Add GetDefaultToolPatternsConfig endpoint with generic pagination in codacy-client
  • Refactor init flags and wiring to use shared domain.InitFlags

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
domain/patternConfiguration.go Remove obsolete IsCustom field from PatternConfiguration.
domain/pagination.go Introduce Pagination struct to model API pagination metadata.
domain/initFlags.go Define InitFlags struct to hold CLI initialization flags.
codacy-client/client.go Implement getRequest, handlePaginationGeneric, and default-pattern endpoint.
codacy-client/client_test.go Add tests for HTTP requests and pagination; placeholder for default-patterns test.
cmd/init.go Refactor init command to use domain.InitFlags and loop default patterns.
cmd/init_test.go Add test for init with no token, creating default configuration files.
Comments suppressed due to low confidence (3)

codacy-client/client_test.go:71

  • The TestGetDefaultToolPatternsConfig_Empty is currently a placeholder and doesn’t invoke the function under test. Consider refactoring GetDefaultToolPatternsConfig to accept a configurable baseURL or HTTP client so you can write a real unit test without making live API calls.
// TODO: Refactor GetDefaultToolPatternsConfig to accept a baseURL for easier testing

cmd/init_test.go:221

  • This test for init with no token will invoke buildDefaultConfigurationFiles and make real HTTP requests via GetDefaultToolPatternsConfig, which may lead to test flakiness. Consider mocking codacyclient.GetDefaultToolPatternsConfig or injecting a fake client to avoid network dependencies.
if err := buildDefaultConfigurationFiles(toolsConfigDir); err != nil {

cmd/init.go:483

  • [nitpick] This standalone comment appears to be a leftover and doesn't add context. Consider removing it for clarity.
//

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances the Codacy CLI by fetching default tool patterns when no API token is provided and adds a new endpoint in the codacy-client package for tool pattern configuration. Key changes include the removal of the IsCustom field from the pattern configuration structure, the addition of a Pagination type and InitFlags struct for improved CLI flag management, and updated initialization and configuration generation logic within the CLI commands.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
domain/patternConfiguration.go Removed the IsCustom field from PatternConfiguration
domain/pagination.go Added a new Pagination struct to aid in API pagination handling
domain/initFlags.go Introduced a new InitFlags struct with exported fields
codacy-client/client.go Added GetDefaultToolPatternsConfig with generic pagination handling
codacy-client/client_test.go Added tests for the new client functions and pagination handling
cmd/init.go & cmd/init_test.go Updated init command and test to fetch default configurations in local mode
Comments suppressed due to low confidence (2)

domain/patternConfiguration.go:26

  • Ensure that the removal of the IsCustom field is intentional and that any consumers of the PatternConfiguration structure are updated to handle its absence, particularly for JSON (un)marshalling.
-	IsCustom          bool `json:"isCustom"`

codacy-client/client_test.go:76

  • Consider refactoring GetDefaultToolPatternsConfig to accept a base URL or use dependency injection so that it becomes fully testable, which would improve test coverage.
// Placeholder: test cannot be run until function is refactored for testability

cmd/init_test.go Outdated
}

toolsConfigDir := config.Config.ToolsConfigDirectory()
if err := os.MkdirAll(toolsConfigDir, 0777); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not using File Perission from cosntants file

// - fetchPage: a function that fetches a page and returns ([]T, nextCursor, error)
//
// Returns a slice of all results of type T and any error encountered.
func handlePaginationGeneric[T any](
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Consider calling this something that makes clear that is fetching all the pages, not just 'handling some output'. Maybe something as 'getAllPages' or something.
  • This should not receive an 'initialCursor' as all the calls will just pass "". The idea of passing a cursor could at most be helpful if you were using recursion, and even then could be an Aux function, but as it is, I see no reason to have this parameter
  • On the fetchPage arg, seems strange to be a func passing. The logic of fetching a page from an API, should not be a function pass, should be something that exists, some 'GetPage[T](url, cursor, parser)' that is clearly defined. Then this method should receive a parser of some sorts, that converts data => T. Then this getAllPages would in a loop call the GetPage propagating the parser and relying on the returned cursor for the next iteration.

Is this reasonable feedback? Maybe @andrzej-janczak has input?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

very reasonable of course. i pushed what I think it's an improvement.

@franciscoovazevedo franciscoovazevedo force-pushed the pluto-1380 branch 2 times, most recently from 9c480d8 to 1b58153 Compare May 15, 2025 12:18
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces support for fetching Codacy's default patterns when no API token is provided and adds a new endpoint in the codacy-client package to fetch tool patterns. Key changes include the removal of the unused "IsCustom" field in pattern configurations, the introduction of new pagination and initialization structs, and updates in both client and command implementations to support default configuration generation.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
domain/patternConfiguration.go Removed the "IsCustom" field from the PatternConfiguration struct.
domain/pagination.go Added a simple Pagination struct for API pagination handling.
domain/initFlags.go Introduced a public InitFlags struct with updated field names.
codacy-client/client_test.go Added tests for getRequest, GetPage, and getAllPages; noted a test for default patterns pending refactoring.
codacy-client/client.go Added GetDefaultToolPatternsConfig and GetRepositoryToolPatterns functions; updated pagination handling in API calls.
cmd/init_test.go Updated tests to verify configuration file creation in “no token” mode.
cmd/init.go Updated flag definition and logic to support fetching default configurations when no token is provided.
Comments suppressed due to low confidence (2)

domain/patternConfiguration.go:26

  • Removal of the 'IsCustom' field should be reviewed to ensure that any logic or tests which previously relied on this property have been properly updated.
-	IsCustom          bool `json:"isCustom"`

codacy-client/client_test.go:103

  • [nitpick] Consider refactoring GetDefaultToolPatternsConfig to accept a base URL or leverage dependency injection, which would improve testability and allow for full coverage of default tool patterns fetching.
// TODO: Refactor GetDefaultToolPatternsConfig to accept a baseURL for easier testing

@franciscoovazevedo franciscoovazevedo merged commit a95a19d into main May 19, 2025
10 checks passed
@alerizzo alerizzo deleted the pluto-1380 branch June 3, 2025 09:45
@machadoit machadoit restored the pluto-1380 branch October 12, 2025 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants