|
| 1 | +import axios from 'axios'; |
| 2 | +import { Request, Response } from 'express'; |
| 3 | + |
| 4 | +const clientId = process.env.GITHUB_CLIENT_ID; |
| 5 | +const clientSecret = process.env.GITHUB_CLIENT_SECRET; |
| 6 | + |
| 7 | +export const handleGitHubOAuth = async (req: Request, res: Response) => { |
| 8 | + const { code } = req.body; |
| 9 | + |
| 10 | + if (!code) { |
| 11 | + return res.status(400).json({ error: 'Authorization code is required' }); |
| 12 | + } |
| 13 | + |
| 14 | + try { |
| 15 | + const tokenResponse = await axios.post('https://github.com/login/oauth/access_token', { |
| 16 | + headers: { 'Content-Type': 'application/json' }, |
| 17 | + client_id: clientId, |
| 18 | + client_secret: clientSecret, |
| 19 | + code: code, |
| 20 | + }) as any; |
| 21 | + |
| 22 | + const tokenData = await tokenResponse.data; |
| 23 | + const params = new URLSearchParams(tokenData); |
| 24 | + const accessToken = params.get('access_token'); |
| 25 | + |
| 26 | + if (!accessToken) { |
| 27 | + return res.status(400).json({ error: 'Failed to retrieve access token' }); |
| 28 | + } |
| 29 | + |
| 30 | + const userResponse = await axios.get('https://api.github.com/user', { |
| 31 | + headers: { Authorization: `Bearer ${accessToken}` }, |
| 32 | + }) as {status: number, data: { login?: string }, json: () => Promise<any>}; |
| 33 | + |
| 34 | + if (userResponse.status !== 200) { |
| 35 | + const errorText = await userResponse.data; |
| 36 | + return res.status(400).json({ error: errorText }); |
| 37 | + } |
| 38 | + |
| 39 | + const userData = await userResponse.data; |
| 40 | + res.json({ username: userData.login }); |
| 41 | + } catch (error) { |
| 42 | + console.error('Error during GitHub OAuth:', error); |
| 43 | + res.status(500).json({ error: 'Internal server error' }); |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +export const fetchGitHubRepos = async (req: Request, res: Response) => { |
| 48 | + const { username } = req.params; |
| 49 | + |
| 50 | + try { |
| 51 | + const apiUrl = `https://api.github.com/users/${username}/repos`; |
| 52 | + const response = await axios.get(apiUrl) as {data: any, status: number}; |
| 53 | + |
| 54 | + if (response.status !== 200) { |
| 55 | + return res.status(400).json({ error: `Error fetching repos: ${response.status}` }); |
| 56 | + } |
| 57 | + |
| 58 | + const repos = await response.data; |
| 59 | + res.json(repos); |
| 60 | + } catch (error) { |
| 61 | + console.error('Error fetching repos:', error); |
| 62 | + res.status(500).json({ error: 'Internal server error' }); |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +export const fetchRepoLanguages = async (req: Request, res: Response) => { |
| 67 | + const { repoUrl } = req.query; |
| 68 | + |
| 69 | + if (!repoUrl) { |
| 70 | + return res.status(400).json({ error: 'Repository URL is required' }); |
| 71 | + } |
| 72 | + |
| 73 | + try { |
| 74 | + // Convert the repoUrl to the GitHub API URL if necessary |
| 75 | + const apiUrl = (repoUrl as string).replace('https://github.com/', 'https://api.github.com/repos/'); |
| 76 | + const response = await axios.get(`${apiUrl}/languages`); |
| 77 | + |
| 78 | + if (response.status !== 200) { |
| 79 | + return res.status(400).json({ error: `Error fetching languages: ${response.statusText}` }); |
| 80 | + } |
| 81 | + |
| 82 | + res.json(response.data); |
| 83 | + } catch (error) { |
| 84 | + console.error('Error fetching languages:', error); |
| 85 | + res.status(500).json({ error: 'Internal server error' }); |
| 86 | + } |
| 87 | +}; |
0 commit comments