-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_sync.js
More file actions
280 lines (245 loc) · 7.56 KB
/
github_sync.js
File metadata and controls
280 lines (245 loc) · 7.56 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
// GitHub Repository Sync Implementation
// This script syncs your GitHub repositories with your database
import { createClient } from '@supabase/supabase-js';
import { Octokit } from '@octokit/rest';
// Configuration
const config = {
github: {
token: process.env.GITHUB_TOKEN,
username: 'CryptoJym'
},
supabase: {
url: process.env.SUPABASE_URL,
key: process.env.SUPABASE_ANON_KEY
},
notion: {
token: process.env.NOTION_TOKEN,
databaseId: process.env.NOTION_PROJECTS_DB
}
};
// Initialize clients
const octokit = new Octokit({ auth: config.github.token });
const supabase = createClient(config.supabase.url, config.supabase.key);
// Repository categories based on analysis
const REPO_CATEGORIES = {
'Vuplicity': 'background-check',
'fcra-compliance-system': 'background-check',
'background-check-mcp': 'background-check',
'mcp-background-check-agent': 'background-check',
'Vuplicity-Progress': 'background-check',
'Utlyze-optionB': 'background-check',
'utah-bgc-mesh': 'background-check',
'ai-trinity-data-extraction': 'ai-automation',
'ai-image-animation-pipeline': 'ai-automation',
'heygen-mcp-adapter': 'ai-automation',
'tiktok-mcp-adapter': 'ai-automation',
'Agent-starter-kit': 'ai-automation',
'Overseer': 'ai-automation',
'website-eater': 'ai-automation',
'fishrewards-nft': 'crypto-web3',
'royal-rwa': 'crypto-web3',
'blockchain-reward-hub': 'crypto-web3',
'Chrysalis': 'crypto-web3',
'RoyalCalcV2': 'tools-utilities',
'FlashCardsofPOWER': 'tools-utilities',
'loom-autopublisher': 'tools-utilities',
'TimingApp-MCP': 'tools-utilities'
};
// Main sync function
async function syncGitHubRepos() {
console.log('🚀 Starting GitHub repository sync...');
try {
// 1. Fetch all repositories
const repos = await fetchAllRepositories();
console.log(`📊 Found ${repos.length} repositories`);
// 2. Process each repository
for (const repo of repos) {
await processRepository(repo);
}
// 3. Generate analytics
await generateAnalytics();
console.log('✅ Sync completed successfully!');
} catch (error) {
console.error('❌ Sync failed:', error);
}
}
// Fetch all repositories with pagination
async function fetchAllRepositories() {
const repos = [];
let page = 1;
let hasMore = true;
while (hasMore) {
const { data } = await octokit.repos.listForUser({
username: config.github.username,
per_page: 100,
page,
type: 'all'
});
repos.push(...data);
hasMore = data.length === 100;
page++;
}
return repos;
}
// Process individual repository
async function processRepository(repo) {
console.log(`📝 Processing: ${repo.name}`);
// Determine category
const category = REPO_CATEGORIES[repo.name] || 'other';
// Prepare repository data
const repoData = {
repo_id: repo.id,
repo_name: repo.name,
description: repo.description,
is_private: repo.private,
category,
language: repo.language,
created_at: repo.created_at,
updated_at: repo.updated_at,
last_push: repo.pushed_at,
stars: repo.stargazers_count,
forks: repo.forks_count,
open_issues: repo.open_issues_count,
html_url: repo.html_url,
default_branch: repo.default_branch
};
// Upsert to database
const { error } = await supabase
.from('github_projects')
.upsert(repoData, { onConflict: 'repo_id' });
if (error) {
console.error(`Failed to sync ${repo.name}:`, error);
return;
}
// Fetch additional details for important repos
if (category === 'background-check' || category === 'ai-automation') {
await fetchRepoDetails(repo);
}
}
// Fetch detailed repository information
async function fetchRepoDetails(repo) {
try {
// Get recent commits
const { data: commits } = await octokit.repos.listCommits({
owner: config.github.username,
repo: repo.name,
per_page: 10
});
// Store commits
for (const commit of commits) {
await supabase.from('github_commits').upsert({
repo_id: repo.id,
commit_sha: commit.sha,
message: commit.commit.message,
author: commit.commit.author.name,
timestamp: commit.commit.author.date
}, { onConflict: 'commit_sha' });
}
// Get open issues
const { data: issues } = await octokit.issues.listForRepo({
owner: config.github.username,
repo: repo.name,
state: 'open',
per_page: 20
});
// Store issues
for (const issue of issues) {
await supabase.from('github_issues').upsert({
repo_id: repo.id,
issue_number: issue.number,
title: issue.title,
state: issue.state,
labels: issue.labels.map(l => l.name),
assignees: issue.assignees.map(a => a.login),
created_at: issue.created_at,
updated_at: issue.updated_at
}, { onConflict: ['repo_id', 'issue_number'] });
}
} catch (error) {
console.error(`Failed to fetch details for ${repo.name}:`, error);
}
}
// Generate analytics and insights
async function generateAnalytics() {
console.log('📊 Generating analytics...');
// Category breakdown
const { data: categoryStats } = await supabase
.from('github_projects')
.select('category')
.select('category, count(*)')
.group('category');
// Activity metrics
const { data: activityStats } = await supabase
.from('github_commits')
.select('repo_id, count(*)')
.gte('timestamp', new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString())
.group('repo_id');
// Save analytics
await supabase.from('github_analytics').insert({
generated_at: new Date().toISOString(),
category_breakdown: categoryStats,
recent_activity: activityStats,
total_repos: categoryStats?.reduce((sum, cat) => sum + cat.count, 0) || 0
});
}
// Create database tables (run once)
async function createTables() {
const tables = [
`CREATE TABLE IF NOT EXISTS github_projects (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
repo_id INTEGER UNIQUE NOT NULL,
repo_name TEXT NOT NULL,
description TEXT,
is_private BOOLEAN,
category TEXT,
language TEXT,
created_at TIMESTAMP,
updated_at TIMESTAMP,
last_push TIMESTAMP,
stars INTEGER,
forks INTEGER,
open_issues INTEGER,
html_url TEXT,
default_branch TEXT,
notion_page_id TEXT,
cloze_project_id TEXT
)`,
`CREATE TABLE IF NOT EXISTS github_commits (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
repo_id INTEGER REFERENCES github_projects(repo_id),
commit_sha TEXT UNIQUE,
message TEXT,
author TEXT,
timestamp TIMESTAMP
)`,
`CREATE TABLE IF NOT EXISTS github_issues (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
repo_id INTEGER,
issue_number INTEGER,
title TEXT,
state TEXT,
labels JSONB,
assignees JSONB,
created_at TIMESTAMP,
updated_at TIMESTAMP,
UNIQUE(repo_id, issue_number)
)`,
`CREATE TABLE IF NOT EXISTS github_analytics (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
generated_at TIMESTAMP,
category_breakdown JSONB,
recent_activity JSONB,
total_repos INTEGER
)`
];
for (const sql of tables) {
const { error } = await supabase.rpc('exec_sql', { sql });
if (error) console.error('Table creation error:', error);
}
}
// Export functions
export { syncGitHubRepos, createTables, fetchAllRepositories };
// Run if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
syncGitHubRepos();
}