【问题标题】:Bitbucket: Enforce merge-only by pull request on branch workflowBitbucket:通过对分支工作流程的拉取请求强制仅合并
【发布时间】:2015-03-20 21:05:18
【问题描述】:

我们的团队使用 Bitbucket 托管 Mercurial 存储库。我们有一个单一的仓库,它在配置它们时被克隆到我们的开发虚拟机上。我们使用一个相当典型的功能分支 -> 拉取请求 -> 审查 -> 将功能合并到 PR 工作流程中的 default

我们想要的是:能够限制无法通过命令行推送到default 分支的内容(以避免意外提交到该分支)。即 - 我们希望强制执行它,以便修改 default 的唯一方法是通过拉取请求。

请注意,由于虚拟机设置,分叉并不是一个真正的选项(我们必须增加虚拟机配置的复杂性才能进行分叉,并在配置的虚拟机上设置所有这些,即使这样也只是意味着当有人不小心推送到default 时,他们只是在搞砸他们的分叉)。

Branch Restrictions 看起来很有希望,虽然我们可以将其设置为没有人可以通过命令行推送,但这意味着只有一个命名用户或组可以进行 PR 的实际合并(我们不希望,理想情况下,团队中的任何人都可以合并,只需通过 Bitbucket PR)。

这可能吗?有什么建议吗?

【问题讨论】:

    标签: mercurial push bitbucket


    【解决方案1】:

    所以我最终用 Mercurial 钩子解决了这个问题。具体来说,我创建了以下文件,我将其命名为 prevent_default_push.py 并放在我的克隆的 .hg 目录中。

    # branches to prevent being pushed to by command line.
    # Separate branches by spaces
    restricted_branches = "default".lower().split()
    
    def branch_name(repo):
        return repo[None].branch().lower()
    
    def is_restricted(branch):
        return branch in restricted_branches
    
    def prevent_default(ui, repo, *args, **kwargs):
        if is_restricted(branch_name(repo)): 
            print("Preventing push to default branch")
            return True
        return False
    
    def prevent_commit(ui, repo, *args, **kwargs):
        branch = branch_name(repo)
        if is_restricted(branch): 
            print("You're on a restricted branch (%s), are you sure you want to commit? [YN]" % branch)
            res = raw_input().strip().lower()
            return res != "y"
        return False
    

    然后编辑.hg/hgrc 文件以使用这些钩子:

    [hooks]
    pre-push = python:.hg/prevent_default_push.py:prevent_default 
    pre-commit = python:.hg/prevent_default_push.py:prevent_commit
    

    然后,当您尝试在 default 分支上进行提交时,您会收到确认提示,如果您尝试向 default 推送,则会直接被拒绝。

    示例运行:

    $ hg commit
    You're on a restricted branch (default), are you sure you want to commit? [YN]
    n
    abort: pre-commit hook failed
    

    其中“n”是我输入的内容。

    这样做的好处是,虽然您的 .hg 目录中的内容不受版本控制(因此克隆不会得到它),但我们可以将这些钩子自动放入我们的配置机制中放置在已配置的 VM 上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      • 2020-08-16
      • 2021-12-26
      • 1970-01-01
      相关资源
      最近更新 更多