Gitlab中的Pylint徽章 [英] Pylint badge in gitlab

查看:337
本文介绍了Gitlab中的Pylint徽章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gitlab具有生成关于构建状态和覆盖率徽章的功能.
是否可以创建自定义徽章以显示Pylint结果? 还是只在README.md中显示此结果?
我已经有Pylint的CI职位

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

推荐答案

我写了一个Python徽章生成包,该软件包生成的徽章在视觉上与主要徽章服务非常相似.它具有高度的灵活性,您可以在python代码中导入和使用,也可以从命令行运行.

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.

我在GitLab CI中使用它来显示pylint和覆盖率得分.

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

还有其他方法可以使用shields.io(请参见其他答案): //stackoverflow.com/users/7153499/kubouch">kubouch ),但是这种方法可用于您可能无法访问外部Internet的情况,例如在具有防火墙或代理服务器的公司/企业环境中阻止互联网访问.

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.

我的CI管道有一个运行pylint的步骤,我使用了sed从输出文本中提取分数.然后,我使用anybadge(下面的详细信息)生成一个pylint得分徽章,并将其另存为public/pylint.svg.

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

如果pylint生成一个非零的rc,则GitLab将看到这是命令错误,并且作业将失败,这意味着将不生成徽章,并且丢失的图像将显示使用徽章的位置.

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.

注意:pylint经常会生成非零的返回码,因为它使用退出码来传达棉绒检查的状态.我建议使用 pylint-exit 之类的东西来处理CI管道中的pylint返回代码.

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.

我通过将生成的徽章文件包含在.gitlab-ci.yml中来将其注册为CI作业中的工件:

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.将徽章发布到GitLab页面上

我包括一个页面发布步骤,该步骤将公共目录中的所有内容都部署到GitLab页面:

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.在README.md

中包括徽章

为项目运行主管道时,将pylint.svg文件发布到GitLab Pages,然后我可以引用项目README.md中的图像,以便显示最新的pylint徽章.

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.

如果您在项目中使用 https://gitlab.com ,则该URL用于svg工件的通常是这样的(用您的用户名替换NAMESPACE,如果您的项目在一个组下,则将其替换为组名-

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

在您的README.md文件中,您可以包含以下图像:

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软件包的更多信息:

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

您可以设置徽章标签和值,还可以基于阈值设置颜色.有针对pylint,coverage和管道成功的预建设置,但是您可以创建自己喜欢的任何徽章.

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.

这里是指向github项目的链接,其中包含更详细的文档: https://github.com/jongracecox/anybadge

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

使用pip install anybadge

示例python代码:

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')

示例命令行用法:

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

更新2019

不再需要使用GitLab页面

现在可以直接直接访问最新的人工制品,从而简化了解决方法.

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. 使用专用的pylint工件而不是public,并删除不必要的deploy步骤(如果已使用,请对其进行编辑):
  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/

请注意,这里我将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.

徽章图像将在https://gitlab.example.com/john-doe/foo/-/jobs/artifacts/master/raw/pylint/pylint.svg?job=pylint可用,而Pylint日志在https://gitlab.example.com/john-doe/foo/-/jobs/artifacts/master/raw/pylint/pylint.log?job=pylint.

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.

manbetx客户端打不开现在可以在项目或项目组中包含徽章,这将显示在项目标题中.

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

转到Settings/General/Badges,然后通过设置其链接和图像链接来创建新徽章,如上所述:

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

这篇关于Gitlab中的Pylint徽章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆