Skip to content

Commit 7f1a7b7

Browse files
committed
fix: enhance GitHub sync Modal with need-auth modal for new user
1 parent 37f8b84 commit 7f1a7b7

3 files changed

Lines changed: 99 additions & 14 deletions

File tree

services/web/modules/github-sync/app/src/GitHubSyncController.mjs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ async function getStatus(req, res) {
2323
if (!status) {
2424
return res.json({ enabled: false })
2525
}
26-
2726
res.json(status)
2827
}
2928

@@ -62,6 +61,16 @@ async function getProjectStatus(req, res) {
6261
status.owner = owner
6362
}
6463

64+
// check if owner's GitHub credentials are still valid. If not, return enabled: false to trigger re-auth flow in frontend
65+
const credentials = await GitHubSyncHandler.promises.getGitHubAccessTokenForUser(ownerId)
66+
if (!credentials) {
67+
status.enabled = false
68+
return res.json({
69+
enabled: false
70+
}
71+
)
72+
}
73+
6574
// remove status. last_sync_sha and .last_sync_version
6675
if (status.last_sync_sha)
6776
delete status.last_sync_sha

services/web/modules/github-sync/app/src/GitHubSyncHandler.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ async function getUserGitHubStatus(userId) {
1515
if (!credentials) {
1616
return { available: true, enabled: false }
1717
}
18+
19+
// test if the token is still valid by making an API call to GitHub
20+
const token = await SecretsHelper.decryptAccessToken(credentials.auth_token_encrypted)
21+
try {
22+
await GitHubApiClient.listRepos(token, { per_page: 1 })
23+
} catch (err) {
24+
logger.warn({ userId, err }, 'GitHub token invalid, treating as not connected')
25+
return { available: true, enabled: false }
26+
}
27+
1828
return {
1929
available: true,
2030
enabled: true

services/web/modules/github-sync/frontend/js/components/github-integration-card.tsx

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,44 @@ import { useEditorManagerContext } from '@/features/ide-react/context/editor-man
3131
import { Trans } from 'react-i18next'
3232

3333

34-
type GitHubSyncModalStatus = 'loading' | 'export' | 'merge' | 'pushSubmit' | 'syncing' | 'conflict'
34+
type GitHubSyncModalStatus = 'loading' | 'export' | 'merge' | 'pushSubmit' | 'syncing' | 'conflict' | 'need-auth'
35+
36+
type GitHubSyncModalNeedAuthProps = {
37+
handleHide: () => void
38+
}
39+
40+
const GitHubSyncModalNeedAuth = ({ handleHide }: GitHubSyncModalNeedAuthProps) => {
41+
const { t } = useTranslation()
42+
const { appName } = getMeta('ol-ExposedSettings')
43+
return (
44+
<>
45+
<OLModalBody>
46+
<p>{t('link_to_github_description', { appName })}</p>
47+
</OLModalBody>
48+
<OLModalFooter>
49+
<OLButton
50+
variant="secondary"
51+
onClick={handleHide}
52+
>
53+
{t('close')}
54+
</OLButton>
55+
56+
<OLButton
57+
variant="primary"
58+
onClick={() => {
59+
window.open(
60+
'/github-sync/beginAuth',
61+
'githubAuth',
62+
'width=600,height=700'
63+
)
64+
}}
65+
>
66+
{t('link_to_github')}
67+
</OLButton>
68+
</OLModalFooter>
69+
</>
70+
)
71+
}
3572

3673
type GitHubSyncModalSyncingProps = {
3774
handleHide: () => void
@@ -607,27 +644,53 @@ function GitHubSyncModal({
607644
if (!show || !project || modalStatus !== 'loading') {
608645
return
609646
}
610-
const fetchGitHubSyncStatus = async () => {
647+
648+
const fetchUserGitHubSyncStatus = async () => {
611649
try {
612-
const data = await getJSON(`/project/${projectId}/github-sync/status`)
613-
if (data.enabled && data.merge_status === 'success') {
614-
setModalStatus('merge')
615-
setProjectSyncStatus(data)
616-
} else if (data.enabled && data.merge_status === 'failure') {
617-
setModalStatus('conflict')
618-
setProjectSyncStatus(data)
619-
}
620-
else {
621-
setModalStatus('export')
650+
const data = await getJSON('/user/github-sync/status')
651+
if (data.enabled) {
652+
// fetch project github sync status
653+
const projectStatus = await getJSON(`/project/${projectId}/github-sync/status`)
654+
if (projectStatus.enabled && projectStatus.merge_status === 'success') {
655+
setModalStatus('merge')
656+
setProjectSyncStatus(projectStatus)
657+
} else if (projectStatus.enabled && projectStatus.merge_status === 'failure') {
658+
setModalStatus('conflict')
659+
setProjectSyncStatus(projectStatus)
660+
} else {
661+
setModalStatus('export')
662+
}
663+
} else {
664+
setModalStatus('need-auth')
622665
}
623666
} catch (err: any) {
624667
console.error('Failed to fetch GitHub sync status', err)
625668
}
626669
}
627670

628-
fetchGitHubSyncStatus()
671+
// const fetchGitHubSyncStatus = async () => {
672+
// try {
673+
// const data = await getJSON(`/project/${projectId}/github-sync/status`)
674+
// if (data.enabled && data.merge_status === 'success') {
675+
// setModalStatus('merge')
676+
// setProjectSyncStatus(data)
677+
// } else if (data.enabled && data.merge_status === 'failure') {
678+
// setModalStatus('conflict')
679+
// setProjectSyncStatus(data)
680+
// }
681+
// else {
682+
// setModalStatus('export')
683+
// }
684+
// } catch (err: any) {
685+
// console.error('Failed to fetch GitHub sync status', err)
686+
// }
687+
// }
688+
689+
fetchUserGitHubSyncStatus()
629690
}, [show, modalStatus])
630691

692+
693+
631694
useEffect(() => {
632695
if (!show)
633696
setCommitMessage('')
@@ -662,6 +725,9 @@ function GitHubSyncModal({
662725
setModalStatus={setModalStatus}
663726
/>
664727
}
728+
{
729+
modalStatus === 'need-auth' && <GitHubSyncModalNeedAuth handleHide={handleHide} />
730+
}
665731

666732
</OLModal >
667733
)

0 commit comments

Comments
 (0)