Home Education Tutorials Using the API to add Codacy Grade details to the Readme

Using the API to add Codacy Grade details to the Readme

Author

Date

Category

Some context

Prerequisites — A Placeholder

# An amazing repository with code<!-- codacy-status --> 
<!-- /codacy-status -->

Hands-on

name: Update Readmeon:
  push:
    branches-ignore:
      - masterjobs:
  update-readme:
    runs-on: ubuntu-latest
    steps:
     - uses: actions/checkout@v2
       with:
        ref: ${{ github.head_ref }}
     - name: update README.md with codacy status
       run: |
          RETRIES=0
          ANALYZED=$(curl -X GET https://app.codacy.com/api/v3/analysis/organizations/gh/codacy-acme/repositories/nodeproject/commits/${GITHUB_SHA}/deltaStatistics -H 'api-token: ${{ secrets.API_TOKEN }}' | jq '.analyzed')
          while [ $ANALYZED != "true" ]
          do
          sleep 10s
          RETRIES=$((RETRIES+1))
          if [ "$RETRIES" -gt "10" ]; then
            echo "pipeline will exit due to max retries (10)"
            exit 0
          fi
          ANALYZED=$(curl -X GET https://app.codacy.com/api/v3/analysis/organizations/gh/codacy-acme/repositories/nodeproject/commits/${GITHUB_SHA}/deltaStatistics -H 'api-token: ${{ secrets.API_TOKEN }}' | jq '.analyzed')
          done
          RESULT=$(curl https://app.codacy.com/api/v3/analysis/organizations/gh/codacy-acme/repositories/nodeproject | jq -r -j '["Grade: \(.data.grade) |", "Issues: \(.data.issuesPercentage)% |", "Complex Files: \(.data.complexFilesPercentage)% |", "Coverage: \(.data.coverage.coveragePercentage)%"]' | tr -d \" | tr -d \[ | tr  -d \] | tr -d , | tr -d '\n')
          sed -i ":a;N;\$!ba;s/\(<!-- codacy-status -->\).*\(<!-- \/codacy-status -->\)/\1 \n$RESULT\n \2/g" README.md
       env:
        CI: true
     - uses: stefanzweifel/git-auto-commit-action@v4
       with:
        commit_message: "Update README.md with Project Grade"
        file_pattern: README.md

Downside

 

If you’d like to learn more about using the Codacy API, please check our docs page on the API v3. For more on our product and updates on our static analysis tool, check out Codacy’s product page.

This article was originally published by Hélio, Sr. Solutions Engineer at Codacy on his Medium account. 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Recent posts

What to expect after adopting a code review tool like Codacy

In a previous article, we’ve covered how much technical debt and code quality issues are costing you and how Codacy can...

Why our customers made the switch to Codacy

Software engineering teams across several industries are always looking for the best tools to use on their CI/CD pipelines. Here are...

Flexible vacation days: our commitment to employees’ well-being

At Codacy, teams and individuals are encouraged to manage their own time. We want to build an environment of trust that...

Don’t drop the ball: Keeping your code up to standards

On April 27th, we did a webinar called Don’t drop the ball: Keeping your code up to standards. 

April Product Update 🚀

Hi there 👋, Here are some exciting news from April! We have a major announcement, so keep...