Skip to content
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
64 changes: 40 additions & 24 deletions web-server/src/content/Dashboards/useIntegrationHandlers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import { fetchCurrentOrg } from '@/slices/auth';
import {
unlinkProvider,
checkGitHubValidity,
linkProvider
linkProvider,
getMissingPATScopes
} from '@/utils/auth';
import { depFn } from '@/utils/fn';

Expand Down Expand Up @@ -70,7 +71,7 @@ const ConfigureGithubModalBody: FC<{

return (
<FlexBox gap2>
<FlexBox gap={2} minWidth={'400px'} col>
<FlexBox gap={2} minWidth={'400px'} maxHeight={'255px'} col>
<FlexBox>Enter you Github token below.</FlexBox>
<FlexBox fullWidth minHeight={'80px'} col>
<TextField
Expand All @@ -87,7 +88,7 @@ const ConfigureGithubModalBody: FC<{
</Line>
</FlexBox>

<FlexBox gap={4} alignCenter justifyBetween>
<FlexBox justifyBetween alignCenter mt={'auto'}>
<FlexBox col sx={{ opacity: 0.8 }}>
<Line>Learn more about Github</Line>
<Line>
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions web-server/src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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);
}
};