简体   繁体   中英

How to get group and repository name in server-side Gitlab global hooks starting with 13.1

My organization uses Gitlab CE and server-side git hooks. Specifically we use global pre-recieve hooks to validate commit messages (the one placed in /opt/gitlab/embedded/service/gitlab-shell/hooks/pre-receive.d ). Validation rules are different for different Gitlab groups, so the script parses the full path to the repository and based on the group applies different rules.

We used to get the full path to the repository, including group, using PWD environmental variable in the past. But it looks like starting with Gitlab 13.1 this variable is no longer available. Also since new repositories are created in @hashed folder this approach anyway needs to be adjusted.

We've tried to use git config --get gitlab.fullpath in the pre-recieve hook and so far it works fine for all repositories except wiki repositories (like <project_name>.wiki.git ). Also it's not a documented approach, so it might break again in the future. Is there any official way of getting the group name in pre-recieve hook?

UPD: it looks like there is a workaround using gitlab-rails, but it's far from optimal:

  1. GIT_OBJECT_DIRECTORY environment variable contains the path like /var/opt/gitlab/git-data/repositories/@hashed/dd/8e/dd8e8c8c9dae8978f122d7bcf3d0d49f6a0e86b9fc35528f55e78f7408927bb1.git/objects/incoming-16WBe9 which is a directory inside the repo directory
  2. Extract the repo hash from the path above and use the following command to lookup the full path: gitlab-rails runner "puts ProjectRepository.find_by(disk_path: '@hashed/dd/8e/dd8e8c8c9dae8978f122d7bcf3d0d49f6a0e86b9fc35528f55e78f7408927bb1').project.full_path"

But the issue with this approach is that GIT_OBJECT_DIRECTORY might get changed in future and that gitlab_rails command takes ~30 seconds to execute on our installation, which is unacceptably long

UPD2: there is GL_REPOSITORY environment variable with the project ID , that can be used to find out the full path: https://stackoverflow.com/a/57399790/13881221

UPD3 2020-07-07: there is GL_PROJECT_PATH environment variable included starting with Gitlab 13.1.3 that resolves the issue https://gitlab.com/gitlab-org/gitaly/-/merge_requests/2313

Actually GL_PROJECT_PATH has this format:
group/subgroup/repo_name
You can customize update hook on git server and from there, use group or subgroup.
For example, you can use regex to get group name like this:
cut -d'/' -f1 <<< $GL_PROJECT_PATH
more information about git hooks: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM