forked from inveniosoftware/invenio-rdm-records
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvcs.py
More file actions
164 lines (131 loc) · 5.33 KB
/
vcs.py
File metadata and controls
164 lines (131 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- coding: utf-8 -*-
#
# Copyright (C) 2026 CERN.
#
# Invenio-RDM-Records is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
"""
Notification builders for VCS.
These are kept in a separate file as invenio-vcs is an optional dependency, so imports would fail if the package isn't available.
"""
from invenio_notifications.models import Notification
from invenio_notifications.registry import EntityResolverRegistry
from invenio_notifications.services.builders import NotificationBuilder
from invenio_notifications.services.generators import EntityResolve, UserEmailBackend
from invenio_users_resources.notifications.filters import UserPreferencesRecipientFilter
from invenio_vcs.generic_models import GenericRelease, GenericRepository
from invenio_vcs.notifications.generators import RepositoryUsersRecipient
class RepositoryReleaseNotificationBuilder(NotificationBuilder):
"""Notification builder for repository release events."""
type = "repository-release"
@classmethod
def build(
cls,
provider: str,
generic_repository: GenericRepository,
generic_release: GenericRelease,
):
"""Build the notification."""
return Notification(
type=cls.type,
context={
"provider": provider,
"repository_provider_id": generic_repository.id,
"repository_full_name": generic_repository.full_name,
"release_tag": generic_release.tag_name,
},
)
context = []
recipients = [RepositoryUsersRecipient("provider", "repository_provider_id")]
recipient_filters = [UserPreferencesRecipientFilter()]
recipient_backends = [UserEmailBackend()]
class RepositoryReleaseSuccessNotificationBuilder(RepositoryReleaseNotificationBuilder):
"""Notification builder for successful repository release events."""
type = f"{RepositoryReleaseNotificationBuilder.type}.success"
@classmethod
def build(
cls,
provider: str,
generic_repository: GenericRepository,
generic_release: GenericRelease,
record,
):
"""Build the notification."""
notification = super().build(provider, generic_repository, generic_release)
notification.context["record"] = EntityResolverRegistry.reference_entity(record)
return notification
context = [EntityResolve(key="record")]
class RepositoryReleaseFailureNotificationBuilder(RepositoryReleaseNotificationBuilder):
"""
Notification builder for failed repository release events.
The failure might occur before or after a draft has been successfully saved, so `draft` is allowed
to be `None`. The notification message should include a link to edit the draft if it's available.
"""
type = f"{RepositoryReleaseNotificationBuilder.type}.failure"
@classmethod
def build(
cls,
provider: str,
generic_repository: GenericRepository,
generic_release: GenericRelease,
error_message: str,
draft=None,
):
"""Build the notification."""
notification = super().build(provider, generic_repository, generic_release)
notification.context["error_message"] = error_message
if draft is not None:
notification.context["draft"] = EntityResolverRegistry.reference_entity(
draft
)
else:
notification.context["draft"] = None
return notification
context = [EntityResolve(key="draft")]
class RepositoryReleaseCommunityRequiredNotificationBuilder(
RepositoryReleaseNotificationBuilder
):
"""
Release is saved as a draft but the user needs to add a community.
Notification builder for when a release is saved as a draft but
fails to be published because the user needs to manually select
a community for the draft.
"""
type = f"{RepositoryReleaseNotificationBuilder.type}.community-required"
@classmethod
def build(
cls,
provider: str,
generic_repository: GenericRepository,
generic_release: GenericRelease,
draft,
):
"""Build the notification."""
notification = super().build(provider, generic_repository, generic_release)
notification.context["draft"] = EntityResolverRegistry.reference_entity(draft)
return notification
context = [EntityResolve(key="draft")]
class RepositoryReleaseCommunitySubmittedNotificationBuilder(
RepositoryReleaseNotificationBuilder
):
"""Notification builder for when a release is submitted for review by a community."""
type = f"{RepositoryReleaseNotificationBuilder.type}.community-submitted"
@classmethod
def build(
cls,
provider: str,
generic_repository: GenericRepository,
generic_release: GenericRelease,
request,
community,
):
"""Build the notification."""
notification = super().build(provider, generic_repository, generic_release)
notification.context["request"] = EntityResolverRegistry.reference_entity(
request
)
notification.context["community"] = EntityResolverRegistry.reference_entity(
community
)
return notification
context = [EntityResolve(key="request"), EntityResolve(key="community")]