Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pylint badge in gitlab

Gitlab has functionality to generade badges about build status and coverage percentage.
Is it possible to create custom badge to display Pylint results? Or just display this results in README.md?
I already have CI job for Pylint

like image 369
Djent Avatar asked Mar 30 '17 19:03

Djent


People also ask

How do I create a custom badge in Gitlab?

Go to project Settings > General> Badges and add URL link and image URL click on Add Badge and it will be available on your project page.

How do Gitlab badges work?

Gitlab badges are a visual way of presenting summary information about your projects. They consist of a small image and a URL that the image points to as a link. We can use badges for example to show information about pipeline status, test coverage, code smells, etc.


2 Answers

I have written a python badge generation package that produces badges very visually similar to the main badge services. It is highly flexible, you can import and use in your python code, or run from the command line.

I use this in GitLab CI to display pylint and coverage scores.

There are other ways to do this using shields.io (see other answer from kubouch), but this approach can be used in situations where you may not have external internet access, such as in a corporate / enterprise setting where firewalls or proxies are blocking internet access.

GitLab CI Setup

1. Generate the badge

My CI pipeline has a step that runs pylint, and I used sed to extract the score from the output text. I then use anybadge (details below) to generate a pylint score badge, and save it as public/pylint.svg.

pylint:
  stage: test
  script:
    - pylint --rcfile=.pylintrc --output-format=text <LIST-OF-FILES-TO-RUN-PYLINT-AGAINST> | tee pylint.txt
    - score=$(sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' pylint.txt)
    - echo "Pylint score was $score"
    - anybadge --value=$score --file=public/pylint.svg pylint

If pylint generates a non-zero rc then GitLab will see that as a command error and the job will fail, meaning no badge is generated, and missing image will show where the badge is used.

NOTE: pylint WILL OFTEN generate non-zero return codes since it uses the exit code to communicate the status of the lint check. I suggest using something like pylint-exit to handle pylint return codes in CI pipelines.

2. Register badge as pipeline artifact

I register the generated badge file as an artifact in the CI job by including this in the .gitlab-ci.yml:

pylint:
    ...
    - echo "Pylint score was $score"
    - anybadge --value=$score --file=public/pylint.svg pylint
  artifacts:
    paths:
      - public/pylint.svg

3. Publish badge to GitLab Pages

I include a pages publish step, which deploys everything in the public directory to GitLab pages:

pages:
  stage: deploy
  artifacts:
    paths:
    - public
  only:
  - master

4. Include badge in README.md

When the master pipeline runs for the project, the pylint.svg file is published to GitLab Pages, and I can then reference the image from my project README.md so that the latest pylint badge is displayed.

If you are using https://gitlab.com for your project then the URL for the svg artifact will usually be something like this (replace NAMESPACE with your username, or group name if your project is under a group - more details here):

https://NAMESPACE.gitlab.io/pyling.svg

In your README.md you can include an image with:

![pylint](https://NAMESPACE.gitlab.io/pyling.svg)

If you want to make the image into a link you can use:

[![pylint](https://NAMESPACE.gitlab.io/pyling.svg)](LINKTARGET)

Let me know if you need more information on any of the setup.

Anybadge Python Package

Here's some more info on the anybadge Python package:

You can set the badge label and value, and you can set the color based on thresholds. There are pre-built settings for pylint, coverage, and pipeline success, but you can create any badge you like.

Here is a link to the github project with more detailed documentation: https://github.com/jongracecox/anybadge

Install with pip install anybadge

Example python code:

import anybadge

# Define thresholds: <2=red, <4=orange <8=yellow <10=green
thresholds = {2: 'red',
              4: 'orange',
              6: 'yellow',
              10: 'green'}

badge = anybadge.Badge('pylint', 2.22, thresholds=thresholds)

badge.write_badge('pylint.svg')

Example command line use:

anybadge --label pylint --value 2.22 --file pylint.svg 2=red 4=orange 8=yellow 10=green

Update 2019

Using GitLab Pages is no longer required

It is now possible to directly access to the latest articfact, which simplify the workaround.

  1. Use a dedicated pylint artifact instead of public, and remove the unnecessary deploy step (or edit it if already used):
pylint:
  stage: test
  before_script:
    - pip install pylint pylint-exit anybadge
  script:
    - mkdir ./pylint
    - pylint --output-format=text . | tee ./pylint/pylint.log || pylint-exit $?
    - PYLINT_SCORE=$(sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' ./pylint/pylint.log)
    - anybadge --label=Pylint --file=pylint/pylint.svg --value=$PYLINT_SCORE 2=red 4=orange 8=yellow 10=green
    - echo "Pylint score is $PYLINT_SCORE"
  artifacts:
    paths:
      - ./pylint/

Note that here I copy the Pylint log file in the folder artifact, in this way it will be accessible without looking at the pipeline logs.

The badge image will then be available at https://gitlab.example.com/john-doe/foo/-/jobs/artifacts/master/raw/pylint/pylint.svg?job=pylint, and the Pylint log at https://gitlab.example.com/john-doe/foo/-/jobs/artifacts/master/raw/pylint/pylint.log?job=pylint.

2. You can use GitLab's builtin badges instead of images in README

GitLab can now include badges in a projet or group, that will be displayed in the project header.

Got to Settings / General / Badges, then create a new badge by setting its link and image link, as described above:

screenshot of gitlab badges settings

like image 90
JGC Avatar answered Sep 29 '22 05:09

JGC


If you don't want to use the README, gitlab pages, anybadge or dropbox you can use https://img.shields.io/badge/lint%20score-$score-blue.svg to 'create' a badge (which is just an URL) and change the badge image URL via the gitlab API.

enter image description here

Details and the lint stage of my .gitlab-ci.yml

like image 6
Manuel Avatar answered Sep 30 '22 05:09

Manuel