-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathgetRefs.ts
More file actions
68 lines (54 loc) · 2.07 KB
/
getRefs.ts
File metadata and controls
68 lines (54 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import 'server-only';
import { sew } from '@/actions';
import { notFound, unexpectedError } from '@/lib/serviceError';
import { withOptionalAuthV2 } from '@/withAuthV2';
import { createLogger, getRepoPath, repoMetadataSchema } from '@sourcebot/shared';
import { simpleGit } from 'simple-git';
const logger = createLogger('refs');
export const getRefs = async (params: { repoName: string }) => sew(() =>
withOptionalAuthV2(async ({ org, prisma }) => {
const { repoName } = params;
const repo = await prisma.repo.findFirst({
where: {
name: repoName,
orgId: org.id,
},
});
if (!repo) {
return notFound();
}
const metadata = repoMetadataSchema.safeParse(repo.metadata);
const indexedRevisions = metadata.success ? (metadata.data.indexedRevisions || []) : [];
const { path: repoPath } = getRepoPath(repo);
const git = simpleGit().cwd(repoPath);
let allBranches: string[] = [];
let allTags: string[] = [];
let defaultBranch: string | null = null;
try {
const branchResult = await git.branch();
allBranches = branchResult.all;
defaultBranch = branchResult.current || null;
} catch (error) {
logger.error('git branch failed.', { error });
return unexpectedError('git branch command failed.');
}
try {
const tagResult = await git.tags();
allTags = tagResult.all;
} catch (error) {
logger.error('git tags failed.', { error });
return unexpectedError('git tags command failed.');
}
const indexedRefsSet = new Set(indexedRevisions);
const branches = allBranches.filter(branch => {
return indexedRefsSet.has(`refs/heads/${branch}`);
});
const tags = allTags.filter(tag => {
return indexedRefsSet.has(`refs/tags/${tag}`);
});
return {
branches,
tags,
defaultBranch,
};
}));