Skip to content

Commit 2c22a34

Browse files
esabouraudGauvain Pocentek
authored andcommitted
Add project push rules configuration (#520)
1 parent 617aa64 commit 2c22a34

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

docs/gl_objects/projects.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,3 +619,37 @@ markdown to reference the uploaded file::
619619
issue.notes.create({
620620
"body": "See the [attached file]({})".format(uploaded_file["url"])
621621
})
622+
623+
Project push rules
624+
==================
625+
626+
Reference
627+
---------
628+
629+
* v4 API:
630+
631+
+ :class:`gitlab.v4.objects.ProjectPushRules`
632+
+ :class:`gitlab.v4.objects.ProjectPushRulesManager`
633+
+ :attr:`gitlab.v4.objects.Project.pushrules`
634+
635+
* GitLab API: https://docs.gitlab.com/ee/api/projects.html#push-rules
636+
637+
Examples
638+
---------
639+
640+
Create project push rules (at least one rule is necessary)::
641+
642+
project.pushrules.create({'deny_delete_tag': True})
643+
644+
Get project push rules (returns None is there are no push rules)::
645+
646+
pr = project.pushrules.get()
647+
648+
Edit project push rules::
649+
650+
pr.branch_name_regex = '^(master|develop|support-\d+|release-\d+\..+|hotfix-.+|feature-.+)$'
651+
pr.save()
652+
653+
Delete project push rules::
654+
655+
pr.delete()

gitlab/mixins.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ def get(self, id=None, **kwargs):
6666
GitlabGetError: If the server cannot perform the request
6767
"""
6868
server_data = self.gitlab.http_get(self.path, **kwargs)
69+
if server_data is None:
70+
return None
6971
return self._obj_cls(self, server_data)
7072

7173

@@ -317,9 +319,12 @@ def delete(self, id, **kwargs):
317319
GitlabAuthenticationError: If authentication is not correct
318320
GitlabDeleteError: If the server cannot perform the request
319321
"""
320-
if not isinstance(id, int):
321-
id = id.replace('/', '%2F')
322-
path = '%s/%s' % (self.path, id)
322+
if id is None:
323+
path = self.path
324+
else:
325+
if not isinstance(id, int):
326+
id = id.replace('/', '%2F')
327+
path = '%s/%s' % (self.path, id)
323328
self.gitlab.http_delete(path, **kwargs)
324329

325330

gitlab/v4/cli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,9 @@ def _populate_sub_parser_by_class(cls, sub_parser):
143143
action='store_true')
144144

145145
if action_name == 'delete':
146-
id_attr = cls._id_attr.replace('_', '-')
147-
sub_parser_action.add_argument("--%s" % id_attr, required=True)
146+
if cls._id_attr is not None:
147+
id_attr = cls._id_attr.replace('_', '-')
148+
sub_parser_action.add_argument("--%s" % id_attr, required=True)
148149

149150
if action_name == "get":
150151
if gitlab.mixins.GetWithoutIdMixin not in inspect.getmro(cls):

gitlab/v4/objects.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,27 @@ class ProjectPipelineScheduleManager(CRUDMixin, RESTManager):
24592459
('description', 'ref', 'cron', 'cron_timezone', 'active'))
24602460

24612461

2462+
class ProjectPushRules(SaveMixin, ObjectDeleteMixin, RESTObject):
2463+
_id_attr = None
2464+
2465+
2466+
class ProjectPushRulesManager(GetWithoutIdMixin, CreateMixin, UpdateMixin,
2467+
DeleteMixin, RESTManager):
2468+
_path = '/projects/%(project_id)s/push_rule'
2469+
_obj_cls = ProjectPushRules
2470+
_from_parent_attrs = {'project_id': 'id'}
2471+
_create_attrs = (tuple(),
2472+
('deny_delete_tag', 'member_check',
2473+
'prevent_secrets', 'commit_message_regex',
2474+
'branch_name_regex', 'author_email_regex',
2475+
'file_name_regex', 'max_file_size'))
2476+
_update_attrs = (tuple(),
2477+
('deny_delete_tag', 'member_check',
2478+
'prevent_secrets', 'commit_message_regex',
2479+
'branch_name_regex', 'author_email_regex',
2480+
'file_name_regex', 'max_file_size'))
2481+
2482+
24622483
class ProjectSnippetNoteAwardEmoji(ObjectDeleteMixin, RESTObject):
24632484
pass
24642485

@@ -2887,6 +2908,7 @@ class Project(SaveMixin, ObjectDeleteMixin, RESTObject):
28872908
('pipelines', 'ProjectPipelineManager'),
28882909
('protectedbranches', 'ProjectProtectedBranchManager'),
28892910
('pipelineschedules', 'ProjectPipelineScheduleManager'),
2911+
('pushrules', 'ProjectPushRulesManager'),
28902912
('runners', 'ProjectRunnerManager'),
28912913
('services', 'ProjectServiceManager'),
28922914
('snippets', 'ProjectSnippetManager'),

0 commit comments

Comments
 (0)