github - 有没有办法只允许分支创建者在 Github 中推送它?
问题描述
- 我有一个 Github 组织。
- 我创建了一个存储库和一个团队来处理它。
- 默认情况下,我想将任何分支的推送限制为它的创建者。
- 这样没有人可以推送到别人的分支。
- 除了手动为每个分支添加规则之外,还有什么方法可以做到这一点?
我尝试手动添加规则,但是由于加入团队的人很多,这很耗时。
更新:
- 我去了我的组织-> 团队。
- 选择团队,去找成员,选择成员。
- 单击一个存储库旁边的“管理访问”,单击“编辑”。
- 将“基本权限”设置为“写入”。
- 将“存储库创建”设置为“禁用”。
- 其他一切都未选中。
解决方案
我不会评论这是否是一个好主意。可以说,一旦导致我创建以下更新挂钩以应用于规范回购,我就很生气(但请注意,Github 不允许自定义更新挂钩,所以你在那里不走运):
#!/usr/bin/env python
import os
import re
import sys
from pathlib import Path
from subprocess import check_output
branch = sys.argv[1]
old_commit = sys.argv[2]
new_commit = sys.argv[3]
zero = "0000000000000000000000000000000000000000"
branch_pattern = 'feature/.+'
def get_author(commit):
return str(check_output(['git', '--no-pager', 'show', '-s', "--format='%an'", commit]).strip(), 'utf-8')
def allow(message=None):
if message:
print(message)
exit(0)
def deny(message=None):
if message:
print(message)
exit(1)
# if this isn't a feature branch bail
if not re.match(branch_pattern, branch):
allow()
# if the update is a delete bail
if new_commit == zero:
allow("update: deleting branch '%s': OK" % branch)
# if this is the first commit on the branch bail
if old_commit == zero:
allow("update: creating branch '%s': OK" % branch)
branch_log = Path('.git', 'logs', 'refs', 'heads').joinpath(*branch.split(os.path.sep))
with open(branch_log, 'r') as log:
first_commit = log.readline(81).split(sep=' ', maxsplit=1)[1]
branch_author = get_author(first_commit)
new_commit_author = get_author(new_commit)
print("update: branch = '%s'; branch author = %s; commit author = %s" % (branch, branch_author, new_commit_author))
if new_commit_author == branch_author:
allow("update: commit author == branch author: OK")
else:
deny(
"update: branch author != commit author: REJECTED\n"
"update: create a branch for your changes from the tip of %s and request a pull instead"
% branch
)
推荐阅读
- java - 如何在 Spring Boot 中创建自定义 @PreAuthorize MethodSecurity?
- javascript - 修改函数内全局变量的值
- java - Android Retrofit response.body() 始终为空
- orocommerce - 为 OroCommerce 获取不记名令牌返回错误 405 方法不允许
- python - 在 Numpy 中添加 (2,) 和 (2, 1) 数组
- javascript - 一种使用 node.js 直接对等数据传输的方法?
- spring - spring 功能绑定在spring cloud中将多个输入命名为一个输出
- javascript - 两个孩子有相同的钥匙……反应(离子),虽然钥匙是唯一的
- python - 我正在尝试使用 GUI 进行快速排序,但我的代码已损坏
- android - Android广播接收器在应用程序未运行或未启动时甚至在运行时都不会触发