Skip to content

feat(api): Handle project redirect in API client#8799

Merged
billyvg merged 8 commits intomasterfrom
feat/project-redirect/handle-in-api-client
Jul 26, 2018
Merged

feat(api): Handle project redirect in API client#8799
billyvg merged 8 commits intomasterfrom
feat/project-redirect/handle-in-api-client

Conversation

@billyvg
Copy link
Member

@billyvg billyvg commented Jun 20, 2018

Previously we would only be handling API responses with a 302 in
ProjectContext, which is only a subset of requests that can return a 302.
Move the handling up to the API client.

Also changes the 302 response body to be an object more consistent with our
error response objects.

Previously the API would return an object with a new slug, this changes it to return a proper redirect response using the Location response header.

Currently undecided how to handle this on the frontend because the user can be at an old URL in the browser while the API requests will transparently query the updated project (since the XHR requests will follow the redirects).

There doesn't seem to be an easy way to intercept 302s using our API client.

Fixes JAVASCRIPT-3JM

@billyvg billyvg force-pushed the feat/project-redirect/handle-in-api-client branch from 87cc08e to 907dcbb Compare June 20, 2018 23:04
@billyvg billyvg requested a review from evanpurkhiser June 20, 2018 23:05
class ResourceMoved(SentryAPIException):
status_code = status.HTTP_302_FOUND
code = 'project-moved'
message = 'Project slug was renamed'
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't know if we should couple this with Projects, specifically. Can we make it more generic to cover other redirects in the future?

)

raise ResourceMoved(detail={'slug': redirect.project.slug})
new_url = request.path.replace('projects/%s/%s/' % (organization_slug, project_slug), 'projects/%s/%s/' % (organization_slug, redirect.project.slug))
Copy link
Contributor

Choose a reason for hiding this comment

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

This is going to drop any query strings.

You can leverage request.get_full_path() https://docs.djangoproject.com/en/1.7/ref/request-response/#django.http.HttpRequest.get_full_path

Which also includes the querystring.

I'd also introduce a check for like, if new_url != old_url before triggering a redirect, otherwise, you'll end in a redirect loop. This would happen if say, a different url that didn't follow this pattern of projects/%s/%s/ came through. I'd say we'd want to raise a 404 in that case instead of an endless loop of redirection.

@billyvg billyvg changed the title feat(ui): Handle project redirect in API client feat(api): Handle project redirect in API client Jun 21, 2018
@billyvg
Copy link
Member Author

billyvg commented Jun 21, 2018

This will break sentry-cli:(

@mattrobenolt
Copy link
Contributor

Couldn't we just retain the same JSON response body as before to not break sentry-cli?

I'd argue that this change should only be adding an additional Location header. The body should remain the same.

Copy link
Member

@evanpurkhiser evanpurkhiser left a comment

Choose a reason for hiding this comment

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

Looks pretty reasonable. Looks like this does a couple things right:

  • Add the location header when the project has been moved
  • Provide a better user experience on the frontend with a modal indicating that the project was renamed.

super(SentryAPIException, self).__init__(detail=detail)


class ResourceMoved(SentryAPIException):
Copy link
Member

Choose a reason for hiding this comment

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

Can we just rename this ProjectMoved since the slug is in the init

billyvg added 8 commits July 26, 2018 10:17
Previously we would only be handling API responses with a 302 in `ProjectContext`, which is only a subset of requests that can return a 302. Move the handling up to the API client.

Also changes the 302 response body to be an object more consistent with our error response objects.

Fixes JAVASCRIPT-3JM
@billyvg billyvg force-pushed the feat/project-redirect/handle-in-api-client branch from e3ed002 to 6809c66 Compare July 26, 2018 17:54
@billyvg billyvg merged commit 4473b1e into master Jul 26, 2018
@billyvg billyvg deleted the feat/project-redirect/handle-in-api-client branch July 26, 2018 18:57
@dashed dashed mentioned this pull request Jul 26, 2019
1 task
@github-actions github-actions bot locked and limited conversation to collaborators Dec 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants