gitlab CI管道以检查代码格式

问题描述 投票:2回答:1

我正在我的多个项目中安装google-java-formatter

但是在提交代码时,我希望CI提示行检查格式是否已首先完成。

[我知道我可以使用根目录中的.gitlab-ci.yml文件来完成此操作,但是我不确定如何实现检查所有文件格式是否正确的目标,谁能帮助我解决如何使用请google-java-formattergitlab

gitlab-ci pipeline githooks pre-commit-hook linter
1个回答
1
投票

google-java-formatter用于代码格式化

由于google-java-formatter对代码进行了修改(格式化),所以它更改了要提交的代码。

请参见google-java-format源代码:

[google-java-format是重新格式化Java源代码以符合Google Java Style的程序。

所以,您需要一个pre-commit钩子。

例如您可以使用pre-commit-一种用于管理和维护多语言预提交挂钩的框架。

示例文件,您可以看到here

。pre-commit-hooks.yaml:


- id: eclipse-formatter
  name: Eclipse Java Formatter
  description: This hook formats Java code with the Eclipse formatter.
  entry: eclipse-formatter
  language: python
  types:
    - java
- id: google-java-formatter
  name: Google Java Formatter
  description: This hook formats Java code with Google Java Formatter.
  entry: google-java-formatter
  language: python
  types:
    - java

如果您确实想将git钩子与GitLab集成在一起,请尝试创建自定义GitLab Server hook

linters vs formatters

正如您所说:

提交代码时,我希望CI管道检查格式是否已首先完成。

您问的是-checkstyle linting不是formatting因此,要检查格式化是否已完成,可以使用许多不同的短绒。

from this answer

  • [SpotBugs(先前为Findbugs),用于查找现有错误。很好!
  • [PMD用于查找可能导致错误的模式(例如,未使用的变量)]
  • [Checkstyle以实施编码标准和约定(例如,空白,Javadoc)]
  • [Error Prone与应用程序的编译步骤挂钩
  • 所以,让checkstyle

[这方面有很多指南,例如:GitLab CI/CD Pipeline for Maven-Based Applications – The Blog of Ivan Krizsan

也有带有[checkstyle的250+ samples of .gitlab-ci.yml

.gitlab-ci.yml    
© www.soinside.com 2019 - 2024. All rights reserved.