|
| 1 | +import { sew } from "@/middleware/sew"; |
| 2 | +import { invalidGitRef, notFound, ServiceError, unexpectedError } from '@/lib/serviceError'; |
| 3 | +import { withOptionalAuth } from '@/middleware/withAuth'; |
| 4 | +import { getRepoPath } from '@sourcebot/shared'; |
| 5 | +import { z } from 'zod'; |
| 6 | +import { simpleGit } from 'simple-git'; |
| 7 | +import { commitDetailSchema } from './schemas'; |
| 8 | +import { isGitRefValid } from './utils'; |
| 9 | + |
| 10 | +export type CommitDetail = z.infer<typeof commitDetailSchema>; |
| 11 | + |
| 12 | +type GetCommitRequest = { |
| 13 | + repo: string; |
| 14 | + ref: string; |
| 15 | +} |
| 16 | + |
| 17 | +// Field separator that won't appear in commit data |
| 18 | +const FIELD_SEP = '\x1f'; |
| 19 | +const FORMAT = [ |
| 20 | + '%H', // hash |
| 21 | + '%aI', // author date ISO 8601 |
| 22 | + '%s', // subject |
| 23 | + '%D', // refs |
| 24 | + '%b', // body |
| 25 | + '%aN', // author name |
| 26 | + '%aE', // author email |
| 27 | + '%P', // parent hashes (space-separated) |
| 28 | +].join(FIELD_SEP); |
| 29 | + |
| 30 | +export const getCommit = async ({ |
| 31 | + repo: repoName, |
| 32 | + ref, |
| 33 | +}: GetCommitRequest): Promise<CommitDetail | ServiceError> => sew(() => |
| 34 | + withOptionalAuth(async ({ org, prisma }) => { |
| 35 | + const repo = await prisma.repo.findFirst({ |
| 36 | + where: { |
| 37 | + name: repoName, |
| 38 | + orgId: org.id, |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + if (!repo) { |
| 43 | + return notFound(`Repository "${repoName}" not found.`); |
| 44 | + } |
| 45 | + |
| 46 | + if (!isGitRefValid(ref)) { |
| 47 | + return invalidGitRef(ref); |
| 48 | + } |
| 49 | + |
| 50 | + const { path: repoPath } = getRepoPath(repo); |
| 51 | + const git = simpleGit().cwd(repoPath); |
| 52 | + |
| 53 | + try { |
| 54 | + const output = (await git.raw([ |
| 55 | + 'log', |
| 56 | + '-1', |
| 57 | + `--format=${FORMAT}`, |
| 58 | + ref, |
| 59 | + ])).trim(); |
| 60 | + |
| 61 | + const fields = output.split(FIELD_SEP); |
| 62 | + if (fields.length < 8) { |
| 63 | + return unexpectedError(`Failed to parse commit data for revision "${ref}".`); |
| 64 | + } |
| 65 | + |
| 66 | + const [hash, date, message, refs, body, authorName, authorEmail, parentStr] = fields; |
| 67 | + const parents = parentStr.trim() === '' ? [] : parentStr.trim().split(' '); |
| 68 | + |
| 69 | + return { |
| 70 | + hash, |
| 71 | + date, |
| 72 | + message, |
| 73 | + refs, |
| 74 | + body, |
| 75 | + authorName, |
| 76 | + authorEmail, |
| 77 | + parents, |
| 78 | + }; |
| 79 | + } catch (error: unknown) { |
| 80 | + const errorMessage = error instanceof Error ? error.message : String(error); |
| 81 | + |
| 82 | + if (errorMessage.includes('not a git repository')) { |
| 83 | + return unexpectedError( |
| 84 | + `Invalid git repository at ${repoPath}. ` + |
| 85 | + `The directory exists but is not a valid git repository.` |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + if (errorMessage.includes('unknown revision') || errorMessage.includes('bad object')) { |
| 90 | + return notFound(`Revision "${ref}" not found in repository "${repoName}".`); |
| 91 | + } |
| 92 | + |
| 93 | + if (error instanceof Error) { |
| 94 | + throw new Error( |
| 95 | + `Failed to get commit in repository ${repoName}: ${error.message}` |
| 96 | + ); |
| 97 | + } else { |
| 98 | + throw new Error( |
| 99 | + `Failed to get commit in repository ${repoName}: ${errorMessage}` |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + })); |
0 commit comments