-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: swap_vanity_urls()
works predictably with unset vanity URLs
#361
Conversation
- delete_vanity_url() returns an error when it fails - swap_vanity_urls() returns an error if it does not have permission to modify either of the content items’ vanity urls. if content_b fails, content_a’s url is rolled back.
@@ -464,14 +464,14 @@ delete_vanity_url <- function(content) { | |||
error_if_less_than(con$version, "1.8.6") | |||
guid <- content$get_content()$guid | |||
|
|||
con$DELETE(v1_url("content", guid, "vanity")) | |||
con$DELETE(v1_url("content", guid, "vanity"), parser = "parsed") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that it returns an error if it receives an HTTP error status.
swap_vanity_urls()
works predictably with unset vanity URLs [DRAFT]swap_vanity_urls()
works predictably with unset vanity URLs
now that the function works properly with unset vanities, the test condition was incorrect
tryCatch( | ||
delete_vanity_url(content_b), | ||
error = function(e) { | ||
set_vanity_url(content_a, vanity_a) | ||
stop("Unable to modify the vanity URL for content_b: ", e$message, call. = FALSE) | ||
} | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since these transactions aren't atomic at the database level, there is no way to ensure the swap happens successfully. Nevertheless, these types of fallbacks are nice to have.
If logging is established in this project, I would add a log info statement describing each step as it happens. That way the user knows A was reset to its original state.
vanity_a <- get_vanity_url(content_a) | ||
vanity_b <- get_vanity_url(content_b) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, this is a good idea. The server does mutate the submitted vanity url in some cases (i.e., leading '/').
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logical ✅
Test coverage ✅
Documentation ✅
Looks good to me!
Intent
Update
swap_vanity_url()
with numerous fixes and changes. Changes include:swap_vanity_url()
was called on a piece of content without a vanity URL, the content item set to receive that would instead receive a randomized placeholder URL. Now the function works as intended when one content item has no vanity URL: the other content item receives an unset vanity URL!swap_vanity_urls()
because you're swapping two vanity URLs between two content items. The arguments are now calledcontent_a
andcontent_b
because the old argument names,from
andto
, don't make sense when vanity URLs move from and to both items. The old function name remains but is deprecated, and just shims argument and return object names to the new function.Fixes #360
Approach
Rewrote the function's internal workflow. Pseudocode:
content_a
fails, stop and err. Ifcontent_b
fails, restorecontent_a
's original vanity URL and err.delete
andset
have the same permissions.Perhaps a better approach would be to call
set_vanity_url(content_a, vanity_b, force = TRUE)
in 2., as this doesn't risk the (admittedly very unlikely) case that another piece of content snipes the URL before it's reassigned. But this actually introduces more complexity:set
ordelete
in this step.force = TRUE
also modifiescontent_b
's vanity URL, if we lacked permission to modifycontent_b
, that operation would fail — but only ifcontent_b
initially had a vanity URL (if its vanity was unset, we'd just be unsettingvanity_a
's URL). So the workflow for rollbacks would be much more complex, and harder to reason about / test / maintain.Checklist
NEWS.md
(referencing the connected issue if necessary)?devtools::document()
?