Gitlab 12.X - Get project id from storage path in a pre-receive hook

情到浓时终转凉″ 提交于 2021-01-29 11:26:03

问题


With legacy storage, when a pre-receive hook fires, it is easy to guess the URL of the repository involved as the path is "group/project-name.git".

With new hashed storage in Gitlab, the "pwd" command will return a path such as d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35.git if the project id is 2.

=> How can I get the project id based on the hashed path ?

My aim is to check that all the commits being pushed by someone are authored by people who are actually authorized to write inside that project. I cannot do so if I do not know which project is involved. Although I can launch the "gitlab-rails console" and used "ProjectRepository.find_by", it is not practical to use that script in the context of a hook.

There are Push Rules that allows to do that. However, Push Rules are not free and are not available in Gitlab CE.

Also, from an admin point of view, it is important to be able to find quickly the project id of a repository based on its repository storage path without necessarily having to launch the "gitlab-rails console".


回答1:


There is an environment variable available to the pre-receive hook - GL_REPOSITORY which will have a value like project-123. This is the project ID.



来源:https://stackoverflow.com/questions/57379968/gitlab-12-x-get-project-id-from-storage-path-in-a-pre-receive-hook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!