Skip to content
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

update isSameCredentials check to prefer internalId when available #3173

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/heavy-colts-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/figma-plugin": patch
---

Fixed an issue where sometimes duplicate sync providers would show up
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ describe('isSameCredentials', () => {
branch: 'main',
internalId: '123',
};
const storedSupernova = {
id: '456',
provider: StorageProviderType.SUPERNOVA,
designSystemUrl: 'https://example.com',
mapping: { foo: 'bar' },
};
const storedTokensStudio = {
id: '789',
provider: StorageProviderType.TOKENS_STUDIO,
};
const correctCredentials = {
id: '123',
provider: StorageProviderType.JSONBIN,
Expand All @@ -39,12 +49,65 @@ describe('isSameCredentials', () => {
branch: 'default',
internalId: '123',
};
const correctSupernovaCredentials = {
id: '456',
provider: StorageProviderType.SUPERNOVA,
secret: 'def',
name: 'supernova',
designSystemUrl: 'https://example.com',
mapping: { foo: 'bar' },
};
const correctTokensStudioCredentials = {
id: '789',
provider: StorageProviderType.TOKENS_STUDIO,
secret: 'ghi',
name: 'tokensstudio',
};

expect(isSameCredentials(correctCredentials, storedJSONBin)).toBe(true);
expect(isSameCredentials({ ...correctCredentials, id: '456' }, storedJSONBin)).toBe(false);
expect(isSameCredentials(correctGitHubCredentials, storedGitHub)).toBe(true);
expect(isSameCredentials({ ...correctGitHubCredentials, filePath: 'tokens2.json' }, storedGitHub)).toBe(false);
expect(isSameCredentials({ ...correctGitHubCredentials, branch: 'next' }, storedGitHub)).toBe(false);
expect(isSameCredentials(gitHubCredentialsWithDifferentBranch, storedGitHub)).toBe(true);
expect(isSameCredentials(correctSupernovaCredentials, storedSupernova)).toBe(true);
expect(isSameCredentials({ ...correctSupernovaCredentials, designSystemUrl: 'https://different.com' }, storedSupernova)).toBe(false);
expect(isSameCredentials({ ...correctSupernovaCredentials, mapping: { baz: 'qux' } }, storedSupernova)).toBe(false);
expect(isSameCredentials(correctTokensStudioCredentials, storedTokensStudio)).toBe(true);
expect(isSameCredentials({ ...correctTokensStudioCredentials, id: '987' }, storedTokensStudio)).toBe(false);
});

it('should use internalId for comparison when available', () => {
const storedWithInternalId = {
id: 'old-id',
provider: StorageProviderType.GITHUB,
filePath: 'old-path.json',
branch: 'old-branch',
internalId: 'internal-123',
};
const credentialWithInternalId = {
id: 'new-id',
provider: StorageProviderType.GITHUB,
filePath: 'new-path.json',
branch: 'new-branch',
internalId: 'internal-123',
};

expect(isSameCredentials(credentialWithInternalId, storedWithInternalId)).toBe(true);
});

it('should return false for unsupported provider types', () => {
const unsupportedProvider = {
id: '999',
provider: 'UNSUPPORTED' as StorageProviderType,
};
const credential = {
id: '999',
provider: 'UNSUPPORTED' as StorageProviderType,
secret: 'xyz',
name: 'unsupported',
};

expect(isSameCredentials(credential, unsupportedProvider)).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ function isSameCredentials(
credential: StorageTypeCredentials,
stored: StorageType | StorageTypeFormValues<false>,
): boolean {
// If the internalId is present, we use it to check for equality
if (stored.provider !== StorageProviderType.LOCAL && credential.internalId && stored.internalId) {
return credential.internalId === stored.internalId;
}

switch (stored.provider) {
case StorageProviderType.GITHUB:
case StorageProviderType.GITLAB:
case StorageProviderType.ADO:
case StorageProviderType.BITBUCKET: {
if (credential.internalId && stored.internalId && credential.internalId === stored.internalId) {
return true;
}
return (
credential.id === stored.id
&& credential.provider === stored.provider
Expand All @@ -30,6 +32,7 @@ function isSameCredentials(
credential.id === stored.id
&& credential.provider === stored.provider
&& credential.designSystemUrl === stored.designSystemUrl
&& JSON.stringify(credential.mapping) === JSON.stringify(stored.mapping)
);
case StorageProviderType.TOKENS_STUDIO:
return (
Expand Down
Loading