gitlab CI 管道检查代码格式

Bro*_*ind 4 pipeline pre-commit-hook githooks gitlab-ci linter

我希望google-java-formatter在我的几个项目中进行安装。

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

我知道我可以使用.gitlab-ci.yml根目录中的文件来完成此操作,但是我非常不确定如何实现检查所有文件格式是否正确的目标,任何人都可以帮助我解决如何执行此操作的google-java-formatter问题gitlab

Yas*_*sen 6

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

\n

自从google-java-formatter对代码进行了修改(格式化),因此它会更改要提交的代码。

\n

根据google-java-format源代码:

\n
\n

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

\n
\n

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

\n

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

\n

您可以在此处查看示例文件

\n

.pre-commit-hooks.yaml:

\n
\n- id: eclipse-formatter\n  name: Eclipse Java Formatter\n  description: This hook formats Java code with the Eclipse formatter.\n  entry: eclipse-formatter\n  language: python\n  types:\n    - java\n- id: google-java-formatter\n  name: Google Java Formatter\n  description: This hook formats Java code with Google Java Formatter.\n  entry: google-java-formatter\n  language: python\n  types:\n    - java\n
Run Code Online (Sandbox Code Playgroud)\n

如果您确实想将 git hooks 与 集成GitLab,请尝试创建自定义GitLab Server hook

\n

linter 与格式化程序

\n

如你所说:

\n
\n

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

\n
\n

你在问什么 -checkstyle linting不是formatting n所以,要检查格式化是否已经完成,你可以使用一些 linter。

\n

来自这个答案

\n
    \n
  • SpotBugs(早期的Findbugs)用于查找现有错误。非常好!
  • \n
  • PMD用于查找可能导致错误的模式(例如未使用的变量)
  • \n
  • Checkstyle强制执行编码标准和约定(例如空格、Javadoc)
  • \n
  • 容易出错直接挂钩到应用程序的编译步骤
  • \n
\n

checkstyle短绒的使用

\n

有很多关于这方面的指南,例如:GitLab CI/CD Pipeline for Maven-Based applications \xe2\x80\x93 Ivan Krizsan 的博客

\n

此外,还有250 多个带有 `checkstyle.js 的示例.gitlab-ci.yml

\n
\n- id: eclipse-formatter\n  name: Eclipse Java Formatter\n  description: This hook formats Java code with the Eclipse formatter.\n  entry: eclipse-formatter\n  language: python\n  types:\n    - java\n- id: google-java-formatter\n  name: Google Java Formatter\n  description: This hook formats Java code with Google Java Formatter.\n  entry: google-java-formatter\n  language: python\n  types:\n    - java\n
Run Code Online (Sandbox Code Playgroud)\n