Repository navigation
fix: complete the subscriptions API - #95
Conversation
Subscriptions need type and value, which notifications use. Requests for subscriptions and builds store only the fields that the API defines. Deleting a subscription removes it from the lists of its user and repository, and a user without subscriptions gets an empty list. A build is found only under its own repository, and the spec declares the 404 of the calls that can answer it. API version 2.1.0, with an upgrade note. Closes #93 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Declare the 404 of the user calls and of GET /subscriptions too. Verifying a user sets only its flag, so that it can't undo a concurrent change of its subscriptions. Unit test for builds of another repository; docs: required fields, top-level filtering, subscriptions deleted before the upgrade, and a pointer from the API page. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Deleting a subscription that is gone removes it from its user's list, and verifying a user keeps a change of its subscriptions made meanwhile. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideAPIReview the API 2.1.0 contract together: the version bump, newly declared 404 responses, and required subscription fields.
ImplementationReview build request filtering and repository-scoped build lookup, including the server caller that supplies the repository identifier.
Review subscription field filtering, empty-list behavior, deletion cleanup across user and repository lists, and handling of legacy incomplete subscriptions.
Review the user-status update as a targeted flag change that preserves concurrent subscription-list updates.
DocumentationReview the API overview pointer and upgrade guidance for the 2.1.0 behavior changes.
Review the updated subscription usage requirements and the example and explanation for deleting subscriptions.
TestsReview integration and unit coverage for required subscription fields, top-level field filtering, deletion cleanup, empty listings, legacy matching, repository-scoped builds, and concurrent user updates.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #95 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 17 17
Lines 1518 1518
=========================================
Hits 1518 1518 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="pubgrade/modules/endpoints/subscriptions.py" line_range="269" />
<code_context>
+ db_collection_user.update_one(
+ {"uid": uid}, {"$pull": {"subscription_list": subscription_id}}
+ )
+ if deleted is None:
raise SubscriptionNotFound
+ collection("repositories").update_one(
</code_context>
<issue_to_address>
**Deleted IDs remain in repositories**
When the subscription is already absent or deletion is interrupted after `find_one_and_delete`, `delete_subscription` raises before the repository `$pull` when `deleted` is `None`; after partial writes, the user-list pull makes retries fail membership checks. The stale repository ID keeps producing skipped notifications and consuming build work.
Make cleanup idempotently pull the ID from repository lists even when the subscription document is absent, and let retries finish partial cleanup.
Also at `pubgrade/modules/endpoints/subscriptions.py:270-271`.
</issue_to_address>
### Comment 2
<location path="pubgrade/api/pubgrade.yaml" line_range="583-584" />
<code_context>
required:
- repository_id
- callback_url
+ - type
+ - value
- access_token
#additionalProperties: false #shows error bad request on using
</code_context>
<issue_to_address>
**Malformed subscriptions fail notifications**
When old and new pods overlap during deployment and a subscription without `type` or `value` reaches an old pod, the old pod accepts and stores the subscription, then the new pod's `matches` indexes `subscription["type"]` and raises `KeyError`. Notification handling records the failure, so subscribers miss the notification.
Keep notification handling compatible with subscriptions stored by old pods without `type` or `value`, rather than indexing those fields unconditionally.
</issue_to_address>
### Comment 3
<location path="pubgrade/modules/endpoints/builds.py" line_range="259-262" />
<code_context>
UndeclaredBuildType: Raised when the repository doesn't declare the
build's `build_type`.
"""
+ # Only the fields that the API defines are stored
+ build_data = {
+ key: value for key, value in build_data.items() if key in BUILD_FIELDS
+ }
db_collection_builds = collection("builds")
db_collection_repositories = collection("repositories")
</code_context>
<issue_to_address>
**Legacy fields still appear in GET**
When the database contains build or subscription documents written before 2.1.0 with unknown top-level fields, the new filters apply only when a build or subscription is written. Existing documents containing unknown top-level fields remain unchanged, and the GET paths return those fields, so callers still receive fields the upgraded API says it ignores instead of returning.
Strip non-API top-level fields from existing documents during upgrade or filter them from GET responses.
Also at `pubgrade/modules/endpoints/subscriptions.py:82`.
</issue_to_address>Linked issues
#93 fix: complete the subscriptions API · 5 of 6 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Require type and value in the subscription spec. |
✅ Met | Both fields are included in the request schema's required list. |
| 2 | Store only API-defined top-level fields for subscriptions and builds. | ✅ Met | The registration functions filter request data against their respective field sets before storing it. |
| 3 | Remove a deleted subscription identifier from both the user's and repository's lists. | 🟡 Partly met | When the subscription document exists, both lists are updated; when it is already gone, the function removes it from the user's list and raises 404 before updating the repository's list. |
| 4 | Return an empty list from GET /subscriptions when the user has no subscriptions. |
✅ Met | The endpoint returns a list built from subscription_list, defaulting to an empty list. |
| 5 | Declare 404 responses for the listed repository, build, and subscription calls. | ✅ Met | The spec adds 404 responses for GET /repositories/{id}, the listed builds endpoints, and the listed subscriptions endpoints. |
| 6 | Return 404 when a build is not listed for the requested repository. | ✅ Met | get_build_info checks the repository's build_list before returning the build. |
Sourcery assessment
Needs a human reviewer. 3 findings to address first, and deleting a subscription now also removes its ID from the user's and repository's stored lists. If that cleanup targets the wrong association, reverting will not restore it; the bounded damage can be repaired by recreating the subscription. The repository check for build lookups changes which builds can be returned, but a mistake there is reversible runtime behavior.
Blocking findings: pubgrade/modules/endpoints/subscriptions.py:269, pubgrade/api/pubgrade.yaml:584, pubgrade/modules/endpoints/builds.py:262
Deleting a subscription removes it from every repository's list first, also when the subscription document is gone already, so a retry of an interrupted deletion finishes the cleanup. Subscriptions stored without type or value match no build instead of failing their notification. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
Completes the subscriptions API (API version 2.1.0):
POST /subscriptionsrequirestypeandvalue(spec); without them, notifications for the subscription failed on every build.POST /subscriptionsandPOST /repositories/{id}/buildsstore only the top-level fields that the API defines.DELETE /subscriptions/{subscription_id}removes the subscription from the lists of its user and of every repository (also when the subscription document is gone already, so a retry finishes an interrupted deletion).typeorvaluematch no build (skipped) instead of failing their notification.GET /subscriptionsreturns[]for a user without subscriptions instead of a 404.GET /repositories/{id}/builds/{build_id}answers 404 for builds that repository{id}doesn't list.isVerified($set) instead of replacing the document, so it can't undo a concurrent change of the user's subscriptions.docs/deployment.md, required fields and deletion indocs/usage.md, pointer indocs/api.md.Closes #93
Verification
.venv/bin/pytest --cov: 657 passed, coverage 100% (unit and integration tests with mongomock; new integration tests intests/integration/test_api.pyfor each item, new and extended unit tests).ruff check pubgrade tests,ruff format --check pubgrade tests,mypy pubgrade tests: clean.Lane
Full review:
adversarial-reviewerandquality-reviewerin parallel, then round 2 of both (round 1 had medium findings); fixes in the second and third commits. Sourcery: 2 threads fixed (b8a380d), 1 declined (below).Declined review findings
repository_idofget_build_inforequired: an internal caller (builds.py, cancel path) reads builds without a repository.imagesandhead_committoo: out of scope; the docs say that only top-level fields are filtered.GET: no migration mechanism, and filtering would hide fields operators may rely on; the upgrade notes say stored documents keep their fields.Open questions
None.
🤖 Generated with Claude Code