Repository navigation
fix(cross-dataset-duplicator): use destination workspace auth for cross-project duplication #2265
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
Open
tnaughts
wants to merge
3
commits into
main
Choose a base branch
from
fix/cdd-cross-project-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
bdd21bd
fix(cross-dataset-duplicator): use destination workspace auth for cro…
tnaughts 9aea088
test(cross-dataset-duplicator): annotate mock type assertions for oxlint
tnaughts f8c71cf
fix(cross-dataset-duplicator): keep payload listed when not logged in…
tnaughts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sanity/cross-dataset-duplicator': patch | ||
| --- | ||
|
|
||
| Fix duplicating to a Workspace in another Project failing with "Session does not match project host". The destination client now comes from the destination Workspace's own auth store instead of re-pointing the origin client, whose credentials are scoped to the origin Project. |
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
96 changes: 96 additions & 0 deletions
96
plugins/@sanity/cross-dataset-duplicator/src/helpers/getDestinationClient.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import type {SanityClient} from '@sanity/client' | ||
| import type {AuthState, AuthStore} from 'sanity' | ||
| import {describe, expect, test, vi} from 'vitest' | ||
|
|
||
| import {getDestinationClient} from './getDestinationClient' | ||
|
|
||
| type ClientConfig = {projectId: string; dataset?: string; apiVersion?: string; token?: string} | ||
|
|
||
| function createClient(config: ClientConfig): SanityClient { | ||
| const client = { | ||
| config: () => config, | ||
| withConfig: (next: Partial<ClientConfig>) => createClient({...config, ...next}), | ||
| } | ||
|
|
||
| // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- only config() and withConfig() are called | ||
| return client as unknown as SanityClient | ||
| } | ||
|
|
||
| // Minimal auth store whose `state` emits synchronously on subscribe, like the | ||
| // Studio's replayed auth state | ||
| function createAuth(state: Partial<AuthState>) { | ||
| const unsubscribe = vi.fn() | ||
| const store = { | ||
| state: { | ||
| subscribe: ({next}: {next: (value: AuthState) => void}) => { | ||
| // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- only authenticated and client are read | ||
| next(state as AuthState) | ||
| return {unsubscribe} | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- only state.subscribe() is called | ||
| const auth = store as unknown as AuthStore | ||
|
|
||
| return {auth, unsubscribe} | ||
| } | ||
|
|
||
| const originClient = createClient({ | ||
| projectId: 'projA', | ||
| dataset: 'production', | ||
| token: 'projA-token', | ||
| }) | ||
|
|
||
| describe('getDestinationClient', () => { | ||
| test('re-points the origin client for a destination in the same project', async () => { | ||
| const {auth} = createAuth({authenticated: false}) | ||
| const client = await getDestinationClient({ | ||
| originClient, | ||
| destination: {projectId: 'projA', dataset: 'staging', auth}, | ||
| apiVersion: '2025-02-19', | ||
| }) | ||
|
|
||
| expect(client?.config()).toEqual({ | ||
| projectId: 'projA', | ||
| dataset: 'staging', | ||
| apiVersion: '2025-02-19', | ||
| token: 'projA-token', | ||
| }) | ||
| }) | ||
|
|
||
| test("uses the destination workspace's client for a destination in another project", async () => { | ||
| const {auth, unsubscribe} = createAuth({ | ||
| authenticated: true, | ||
| client: createClient({projectId: 'projB', dataset: 'production', token: 'projB-token'}), | ||
| }) | ||
| const client = await getDestinationClient({ | ||
| originClient, | ||
| destination: {projectId: 'projB', dataset: 'staging', auth}, | ||
| apiVersion: '2025-02-19', | ||
| }) | ||
|
|
||
| expect(client?.config()).toEqual({ | ||
| projectId: 'projB', | ||
| dataset: 'staging', | ||
| apiVersion: '2025-02-19', | ||
| token: 'projB-token', | ||
| }) | ||
|
|
||
| await Promise.resolve() | ||
| expect(unsubscribe).toHaveBeenCalledOnce() | ||
| }) | ||
|
|
||
| test('returns null when not logged in to a destination in another project', async () => { | ||
| const {auth} = createAuth({ | ||
| authenticated: false, | ||
| client: createClient({projectId: 'projB'}), | ||
| }) | ||
| const client = await getDestinationClient({ | ||
| originClient, | ||
| destination: {projectId: 'projB', dataset: 'production', auth}, | ||
| }) | ||
|
|
||
| expect(client).toBeNull() | ||
| }) | ||
| }) |
56 changes: 56 additions & 0 deletions
56
plugins/@sanity/cross-dataset-duplicator/src/helpers/getDestinationClient.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import type {SanityClient} from '@sanity/client' | ||
| import type {AuthState, AuthStore, WorkspaceSummary} from 'sanity' | ||
|
|
||
| type GetDestinationClientOptions = { | ||
| originClient: SanityClient | ||
| destination: Pick<WorkspaceSummary, 'projectId' | 'dataset' | 'auth'> | ||
| apiVersion?: string | ||
| } | ||
|
|
||
| // Resolve with the first auth state the store emits. The subscription may emit | ||
| // synchronously, so unsubscribe on the next microtask rather than inside `next`. | ||
| function getCurrentAuthState(auth: AuthStore): Promise<AuthState> { | ||
| return new Promise((resolve, reject) => { | ||
| const subscription = auth.state.subscribe({ | ||
| next: (state) => { | ||
| resolve(state) | ||
| queueMicrotask(() => subscription.unsubscribe()) | ||
| }, | ||
| error: reject, | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Get a client that can read from and write to the destination Workspace. | ||
| * | ||
| * Within the same Project the origin client's credentials are valid, so it is | ||
| * re-pointed at the destination Dataset. Credentials are scoped to a Project | ||
| * though (e.g. the token stored by the default `dual` login method), so for a | ||
| * destination in another Project the client comes from that Workspace's own | ||
| * auth store instead. | ||
| * | ||
| * Returns `null` if the user is not logged in to the destination Workspace. | ||
| */ | ||
| export async function getDestinationClient( | ||
| options: GetDestinationClientOptions, | ||
| ): Promise<SanityClient | null> { | ||
| const {originClient, destination, apiVersion} = options | ||
| const config = apiVersion ? {apiVersion} : {} | ||
|
|
||
| if (destination.projectId === originClient.config().projectId) { | ||
| return originClient.withConfig({ | ||
| ...config, | ||
| dataset: destination.dataset, | ||
| projectId: destination.projectId, | ||
| }) | ||
| } | ||
|
|
||
| const {authenticated, client} = await getCurrentAuthState(destination.auth) | ||
|
|
||
| if (!authenticated) { | ||
| return null | ||
| } | ||
|
|
||
| return client.withConfig({...config, dataset: destination.dataset}) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.