GitLab提交时检测用户名和邮箱
GitLab 限制 commit 提交时用户和邮箱一致才能 push
GitLab 使用 Server Hooks 校验 Commit 用户名与邮箱,只有 username
与 email
与 GitLab 中的一致才允许 push,否则拒绝 push 并提示修改。
1、建立 Hook 脚本
- 进入 GitLab 容器:
docker exec -it gitlab bash
- 创建 Hook 脚本存放目录:
mkdir -p /var/opt/gitlab/gitaly/custom_hooks/pre-receive.d
- 进入该目录:
cd /var/opt/gitlab/gitaly/custom_hooks/pre-receive.d
- 创建并编辑
commit-current-user-check.sh
脚本:
vi commit-current-user-check.sh
脚本内容如下:
#!/usr/bin/env bash
# 访问 GitLab API 所需的 token, 需使用 admin 用户生成, scope 选择 read_user 即可# https://docs.gitlab.com/ee/user/profile/personal_access_tokens.htmlTOKEN="GitLab admin user read_user TOKEN"
# GitLab 服务的访问地址, 因为该脚本是放置在 GitLab 服务中, 所以使用本机地址即可GITLAB_URL=http://127.0.0.1
# 使用 python 提取 json 中的 name 和 email 代码get_name_and_email="import sys, json;try: obj=json.load(sys.stdin); print(obj[0]['name']+':'+obj[0]['email'])except: print('error')"
# 访问 GitLab Users API 获取用户显示名称与 email# GL_USERNAME 为 GitLab 的 username, 一般为英文, 而 commit 中的 username 我们一般设置为中文# API 返回的数据为 json 格式, 通过 python 代码进行提取显示名称与 email# 因为显示名称为中文, 为了解决乱码问题, 配置 PYTHONIOENCODING='UTF-8'# python 返回的格式为 name:email# https://docs.gitlab.com/ee/api/users.html#list-usersGITLAB_NAME_EMAIL=`curl -s --header "Private-Token: ${TOKEN}" "${GITLAB_URL}/api/v4/users?username=${GL_USERNAME}" | PYTHONIOENCODING='UTF-8' python3 -c "${get_name_and_email}"`
if [ "${GITLAB_NAME_EMAIL}" == "error" ]; then echo "Push 异常: GitLab 获取用户信息异常, 请通知管理员进行排查" exit 1fi
# 截取显示名称GITLAB_USER_NAME=${GITLAB_NAME_EMAIL%:*}# 截取 emailGITLAB_USER_EMAIL=${GITLAB_NAME_EMAIL#*:}
zero_commit="0000000000000000000000000000000000000000"
excludeExisting="--not --all"
while read oldrev newrev refname; do # branch or tag get deleted if [ "$newrev" = "$zero_commit" ]; then continue fi
# Check for new branch or tag if [ "$oldrev" = "$zero_commit" ]; then span=`git rev-list $newrev $excludeExisting` else span=`git rev-list $oldrev..$newrev $excludeExisting` fi
for COMMIT in $span; do AUTHOR_USER=`git log --format=%an -n 1 ${COMMIT}` AUTHOR_EMAIL=`git log --format=%ae -n 1 ${COMMIT}` COMMIT_USER=`git log --format=%cn -n 1 ${COMMIT}` COMMIT_EMAIL=`git log --format=%ce -n 1 ${COMMIT}`
# 进行 username 与 email 校验 # 在 GitHub 的示例脚本中启用了 AUTHOR_USER 与 AUTHOR_EMAIL 校验, 但是使用时可能存在 author 与 committer 不是同一个人的情况, 故注释校验 AUTHOR_USER 与 AUTHOR_EMAIL 的代码 # 如果自己公司实际使用时不存在这种情况, 可以取消注释 # author 与 committer 区别 https://stackoverflow.com/q/6755824
# if [[ ${AUTHOR_USER} != ${GITLAB_USER_NAME} ]]; then# echo -e "Push 异常: ${COMMIT} 的 author (${AUTHOR_USER}) 不是 GitLab 中的中文名 (${GITLAB_USER_NAME})"# exit 20# fi
if [[ ${COMMIT_USER} != ${GITLAB_USER_NAME} ]]; then echo -e "Push 异常: ${COMMIT} 的 committer (${COMMIT_USER}) 不是 GitLab 中的中文名 (${GITLAB_USER_NAME})" exit 30 fi
# if [[ ${AUTHOR_EMAIL} != ${GITLAB_USER_EMAIL} ]]; then# echo -e "Push 异常: ${COMMIT} 的 author 的邮箱 (${AUTHOR_EMAIL}) 不是 GitLab 中的邮箱 (${GITLAB_USER_EMAIL})"# exit 40# fi
if [[ ${COMMIT_EMAIL} != ${GITLAB_USER_EMAIL} ]]; then echo -e "Push 异常: ${COMMIT} 的 committer 的邮箱 (${COMMIT_EMAIL}) 不是 GitLab 中的邮箱 (${GITLAB_USER_EMAIL})" exit 50 fi donedone
exit 0
- 赋予脚本执行权限并设置所有者:
chmod u+x commit-current-user-check.shchown git:git commit-current-user-check.sh
2、修改配置
- 编辑
gitlab.rb
文件:
vi /etc/gitlab/gitlab.rb
添加以下内容:
gitaly['configuration'] = { hooks: { custom_hooks_dir: '/var/opt/gitlab/gitaly/custom_hooks', },}
- 重新配置 GitLab:
gitlab-ctl reconfigure
3、提交代码
- 设置 Git 用户名和邮箱(必须与 GitLab 后台用户邮箱一致才能 push):
git config --global user.name "gaojinbo"git config --global user.email "admin@gaojinbo.com"
- 编辑全局配置(可选):
git config --global --edit
- 重置提交的文件为新的用户和邮箱(重要):
git commit --amend --reset-author
- 推送代码:
git push
TOP