api/src/routes/private/github.ts

144 lines · 4166 bytes

  1// GitHub repo info, aggregates info, languages, tags, contributors, commits
  2import { createRoute, z } from '@hono/zod-openapi'
  3import { fetchJson } from '@/lib/fetch'
  4import { githubHeaders } from '@/lib/github'
  5import { freshSeconds } from '@/lib/cache/freshness'
  6import { newApp } from '@/lib/openapi'
  7import { ErrorResponse, GithubRepoSchema, Ok } from '@/schemas'
  8
  9const app = newApp()
 10
 11const FRESH_TTL = freshSeconds('github')
 12
 13interface RepoInfo {
 14  owner?: { login?: string; avatar_url?: string }
 15  description?: string
 16  html_url?: string
 17  homepage?: string
 18  language?: string
 19  topics?: string[]
 20  license?: { spdx_id?: string; name?: string }
 21  fork?: boolean
 22  archived?: boolean
 23  parent?: { full_name?: string }
 24  created_at?: string
 25  updated_at?: string
 26  pushed_at?: string
 27  size?: number
 28  stargazers_count?: number
 29  forks_count?: number
 30  watchers_count?: number
 31  open_issues_count?: number
 32}
 33
 34interface Tag {
 35  name: string
 36  commit?: { sha?: string }
 37  zipball_url?: string
 38  tarball_url?: string
 39}
 40
 41interface Contributor {
 42  login?: string
 43  avatar_url?: string
 44  html_url?: string
 45  contributions?: number
 46}
 47
 48interface CommitAuthor {
 49  name?: string
 50  date?: string
 51}
 52
 53interface Commit {
 54  sha?: string
 55  commit?: { author?: CommitAuthor; message?: string }
 56  author?: { login?: string; avatar_url?: string }
 57}
 58
 59const mapInfo = (info: RepoInfo) => ({
 60  ownerUsername: info.owner?.login ?? '',
 61  ownerAvatar: info.owner?.avatar_url ?? '',
 62  description: info.description ?? '',
 63  url: info.html_url ?? '',
 64  homepage: info.homepage ?? '',
 65  language: info.language ?? '',
 66  topics: info.topics ?? [],
 67  license: info.license?.spdx_id ?? '',
 68  licenseName: info.license?.name ?? '',
 69  isFork: info.fork ?? false,
 70  isArchived: info.archived ?? false,
 71  forkParent: info.parent?.full_name ?? '',
 72  createdAt: info.created_at ?? '',
 73  updatedAt: info.updated_at ?? '',
 74  pushedAt: info.pushed_at ?? '',
 75  size: info.size ?? 0,
 76  starCount: info.stargazers_count ?? 0,
 77  forksCount: info.forks_count ?? 0,
 78  watchersCount: info.watchers_count ?? 0,
 79  openIssues: info.open_issues_count ?? 0,
 80})
 81
 82const mapVersion = (tag: Tag) => ({
 83  name: tag.name,
 84  commit: tag.commit?.sha ?? '',
 85  zipball: tag.zipball_url ?? '',
 86  tarball: tag.tarball_url ?? '',
 87})
 88
 89const mapContributor = (person: Contributor) => ({
 90  username: person.login ?? '',
 91  avatar: person.avatar_url ?? '',
 92  url: person.html_url ?? '',
 93  contributions: person.contributions ?? 0,
 94})
 95
 96const mapCommit = (commit: Commit) => ({
 97  sha: commit.sha ?? '',
 98  authorName: commit.commit?.author?.name ?? '',
 99  authorDate: commit.commit?.author?.date ?? '',
100  message: commit.commit?.message ?? '',
101  authorUsername: commit.author?.login ?? '',
102  authorAvatar: commit.author?.avatar_url ?? '',
103})
104
105const route = createRoute({
106  method: 'get',
107  path: '/enrich/github/{owner}/{repo}',
108  tags: ['Enrichment'],
109  summary: 'GitHub repo info',
110  request: { params: z.object({ owner: z.string(), repo: z.string() }) },
111  responses: {
112    200: Ok(GithubRepoSchema),
113    401: ErrorResponse,
114    500: ErrorResponse,
115    502: ErrorResponse,
116  },
117})
118
119// Fan out to the repo, languages, tags, contributors and commits endpoints
120app.openapi(route, async (c) => {
121  const { owner, repo } = c.req.valid('param')
122  const slug = `${owner}/${repo}`
123  const headers = githubHeaders(c.env.GITHUB_TOKEN)
124  const data = await c.var.storage.fetch(`gh:${slug}`, FRESH_TTL, async () => {
125    const base = `https://api.github.com/repos/${slug}`
126    const [info, languages, tags, contributors, commits] = await Promise.all([
127      fetchJson<RepoInfo>(base, { headers }),
128      fetchJson<Record<string, number>>(`${base}/languages`, { headers }),
129      fetchJson<Tag[]>(`${base}/tags`, { headers }),
130      fetchJson<Contributor[]>(`${base}/contributors?per_page=100`, { headers }),
131      fetchJson<Commit[]>(`${base}/commits`, { headers }),
132    ])
133    return {
134      info: mapInfo(info),
135      languages: languages ?? {},
136      versions: tags.map(mapVersion),
137      contributors: contributors.map(mapContributor),
138      commits: commits.map(mapCommit),
139    }
140  })
141  return c.json(data, 200)
142})
143
144export default app