Skip to content

Commit

Permalink
2822 gitlab sync is not allowing users to push tokens (#2854)
Browse files Browse the repository at this point in the history
* feat: add token format option to get exact resolved value

* revert the DTCG format

* feat: add error handler message to remote token storage

* fix typo error

* remove un-necessary code
  • Loading branch information
robinhoodie0823 authored Jun 15, 2024
1 parent 1a7809e commit f38aa4d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function useGitLab() {
themes,
metadata: {},
};
} catch (e) {
} catch (e: any) {
closePushDialog();
console.log('Error pushing to GitLab', e);
if (e instanceof Error && e.message === ErrorMessages.GIT_MULTIFILE_PERMISSION_ERROR) {
Expand All @@ -110,6 +110,12 @@ export function useGitLab() {
errorMessage: ErrorMessages.GIT_MULTIFILE_PERMISSION_ERROR,
};
}
if (e instanceof Error && e.message === ErrorMessages.GITLAB_PUSH_TO_PROTECTED_BRANCH_ERROR) {
return {
status: 'failure',
errorMessage: ErrorMessages.GITLAB_PUSH_TO_PROTECTED_BRANCH_ERROR,
};
}
return {
status: 'failure',
errorMessage: ErrorMessages.GITLAB_CREDENTIAL_ERROR,
Expand Down
17 changes: 11 additions & 6 deletions packages/tokens-studio-for-figma/src/app/store/remoteTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { isEqual } from '@/utils/isEqual';
import usePullDialog from '../hooks/usePullDialog';
import { Tabs } from '@/constants/Tabs';
import { useTokensStudio } from './providers/tokens-studio';
import { notifyToUI } from '@/plugin/notifiers';

export type PushOverrides = { branch: string, commitMessage: string };

Expand Down Expand Up @@ -325,34 +326,38 @@ export default function useRemoteTokens() {
async ({ context = api, overrides }: { context?: StorageTypeCredentials, overrides?: PushOverrides } = {}) => {
const isFolder = 'filePath' in context && !context.filePath?.endsWith('.json');
track('pushTokens', { provider: context.provider, isFolder });
let pushResult;
switch (context.provider) {
case StorageProviderType.GITHUB: {
await pushTokensToGitHub(context, overrides);
pushResult = await pushTokensToGitHub(context, overrides);
break;
}
case StorageProviderType.GITLAB: {
await pushTokensToGitLab(context);
pushResult = await pushTokensToGitLab(context);
break;
}
case StorageProviderType.BITBUCKET: {
await pushTokensToBitbucket(context);
pushResult = await pushTokensToBitbucket(context);
break;
}
case StorageProviderType.ADO: {
await pushTokensToADO(context);
pushResult = await pushTokensToADO(context);
break;
}
case StorageProviderType.SUPERNOVA: {
await pushTokensToSupernova(context);
pushResult = await pushTokensToSupernova(context);
break;
}
case StorageProviderType.TOKENS_STUDIO: {
await pushTokensToTokensStudio(context);
pushResult = await pushTokensToTokensStudio(context);
break;
}
default:
throw new Error('Not implemented');
}
if (pushResult.status && pushResult.status === 'failure') {
notifyToUI(pushResult.errorMessage, { error: true });
}
},
[
api,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export enum ErrorMessages {
ID_NON_EXIST_ERROR = 'ID or Secret should be exist',
JSONBIN_CREATE_ERROR = 'Error creating JSONbin token storage',
GIT_MULTIFILE_PERMISSION_ERROR = 'You try to save a multi-file project as a free user. Upgrade to Pro or add a json file at the end of the filepath (tokens.json)',
GITLAB_PUSH_TO_PROTECTED_BRANCH_ERROR = '403 Forbidden - You are not allowed to push into this branch',
}
27 changes: 17 additions & 10 deletions packages/tokens-studio-for-figma/src/storage/GitlabTokenStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,16 +229,23 @@ export class GitlabTokenStorage extends GitTokenStorage {
})));
}

const response = await this.gitlabClient.Commits.create(
this.projectId,
branch,
message,
gitlabActions,
shouldCreateBranch ? {
startBranch: branches[0],
} : undefined,
);
return !!response;
try {
const response = await this.gitlabClient.Commits.create(
this.projectId,
branch,
message,
gitlabActions,
shouldCreateBranch ? {
startBranch: branches[0],
} : undefined,
);
return !!response;
} catch (e: any) {
if (e.cause.description && String(e.cause.description).includes(ErrorMessages.GITLAB_PUSH_TO_PROTECTED_BRANCH_ERROR)) {
throw new Error(ErrorMessages.GITLAB_PUSH_TO_PROTECTED_BRANCH_ERROR);
}
throw new Error(e);
}
}

public async getLatestCommitDate(): Promise<Date | null> {
Expand Down

0 comments on commit f38aa4d

Please sign in to comment.