使用 ggshield 为预提交创建 git-hooks

Creating git-hooks for pre-commit using ggshield

我有一个 android 项目,想要安装预提交。

我正在关注这个例子: https://docs.gitguardian.com/internal-repositories-monitoring/integrations/git_hooks/pre_commit

repos:
  - repo: https://github.com/gitguardian/ggshield
    rev: main
    hooks:
      - id: ggshield
        language_version: python3
        stages: [commit]

我还做了以下事情:

pre-commit autoupdate
Updating https://github.com/gitguardian/ggshield ... already up to date.

但是,当我尝试提交时,我收到以下错误消息:

git commit -am"Updated pre-commit file"
GitGuardian Shield (pre-commit)..........................................Failed
- hook id: ggshield
- exit code: 1

Traceback (most recent call last):
  File "/home/steve/.cache/pre-commit/repol8_kb04j/py_env-python3/bin/ggshield", line 8, in <module>
    sys.exit(cli_wrapper())
  File "/home/steve/.cache/pre-commit/repol8_kb04j/py_env-python3/lib/python3.9/site-packages/ggshield/cmd.py", line 229, in cli_wrapper
    return_code: int = cli.main(standalone_mode=False)
  File "/home/steve/.cache/pre-commit/repol8_kb04j/py_env-python3/lib/python3.9/site-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/home/steve/.cache/pre-commit/repol8_kb04j/py_env-python3/lib/python3.9/site-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/steve/.cache/pre-commit/repol8_kb04j/py_env-python3/lib/python3.9/site-packages/click/core.py", line 1656, in invoke
    super().invoke(ctx)
  File "/home/steve/.cache/pre-commit/repol8_kb04j/py_env-python3/lib/python3.9/site-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/steve/.cache/pre-commit/repol8_kb04j/py_env-python3/lib/python3.9/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/home/steve/.cache/pre-commit/repol8_kb04j/py_env-python3/lib/python3.9/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/steve/.cache/pre-commit/repol8_kb04j/py_env-python3/lib/python3.9/site-packages/ggshield/cmd.py", line 121, in scan
    ctx.obj["client"] = retrieve_client(ctx)
  File "/home/steve/.cache/pre-commit/repol8_kb04j/py_env-python3/lib/python3.9/site-packages/ggshield/utils.py", line 248, in retrieve_client
    raise click.ClickException("GitGuardian API Key is needed.")
click.exceptions.ClickException: GitGuardian API Key is needed.

您必须设置 a gitguardian api key 才能使用他们的服务(它不是免费提供的 public 服务)。 git 钩子和 cli 是 GitGuardian api

的薄包装

如果您关注错误堆栈,您会看到消息

'click.exceptions.ClickException: GitGuardian API Key is needed.'

这意味着您还没有设置 GITGUARDIAN_API_KEY。按照 GitGaurdian documentation here 创建一个并设置它