代码开发约定以及风格指南

本文主要介绍极狐Gitlab开发相关约定及规范。更多未尽事项请参考 GitLab EE 代码开发约定/规范

使用Lefthook进行pre-push静态代码检查

Lefthook 是一款Git hooks管理工具,极狐Gitlab使用它进行提交前的代码检查工作,从而减少由于代码风格错误造成的pipeline失败。

如果您的lefthook全部执行时间较长,可以在本地lefthook-local.yml选择跳过一些检查,但是强烈建议您保留如下检查: - eslint - haml-lint - stylelint - prettier - rubocop

安装

安装过程请参考 Lefthook安装与配置

Lefthook 配置

参考Lefthook安装与配置 配置完成后。 由于极狐Gitlab源分支名和Gitlab上游不同,所以需要针对lefthook.yml进行本地配置。方式如下: 1. 在gitlab目录下新建lefthook-local.yml文件。 2. 复制lefthook.yml文件全部内容到lefthook-local.yml。 3. 将lefthook-local.yml文件内所有files配置项中的origin/master改为origin/main-jh。 4. 在gitlab目录下重新执行: bash bundle exec lefthook install

note由于danger在本地执行经常失败,且本地执行并无太大意义。所以我们此处选择跳过danger

lefthook-local.yml参考配置如下: ```yml pre-push: parallel: true commands: danger: skip: true eslint: tags: frontend style files: git diff –name-only –diff-filter=d $(git merge-base origin/main-jh HEAD)..HEAD glob: ‘.{js,vue}’ run: yarn run lint:eslint {files} haml-lint: tags: view haml style files: git diff –name-only –diff-filter=d $(git merge-base origin/main-jh HEAD)..HEAD glob: ‘.html.haml’ run: bundle exec haml-lint –config .haml-lint.yml {files} markdownlint: tags: documentation style files: git diff –name-only –diff-filter=d $(git merge-base origin/main-jh HEAD)..HEAD glob: ‘doc/.md’ run: yarn markdownlint {files} stylelint: tags: stylesheet css style files: git diff –name-only –diff-filter=d $(git merge-base origin/main-jh HEAD)..HEAD glob: ‘.scss{,.css}’ run: yarn stylelint {files} prettier: tags: frontend style files: git diff –name-only –diff-filter=d $(git merge-base origin/main-jh HEAD)..HEAD glob: ‘.{js,vue,graphql}’ run: yarn run prettier –check {files} rubocop: tags: backend style files: git diff –name-only –diff-filter=d $(git merge-base origin/main-jh HEAD)..HEAD glob: ‘.rb’ run: REVEAL_RUBOCOP_TODO=0 bundle exec rubocop –parallel –force-exclusion {files} graphql_docs: tags: documentation files: git diff –name-only –diff-filter=d $(git merge-base origin/main-jh HEAD)..HEAD glob: ‘{app/graphql/*/.rb,ee/app/graphql/*/.rb}’ run: bundle exec rake gitlab:graphql:check_docs vale: # Requires Vale: https://docs.gitlab.com/ee/development/documentation/#install-linters tags: documentation style files: git diff –name-only –diff-filter=d $(git merge-base origin/main-jh HEAD)..HEAD glob: ‘doc/.md’ run: if command -v vale 2> /dev/null; then vale –config .vale.ini –minAlertLevel error {files}; else echo “Vale not found. Install Vale”; fi gettext: skip: true # This is disabled by default. You can enable this check by adding skip: false in lefhook-local.yml https://github.com/evilmartians/lefthook/blob/main-jh/docs/full_guide.md#skipping-commands tags: backend frontend view haml files: git diff –name-only –diff-filter=d $(git merge-base origin/main-jh HEAD)..HEAD | while read file;do git diff –unified=1 $(git merge-base origin/main-jh HEAD)..HEAD $file | grep -Fqe ‘_(‘ && echo $file;done; true glob: “.{haml,rb,js,vue}” run: bin/rake gettext:updated_check docs-metadata: # See https://docs.gitlab.com/ee/development/documentation/#metadata tags: documentation style files: git diff –name-only –diff-filter=d $(git merge-base origin/main-jh HEAD)..HEAD glob: ‘doc/*.md’ run: scripts/lint-docs-metadata.sh {files}


### 故障排除
当您使用`Lefthook`出现问题时,可以尝试如下方式:
1. 在`gitlab`目录下执行:
```bash
lefthook uninstall
  1. 从远端还原lefthook.yml文件。
  2. 重新安装lefthook