如何编写自定义GitLab批准规则?

发布于 2025-01-30 17:18:27 字数 467 浏览 3 评论 0 原文

我的团队正在使用Gitlab Premium。在项目设置中,根据合并请求批准,有一个批准规则部分。在第一行中,我们将所需的批准编号提高到1,以便每个合并请求必须至少由其他一个队友批准。到目前为止,一切都很好。

我可以看到,还有一个许可检查规则和 coverage-check-check 规则,可以启用,而他们所做的事情是相当不言自明的。还有一个添加批准规则按钮,您可以在其中为新规则设置名称和批准者,但是您实际上无法描述/选择实际规则应该是什么。我能找到的所有文档只需说明如何单击该UI即可。

您如何实际设置新规则,并且可以编写自己的自定义规则吗?

例如,我们想设置一个规则,只有团队的一个子集才能批准“大”合并请求(其中“大”是由更改的行数定义的,如GIT日志所报告)。我想这将类似于许可检查覆盖范围检查,但由我们写。有可能吗?

My team is using GitLab Premium. In the project settings, under Merge request approvals, there is an Approval rules section. In the first row, we increased the Approvals required number to 1 so that every merge request must be approved by at least one other teammate. So far so good.

I can see there is also a License-Check rule and Coverage-Check rule that can be enabled and what they do is pretty self-explanatory. There is also an Add approval rule button where you can set a name and approvers for a new rule, but you can't actually describe/choose what the actual rule should be. All the documentation I could find simply explains how to click around that UI.

How do you actually set new rules and is it possible to write own custom rules?

For example, we'd like to set a rule that only a subset of the team can approve "large" merge requests (where "large" is defined by the number of lines changed, as reported by git log). I imagine that would be something similar to License-Check or Coverage-Check, but written by us. Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

◇流星雨 2025-02-06 17:18:27

我认为这是不可能的。目前,批准规则是特定于分支机构的。合并请求的内容无关紧要。

但是,我认为您有一个解决方案:GitLab允许您编写可以访问Gitlab API的机器人。您需要的是聆听新合并请求的外部服务。然后,您的工具将不得不以某种方式找出实际进行了多少个更改(合并请求api )。根据此的不同,您可以通过API通过API进行相应的MR()。

这种方法对您有帮助吗?

您也可以在

I don't think this is possible yet. For now, approval rules are branch-specific. The content of a merge request doesn't matter.

Nevertheless, I think there is a solution for you: GitLab allows you to write bots that can access the GitLab API. What you'd need is an external service listening to new merge requests. Your tool would then have to find out in some way how many changes were actually made (this is not yet possible with the Merge Request API as far as I can tell). Depending on this, you can then adjust the approval rules via the API for the corresponding MR (API reference).

Does this approach help you?

You can also ask a question in the GitLab Community Forum, there you might get better help.

和影子一齐双人舞 2025-02-06 17:18:27

现在,我了解添加批准规则的重点(仅定义给定分支的一组批准者,无非是)。

实际上,批准规则将适用于分支机构,或所有分支机构,或者,因为 gitlab 15.3 (2022年8月):

添加所有受保护分支的批准规则

您现在可以创建一个并将其应用于您的保护分支
项目。
这是一个很好的改进,可让您更有选择地应用粒度的合规性控制。

以前,添加MR批准规则将其应用于所有分支机构。这是确保在执行适当工作流程之前的好方法
代码达到生产,但这也意味着特征分支,短期分支或实验分支的MRS都必须使用相同
工作流程。
这可能会减慢不打算致力于受保护的分支机构并且可能不需要相同水平的开发人员
工作流执行。

为受保护分支机构制定MR批准规则,使您可以确信您依赖的敏感分支将具有适当的
在不需要相同控制水平的其他分支机构的发展方面,适用于他们的工作流程。

“

请参阅 and 问题

但这与其他规则不同,这些规则无法轻易通过批准规则复制。


gitlab 15.9 (2023年2月)提供了一种新选择,但仅用于高级或最终:

需要代码所有者的多次批准

您现在可以精确定义哪些文件,文件类型或目录批准
已被指定为可选的,需要一位用户或多个用户批准。
后者是 Coleller的新改进文件。

到目前为止,如果您出于合规原因或其他原因需要多个批准者,
您只能在批准规则中这样做。

但是,与代码所有者批准不同,
批准规则适用于整个分支机构,不能完善以适用于特定
代码库的一部分。因此,也需要多次批准
对于不需要高水平审查导致不必要评论的更改。

参见 and evarese

请注意,拒绝许可检查(在gitlab 16.0+中删除)。
Gitlab Ultimate提出

Now I understand the point of Add approval rule (it's only to define a set of approvers for a given branch, nothing more than that).

Actually, an Approval Rule would apply to a branch, or all branches or, since GitLab 15.3 (August 2022):

Add approval rules for all protected branches

You can now create an MR approval rule and apply it to only protected branches in your
project.
This is a great improvement that allows you to more selectively apply compliance controls with increased granularity.

Previously, adding an MR approval rule would apply it to all branches. This was a great way to ensure that proper workflows were enforced before
code reached production, but it also meant that MRs for feature branches, short-lived branches, or experimental branches all had to use the same
workflow.
This could slow down developers that did not intend to commit to protected branches and who likely did not need the same level
of workflow enforcement.

Creating MR approval rules for protected branches allows you to be confident that the sensitive branches you depend on will have proper
workflows applied to them while not slowing down development on other branches that do not need the same level of control.

https://about.gitlab.com/images/15_3/manage-all-protected-branches.png -- Add approval rules for all protected branches

See Documentation and Issue.

But that differs other rules, which cannot easily be replicated through Approval rules.


GitLab 15.9 (February 2023) provides a new option, but only for Premium or Ultimate:

Require multiple approvals from Code Owners

You can now precisely define for which files, file types, or directories approval
has been designated as optional, required approval by one user, or by multiple users.
The latter being the new improvement of the CODEOWNERS file.

So far, if you needed to require multiple approvers be it for compliance reasons or other reasons,
you could only do so with an approval rule.

However, unlike Code Owner approvals,
approval rules apply to entire branches and cannot be refined to apply to specific
parts of your code base. So, the multiple approvals would have also been required
for changes that do not need a high level of scrutiny leading to unnecessary reviews.

https://about.gitlab.com/images/15_9/require_multiple_approvals_from_codeowners.png -- Require multiple approvals from Code Owners

See Documentation and Issue.

Note that License-Check is deprecated (to be removed in GitLab 16.0+).
GitLab Ultimate proposes flexible license approval policies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文