import githubPaginate from 'kody:@kentcdodds/github/paginate'
import githubRequest from 'kody:@kentcdodds/github/request'
import { repoHtmlUrl } from './repo.ts'
import type { RepoSettings } from './types.ts'
export { repoHtmlUrl }
export type GithubCommitListItem = {
sha: string
html_url: string
commit: {
message: string
author: { name?: string; date?: string } | null
committer: { name?: string; date?: string } | null
}
parents: Array<{ sha: string }>
}
export type GithubCommitFile = {
filename: string
status: string
additions: number
deletions: number
changes: number
patch?: string
}
export type GithubCommitDetail = GithubCommitListItem & {
stats?: { additions: number; deletions: number; total: number }
files?: GithubCommitFile[]
}
export async function listBranchCommits(
repo: RepoSettings,
options: { maxPages?: number } = {},
): Promise<GithubCommitListItem[]> {
const result = await githubPaginate({
account: 'bot',
path: `/repos/${repo.owner}/${repo.repo}/commits`,
query: { sha: repo.branch, per_page: 100 },
maxPages: options.maxPages ?? 40,
})
return result.items as GithubCommitListItem[]
}
export async function getCommitDetail(
repo: RepoSettings,
sha: string,
): Promise<GithubCommitDetail> {
const response = await githubRequest({
account: 'bot',
path: `/repos/${repo.owner}/${repo.repo}/commits/${sha}`,
throwOnError: true,
})
return response.data as GithubCommitDetail
}
export async function getRepoTextFile(
repo: RepoSettings,
path: string,
): Promise<string | null> {
const response = await githubRequest({
account: 'bot',
path: `/repos/${repo.owner}/${repo.repo}/contents/${path}`,
headers: { Accept: 'application/vnd.github.raw' },
query: { ref: repo.branch },
})
if (!response.ok) return null
if (typeof response.text === 'string' && response.text && !response.text.startsWith('{')) {
return response.text
}
if (typeof response.data === 'string') return response.data
return null
}