-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-api.js
More file actions
119 lines (108 loc) · 3.1 KB
/
github-api.js
File metadata and controls
119 lines (108 loc) · 3.1 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
// github-api.js
const { Octokit } = require("@octokit/rest");
class GitHubAPI {
constructor(accessToken = null) {
this.octokit = new Octokit(
accessToken ? { auth: accessToken } : {}
);
this.authenticated = !!accessToken;
}
// Get repositories for authenticated user
async getUserRepositories() {
if (!this.authenticated) {
throw new Error("Authentication required");
}
try {
const response = await this.octokit.rest.repos.listForAuthenticatedUser({
sort: "updated",
per_page: 100,
});
return response.data;
} catch (error) {
console.error("❌ Error fetching user repositories:", error);
throw error;
}
}
// Search public repositories
async searchRepositories(query, page = 1) {
try {
const response = await this.octokit.rest.search.repos({
q: query,
sort: "stars",
order: "desc",
per_page: 30,
page,
});
return response.data;
} catch (error) {
console.error("❌ Error searching repositories:", error);
throw error;
}
}
// Get repository metadata
async getRepository(owner, repo) {
try {
const response = await this.octokit.rest.repos.get({ owner, repo });
return response.data;
} catch (error) {
console.error("❌ Error fetching repository:", error);
throw error;
}
}
// Get repository contents at a path
async getRepositoryContents(owner, repo, path = "") {
try {
const response = await this.octokit.rest.repos.getContent({
owner,
repo,
path,
});
return response.data;
} catch (error) {
console.error("❌ Error fetching repository contents:", error);
throw error;
}
}
// Get file content (decoded from base64)
async getFileContent(owner, repo, path) {
try {
const response = await this.octokit.rest.repos.getContent({
owner,
repo,
path,
});
// ⚠ FIX: Node doesn’t have atob; use Buffer
return Buffer.from(response.data.content, "base64").toString("utf-8");
} catch (error) {
console.error("❌ Error fetching file content:", error);
throw error;
}
}
// Recursively get all files in a repo
async getAllRepositoryFiles(owner, repo, path = "") {
try {
const contents = await this.getRepositoryContents(owner, repo, path);
let allFiles = [];
for (const item of contents) {
if (item.type === "file") {
const content = await this.getFileContent(owner, repo, item.path);
allFiles.push({
path: item.path,
content,
type: item.type,
});
} else if (item.type === "dir") {
// Recurse into directories
const subFiles = await this.getAllRepositoryFiles(owner, repo, item.path);
allFiles = allFiles.concat(subFiles);
}
}
return allFiles;
} catch (error) {
console.error("❌ Error fetching all repository files:", error);
throw error;
}
}
}
// ⚠ FIX: Match class name exactly
module.exports = GitHubAPI;