Pylint badge in gitlab

后端 未结 3 925
一个人的身影
一个人的身影 2021-01-31 05:55

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

相关标签:
3条回答
  • 2021-01-31 06:43

    I developed a workaround solution to real-time per-job badges. It is not Pylint specific but the approach is general and you can easily modify it into what you need.

    This example repo (branch badges) creates a custom badge for each CI job. There is also a complete walkthrough so I won't copy-paste it here.

    The core idea is (assuming you're now inside a running CI job):

    • Create a badge (e.g. fetch it from shields.io into a file).
    • Upload the badge file to some real-time storage from where it can be linked (e.g. Dropbox).

    Dropbox supports calling its API via HTTP requests (see this). Thus, all the above can be done using e.g. curl or Python requests - basic tools. You just need to pass the Dropbox access token as secret variable and save the file under the same name to overwrite the old badge.

    Then, you can then directly link the Dropbox badge wherever you need. There are some tricks to it so be sure to check my example repo if you want to use it. For me it works quite well and seems to be fast.

    The advantage of this method is that you don't have to mess with GitLab Pages. Instead of publishing on Pages you put it to Dropbox. That is a simple file transfer called by HTTP request. No more to that.

    0 讨论(0)
  • 2021-01-31 06:56

    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.

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

    0 讨论(0)
  • 2021-01-31 06:58

    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

    0 讨论(0)
提交回复
热议问题