Support merging tags through the API - #1446
Open
sethfitz wants to merge 1 commit into
Open
Conversation
The tags API supported list, retrieve, create, and delete, but not the tag merge that the web UI has had since v1.43.0, so a scripted consolidation could not reach it. This adds merge as a detail action on the target tag. The merge transaction moves from the tags view to bookmarks.services.tags unchanged, so both callers share it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Support merging tags through the API
Tag merge has been available in the web UI since v1.43.0 (#1175), but the tags API only
supports list, retrieve, create, and delete. A scripted consolidation therefore has to
hand-roll it: PATCH every affected bookmark's
tag_namesone at a time, then delete thetag that is left behind. That is not atomic, and it leaves an orphaned tag if the caller
stops halfway or forgets the cleanup — which the web UI's merge never does.
This exposes the existing merge as a detail action on the target tag, in the same spirit as
#1411 exposing tag delete.
Returns
204 No Content.Design decisions
Detail action on the target, not
POST /api/tags/merge/. The tags API addresses tags byID everywhere else, so the target belongs in the path like it does for retrieve and delete.
It also follows the convention the bookmarks API already sets for non-CRUD operations on a
single resource —
POST /api/bookmarks/<id>/archive/— rather than introducing acollection-level pseudo-resource. The practical benefit is that the target goes through
get_object(), so a tag belonging to another user 404s through exactly the same mechanismas retrieve and delete, with nothing new to get wrong.
Merge tags are named by ID. Mixing an ID in the path with names in the body would be
incoherent, and IDs avoid the question of what a name that does not resolve should mean.
TagMergeFormmatches names case-insensitively because it is fed by the tag autocomplete;an API client that has names already has the IDs from
GET /api/tags/.Unknown or foreign merge tag IDs are a
400, not a404. The addressed resource — thetarget tag — does exist; the payload is what is invalid, and the response names the offending
IDs. Merge tags are resolved through
get_queryset(), so another user's tag is rejected asnon-existent rather than merged, and ownership has one enforcement point rather than two.
204 No Content, matchingarchive/unarchiveand tag delete. Nothing about the targettag's own representation changes. Returning a count of retagged bookmarks would be useful for
tooling — happy to add that if you would prefer it, but it seemed out of scope for parity with
the web UI.
Shared transaction
The
transaction.atomic()block moves out ofbookmarks/views/tags.pyintobookmarks.services.tags.merge_tags()unchanged — same queries, same order — so the view andthe API action cannot drift. The view keeps its success message and its existing tests, which
pass untouched.
Tests
Seven tests in
bookmarks/tests/test_tags_api.py, covering the merge itself, theexclude(tags=target_tag)branch where a bookmark already carries the target tag, a merge tagthat does not exist, the target listed among the merge tags, a missing/empty
merge_tag_ids,and both cross-user directions (foreign target 404s, foreign merge tag 400s and is left
untouched).