Skip to content

Commit

Permalink
update isSameCredentials check to prefer internalId when available
Browse files Browse the repository at this point in the history
  • Loading branch information
six7 committed Oct 12, 2024
1 parent 7be0b29 commit 6f75c8b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
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

0 comments on commit 6f75c8b

Please sign in to comment.