Repository navigation
Expand file tree
/
Copy pathgithub-api.js
More file actions
127 lines (112 loc) · 3.67 KB
/
Copy pathgithub-api.js
File metadata and controls
127 lines (112 loc) · 3.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
const { Octokit } = require('@octokit/rest');
/**
* GitHub API wrapper with comprehensive logging
* All GitHub API interactions are centralized here
*/
// Logging helpers
function logApiCall(method, endpoint, params = {}) {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] GitHub API Call: ${method} ${endpoint}`,
Object.keys(params).length > 0 ? params : '');
}
function logApiResponse(method, endpoint, status, dataPreview) {
const timestamp = new Date().toISOString();
const preview = Array.isArray(dataPreview)
? `Array(${dataPreview.length})`
: typeof dataPreview === 'object'
? `Object{${Object.keys(dataPreview).slice(0, 3).join(', ')}...}`
: dataPreview;
console.log(`[${timestamp}] GitHub API Response: ${method} ${endpoint} - Status: ${status}`, preview);
}
function logApiError(method, endpoint, error) {
const timestamp = new Date().toISOString();
console.error(`[${timestamp}] GitHub API Error: ${method} ${endpoint} - Status: ${error.status || 'N/A'} - ${error.message}`);
}
/**
* Get authenticated user information
*/
async function getUserInfo(accessToken) {
const octokit = new Octokit({ auth: accessToken });
try {
logApiCall('GET', '/user');
const { data, status } = await octokit.rest.users.getAuthenticated();
logApiResponse('GET', '/user', status, data);
return data;
} catch (error) {
logApiError('GET', '/user', error);
throw error;
}
}
/**
* List all repositories for authenticated user with pagination
* Filters to only include repositories where user has admin access
*/
async function listRepositories(accessToken) {
const octokit = new Octokit({ auth: accessToken });
const allRepos = [];
try {
logApiCall('GET', '/user/repos', { per_page: 100, sort: 'updated', affiliation: 'owner,collaborator' });
for await (const response of octokit.paginate.iterator(
octokit.rest.repos.listForAuthenticatedUser,
{
per_page: 100,
sort: 'updated',
direction: 'desc',
affiliation: 'owner,collaborator'
}
)) {
logApiResponse('GET', '/user/repos', response.status, response.data);
// Filter to only repos where user has admin permission
const adminRepos = response.data.filter(repo => repo.permissions?.admin === true);
allRepos.push(...adminRepos);
}
console.log(`[${new Date().toISOString()}] Total repositories with admin access: ${allRepos.length}`);
return allRepos;
} catch (error) {
logApiError('GET', '/user/repos', error);
throw error;
}
}
/**
* Archive a repository
*/
async function archiveRepository(accessToken, owner, repo) {
const octokit = new Octokit({ auth: accessToken });
try {
logApiCall('PATCH', `/repos/${owner}/${repo}`, { archived: true });
const { data, status } = await octokit.rest.repos.update({
owner,
repo,
archived: true
});
logApiResponse('PATCH', `/repos/${owner}/${repo}`, status, data);
return data;
} catch (error) {
logApiError('PATCH', `/repos/${owner}/${repo}`, error);
throw error;
}
}
/**
* Delete a repository
*/
async function deleteRepository(accessToken, owner, repo) {
const octokit = new Octokit({ auth: accessToken });
try {
logApiCall('DELETE', `/repos/${owner}/${repo}`);
const { status } = await octokit.rest.repos.delete({
owner,
repo
});
logApiResponse('DELETE', `/repos/${owner}/${repo}`, status, 'Repository deleted');
return { success: true };
} catch (error) {
logApiError('DELETE', `/repos/${owner}/${repo}`, error);
throw error;
}
}
module.exports = {
getUserInfo,
listRepositories,
archiveRepository,
deleteRepository
};