-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch-utils.ts
More file actions
106 lines (96 loc) · 1.97 KB
/
branch-utils.ts
File metadata and controls
106 lines (96 loc) · 1.97 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import type { GitHubContext, RepoInfo } from './types.js';
/**
* Checks if a branch exists in the repository
*/
export async function checkBranchExists({
ctx,
repo,
branch,
}: {
ctx: GitHubContext;
repo: RepoInfo;
branch: string;
}): Promise<boolean> {
try {
await ctx.github.rest.repos.getBranch({
...repo,
branch,
});
return true;
} catch (error: unknown) {
if (error && typeof error === 'object' && 'status' in error && error.status === 404) {
return false;
}
throw error;
}
}
/**
* Lists all branches in the repository
*/
export async function listAllBranches({
ctx,
repo,
limit = 100,
}: {
ctx: GitHubContext;
repo: RepoInfo;
limit?: number;
}): Promise<string[]> {
const branches: string[] = [];
let page = 1;
const perPage = Math.min(limit, 100);
while (branches.length < limit) {
const { data } = await ctx.github.rest.repos.listBranches({
...repo,
per_page: perPage,
page,
});
if (data.length === 0) {
break;
}
branches.push(...data.map((branch) => branch.name));
if (data.length < perPage) {
break; // No more pages
}
page++;
}
return branches.slice(0, limit);
}
/**
* Gets branch protection rules for a branch
*/
export async function getBranchProtection({
ctx,
repo,
branch,
}: {
ctx: GitHubContext;
repo: RepoInfo;
branch: string;
}) {
try {
const { data } = await ctx.github.rest.repos.getBranchProtection({
...repo,
branch,
});
return data;
} catch (error: unknown) {
if (error && typeof error === 'object' && 'status' in error && error.status === 404) {
return null; // No protection rules
}
throw error;
}
}
/**
* Gets the default branch of the repository
*/
export async function getDefaultBranch({
ctx,
repo,
}: {
ctx: GitHubContext;
repo: RepoInfo;
}): Promise<string> {
const { data } = await ctx.github.rest.repos.get(repo);
return data.default_branch;
}