Skip to content

Add project push rules configuration #520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/gl_objects/projects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -618,3 +618,37 @@ markdown to reference the uploaded file::
issue.notes.create({
"body": "See the [attached file]({})".format(uploaded_file["url"])
})

Project push rules
==================

Reference
---------

* v4 API:

+ :class:`gitlab.v4.objects.ProjectPushRules`
+ :class:`gitlab.v4.objects.ProjectPushRulesManager`
+ :attr:`gitlab.v4.objects.Project.pushrules`

* GitLab API: https://docs.gitlab.com/ee/api/projects.html#push-rules

Examples
---------

Create project push rules (at least one rule is necessary)::

project.pushrules.create({'deny_delete_tag': True})

Get project push rules (returns None is there are no push rules)::

pr = project.pushrules.get()

Edit project push rules::

pr.branch_name_regex = '^(master|develop|support-\d+|release-\d+\..+|hotfix-.+|feature-.+)$'
pr.save()

Delete project push rules::

pr.delete()
11 changes: 8 additions & 3 deletions gitlab/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def get(self, id=None, **kwargs):
GitlabGetError: If the server cannot perform the request
"""
server_data = self.gitlab.http_get(self.path, **kwargs)
if server_data is None:
return None
return self._obj_cls(self, server_data)


Expand Down Expand Up @@ -305,9 +307,12 @@ def delete(self, id, **kwargs):
GitlabAuthenticationError: If authentication is not correct
GitlabDeleteError: If the server cannot perform the request
"""
if not isinstance(id, int):
id = id.replace('/', '%2F')
path = '%s/%s' % (self.path, id)
if id is None:
path = self.path
else:
if not isinstance(id, int):
id = id.replace('/', '%2F')
path = '%s/%s' % (self.path, id)
self.gitlab.http_delete(path, **kwargs)


Expand Down
5 changes: 3 additions & 2 deletions gitlab/v4/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ def _populate_sub_parser_by_class(cls, sub_parser):
action='store_true')

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

if action_name == "get":
if gitlab.mixins.GetWithoutIdMixin not in inspect.getmro(cls):
Expand Down
22 changes: 22 additions & 0 deletions gitlab/v4/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2364,6 +2364,27 @@ class ProjectPipelineScheduleManager(CRUDMixin, RESTManager):
('description', 'ref', 'cron', 'cron_timezone', 'active'))


class ProjectPushRules(SaveMixin, ObjectDeleteMixin, RESTObject):
_id_attr = None


class ProjectPushRulesManager(GetWithoutIdMixin, CreateMixin, UpdateMixin,
DeleteMixin, RESTManager):
_path = '/projects/%(project_id)s/push_rule'
_obj_cls = ProjectPushRules
_from_parent_attrs = {'project_id': 'id'}
_create_attrs = (tuple(),
('deny_delete_tag', 'member_check',
'prevent_secrets', 'commit_message_regex',
'branch_name_regex', 'author_email_regex',
'file_name_regex', 'max_file_size'))
_update_attrs = (tuple(),
('deny_delete_tag', 'member_check',
'prevent_secrets', 'commit_message_regex',
'branch_name_regex', 'author_email_regex',
'file_name_regex', 'max_file_size'))


class ProjectSnippetNoteAwardEmoji(ObjectDeleteMixin, RESTObject):
pass

Expand Down Expand Up @@ -2755,6 +2776,7 @@ class Project(SaveMixin, ObjectDeleteMixin, RESTObject):
('pipelines', 'ProjectPipelineManager'),
('protectedbranches', 'ProjectProtectedBranchManager'),
('pipelineschedules', 'ProjectPipelineScheduleManager'),
('pushrules', 'ProjectPushRulesManager'),
('runners', 'ProjectRunnerManager'),
('services', 'ProjectServiceManager'),
('snippets', 'ProjectSnippetManager'),
Expand Down