GitLab CI/CD使用经验,来自于莫纳什大学的考试任务解析

CI/CD简介

CI/CD的作用在于自动化和加速软件开发、测试和交付流程,通过持续集成确保代码协同工作和质量,通过持续交付降低风险,使每次代码变更都能够快速、高质量地交付到生产环境,从而提高软件开发效率、质量和协作。

作业要求

学校给出一个售货系统的代码,需要学生对于代码进行构建CI/CD(flake8,mypy,pycodestyle,pydocstyle,pylint),并且使用Python完善软件测试(test)。

解析方案

在构建CI/CD管道时,在使用配置文件创建静态分析时,我最初没有区分地将所有阶段合并在一起。在后来的过程中,我通过为每个阶段使用相同的阶段名称来解决这个问题。当测试没有被代码纠正时发生的测试用例。

Below is my code:

yml 复制代码
stages:
  - Static Analysis 
  - Test

flake8:
  stage: Static Analysis
  script:
    - pip install flake8
    - flake8 megamart.py
  allow_failure: true

mypy:
  stage: Static Analysis  
  script:
    - pip install mypy
    - mypy megamart.py
  allow_failure: true

pycodestyle:
  stage: Static Analysis
  script: 
    - pip install pycodestyle
    - pycodestyle megamart.py 
  allow_failure: true

pydocstyle:
  stage: Static Analysis
  script:
    - pip install pydocstyle
    - pydocstyle megamart.py
  allow_failure: true

pylint:
  stage: Static Analysis
  script:
    - pip install pylint
    - pylint megamart.py
  allow_failure: true

testing:
  stage: Test
  script:
    - python -m unittest test_megamart.py
  allow_failure: false

在修改完测试用例后可以登录你的GitLab可以看到你的测试结果已经通过,但是你的代码风格分析产生一些错误异常

分析代码风格产生异常的原因

PyCodeStyles

使用Python绘制一个柱状图来查看代码风格产生异常的错误代码

接下来根据每一个错误给出解决错误的方式

D200: One-line docstring should fit on one line with quotes (found 4):

这个错误只需要在导入一个模块的时候加上备注写入对于模块的描述就可以解决这个问题,下面是对比:

An Example of the Original Version.

python 复制代码
from datetime import datetime

An Example of the Fixed Version.

python 复制代码
"""
This is the Megamart module.

It is Megamart
"""
from datetime import datetime

D205: 1 blank line required between summary line and description

An Example of the Original Version.

python 复制代码
def is_not_allowed_to_purchase_item(item: Item, customer: Customer, purchase_date_string: str) -> bool:
  """
  Returns True if the customer is not allowed to purchase the specified item, False otherwise.
  If an item object or the purchase date string was not actually provided, an Exception should be raised.
  Items that are under the alcohol, tobacco or knives category may only be sold to customers who are aged 18+ and have their ID verified.
  An item potentially belongs to many categories - as long as it belongs to at least one of the three categories above, restrictions apply to that item.
  The checking of an item's categories against restricted categories should be done in a case-insensitive manner.
   For example, if an item A is in the category ['Alcohol'] and item B is in the category ['ALCOHOL'], both items A and B should be identified as restricted items.
  Even if the customer is aged 18+ and is verified, they must provide/link their member account to the transaction when purchasing restricted items.
  Otherwise, if a member account is not provided, they will not be allowed to purchase the restricted item even if normally allowed to.
  It is optional for customers to provide their date of birth in their profile.
  Purchase date string should be of the format dd/mm/yyyy.
  The age of the customer is calculated from their specified date of birth, which is also of the format dd/mm/yyyy.
  If an item is a restricted item but the purchase or birth date is in the incorrect format, an Exception should be raised.
  A customer whose date of birth is 01/08/2005 is only considered to be age 18+ on or after 01/08/2023.
  """

An Example of the Fixed Version.

python 复制代码
def is_not_allowed_to_purchase_item(item: Item, customer: Customer, purchase_date_string: str) -> bool:
  """
  Returns True if the customer is not allowed to purchase the specified item, False otherwise.

  Args:
      item (Item): The item being purchased.
      customer (Customer): The customer attempting the purchase.
      purchase_date_string (str): The purchase date in the format dd/mm/yyyy.

  Raises:
      Exception: If the provided item is None.

  If an item object or the purchase date string was not actually provided, an Exception should be raised.
  Items that are under the alcohol, tobacco or knives category may only be sold to customers who are aged 18+ and have their ID verified.
  An item potentially belongs to many categories - as long as it belongs to at least one of the three categories above, restrictions apply to that item.
  The checking of an item's categories against restricted categories should be done in a case-insensitive manner.
  For example, if an item A is in the category ['Alcohol'] and item B is in the category ['ALCOHOL'], both items A and B should be identified as restricted items.
  Even if the customer is aged 18+ and is verified, they must provide/link their member account to the transaction when purchasing restricted items.
  Otherwise, if a member account is not provided, they will not be allowed to purchase the restricted item even if normally allowed to.
  It is optional for customers to provide their date of birth in their profile.
  Purchase date string should be of the format dd/mm/yyyy.
  The age of the customer is calculated from their specified date of birth, which is also of the format dd/mm/yyyy.
  If an item is a restricted item but the purchase or birth date is in the incorrect format, an Exception should be raised.
  A customer whose date of birth is 01/08/2005 is only considered to be age 18+ on or after 01/08/2023.
  """

pyLine

C0301: Line too long (103/100) (line-too-long)

An Example of the Original Version.

python 复制代码
def is_not_allowed_to_purchase_item(item: Item, customer: Customer, purchase_date_string: str) -> bool:
  """
  Determine if the customer is allowed to purchase the specified item.

  Returns True if the customer is not allowed to purchase the specified item, False otherwise.
python 复制代码
def is_not_allowed_to_purchase_item(item: Item, 
                                    customer: Customer, 
                                    purchase_date_string: str) -> bool:
  """
  Determine if the customer is allowed to purchase the specified item.

  Returns True if the customer is not allowed to purchase the specified item, False otherwise.

  Args:
      item (Item): The item being purchased.
      customer (Customer): The customer attempting the purchase.
      purchase_date_string (str): The purchase date in the format dd/mm/yyyy.

PyDocstyle

Lizard

Mypy

Flake8

对比汇总表

其实这个项目中出现的错误有很多,但是因为篇幅问题我没有办法一一把示例代码给出,所以在此我制作出一个统计表格,总结下来CI/CD可以检测代码风格进行统一管理,方便于不同工程师之间进行协同时,因为有统一的代码和备注风格所以减少协同成本。

结论和建议

在"megamart"文件中,最常见的问题是"pyflakes ",其次是"pydocstyles "。这些问题大多与代码注释有关。为了解决这些问题,建议保持代码注释简洁,并遵循一致的代码格式标准。如果您正在使用集成开发环境(IDE),请考虑为自动代码格式化配置插件。

最复杂的函数是"checkout",因为它涉及18个逻辑条件,包括检查空值和特定数据类型。

相关推荐
A ?Charis1 天前
Gitlab-runner running on Kubernetes - hostAliases
容器·kubernetes·gitlab
IT-民工211101 天前
CI/CD 实践总结
运维·ci/cd·自动化
秋说1 天前
开源代码管理平台Gitlab如何本地化部署并实现公网环境远程访问私有仓库
gitlab·源代码管理
极小狐2 天前
驭码上新,AI Code Review、基于代码库的知识问答,让研发起飞
gitlab·devsecops·devops·极狐gitlab·安全合规
蚊子不吸吸2 天前
DevOps开发运维简述
linux·运维·ci/cd·oracle·kubernetes·gitlab·devops
老攀呀3 天前
CI/CD 的概念
ci/cd
aklry3 天前
CI_CD
ci/cd
花开莫与流年错_5 天前
GitLab代码仓管理安装配置使用
运维·服务器·git·gitlab·配置·代码仓
flying robot6 天前
GitHub Actions的 CI/CD
ci/cd·github
程序员雷叔6 天前
自动化测试类型与持续集成频率的关系
功能测试·测试工具·jmeter·ci/cd·单元测试·测试用例·postman