-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-api.js
More file actions
317 lines (281 loc) · 11.4 KB
/
Copy pathgithub-api.js
File metadata and controls
317 lines (281 loc) · 11.4 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
/**
* Fetches a list of repositories for a GitHub user
* @param {string} username - GitHub username
* @param {number} count - Maximum number of repositories to fetch
* @returns {Promise<string>} - JSON string with repository data
*/
// CORS proxy configuration
const CORS_PROXY = 'https://cors-anywhere.herokuapp.com/';
/**
* Creates CORS headers for GitHub API requests
* @returns {Object} Headers object with CORS and GitHub API settings
*/
function createGitHubHeaders() {
return {
'Accept': 'application/vnd.github.v3+json',
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
};
}
/**
* Makes a CORS-enabled fetch request to GitHub API
* @param {string} url - GitHub API endpoint
* @param {Object} options - Fetch options
* @returns {Promise<Response>} Fetch response
*/
async function fetchWithCors(url, options = {}) {
const headers = {
...createGitHubHeaders(),
...(options.headers || {})
};
const requestOptions = {
...options,
headers,
mode: 'cors',
credentials: 'omit',
cache: 'default'
};
try {
// First try direct fetch
let response = await fetch(url, requestOptions);
// If CORS fails, try with proxy
if (!response.ok && response.type === 'opaque') {
console.log('Direct CORS request failed, trying with proxy...');
response = await fetch(`${CORS_PROXY}${url}`, {
...requestOptions,
headers: {
...requestOptions.headers,
'X-Requested-With': 'XMLHttpRequest'
}
});
}
return response;
} catch (error) {
console.error('Fetch error:', error);
throw error;
}
}
/**
* Fetches a list of repositories for a GitHub user
* @param {string} username - GitHub username
* @param {number} count - Maximum number of repositories to fetch
* @returns {Promise<string>} - JSON string with repository data
*/
async function fetchUserRepositories(username, count = 10) {
const apiUrl = `https://api.github.com/users/${username}/repos?per_page=${count}&sort=updated`;
try {
const response = await fetchWithCors(apiUrl, {
method: 'GET'
});
if (!response.ok) {
const errorText = await response.text();
let errorData;
try {
errorData = JSON.parse(errorText);
} catch (e) {
errorData = { message: errorText };
}
console.error(`GitHub API error! Status: ${response.status}, Message: ${errorData.message}`);
return JSON.stringify({
error: true,
status: response.status,
message: `GitHub API error: ${errorData.message || response.statusText}`
});
}
const reposData = await response.json();
if (!Array.isArray(reposData)) {
console.error("GitHub API did not return an array for repositories:", reposData);
return JSON.stringify({
error: true,
message: "Invalid data format received from GitHub API for repositories."
});
}
const processedRepos = reposData.map(repo => ({
name: repo.name,
description: repo.description,
language: repo.language,
stars: repo.stargazers_count,
forks: repo.forks_count,
updated_at: new Date(repo.updated_at).toLocaleString(),
url: repo.html_url
}));
return JSON.stringify(processedRepos);
} catch (error) {
console.error('Error fetching or processing GitHub repositories:', error);
return JSON.stringify({
error: true,
message: `Network or processing error fetching repositories: ${error.message}`
});
}
}
/**
* Fetches latest commits from a specific GitHub repository
* @param {string} username - GitHub username
* @param {string} repoName - Repository name
* @param {number} count - Maximum number of commits to fetch
* @returns {Promise<string>} - JSON string with commit data
*/
async function fetchLatestCommitsFromGitHub(username, repoName, count = 3) {
const apiUrl = `https://api.github.com/repos/${encodeURIComponent(username)}/${encodeURIComponent(repoName)}/commits?per_page=${count}`;
try {
const response = await fetchWithCors(apiUrl, {
method: 'GET'
});
if (!response.ok) {
const errorText = await response.text();
let errorData;
try {
errorData = JSON.parse(errorText);
} catch (e) {
errorData = { message: errorText };
}
console.error(`GitHub API error! Status: ${response.status}, Message: ${errorData.message}`);
return JSON.stringify({
error: true,
status: response.status,
message: `GitHub API error: ${errorData.message || response.statusText}`
});
}
const commitsData = await response.json();
if (!Array.isArray(commitsData)) {
console.error("GitHub API did not return an array for commits:", commitsData);
return JSON.stringify({
error: true,
message: "Invalid data format received from GitHub API for commits."
});
}
const processedCommits = commitsData.map(commitEntry => ({
sha: commitEntry.sha.substring(0, 7), // Short SHA
message: commitEntry.commit.message.split('\n')[0], // First line of commit message
author: commitEntry.commit.author.name,
date: new Date(commitEntry.commit.author.date).toLocaleString(),
url: commitEntry.html_url
}));
console.log("Processed commits:", processedCommits);
return JSON.stringify(processedCommits); // Return as a JSON string, as expected by the AI tool call response
} catch (error) {
console.error('Error fetching or processing GitHub commits:', error);
return JSON.stringify({
error: true,
message: `Network or processing error fetching commits: ${error.message}`
});
}
}
/**
* Fetches contribution summary across all repositories for a user
* @param {string} username - GitHub username
* @param {number} repoLimit - Maximum number of repositories to check
* @returns {Promise<string>} - JSON string with contribution summary
*/
async function fetchUserContributionSummary(username, repoLimit = 5) {
try {
// First get the user's repositories with enhanced error handling
const reposResponse = await fetchUserRepositories(username, repoLimit);
let repos;
try {
repos = JSON.parse(reposResponse);
} catch (e) {
console.error('Failed to parse repositories response:', e);
return JSON.stringify({
error: true,
message: 'Failed to parse GitHub API response',
details: e.message
});
}
if (repos.error) {
return reposResponse; // Return the original error response
}
// For each repository, get some commit stats with better error handling
const repoPromises = repos.map(async repo => {
try {
if (!repo || !repo.name) {
console.warn('Skipping invalid repository:', repo);
return null;
}
let recentCommits = [];
try {
const commitsResponse = await fetchLatestCommitsFromGitHub(username, repo.name, 3);
const parsedCommits = JSON.parse(commitsResponse);
if (!parsedCommits.error && Array.isArray(parsedCommits)) {
recentCommits = parsedCommits;
}
} catch (commitError) {
console.warn(`Could not fetch commits for ${repo.name}:`, commitError);
// Continue with empty commits array
}
return {
repository: repo.name,
description: repo.description || 'No description',
language: repo.language || 'Not specified',
stars: repo.stargazers_count || 0,
forks: repo.forks_count || 0,
recent_commits: recentCommits,
last_updated: repo.updated_at || new Date().toISOString(),
url: repo.html_url || `https://github.com/${username}/${repo.name}`
};
} catch (error) {
console.error(`Error processing repository ${repo.name || 'unknown'}:`, error);
return null; // Skip this repo if there's an error
}
});
const contributionSummary = await Promise.all(repoPromises);
const filteredSummary = contributionSummary.filter(repo => repo !== null);
console.log(`Generated contribution summary for ${filteredSummary.length} repositories`);
return JSON.stringify({
username: username,
total_repositories: repos.length,
contribution_summary: filteredSummary
});
} catch (error) {
const errorMessage = error.message || 'Unknown error occurred';
const errorStack = error.stack ? error.stack.split('\n').slice(0, 3).join('\n') : 'No stack trace';
console.error('Error in fetchUserContributionSummary:', {
message: errorMessage,
name: error.name,
stack: errorStack,
timestamp: new Date().toISOString()
});
return JSON.stringify({
error: true,
status: 500,
message: `Failed to generate contribution summary: ${errorMessage}`,
timestamp: new Date().toISOString()
});
}
}
// Export the functions for use in other modules
if (typeof window !== 'undefined') {
window.githubApi = {
get_latest_github_commits_for_user: fetchLatestCommitsFromGitHub,
get_user_repositories: fetchUserRepositories,
get_user_contribution_summary: fetchUserContributionSummary,
// Add CORS proxy helper if needed
enableCorsProxy: function(proxyUrl) {
if (!proxyUrl.endsWith('/')) proxyUrl += '/';
this.corsProxy = proxyUrl;
console.log('CORS proxy enabled:', proxyUrl);
}
};
}
// For Node.js/CommonJS environments
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
getLatestGitHubCommitsForUser: fetchLatestCommitsFromGitHub,
getUserRepositories: fetchUserRepositories,
getUserContributionSummary: fetchUserContributionSummary,
// Add version info
VERSION: '1.0.0',
// Add CORS proxy helper
enableCorsProxy: function(proxyUrl) {
if (!proxyUrl.endsWith('/')) proxyUrl += '/';
this.corsProxy = proxyUrl;
console.log('CORS proxy enabled:', proxyUrl);
}
};
}
// Log initialization
console.log('GitHub API Client initialized', {
version: '1.0.0',
timestamp: new Date().toISOString(),
environment: typeof window !== 'undefined' ? 'browser' : 'node'
});