Repository navigation
Closed
Description
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
Labels
No labels
Activity
nejch commentedon Sep 17, 2020
I personally have no way of testing this as it's an EE feature, but the fact that it's returning None only on project creation makes me think the webhook is fired before GitLab has fully configured the project (an asynchronous task, probably).
Can you try to give a generous
sleep()beforepushrules.get()and see if that helps? If that works, then you can find more graceful ways of handling it.L30Bola commentedon Sep 25, 2020
Hmm, sorry for the delay on giving an answer. So, I was wrong on this working on recently-created repositories. I tried creating one and waiting for it to be "workable". Even with the sleep and I still got the error, Push Rules is returning None on repository creation.
nejch commentedon Sep 25, 2020
Ah ok. Heh, then it's probably this:
python-gitlab/gitlab/mixins.py
Lines 69 to 72 in c161852
I guess the response from a plain request with
curl/requestsis empty as well, rather than a 404 response like with some other get endpoints (I can't try myself)?It'd be good then to check the rest of the codebase for similar scenarios to see if this is actually the desired behavior for
GetWithoutIdMixinsince some other people might be using it this way, and simply create the resource when it does not exist. e.g.L30Bola commentedon Sep 25, 2020
YAY! I changed the original function to this:
And now it works! Thanks for the help.