Repository navigation
Expand file tree
/
Copy pathgithub-api.js
More file actions
334 lines (334 loc) · 10.5 KB
/
Copy pathgithub-api.js
File metadata and controls
334 lines (334 loc) · 10.5 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
334
// 封裝所有 GitHub REST API 與 GraphQL 呼叫
class GitHubAPI {
constructor(token) {
this.token = token;
this.baseURL = 'https://api.github.com';
}
// 取得單一 issue 的完整資料
async getIssue(owner, repo, issueNumber) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/issues/${issueNumber}`,
{
cache: 'no-store',
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
}
}
);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
return response.json();
}
// 取得 repo 的所有 issues(含已關閉,最多 100 筆)
async getIssues(owner, repo) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/issues?state=all&per_page=100`,
{
cache: 'no-store',
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
}
}
);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const data = await response.json();
return data.filter(item => !item.pull_request);
}
// 在 repo 建立新 issue
async createIssue(owner, repo, title, body, labels) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/issues`,
{
method: 'POST',
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
},
body: JSON.stringify({ title, body, labels })
}
);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
return response.json();
}
// 更新 issue(PATCH,可變更 state、title、body 等)
async updateIssue(owner, repo, issueNumber, updates) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/issues/${issueNumber}`,
{
method: 'PATCH',
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
},
body: JSON.stringify(updates)
}
);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
return response.json();
}
// 取得 issue 的所有留言
async getIssueComments(owner, repo, issueNumber) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/issues/${issueNumber}/comments?per_page=100`,
{
cache: 'no-store',
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
}
}
);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
return response.json();
}
// REST API 不支援刪除 issue,必須用 GraphQL;需要 repo 的 admin 權限
async deleteIssue(nodeId) {
const response = await fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
'Authorization': `bearer ${this.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
query: `mutation DeleteIssue($id: ID!) { deleteIssue(input: { issueId: $id }) { repository { id } } }`,
variables: { id: nodeId }
})
});
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const data = await response.json();
if (data.errors) {
throw new Error(data.errors[0].message);
}
}
// 列出指定 issue 的所有 sub-issues
async listSubIssues(owner, repo, parentNumber) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/issues/${parentNumber}/sub_issues?per_page=100`,
{
cache: 'no-store',
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
}
}
);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
return response.json();
}
// 將 child issue 加為 parent 的 sub-issue(subIssueId 為 GitHub issue 的 id 而非 number)
async addSubIssue(owner, repo, parentNumber, subIssueId) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/issues/${parentNumber}/sub_issues`,
{
method: 'POST',
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
},
body: JSON.stringify({ sub_issue_id: subIssueId })
}
);
if (!response.ok) {
const text = await response.text();
const err = new Error(`GitHub API error: ${response.status}`);
err.status = response.status;
err.body = text;
throw err;
}
return response.json();
}
// 從 parent 移除指定 sub-issue
async removeSubIssue(owner, repo, parentNumber, subIssueId) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/issues/${parentNumber}/sub_issue`,
{
method: 'DELETE',
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
},
body: JSON.stringify({ sub_issue_id: subIssueId })
}
);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
return response.json();
}
// 取得 repo 的所有 PR(含已關閉與已合併)
async getPullRequests(owner, repo) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/pulls?state=all&per_page=100`,
{
cache: 'no-store',
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
}
}
);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
return response.json();
}
// 在 issue 新增一則留言
async addComment(owner, repo, issueNumber, body) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/issues/${issueNumber}/comments`,
{
method: 'POST',
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
},
body: JSON.stringify({ body })
}
);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
return response.json();
}
// 檢查 repo 是否為空(無任何 commit)
async isRepoEmpty(owner, repo) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/git/refs`,
{
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
}
}
);
// GitHub 對空 repo 回傳 409 Conflict 而非 200 空陣列
if (response.status === 409) {
return true;
}
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const refs = await response.json();
return refs.length === 0;
}
// 在 repo 建立或更新單一檔案,可用於在空 repo 建立初始 commit
async createFile(owner, repo, path, content, message, branch) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/contents/${path}`,
{
method: 'PUT',
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
},
body: JSON.stringify({ message, content: btoa(content), branch })
}
);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
return response.json();
}
// 檢查指定 repo 是否存在(404 視為不存在,其他錯誤照丟)
async repoExists(owner, repo) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}`,
{
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
}
}
);
if (response.status === 404) {
return false;
}
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
return true;
}
// 在 repo 建立 label(已存在則跳過)
async createLabel(owner, repo, name, color) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/labels`,
{
method: 'POST',
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
},
body: JSON.stringify({ name, color })
}
);
// 422 = label 已存在,視為成功
if (!response.ok && response.status !== 422) {
throw new Error(`GitHub API error: ${response.status}`);
}
}
// 整組替換 issue 的 labels(一次 PATCH)
async setIssueLabels(owner, repo, issueNumber, labels) {
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/issues/${issueNumber}/labels`,
{
method: 'PUT',
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
},
body: JSON.stringify({ labels })
}
);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
return response.json();
}
// 確保系統 labels 都存在(status:* 與 priority:*)。先 GET 列表比對,只 POST 缺的,避免 422 雜訊
async ensureSystemLabels(owner, repo) {
const required = [
{ name: 'status:todo', color: 'ededed' },
{ name: 'status:process', color: 'eab308' },
{ name: 'status:review', color: '4caf50' },
{ name: 'cancel', color: '9e9e9e' },
{ name: 'priority:low', color: 'fee2e2' },
{ name: 'priority:medium', color: 'fca5a5' },
{ name: 'priority:high', color: 'ef4444' },
{ name: 'priority:urgent', color: '991b1b' }
];
const response = await fetch(
`${this.baseURL}/repos/${owner}/${repo}/labels?per_page=100`,
{
headers: {
'Authorization': `token ${this.token}`,
'Accept': 'application/vnd.github.v3+json'
}
}
);
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status}`);
}
const existing = await response.json();
const existingNames = new Set(existing.map(l => l.name));
const missing = required.filter(l => !existingNames.has(l.name));
if (missing.length === 0) {
return;
}
await Promise.all(missing.map(l => this.createLabel(owner, repo, l.name, l.color)));
}
}