Skip to content

fix: complete the subscriptions API - #95

Merged
uniqueg merged 4 commits into
devfrom
fix/subscriptions-api
Nov 9, 2026
Merged

uniqueg merged 4 commits into
devfrom
fix/subscriptions-api

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Summary

Completes the subscriptions API (API version 2.1.0):

  • POST /subscriptions requires type and value (spec); without them, notifications for the subscription failed on every build.
  • POST /subscriptions and POST /repositories/{id}/builds store 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).
  • Subscriptions stored without type or value match no build (skipped) instead of failing their notification.
  • GET /subscriptions returns [] 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.
  • The spec declares the 404 of the calls that can answer it (repositories, builds, subscriptions, users).
  • Verifying or unverifying a user sets only isVerified ($set) instead of replacing the document, so it can't undo a concurrent change of the user's subscriptions.
  • Docs: "Upgrading to API 2.1.0" in docs/deployment.md, required fields and deletion in docs/usage.md, pointer in docs/api.md.

Closes #93

Verification

  • .venv/bin/pytest --cov: 657 passed, coverage 100% (unit and integration tests with mongomock; new integration tests in tests/integration/test_api.py for each item, new and extended unit tests).
  • ruff check pubgrade tests, ruff format --check pubgrade tests, mypy pubgrade tests: clean.
  • Not run locally: the integration tests against MongoDB 7.0, the chart, smoke and upgrade tests, the docs build and pre-commit (no Docker MongoDB, Helm or configured pre-commit here).
  • CI: all checks passed (Lint, Test with mongomock and MongoDB 7.0, Chart, Smoke test, Upgrade test, Docs, image scans, CodeQL, Codecov, Read the Docs).

Lane

Full review: adversarial-reviewer and quality-reviewer in 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

  • Make repository_id of get_build_info required: an internal caller (builds.py, cancel path) reads builds without a repository.
  • Filter unknown fields inside images and head_commit too: out of scope; the docs say that only top-level fields are filtered.
  • Normalise the 9-space indentation of the verify/unverify responses: kept consistent with the surrounding blocks of the file.
  • Sourcery: strip unknown top-level fields from documents stored before 2.1.0, or filter them on 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

uniqueg and others added 3 commits November 9, 2026 05:03
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]>
@uniqueg uniqueg added this to the v0.1.0 milestone Nov 9, 2026
@uniqueg uniqueg added the type: bug Something isn't working label Nov 9, 2026
@sourcery-ai

sourcery-ai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

API

Review the API 2.1.0 contract together: the version bump, newly declared 404 responses, and required subscription fields.

  • pubgrade/api/pubgrade.yaml L1004-1011, L1047-1054 (+10 small edits)

Implementation

Review build request filtering and repository-scoped build lookup, including the server caller that supplies the repository identifier.

  • pubgrade/modules/endpoints/builds.py L67-70, L259-262, L472-481 (+2 small edits)
  • pubgrade/modules/server.py L134, L140

Review subscription field filtering, empty-list behavior, deletion cleanup across user and repository lists, and handling of legacy incomplete subscriptions.

  • pubgrade/modules/endpoints/subscriptions.py L46-50, L188-191, L263-276 (+6 small edits)

Review the user-status update as a targeted flag change that preserves concurrent subscription-list updates.

  • pubgrade/modules/endpoints/users.py L179-185, L187 (removed lines)

Documentation

Review the API overview pointer and upgrade guidance for the 2.1.0 behavior changes.

  • docs/api.md L53-55
  • docs/deployment.md L179-205

Review the updated subscription usage requirements and the example and explanation for deleting subscriptions.

  • docs/usage.md L66-69, L245-253

Tests

Review 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.

  • tests/integration/test_api.py L704-797
  • tests/pubgrade/endpoints/test_builds.py L681-692
  • tests/pubgrade/endpoints/test_subscriptions.py L932-938, L955-968, L1033-1045 (+2 small edits)
  • tests/pubgrade/endpoints/test_users.py L238-249, L254-262
  • tests/pubgrade/test_server.py L486-488

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (01be821) to head (b8a380d).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@read-the-docs-community

read-the-docs-community Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 Pubgrade | 🛠️ Build #35744864 | 📁 Comparing b8a380d against latest (01be821)

  🔍 Preview build  

3 files changed
± api/index.html
± deployment/index.html
± usage/index.html

@uniqueg
uniqueg marked this pull request as ready for review November 9, 2026 04:34
@uniqueg
uniqueg requested a review from akash2237778 as a code owner November 9, 2026 04:34

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread pubgrade/api/pubgrade.yaml Resolved
Comment thread pubgrade/modules/endpoints/builds.py Resolved
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]>
@uniqueg
uniqueg merged commit b3b07f0 into dev Nov 9, 2026
40 of 46 checks passed
@uniqueg
uniqueg deleted the fix/subscriptions-api branch November 9, 2026 04:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: complete the subscriptions API

1 participant