Skip to content
This documentation is a preview of the pull request #2301

For the latest updates and improvements in production, open docs.codacy.com instead.

Obtaining code quality metrics for files#

To obtain the code quality information for your files in a flexible way, use the Codacy API endpoint listFiles.

For example, if you're managing your source code using a monorepo strategy you may want to generate separate code quality reports for the subset of files that belong to each component or project in your repository.

Example: Obtaining code quality metrics for a subdirectory of your repository#

This example exports the grade, total issues, complexity, coverage, and duplication in CSV format for all files in the directory src/router of the GitHub repository codacy/website.

The example script:

  1. Defines the account API token used to authenticate on the Codacy API.
  2. Calls the endpoint listFiles to retrieve the code quality metrics, filtering the results by files that include src/router/ in the path.
  3. Uses jq to select only the necessary data fields and convert the results to the CSV format.
CODACY_API_TOKEN="<your account API token>"
GIT_PROVIDER="<your Git provider>" # gh, ghe, gl, gle, bb, or bbe
ORGANIZATION="<your organization name>"
REPOSITORY="<your repository name>"

curl -X GET "https://app.codacy.com/api/v3/organizations/$GIT_PROVIDER/$ORGANIZATION/repositories/$REPOSITORY/files?search=src/router/" \
     -H "api-token: $CODACY_API_TOKEN" \
| jq -r ".data[] | [.path, .gradeLetter, .totalIssues, .complexity, .coverage, .duplication] | @csv"

Example output:

"src/components/router/index.ts","A",0,8,70,0
"src/components/router/Link.tsx","A",0,5,100,0
"src/components/router/Redirect.tsx","B",0,2,14,0
"src/components/router/routes/account.ts","A",0,0,100,0
[...]

Important

For the sake of simplicity, the example doesn't consider paginated results obtained from the Codacy API. Learn how to use pagination to ensure that you process all results returned by the API.

See also#

Share your feedback 📢

Did this page help you?

Thanks for the feedback! Is there anything else you'd like to tell us about this page?

We're sorry to hear that. Please let us know what we can improve:

Alternatively, you can create a more detailed issue on our GitHub repository.

Thanks for helping improve the Codacy documentation.

Edit this page on GitHub if you notice something wrong or missing.

If you have a question or need help please contact support@codacy.com.

Last modified January 25, 2023