Using the API to add Codacy Grade details to the Readme

Codacy has a badge mechanism that can be included in your Readme file. It gives you an idea of the grade of your repository, from A to F, but sometimes that’s just not enough.

Fortunately, the new Codacy V3 API gives us a lot of new endpoints with useful information.

In order to learn more about API consumption on pipelines, I’ve purposed myself to find a way to put these metrics on the Readme file of a sample repository with an handicap: will not use pre-commit.

Since the grade details tendentially changes in each commit, it’s wise to have a placeholder inside the file that we can fill with a sed script. Something like:

My first idea was to use a GitHub Action on the main branch, call the API and update the readme file. It failed 🤬! My main branch is protected and Actions force commits into protected branches are not allowed. Yes, I know I can turn that off but it would defeat the purpose.

So the path is to trigger the action from the feature branches using the following GHA example:

In this snippet, we check if the commit is already analysed and, if it is, we’ll replace the content between the placeholder tags with the new metrics. There’s also a “max retries” mechanism in case PR doesn’t exist yet (other validation mechanisms can be considered here).

After the merge, will have the current grade details

But there’s a…

Following this approach, there’s a huge limitation: the result will be the one before the merge. In order to avoid this, there’s a mandatory convention you should follow: do a rebase before merging.

Not perfect, I know but the purpose of this article is to demonstrate the power of the V3 API and how it can bring value outside the regular use cases. For instance, this endpoint can be used in a pipeline checkpoint.

Do you have any other “weird” use case? Let me know!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store