Skip to content

Merge request approval setting is not working #2071

Closed as not planned
Closed as not planned

Description

@naragani

I am trying with following code snippet , but it does not seems to working . I am trying to add new approval rule and add user ids as approvers.

        repo_id=2345
        project = gl.projects.get(repo_id)
        p_mras = project.approvals.get()
        p_mras.approvals_before_merge = 1
        p_mras.reset_approvals_on_push = 1
        p_mras.disable_overriding_approvers_per_merge_request = 0
        p_mras.merge_requests_author_approval = 1
        p_mras.only_allow_merge_if_all_discussions_are_resolved = 1
        p_mras.save()

        
        project.approvals.set_approvers(approver_ids=[9355])

        project.approvals_before_merge = 1
        project.reset_approvals_on_push = 1
        project.disable_overriding_approvers_per_merge_request = 0
        project.merge_requests_author_approval = 1
        project.only_allow_merge_if_all_discussions_are_resolved = 1
        project.save()

Please let me know what I am doing wrong

Activity

  1. added
    EEIssues related to the enterprise version of GitLab
    on Jun 27, 2022
  2. idwagner commented on Aug 30, 2022

    @idwagner

    @naragani I think what you are looking for is project.approvalrules for approval rules.

    I think ProjectApprovalManager.set_approvers may be a bug, as I can't find any reference to the api path /projects/:id/approvers in the Gitlab docs.

  3. nejch commented on Aug 30, 2022

    @nejch
    Member

    @idwagner I think you're right, some time ago one of the methods was updated to match the v4 API (#1005) but not for this one. It seems like this endpoint has been long gone in GitLab's API so we should remove it or change it.

  4. nightHawk13998 commented on Oct 24, 2022

    @nightHawk13998

    I verified that this should be approvalrules and not approval_rules. According to the documentation if you try this it will fail because its hitting ProjectApprovalManager.set_approvers:

    mr.approval_rules.create({
        "name": "my MR custom approval rule",
        "approvals_required": 2,
        "rule_type": "regular",
        "user_ids": [105],
        "group_ids": [653, 654],
    })
    

    But this works correctly and will create the MR approval rule:

    mr.approvalrules.create({
        "name": "my MR custom approval rule",
        "approvals_required": 2,
        "rule_type": "regular",
        "user_ids": [105],
        "group_ids": [653, 654],
    })
    
  5. crizzs commented on Feb 15, 2023

    @crizzs

    i am also having issues with setting merge approvals.

  6. nejch commented on Feb 15, 2023

    @nejch
    Member

    @crizzs if you read through this thread you'll see you can use mr.approvalrules.create() instead. We should update our docs a bit probably.

  7. jurschel commented on Mar 13, 2023

    @jurschel

    How do you do this if your trying to write something that only modifies a project level merge request approver rule. We have rules for merging to master and I need to update them across hundreds of projects. Is it not possible? I don't see an example in the docs for project user id changes.

  8. aamarques commented on Nov 14, 2023

    @aamarques

    How do you do this if your trying to write something that only modifies a project level merge request approver rule. We have rules for merging to master and I need to update them across hundreds of projects. Is it not possible? I don't see an example in the docs for project user id changes.

    You can't see because there isn't. I don't know why they have implemented this and stay in Epic for years. So you MUST apply for each project individually.

    Message from Gitlab support Team
    "This feature is actually being tracked in a larger epic that can be found here: https://gitlab.com/groups/gitlab-org/-/epics/4367. It expresses much of the same frustration and difficulty associated with managing this setting on a project by project basis. I know this may not be the most ideal solution since it would/will requires waiting for the feature to be implemented. Alternatively you could utilize the Merge request approvals API to programmatically create these project-level rules as available in the API documentation: https://docs.gitlab.com/ee/api/merge_request_approvals.html#create-project-level-rule."

  9. aamarques commented on Nov 14, 2023

    @aamarques

    I verified that this should be approvalrules and not approval_rules. According to the documentation if you try this it will fail because its hitting ProjectApprovalManager.set_approvers:

    mr.approval_rules.create({
        "name": "my MR custom approval rule",
        "approvals_required": 2,
        "rule_type": "regular",
        "user_ids": [105],
        "group_ids": [653, 654],
    })
    

    But this works correctly and will create the MR approval rule:

    mr.approvalrules.create({
        "name": "my MR custom approval rule",
        "approvals_required": 2,
        "rule_type": "regular",
        "user_ids": [105],
        "group_ids": [653, 654],
    })
    

    Ok, but how to do it only for project-level rule ?
    I tried without success. :(

  10. kalavt commented on Apr 17, 2025

    @kalavt

    any update on this?

    I can see there's only ProjectMergeRequestApprovalManager.set_approvers method
    but no ProjectApprovalManager.set_approvers

    hence there's no way to update project level merge request approval rules.

  11. kalavt commented on Apr 17, 2025

    @kalavt

    Oh, sorry, there do have a workaround, call the gitlab API directly

        def update_gitlab_project_approvalrules(
            self, 
            project_path: str, 
            rule_id: int, 
            name: str = None,
            approvals_required: int = None, 
            user_ids: list[int] = None,
            group_ids: list[int] = None,
            applies_to_all_protected_branches: bool = None, 
            protected_branch_ids: list[int] = None
        ):
            """
            Update GitLab project approval rules.
            """
            project = self.gitlab.projects.get(project_path)
            rule = project.approvalrules.get(rule_id)
    
            # Build the parameters dynamically
            params = {
                key: value for key, value in {
                    "name": name,
                    "approvals_required": approvals_required,
                    "user_ids": user_ids,
                    "group_ids": group_ids,
                    "applies_to_all_protected_branches": applies_to_all_protected_branches,
                    "protected_branch_ids": protected_branch_ids,
                }.items() if value is not None
            }
    
            if not params:
                raise ValueError("No parameters provided to update the approval rule.")
    
            # Use HTTP PUT to update the approval rule
            return self.gitlab.http_put(f'{self.gitlab.url}/api/v4/projects/{project.id}/approval_rules/{rule.id}', post_data=params)
    
    
  12. github-actions commented on Jul 9, 2025

    @github-actions

    This issue was marked stale because it has been open 60 days with
    no activity. Please remove the stale label or comment on this
    issue. Otherwise, it will be closed in 15 days.

    As an open-source project, we rely on community contributions to
    address many of the reported issues. Without a proposed fix or
    active work towards a solution it is our policy to close inactive
    issues. This is documented in CONTRIBUTING.rst

    How to keep this issue open:

    • If you are still experiencing this issue and are willing to
      investigate a fix, please comment and let us know.
    • If you (or someone else) can propose a pull request with a
      solution, that would be fantastic.
    • Any significant update or active discussion indicating progress
      will also prevent closure.

    We value your input. If you can help provide a fix, we'd be happy
    to keep this issue open and support your efforts.

    This is documented in CONTRIBUTING.rst
    https://github.com/python-gitlab/python-gitlab/blob/main/CONTRIBUTING.rst

  13. github-actions commented on Jul 24, 2025

    @github-actions

    This issue was closed because it has been marked stale for 15 days
    with no activity.

    This open-source project relies on community contributions, and
    while we value all feedback, we have a limited capacity to address
    every issue without a clear path forward.

    Currently, this issue hasn't received a proposed fix, and there
    hasn't been recent active discussion indicating someone is planning
    to work on it. To maintain a manageable backlog and focus our
    efforts, we will be closing this issue for now.

    This doesn't mean the issue isn't valid or important. If you or
    anyone else in the community is willing to investigate and propose
    a solution (e.g., by submitting a pull request), please do.

    We believe that those who feel a bug is important enough to fix
    should ideally be part of the solution. Your contributions are
    highly welcome.

    Thank you for your understanding and potential future
    contributions.

    This is documented in CONTRIBUTING.rst
    https://github.com/python-gitlab/python-gitlab/blob/main/CONTRIBUTING.rst

  14. locked as resolved and limited conversation to collaborators on Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    EEIssues related to the enterprise version of GitLabhelp wantedstale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions