gitlab分支名包含/符号导致无法解除protected_branches

发布时间 2023-03-29 11:26:45作者: 东方树

背景:项目中使用gitflow管理分支,jenkins打包时会暂时锁定打包分支避免提交

锁定:

curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches?name=feature/chat&push_access_level=30&merge_access_level=30&unprotect_access_level=40"

解锁:

curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches/feature/chat"

查看状态:

curl --request GET --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches/feature/chat"

锁定和查看都没有问题。当解锁时发现一直返回404 not found
原因:gitlab将/识别为路径分隔符,而不是分支名,使用url编码,查表,用%2F替换/

branchName="feature/chat"
replaceBranchName=$(echo "$branchName" | sed 's/\//%2F/g')
curl --request DELETE --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/protected_branches/${replaceBranchName}"