Re-authorize Bitbucket Cloud import when the session token expires
What does this MR do and why?
Bitbucket Cloud access tokens last about two hours, and the import page only needs them while the user is on that page. Refreshing (and rotating) the refresh token in the session raced when more than one tab refreshed at once, and the rotated credentials were never written back, so later requests retried with a dead refresh token.
This MR does not refresh tokens in the controller, and it does not write
refreshed tokens back to the session (that is the race this change
avoids). The Bitbucket gem may still refresh in memory during a request.
When bitbucket_expires_at has passed, or the token is missing, the
controller clears the session and sends the user through authorization
again:
- HTML requests redirect to the Bitbucket authorize URL, as a missing
token already did in
bitbucket_auth. - JSON
statusandcreaterequests return{ error: { redirect } }with404. The import UI follows that redirect for status, jobs, and create (fetchImportFactoryis shared), so the HTML authorize flow can start. Because that action is shared, a Bitbucket ServerConnectionErroron create now also redirects to the credentials page with the flash, instead of a generic alert.
realtime_changes only checks that a session token is present. Import
progress polling does not call Bitbucket, so an expired-but-present
token must not kick the user into the authorize flow mid-import.
create passes ProjectCreator the token, expiry, and refresh token
from client.connection, so if the gem refreshed in memory mid-request
the project stores the rotated credentials rather than a now-dead
refresh token from the session.
The background importer still refreshes through
Import::BitbucketImport::TokenRefreshStrategy on project import_data.
References
Closes #640895 (closed)
Screenshots or screen recordings
The import UI already followed { error: { redirect } } for status and
jobs. This MR uses the same redirect for an expired session on create,
and for a Bitbucket Server connection error, instead of a generic alert.
How to set up and validate locally
- Configure the Bitbucket Cloud OmniAuth provider on your GDK.
- Sign in, visit the Bitbucket Cloud import page, and authorize an account.
- Simulate an expired access token in the session (
bitbucket_expires_atin the past), or wait for the token to expire, then reload the repository list (/-/import/bitbucket/status). - Confirm the session credentials are cleared and you are sent through Bitbucket authorization again, after which the repository list loads.
- Confirm a JSON
statusorcreaterequest made with an expired session returns{ error: { redirect } }and does not redirect the request itself to Bitbucket. - While an import is in progress, simulate an expired-but-present
session token and confirm
realtime_changesstill returns import status (it should not clear the session or redirect).
You can also run:
bin/rspec spec/controllers/import/bitbucket_controller_spec.rbMR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.