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

查看:39
本文介绍了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(请参阅 其他答案 from kubouch),但这种方法可用于您可能无法访问外部互联网的情况,例如在防火墙或代理存在的公司/企业环境中阻止互联网访问.

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 通常会生成非零返回码,因为它使用退出码来传达 lint 检查的状态.我建议使用 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.

我将生成的徽章文件注册为 CI 作业中的工件,方法是将其包含在 .gitlab-ci.yml 中:

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、覆盖率和管道成功的预置设置,但您可以创建任何您喜欢的徽章.

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 页面

现在可以直接访问最新的工件,这简化了解决方法.

  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 日志文件复制到文件夹 artifact 中,这样无需查看管道日志即可访问它.

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,以及位于 https://gitlab.example.com/john-doe/foo/-/jobs/artifacts/master/raw/pylint/pylint.log?job=pylint 的 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.

GitLab 现在可以在项目或组中包含徽章,这些徽章将显示在项目标题中.

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天全站免登陆