Skip to content
Draft
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
21 changes: 21 additions & 0 deletions site/cds_rdm/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,24 @@

Not the final URL — ``archive_link_render`` appends the archive id.
"""

CDS_EP_REPORTNUMBER_ALLOCATOR_URL = None
"""URL of the legacy EP report-number allocation endpoint.

Global switch for the committee-approval workflow. When set, any community whose
report-number ``prefix`` contains ``CERN-EP`` reserves its number from this
(authoritative) legacy counter instead of scanning the local ``apprn`` pidstore,
so legacy submissions and cds-rdm share one serialized counter and never emit
duplicate numbers. Leave unset to keep the local-scan behaviour for all series.
E.g. ``"https://cds.cern.ch/epreportnumber/next"``. Requires
``CDS_EP_REPORTNUMBER_ALLOCATOR_TOKEN``.
"""

CDS_EP_REPORTNUMBER_ALLOCATOR_TOKEN = None
"""Bearer token for ``CDS_EP_REPORTNUMBER_ALLOCATOR_URL``.

Must match ``CFG_EP_REPORTNUMBER_ALLOCATION_TOKEN`` on the legacy side. Required
whenever the allocator URL is in use; if it is unset while the URL is set,
accepting a ``CERN-EP`` request fails fast rather than minting a (possibly
duplicate) local number.
"""
63 changes: 59 additions & 4 deletions site/cds_rdm/requests/committee_approval.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from datetime import date, datetime, timezone
from typing import Final

import requests
from flask import current_app
from flask_principal import Identity
from invenio_access.permissions import system_identity
Expand Down Expand Up @@ -187,8 +188,12 @@ def _community_config(self):
def _next_report_number(self, rn: dict) -> str:
"""Return the next report number for this community without minting a PID.

Builds the scan prefix directly from the structured ``report_number``
config — no string-format parsing needed.
For the ``CERN-EP`` series the legacy counter is still the live issuer, so
when the global ``CDS_EP_REPORTNUMBER_ALLOCATOR_URL`` is configured and the
community's report-number ``prefix`` contains ``CERN-EP``, the number is
reserved from that authoritative legacy endpoint (one serialized counter
shared with legacy submissions → no duplicates). Every other series falls
back to scanning the local ``apprn`` pidstore.

Args:
rn: the ``report_number`` sub-dict from the community config, with keys:
Expand All @@ -197,6 +202,13 @@ def _next_report_number(self, rn: dict) -> str:
- ``counter_digits`` (int, default ``3``): zero-padding width
"""
prefix = rn["prefix"]

allocator_url = current_app.config.get("CDS_EP_REPORTNUMBER_ALLOCATOR_URL")
if allocator_url and "CERN-EP" in prefix:
# Authoritative path: reserve from the single legacy counter so
# legacy submissions and cds-rdm never emit duplicate numbers.
return self._allocate_from_legacy(allocator_url)

digits = rn.get("counter_digits", 3)
year = date.today().year
scan_prefix = (
Expand All @@ -217,6 +229,47 @@ def _next_report_number(self, rn: dict) -> str:
)
return f"{scan_prefix}{max_seq + 1:0{digits}d}"

@staticmethod
def _allocate_from_legacy(allocator_url: str) -> str:
"""Reserve the next report number from the legacy allocation endpoint.

The legacy counter is the single source of truth during the transition
period: it serializes allocation under a file lock and returns the fully
formatted report number (e.g. ``CERN-EP-2026-042``), which we use
verbatim. Raises ``ValidationError`` (aborting the accept) if the token
is unset or the endpoint is unreachable/errors — we must never silently
fall back to local numbering here, as that is exactly what causes the
duplicates this bridge exists to prevent.
"""
token = current_app.config.get("CDS_EP_REPORTNUMBER_ALLOCATOR_TOKEN")
if not token:
raise ValidationError(
"CDS_EP_REPORTNUMBER_ALLOCATOR_TOKEN is not configured; "
"cannot reserve an approval report number."
)
try:
resp = requests.post(
allocator_url,
headers={"Authorization": f"Bearer {token}"},
timeout=10,
)
except requests.RequestException as exc:
raise ValidationError(
"Could not reach the report-number allocation service; "
"the approval was not completed. Please retry."
) from exc
if resp.status_code != 200:
raise ValidationError(
"Report-number allocation service returned "
f"{resp.status_code}: {resp.text.strip()}"
)
report_number = resp.text.strip()
if not report_number:
raise ValidationError(
"Report-number allocation service returned an empty response."
)
return report_number

def _issue_report_number(self, config: dict, record_uuid: str) -> str:
"""Generate the next report number and mint its PID.

Expand Down Expand Up @@ -257,8 +310,10 @@ def execute(self, identity: Identity, uow: UnitOfWork) -> None:

# Flush the parent and defer the DB commit to the UoW so that the
# parent write, the PID insert, and the request status update all land
# in the same transaction. The advisory lock acquired in
# _issue_report_number is held until this transaction commits.
# in the same transaction. Uniqueness of the report number is guaranteed
# upstream by the legacy counter (serialized under a file lock); if this
# transaction rolls back, the allocated number is simply skipped (a gap),
# which is acceptable for report numbers.
uow.register(
ParentRecordCommitOp(
topic.parent,
Expand Down
122 changes: 122 additions & 0 deletions site/tests/test_committee_approval.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from types import SimpleNamespace

import pytest
import requests
from flask_principal import RoleNeed
from invenio_access.permissions import system_identity
from invenio_communities.generators import CommunityRoleNeed
Expand Down Expand Up @@ -321,6 +322,127 @@ def test_generate_report_number_independent_prefix_counters(app, db):
assert action._next_report_number(TH_RN_CONFIG) == f"CERN-TH-{YEAR}-001"


# ---------------------------------------------------------------------------
# Report number generation via the authoritative legacy allocator
# ---------------------------------------------------------------------------


class _FakeResp:
"""Minimal stand-in for a requests.Response."""

def __init__(self, status_code, text):
self.status_code = status_code
self.text = text


ALLOCATOR_URL = "https://cds.cern.ch/epreportnumber/next"


def _enable_allocator(app, monkeypatch, token="s3cret"):
"""Turn on the global legacy allocator for the duration of a test."""
monkeypatch.setitem(app.config, "CDS_EP_REPORTNUMBER_ALLOCATOR_URL", ALLOCATOR_URL)
monkeypatch.setitem(app.config, "CDS_EP_REPORTNUMBER_ALLOCATOR_TOKEN", token)


def test_next_report_number_uses_legacy_allocator_for_ep(app, db, monkeypatch):
"""A CERN-EP prefix with the global allocator set gets the number verbatim."""
captured = {}

def fake_post(url, headers=None, timeout=None):
captured["url"] = url
captured["headers"] = headers
captured["timeout"] = timeout
return _FakeResp(200, f"CERN-EP-{YEAR}-042\n")

monkeypatch.setattr(
"cds_rdm.requests.committee_approval.requests.post", fake_post
)
_enable_allocator(app, monkeypatch)

action = CommitteeApprovalAcceptAction.__new__(CommitteeApprovalAcceptAction)
assert action._next_report_number(EP_RN_CONFIG) == f"CERN-EP-{YEAR}-042"
assert captured["url"] == ALLOCATOR_URL
assert captured["headers"]["Authorization"] == "Bearer s3cret"


def test_next_report_number_non_ep_prefix_ignores_allocator(app, db, monkeypatch):
"""A non CERN-EP prefix keeps scanning locally even when the allocator is on."""
called = {"n": 0}

def fake_post(*a, **k):
called["n"] += 1
return _FakeResp(200, "SHOULD-NOT-BE-USED")

monkeypatch.setattr(
"cds_rdm.requests.committee_approval.requests.post", fake_post
)
_enable_allocator(app, monkeypatch)

action = CommitteeApprovalAcceptAction.__new__(CommitteeApprovalAcceptAction)
assert action._next_report_number(TH_RN_CONFIG) == f"CERN-TH-{YEAR}-001"
assert called["n"] == 0


def test_next_report_number_scans_when_allocator_url_unset(app, db, monkeypatch):
"""A CERN-EP prefix falls back to the local scan when no URL is configured."""
called = {"n": 0}

def fake_post(*a, **k):
called["n"] += 1
return _FakeResp(200, "SHOULD-NOT-BE-USED")

monkeypatch.setattr(
"cds_rdm.requests.committee_approval.requests.post", fake_post
)
monkeypatch.setitem(app.config, "CDS_EP_REPORTNUMBER_ALLOCATOR_URL", None)

action = CommitteeApprovalAcceptAction.__new__(CommitteeApprovalAcceptAction)
assert action._next_report_number(EP_RN_CONFIG) == f"CERN-EP-{YEAR}-001"
assert called["n"] == 0


def test_next_report_number_legacy_requires_token(app, db, monkeypatch):
"""A missing token aborts rather than falling back to local numbering."""
monkeypatch.setattr(
"cds_rdm.requests.committee_approval.requests.post",
lambda *a, **k: _FakeResp(200, f"CERN-EP-{YEAR}-001"),
)
_enable_allocator(app, monkeypatch, token=None)

action = CommitteeApprovalAcceptAction.__new__(CommitteeApprovalAcceptAction)
with pytest.raises(ValidationError):
action._next_report_number(EP_RN_CONFIG)


def test_next_report_number_legacy_raises_on_error_status(app, db, monkeypatch):
"""A non-200 from the allocator aborts the allocation."""
monkeypatch.setattr(
"cds_rdm.requests.committee_approval.requests.post",
lambda *a, **k: _FakeResp(503, "[ERROR] not configured"),
)
_enable_allocator(app, monkeypatch)

action = CommitteeApprovalAcceptAction.__new__(CommitteeApprovalAcceptAction)
with pytest.raises(ValidationError):
action._next_report_number(EP_RN_CONFIG)


def test_next_report_number_legacy_raises_when_unreachable(app, db, monkeypatch):
"""A network error aborts the allocation (never silently local-numbers)."""

def boom(*a, **k):
raise requests.exceptions.ConnectionError("allocator down")

monkeypatch.setattr(
"cds_rdm.requests.committee_approval.requests.post", boom
)
_enable_allocator(app, monkeypatch)

action = CommitteeApprovalAcceptAction.__new__(CommitteeApprovalAcceptAction)
with pytest.raises(ValidationError):
action._next_report_number(EP_RN_CONFIG)


# ---------------------------------------------------------------------------
# Full workflow: submit → accept (enrolled community)
# ---------------------------------------------------------------------------
Expand Down
Loading