diff --git a/web-server/src/content/Dashboards/useIntegrationHandlers.tsx b/web-server/src/content/Dashboards/useIntegrationHandlers.tsx index f86076748..1e76939dd 100644 --- a/web-server/src/content/Dashboards/useIntegrationHandlers.tsx +++ b/web-server/src/content/Dashboards/useIntegrationHandlers.tsx @@ -15,7 +15,8 @@ import { fetchCurrentOrg } from '@/slices/auth'; import { unlinkProvider, checkGitHubValidity, - linkProvider + linkProvider, + getMissingPATScopes } from '@/utils/auth'; import { depFn } from '@/utils/fn'; @@ -70,7 +71,7 @@ const ConfigureGithubModalBody: FC<{ return ( - + Enter you Github token below. - + Learn more about Github @@ -114,29 +115,44 @@ const ConfigureGithubModalBody: FC<{ isLoading.true(); checkGitHubValidity(token.value) .then(async (isValid) => { - if (isValid) { - await linkProvider(token.value, orgId, Integration.GITHUB) - .then(() => { - dispatch(fetchCurrentOrg()); - enqueueSnackbar('Github linked successfully', { - variant: 'success', - autoHideDuration: 2000 - }); - onClose(); - }) - .catch((e) => { - setError( - 'Failed to link token, please try again later' - ); - console.error('Failed to link token', e); - }); - } else { - throw new Error('Invalid token'); + if (!isValid) throw new Error('Invalid token'); + }) + .then(async () => { + try { + const res = await getMissingPATScopes(token.value); + if (res.length) { + throw new Error( + `Token is missing scopes: ${res.join(', ')}` + ); + } + } catch (e) { + // @ts-ignore + throw new Error(e?.message, e); + } + }) + .then(async () => { + try { + return await linkProvider( + token.value, + orgId, + Integration.GITHUB + ); + } catch (e) { + throw new Error('Failed to link Github: ', e); } }) - .catch(() => { - setError('Invalid token, please check and try again'); - console.error('Invalid token'); + .then(() => { + dispatch(fetchCurrentOrg()); + enqueueSnackbar('Github linked successfully', { + variant: 'success', + autoHideDuration: 2000 + }); + onClose(); + }) + .catch((e) => { + setError(e); + console.error('Error while linking token: ', e.message); + setError(e.message); }) .finally(() => { isLoading.false(); diff --git a/web-server/src/utils/auth.ts b/web-server/src/utils/auth.ts index 85646e3f7..31f0504d2 100644 --- a/web-server/src/utils/auth.ts +++ b/web-server/src/utils/auth.ts @@ -34,7 +34,7 @@ export async function checkGitHubValidity( } } -const PAT_SCOPES = ['read:org', 'read:user', 'repo', 'workflow', 'xzczcx']; +const PAT_SCOPES = ['read:org', 'read:user', 'repo', 'workflow']; export const getMissingPATScopes = async (pat: string) => { try { const response = await axios.get('https://api.github.com', { @@ -49,6 +49,6 @@ export const getMissingPATScopes = async (pat: string) => { const userScopes = scopesString.split(',').map((scope) => scope.trim()); return PAT_SCOPES.filter((scope) => !userScopes.includes(scope)); } catch (error) { - return PAT_SCOPES; + throw new Error('Failed to get missing PAT scopes', error); } };