Skip to content

Project Push Rules get method returning NoneType #1192

Closed
@L30Bola

Description

@L30Bola

Description of the problem, including code/CLI snippet

I have this function

def set_project_push_rules_configuration(gitlab, project_id, **kwargs):
    """
    On kwargs you pass arguments that are mutable using the Gitlab Project's Push Rules API
    Documentation on that: https://docs.gitlab.com/ee/api/projects.html#push-rules
    """

    defaults = {
        "prevent_secrets": True,
    }
    defaults.update(kwargs)
    project = gitlab.projects.get(project_id)
    project_push_rules = project.pushrules.get()
    project_push_rules = set_attrs(project_push_rules, defaults)
    try:
        project_push_rules.save()
    except GitlabUpdateError:
        exception(
            "Couldn't update "
            + project.name
            + "'s (ID: "
            + str(project_id)
            + ") Push Rules configurations!"
        )

The above function is available on a Flask App that receives all Gitlab System Hooks and is triggered when a repository is created (the project_create event), alongside some other functions:

 if request_json["event_name"] == "project_create" and request_json[
        "path_with_namespace"
    ].startswith("infrastructure/"):
        set_project_configuration(gl, request_json["project_id"])
        remove_project_approval_rules(gl, request_json["project_id"])
        set_project_merge_request_approvals_configuration(
            gl, request_json["project_id"]
        )
        create_basic_directory_structure(gl, request_json["project_id"])
        set_project_master_branch_protected(gl, request_json["project_id"])
        set_project_push_rules_configuration(gl, request_json["project_id"])

Expected Behavior

This project_push_rules = project.pushrules.get(), should return a <ProjectPushRules> object.

Actual Behavior

When I try on an already created repository, the above code works. But from the system hook project_create event, the line that gets the repository Push Rules (project_push_rules = project.pushrules.get()) return None, which then leads to AttributeError: 'NoneType' object has no attribute 'prevent_secrets'.

Specifications

  • python-gitlab version: 2.5.0
  • API version you are using (v3/v4): v4
  • Gitlab server version (or gitlab.com): 13.3.5-ee

The project Pipfile:

[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[requires]
python_version = "3.8"

[packages]
python-gitlab = "==2.5.0"
requests = "*"
Flask = "==1.1.2"
uwsgi = "==2.0.19.1"
Jinja2 = "==2.11.2"
PyYAML = "==5.3.1"

[dev-packages]
pylint = "*"
pytest = "*"
coverage = "*"
black = "==20.8b1"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions