Skip to content

Obtaining code quality metrics for files and directories#

To obtain the code quality information for your files and directories in a flexible way, use the Codacy API endpoints listFiles, and listDirectories.

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#

Was this page helpful?

Your feedback helps us improve the documentation.