Skip to content
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
164 changes: 164 additions & 0 deletions invenio_rdm_records/notifications/vcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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")]
37 changes: 37 additions & 0 deletions invenio_rdm_records/services/components/vcs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- 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.
"""RDM service component for updating VCS release models when drafts are published."""

from invenio_drafts_resources.services.records.components import ServiceComponent
from invenio_vcs.models import Release, ReleaseStatus


class VCSComponent(ServiceComponent):
"""Service component for VCS."""

def publish(self, identity, draft=None, record=None):
"""Publish."""
if record is None:
return

record_model_id = record.model.id
# See if there's a release that originally failed to publish but was saved in a draft state
db_release = Release.get_for_record(record_model_id, only_draft=True)
# If this record didn't come from a VCS release or the release originally succeeded, we won't find anything.
if db_release is None:
return
if (
db_release.status != ReleaseStatus.FAILED
and db_release.status != ReleaseStatus.PUBLISH_PENDING
):
return

# We are now publishing it, so we can correct the release's status
db_release.status = ReleaseStatus.PUBLISHED
db_release.record_is_draft = False
# We can delete the error that originally happened during publish
db_release.errors = None
10 changes: 8 additions & 2 deletions invenio_rdm_records/services/github/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023 CERN.
# Copyright (C) 2023-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.
"""RDM records implementation of Github."""
"""RDM records implementation of the legacy Invenio-GitHub module.

This module is now deprecated. For now, to allow for an optional migration, the RDM bindings for
Invenio-GitHub have been retained. Please migrate to Invenio-VCS.
"""

# TODO: add link to migration docs.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shelve: document this to an issue so we dont forget :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #2297

7 changes: 7 additions & 0 deletions invenio_rdm_records/services/vcs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023-2025 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.
"""RDM records implementation of Invenio-VCS."""
Loading
Loading