()
| 15 | |
| 16 | // This component handles the GitHub App installation callback |
| 17 | export default function GitHubCallback() { |
| 18 | const router = useRouter(); |
| 19 | const searchParams = useSearchParams(); |
| 20 | const { token } = useAuthContext(); |
| 21 | |
| 22 | const [status, setStatus] = useState<'loading' | 'success' | 'error'>( |
| 23 | 'loading' |
| 24 | ); |
| 25 | const [errorMessage, setErrorMessage] = useState<string | null>(null); |
| 26 | |
| 27 | const hasCalledBackend = useRef(false); // Add guard flag here |
| 28 | |
| 29 | useEffect(() => { |
| 30 | // Extract installation ID from search params |
| 31 | const githubCode = searchParams.get('code'); |
| 32 | const installationId = searchParams.get('installation_id'); |
| 33 | const setupAction = searchParams.get('setup_action'); |
| 34 | |
| 35 | if (!token || hasCalledBackend.current) return; // Prevent multiple calls |
| 36 | |
| 37 | console.log('GitHub Callback:', { |
| 38 | githubCode, |
| 39 | installationId, |
| 40 | setupAction, |
| 41 | }); |
| 42 | |
| 43 | // If there's no installation ID, this might be a cancellation |
| 44 | if (!installationId || !githubCode) { |
| 45 | // Check if it was canceled |
| 46 | if (searchParams.get('canceled') === 'true') { |
| 47 | setStatus('error'); |
| 48 | setErrorMessage('GitHub App installation was canceled.'); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | // Or if it's a delete operation |
| 53 | if (setupAction === 'delete') { |
| 54 | setStatus('success'); |
| 55 | // We could call a different endpoint to remove the installationId |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | setStatus('error'); |
| 60 | setErrorMessage('No installation ID was provided.'); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | hasCalledBackend.current = true; |
| 65 | |
| 66 | // Call the backend directly to store the installation ID |
| 67 | const storeInstallation = async () => { |
| 68 | try { |
| 69 | // Use environment variable or hardcoded value for backend URL |
| 70 | const backendUrl = |
| 71 | process.env.NEXT_PUBLIC_BACKEND_URL || 'http://localhost:8080'; |
| 72 | |
| 73 | const response = await fetch(`${backendUrl}/github/storeInstallation`, { |
| 74 | method: 'POST', |
nothing calls this directly
no test coverage detected