get-deployment-commit.ts 1.2 KB

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