-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-api.js
More file actions
333 lines (305 loc) · 11.6 KB
/
github-api.js
File metadata and controls
333 lines (305 loc) · 11.6 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// DOCS: https://docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28#get-repository-content
async function getFileContent(owner, repo, path, githubToken) {
const token = githubToken || process.env.GITHUB_TOKEN;
try{
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/contents/${path}`, {
method: 'GET',
headers: {
'accept': 'application/vnd.github+json',
'authorization': `Bearer ${token}`
}
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Failed to get file content meta ${path}: ${response.status} - ${text}`);
}
const data = await response.json();
const rawRes = await fetch(data.download_url, {
headers: {
'accept': 'application/vnd.github.v3.raw',
'authorization': `Bearer ${token}`
}
});
if (!rawRes.ok) {
const text = await rawRes.text();
throw new Error(`Failed to download raw file ${path}: ${rawRes.status} - ${text}`);
}
const fileContent = await rawRes.text();
return fileContent;
} catch (error) {
console.error('Error getting file content:', error);
throw error;
}
}
async function getFileContentByContentURL(contentURL, githubToken){
const token = githubToken || process.env.GITHUB_TOKEN;
try{
const response = await fetch(contentURL, {
method: 'GET',
headers: {
'accept': 'application/vnd.github+json',
'authorization': `Bearer ${token}`
}
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Failed to get file by content URL: ${response.status} - ${text}`);
}
return response.text();
} catch (error) {
console.error('Error getting file content by content URL:', error);
throw error;
}
}
// DOCS: https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#get-a-reference
async function getLatestCommit(owner, repo, ref, githubToken) {
const token = githubToken || process.env.GITHUB_TOKEN;
try {
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/ref/${ref}`, {
method: 'GET',
headers: {
'accept': 'application/vnd.github+json',
'authorization': `Bearer ${token}`
}
});
if (!response.ok) {
throw new Error(`Failed to fetch ref: ${ref} - ${response.status} - ${response.statusText}`);
}
return response.json();
} catch (error) {
console.error(`Error fetching latest commit for ref ${ref}:`, error);
throw error;
}
}
// DOCS: https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#create-a-reference
async function createBranch(owner, repo, branchRef, baseRefSha, githubToken) {
const token = githubToken || process.env.GITHUB_TOKEN;
// Try to get the existing branch first
try {
const existingBranch = await getLatestCommit(owner, repo, branchRef, token);
console.log(`Branch ${branchRef} already exists, using existing branch`);
return existingBranch;
} catch (error) {
// Branch doesn't exist, create it
console.log(`Branch ${branchRef} doesn't exist, creating new branch`);
}
// Create the new branch
try {
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/refs`, {
method: 'POST',
headers: {
'accept': 'application/vnd.github+json',
'authorization': `Bearer ${token}`
},
body: JSON.stringify({
ref: `refs/${branchRef}`,
sha: baseRefSha
}),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to create branch: ${response.status} - ${errorText}`);
}
const result = await response.json();
console.log(`Successfully created branch ${branchRef}`);
return result;
} catch (error) {
console.error('Error creating branch:', error);
throw error;
}
}
// DOCS: https://docs.github.com/en/rest/git/blobs?apiVersion=2022-11-28#create-a-blob
// Blob is a file change record in git
async function createBlob(owner, repo, content, githubToken) {
const token = githubToken || process.env.GITHUB_TOKEN;
try {
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/blobs`, {
method: 'POST',
headers: {
'accept': 'application/vnd.github+json',
'authorization': `Bearer ${token}`,
'content-type': 'application/json'
},
body: JSON.stringify({
content: content, // this is a overwrite action, no insert/update/delete available
encoding: 'utf-8'
})
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Failed to create blob: ${response.status} - ${text}`);
}
return response.json();
} catch (error) {
console.error('Error creating blob:', error);
throw error;
}
}
// DOCS: https://docs.github.com/en/rest/git/trees?apiVersion=2022-11-28#create-a-tree
// Tree is a collection of blobs
async function createTree(owner, repo, branchRefSha, treeArray, githubToken) {
const token = githubToken || process.env.GITHUB_TOKEN;
try {
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/trees`, {
method: 'POST',
headers: {
'accept': 'application/vnd.github+json',
'authorization': `Bearer ${token}`,
'content-type': 'application/json'
},
body: JSON.stringify({
base_tree: branchRefSha,
tree: treeArray
})
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Failed to create tree: ${response.status} - ${text}`);
}
return response.json();
} catch (error) {
console.error('Error creating tree:', error);
throw error
}
}
// DOCS: https://docs.github.com/en/rest/git/commits?apiVersion=2022-11-28#create-a-commit
async function commitChanges(owner, repo, treeSha, parentCommitSha, githubToken) {
const token = githubToken || process.env.GITHUB_TOKEN;
try {
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/commits`, {
method: 'POST',
headers: {
'accept': 'application/vnd.github+json',
'authorization': `Bearer ${token}`,
'content-type': 'application/json'
},
body: JSON.stringify({
message: '[ai-generated]Update metadata for all documentation files',
tree: treeSha,
parents: [parentCommitSha]
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to create commit: ${response.status} - ${errorText}`);
}
return await response.json();
} catch (error) {
console.error('Error committing changes:', error);
throw error;
}
}
// DOCS: https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#update-a-reference
async function pushCommit(owner, repo, branchRef, commitSha, githubToken) {
const token = githubToken || process.env.GITHUB_TOKEN;
try {
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/git/refs/${branchRef}`, {
method: 'PATCH',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
sha: commitSha,
force: false
})
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to update ref: ${response.status} - ${errorText}`);
}
return await response.json();
} catch (error) {
console.error('Error pushing commit:', error);
throw error;
}
}
// DOCS: https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#create-a-pull-request
async function createPR(owner, repo, headRef, baseRef, githubToken) {
const token = githubToken || process.env.GITHUB_TOKEN;
try {
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/pulls`, {
method: 'POST',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
title: '[AI PR] Metadata Update: Generated metadata for documentation files',
head: headRef,
base: baseRef
})
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Failed to create PR: ${response.status} - ${text}`);
}
return response.json();
} catch (error) {
console.error('Error creating PR:', error);
throw error;
}
}
async function getFilesInPR(owner, repo, prNumber, githubToken){
const token = githubToken || process.env.GITHUB_TOKEN;
try{
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/files`, {
method: 'GET',
headers: {
'Accept': 'application/vnd.github.v3+json',
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Failed to get PR files: ${response.status} - ${text}`);
}
return response.json();
} catch (error) {
console.error('Error getting files in PR:', error);
throw error;
}
}
// DOCS: https://docs.github.com/en/rest/pulls/reviews?apiVersion=2022-11-28#create-a-review-for-a-pull-request
async function createReview(owner, repo, prNumber, comments, githubToken){
const token = githubToken || process.env.GITHUB_TOKEN;
try{
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/reviews`, {
method: 'POST',
headers: {
'Accept': 'application/vnd.github+json',
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
body: 'AI suggestions',
event: 'COMMENT',
comments: comments
})
});
if (!response.ok) {
const text = await response.text();
throw new Error(`Failed to create review: ${response.status} - ${text}`);
}
return response.json();
} catch (error) {
console.error('Error creating review:', error);
throw error;
}
}
// doing module export instead of exporting functions directly to avoid "SyntaxError: Unexpected token 'export'" in github actions environment
module.exports = {
getFileContent,
getFileContentByContentURL,
getLatestCommit,
createBranch,
createBlob,
createTree,
commitChanges,
pushCommit,
createPR,
getFilesInPR,
createReview
};