forked from ipdxco/github-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.ts
More file actions
155 lines (151 loc) · 4.67 KB
/
github.ts
File metadata and controls
155 lines (151 loc) · 4.67 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import {mock} from 'node:test'
export interface GitHubConfig {
repositories?: {
name: string
collaborators?: {
login: string
}[]
branchProtectionRules?: {
pattern: string
}[]
invitations?: {
login: string
}[]
labels?: {
name: string
}[]
}[]
teams?: {
name: string
members?: {
login: string
}[]
invitations?: {
login: string
}[]
repositories?: {
name: string
}[]
}[]
invitations?: {
login: string
}[]
members?: {
login: string
}[]
}
export function mockGitHub(config: GitHubConfig = {}): void {
mock.module('@octokit/auth-app', {
namedExports: {
createAppAuth: () => () => ({token: undefined})
}
})
mock.module('@octokit/rest', {
namedExports: {
Octokit: {
plugin: () => {
// return Constructor of Octokit-like object
return class {
issues = {
listCommentsForRepo: async () => [],
listForRepo: async () => [],
listLabelsForRepo: async (opts: {repo: string}) =>
config?.repositories?.find(r => r.name === opts.repo)?.labels ??
[]
}
orgs = {
getMembershipForUser: async () => ({
data: {}
}),
listMembers: async () => config?.members ?? [],
listPendingInvitations: async () => config?.invitations ?? []
}
pulls = {
listReviewCommentsForRepo: async () => []
}
repos = {
get: async (opts: {owner: string; repo: string}) => ({
data: {
owner: {
login: opts.owner
},
name: opts.repo,
default_branch: 'main'
}
}),
getContent: async (opts: {
owner: string
repo: string
path: string
ref: string
}) => ({
data: {
path: opts.path,
url: `https://github.com/${opts.owner}/${opts.repo}/blob/${opts.ref}/${opts.path}`,
ref: opts.ref
}
}),
listActivities: async () => [],
listCollaborators: async (opts: {repo: string}) =>
config?.repositories?.find(r => r.name === opts.repo)
?.collaborators ?? [],
listCommitCommentsForRepo: async () => [],
listForOrg: async () => config?.repositories ?? [],
listInvitations: async (opts: {repo: string}) => {
const repository = config?.repositories?.find(
r => r.name === opts.repo
)
return repository?.invitations?.map(invitee => ({
repository,
invitee
}))
}
}
teams = {
getMembershipForUserInOrg: async () => ({
data: {}
}),
list: async () =>
config?.teams?.map(t => ({slug: t.name, ...t})) ?? [],
listMembersInOrg: async (opts: {team_slug: string}) =>
config?.teams?.find(t => t.name === opts.team_slug)?.members ??
[],
listPendingInvitationsInOrg: async (opts: {team_slug: string}) =>
config?.teams?.find(t => t.name === opts.team_slug)
?.invitations ?? [],
listReposInOrg: async (opts: {team_slug: string}) =>
config?.teams?.find(t => t.name === opts.team_slug)
?.repositories ?? []
}
async paginate<T, K>(
f: (opts: K) => Promise<T>,
opts: K
): Promise<T> {
return f(opts)
}
async graphql(query: string): Promise<unknown> {
// extract owner and repo from query using repository\(owner: \"([^\"]+)\", name: \"([^\"]+)\"\)
const match = query.match(
/repository\(owner: "([^"]+)", name: "([^"]+)"\)/
)
if (match === null) {
throw new Error(`Could not find repository in query: ${query}`)
}
const [, , repo] = match
const nodes =
config.repositories?.find(r => r.name === repo)
?.branchProtectionRules ?? []
return {
repository: {
branchProtectionRules: {
nodes
}
}
}
}
}
}
}
}
})
}