get-deployment-commit.ts40 lines · main
| 1 | import { NextApiRequest, NextApiResponse } from 'next' |
| 2 | |
| 3 | async function getCommitTime(commitSha: string) { |
| 4 | try { |
| 5 | const response = await fetch(`https://github.com/briven/briven/commit/${commitSha}.json`, { |
| 6 | headers: { |
| 7 | Accept: 'application/json', |
| 8 | }, |
| 9 | }) |
| 10 | |
| 11 | if (!response.ok) { |
| 12 | throw new Error('Failed to fetch commit details') |
| 13 | } |
| 14 | |
| 15 | const data = await response.json() |
| 16 | return new Date(data.payload.commit.committedDate).toISOString() |
| 17 | } catch (error) { |
| 18 | console.error('Error fetching commit time:', error) |
| 19 | return 'unknown' |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | export default async function handler( |
| 24 | _req: NextApiRequest, |
| 25 | res: NextApiResponse<{ commitSha: string; commitTime: string }> |
| 26 | ) { |
| 27 | // Set cache control headers for 10 minutes so that we don't get banned by GitHub API |
| 28 | res.setHeader('Cache-Control', 's-maxage=600') |
| 29 | |
| 30 | // Get the build commit SHA from Vercel environment variable |
| 31 | const commitSha = process.env.VERCEL_GIT_COMMIT_SHA || 'development' |
| 32 | |
| 33 | // Only fetch commit time if we have a valid SHA |
| 34 | const commitTime = commitSha !== 'development' ? await getCommitTime(commitSha) : 'unknown' |
| 35 | |
| 36 | res.status(200).json({ |
| 37 | commitSha, |
| 38 | commitTime, |
| 39 | }) |
| 40 | } |