Skip to content

Commit b15f17b

Browse files
author
Gauvain Pocentek
committed
Add support for the notification settings API
1 parent 6d3450c commit b15f17b

File tree

7 files changed

+124
-1
lines changed

7 files changed

+124
-1
lines changed

docs/api-objects.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ API objects manipulation
77

88
gl_objects/access_requests
99
gl_objects/branches
10+
gl_objects/messages
1011
gl_objects/builds
1112
gl_objects/commits
1213
gl_objects/deploy_keys
@@ -16,7 +17,7 @@ API objects manipulation
1617
gl_objects/issues
1718
gl_objects/labels
1819
gl_objects/licenses
19-
gl_objects/messages
20+
gl_objects/notifications
2021
gl_objects/mrs
2122
gl_objects/namespaces
2223
gl_objects/milestones

docs/gl_objects/notifications.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# get
2+
# global settings
3+
settings = gl.notificationsettings.get()
4+
# for a group
5+
settings = gl.groups.get(group_id).notificationsettings.get()
6+
# for a project
7+
settings = gl.projects.get(project_id).notificationsettings.get()
8+
# end get
9+
10+
# update
11+
# use a predefined level
12+
settings.level = gitlab.NOTIFICATION_LEVEL_WATCH
13+
# create a custom setup
14+
settings.level = gitlab.NOTIFICATION_LEVEL_CUSTOM
15+
settings.save() # will create additional attributes, but not mandatory
16+
17+
settings.new_merge_request = True
18+
settings.new_issue = True
19+
settings.new_note = True
20+
settings.save()
21+
# end update

docs/gl_objects/notifications.rst

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#####################
2+
Notification settings
3+
#####################
4+
5+
You can define notification settings globally, for groups and for projects.
6+
Valid levels are defined as constants:
7+
8+
* ``NOTIFICATION_LEVEL_DISABLED``
9+
* ``NOTIFICATION_LEVEL_PARTICIPATING``
10+
* ``NOTIFICATION_LEVEL_WATCH``
11+
* ``NOTIFICATION_LEVEL_GLOBAL``
12+
* ``NOTIFICATION_LEVEL_MENTION``
13+
* ``NOTIFICATION_LEVEL_CUSTOM``
14+
15+
You get access to fine-grained settings if you use the
16+
``NOTIFICATION_LEVEL_CUSTOM`` level.
17+
18+
* Object classes: :class:`gitlab.objects.NotificationSettings` (global),
19+
:class:`gitlab.objects.GroupNotificationSettings` (groups) and
20+
:class:`gitlab.objects.ProjectNotificationSettings` (projects)
21+
* Manager objects: :attr:`gitlab.Gitlab.notificationsettings` (global),
22+
:attr:`gitlab.objects.Group.notificationsettings` (groups) and
23+
:attr:`gitlab.objects.Project.notificationsettings` (projects)
24+
25+
Examples
26+
--------
27+
28+
Get the settings:
29+
30+
.. literalinclude:: notifications.py
31+
:start-after: # get
32+
:end-before: # end get
33+
34+
Update the settings:
35+
36+
.. literalinclude:: notifications.py
37+
:start-after: # update
38+
:end-before: # end update

gitlab/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ class Gitlab(object):
8282
issues (IssueManager): Manager for GitLab issues
8383
licenses (LicenseManager): Manager for licenses
8484
namespaces (NamespaceManager): Manager for namespaces
85+
notificationsettings (NotificationSettingsManager): Manager for global
86+
notification settings
8587
project_accessrequests (ProjectAccessRequestManager): Manager for
8688
GitLab projects access requests
8789
project_boards (ProjectBoardManager): Manager for GitLab projects
@@ -181,6 +183,7 @@ def __init__(self, url, private_token=None, email=None, password=None,
181183
self.issues = IssueManager(self)
182184
self.licenses = LicenseManager(self)
183185
self.namespaces = NamespaceManager(self)
186+
self.notificationsettings = NotificationSettingsManager(self)
184187
self.project_accessrequests = ProjectAccessRequestManager(self)
185188
self.project_boards = ProjectBoardManager(self)
186189
self.project_board_listss = ProjectBoardListManager(self)

gitlab/const.py

+7
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@
2424
VISIBILITY_PRIVATE = 0
2525
VISIBILITY_INTERNAL = 10
2626
VISIBILITY_PUBLIC = 20
27+
28+
NOTIFICATION_LEVEL_DISABLED = 'disabled'
29+
NOTIFICATION_LEVEL_PARTICIPATING = 'participating'
30+
NOTIFICATION_LEVEL_WATCH = 'watch'
31+
NOTIFICATION_LEVEL_GLOBAL = 'global'
32+
NOTIFICATION_LEVEL_MENTION = 'mention'
33+
NOTIFICATION_LEVEL_CUSTOM = 'custom'

gitlab/objects.py

+46
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,30 @@ class KeyManager(BaseManager):
752752
obj_cls = Key
753753

754754

755+
class NotificationSettings(GitlabObject):
756+
_url = '/notification_settings'
757+
_id_in_update_url = False
758+
optionalUpdateAttrs = ['level',
759+
'notification_email',
760+
'new_note',
761+
'new_issue',
762+
'reopen_issue',
763+
'close_issue',
764+
'reassign_issue',
765+
'new_merge_request',
766+
'reopen_merge_request',
767+
'close_merge_request',
768+
'reassign_merge_request',
769+
'merge_merge_request']
770+
canList = False
771+
canCreate = False
772+
canDelete = False
773+
774+
775+
class NotificationSettingsManager(BaseManager):
776+
obj_cls = NotificationSettings
777+
778+
755779
class GroupIssue(GitlabObject):
756780
_url = '/groups/%(group_id)s/issues'
757781
canGet = 'from_list'
@@ -783,6 +807,15 @@ class GroupMemberManager(BaseManager):
783807
obj_cls = GroupMember
784808

785809

810+
class GroupNotificationSettings(NotificationSettings):
811+
_url = '/groups/%(group_id)s/notification_settings'
812+
requiredUrlAttrs = ['group_id']
813+
814+
815+
class GroupNotificationSettingsManager(BaseManager):
816+
obj_cls = GroupNotificationSettings
817+
818+
786819
class GroupProject(GitlabObject):
787820
_url = '/groups/%(group_id)s/projects'
788821
canGet = 'from_list'
@@ -835,6 +868,8 @@ class Group(GitlabObject):
835868
managers = [
836869
('accessrequests', GroupAccessRequestManager, [('group_id', 'id')]),
837870
('members', GroupMemberManager, [('group_id', 'id')]),
871+
('notificationsettings', GroupNotificationSettingsManager,
872+
[('group_id', 'id')]),
838873
('projects', GroupProjectManager, [('group_id', 'id')]),
839874
('issues', GroupIssueManager, [('group_id', 'id')])
840875
]
@@ -1385,6 +1420,15 @@ class ProjectNoteManager(BaseManager):
13851420
obj_cls = ProjectNote
13861421

13871422

1423+
class ProjectNotificationSettings(NotificationSettings):
1424+
_url = '/projects/%(project_id)s/notification_settings'
1425+
requiredUrlAttrs = ['project_id']
1426+
1427+
1428+
class ProjectNotificationSettingsManager(BaseManager):
1429+
obj_cls = ProjectNotificationSettings
1430+
1431+
13881432
class ProjectTagRelease(GitlabObject):
13891433
_url = '/projects/%(project_id)s/repository/tags/%(tag_name)/release'
13901434
canDelete = False
@@ -1987,6 +2031,8 @@ class Project(GitlabObject):
19872031
('mergerequests', ProjectMergeRequestManager, [('project_id', 'id')]),
19882032
('milestones', ProjectMilestoneManager, [('project_id', 'id')]),
19892033
('notes', ProjectNoteManager, [('project_id', 'id')]),
2034+
('notificationsettings', ProjectNotificationSettingsManager,
2035+
[('project_id', 'id')]),
19902036
('pipelines', ProjectPipelineManager, [('project_id', 'id')]),
19912037
('services', ProjectServiceManager, [('project_id', 'id')]),
19922038
('snippets', ProjectSnippetManager, [('project_id', 'id')]),

tools/python_test.py

+7
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,10 @@
282282
assert(msg.color == '#444444')
283283
msg.delete()
284284
assert(len(gl.broadcastmessages.list()) == 0)
285+
286+
# notification settings
287+
settings = gl.notificationsettings.get()
288+
settings.level = gitlab.NOTIFICATION_LEVEL_WATCH
289+
settings.save()
290+
settings = gl.notificationsettings.get()
291+
assert(settings.level == gitlab.NOTIFICATION_LEVEL_WATCH)

0 commit comments

Comments
 (0)