-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
473 lines (405 loc) · 15.8 KB
/
index.js
File metadata and controls
473 lines (405 loc) · 15.8 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import fs from 'fs-extra'
import shell from 'shelljs'
import { relative, dirname, basename, extname } from 'path'
import fetch from 'node-fetch'
import { extension } from 'mime-types'
import { glob } from 'glob'
shell.config.fatal = true
const perPage = 100
const retryCount = 10
const retryDelayRateLimit = 6 * 60
const retryDelayOthers = 6
const { USERNAME, TOKEN } = process.env
const folder = '/usr/src/backup'
const metadataPath = `${folder}/metadata.json`
function delay(seconds) {
return new Promise(resolve => {
console.log(`... delay ${seconds}s`)
setTimeout(() => {
return resolve()
}, seconds * 1000)
})
}
function request(path, options = {}) {
return new Promise(async (resolve, reject) => {
const baseUrl = path.substr(0, 4) !== 'http' ? 'https://api.github.com' : ''
console.log(`Request ${baseUrl}${path}`)
for (let n = 1; n <= retryCount; n++) {
let resp
try {
resp = await fetch(`${baseUrl}${path}`, {
...options,
headers: {
Authorization: `Token ${TOKEN}`,
...options.headers || {}
}
})
} catch {
console.log(`... failed at #${n} attempt`)
if (n < retryCount) {
await delay(retryDelayOthers)
continue
} else {
return reject()
}
}
if (resp.ok) {
return resolve(resp)
} else {
const rateLimitHeader = [ ...resp.headers ].find(obj => obj[0] === 'x-ratelimit-remaining')
const rateLimitLimitHeader = [ ...resp.headers ].find(obj => obj[0] === 'x-ratelimit-limit')
const rateLimitRemaining = rateLimitHeader ? parseInt(rateLimitHeader[1]) : null
const rateLimitLimit = rateLimitLimitHeader ? parseInt(rateLimitLimitHeader[1]) : null
console.log(`... failed at #${n} attempt (status ${resp.status})`)
if (rateLimitRemaining === 0) {
console.log(`... API rate limit of ${rateLimitLimit} requests per hour exceeded`)
if (n < retryCount) await delay(retryDelayRateLimit)
} else {
if (n < retryCount) await delay(retryDelayOthers)
}
}
}
return reject()
})
}
function requestJson(path, options) {
return new Promise(async (resolve, reject) => {
try {
const response = await request(path, options)
const json = await response.json()
return resolve(json)
} catch (err) {
return reject(err)
}
})
}
function requestAll(path, options) {
return new Promise(async (resolve, reject) => {
try {
let items = []
let page = 1
while (page !== null) {
const separator = path.indexOf('?') === -1 ? '?' : '&'
const moreItemsResponse = await request(`${path}${separator}per_page=${perPage}&page=${page}`, options)
const moreItems = await moreItemsResponse.json()
if (moreItems.length) {
items = [...items, ...moreItems]
page = moreItems.length === perPage ? page + 1 : null
} else {
page = null
}
}
return resolve(items)
} catch (err) {
return reject(err)
}
})
}
async function requestAllWithRetry(path, options) {
for (let n = 1; n <= retryCount; n++) {
try {
const items = requestAll(path, options)
return items
} catch (err) {
if (n === 10) return err
console.log('... failed at attempt #' + n)
await delay(retryDelayOthers)
}
}
}
function downloadFile(sourceFileUrl, targetFilePath) {
return new Promise(async (resolve, reject) => {
// Skip download if file already exists (check with any extension if none specified)
if (!extname(targetFilePath)) {
const dir = dirname(targetFilePath)
const base = basename(targetFilePath)
if (fs.existsSync(dir)) {
const existing = fs.readdirSync(dir).filter(f => f.startsWith(base + '.'))
if (existing.length > 0) {
return resolve(`${dir}/${existing[0]}`)
}
}
} else if (fs.existsSync(targetFilePath)) {
return resolve(targetFilePath)
}
const response = await request(sourceFileUrl, { headers: { Accept: 'application/octet-stream' }})
if (!extname(targetFilePath)) {
const ext = extension([ ...response.headers ].filter(obj => obj[0] === 'content-type')[0][1])
targetFilePath = targetFilePath + (ext ? '.' + ext : '')
}
fs.ensureDirSync(dirname(targetFilePath))
const fileStream = fs.createWriteStream(targetFilePath)
response.body.pipe(fileStream)
response.body.on('error', () => { return reject() })
fileStream.on('finish', () => {
return resolve(targetFilePath)
})
})
}
function downloadImages(body, folder, filename, baseImagePath = './images') {
return new Promise(async (resolve, reject) => {
try {
const files = []
const images = body?.match(/["(]https:\/\/github\.com\/(.+)\/assets\/(.+)[)"]/g) || []
for (let n = 0; n < images.length; n++) {
const targetFilename = filename.replace('{id}', (n+1).toString().padStart(images.length.toString().length, '0'))
const targetPath = folder + '/' + targetFilename
const sourceUrl = images[n].replace(/^["(](.+)[)"]$/, '$1')
fs.ensureDirSync(folder)
const realTargetFilename = basename(await downloadFile(sourceUrl, targetPath))
files.push(realTargetFilename)
body = body.replace(`"${sourceUrl}"`, `"${baseImagePath}/${realTargetFilename}"`)
body = body.replace(`(${sourceUrl})`, `(${baseImagePath}/${realTargetFilename})`)
}
return resolve({ body, files })
} catch (err) {
return reject(err)
}
})
}
function cleanupImages(imagesDir, prefix, keepFiles) {
if (!fs.existsSync(imagesDir)) return
const keepSet = new Set(keepFiles)
for (const file of fs.readdirSync(imagesDir)) {
if (file.startsWith(prefix) && !keepSet.has(file)) {
console.log(`Removing orphaned image: ${file}`)
fs.removeSync(`${imagesDir}/${file}`)
}
}
}
function writeJSON(path, json) {
fs.ensureDirSync(dirname(path))
fs.writeJsonSync(path, json, { spaces: 2 })
}
function loadMetadata() {
try {
return fs.existsSync(metadataPath) ? fs.readJsonSync(metadataPath) : {}
} catch {
return {}
}
}
function saveMetadata(metadata) {
writeJSON(metadataPath, metadata)
}
async function backup() {
try {
// Ensure backup folder exists (no longer wiped)
fs.ensureDirSync(folder)
// Load metadata from previous backup
const metadata = loadMetadata()
const repoMeta = metadata.repositories || {}
const starredMeta = metadata.starred || {}
// Get repositories
const repositories = await requestAllWithRetry('/user/repos')
// Determine which repos were removed from GitHub
const currentRepoNames = new Set(repositories.map(r => r.name))
const existingRepoNames = new Set(Object.keys(repoMeta))
for (const name of existingRepoNames) {
if (!currentRepoNames.has(name)) {
console.log(`Removing deleted repository: ${name}`)
fs.removeSync(`${folder}/repositories/${name}`)
}
}
// Save repositories
writeJSON(`${folder}/repositories.json`, repositories)
// Track new metadata
const newRepoMeta = {}
// Loop repositories
for (const repository of repositories) {
const prev = repoMeta[repository.name]
const repoDir = `${folder}/repositories/${repository.name}`
const repoPath = `${repoDir}/repository`
const localExists = fs.existsSync(`${repoPath}/.git`)
const isNew = !prev || !localExists
const dataChanged = isNew || prev.updated_at !== repository.updated_at
const codeChanged = isNew || prev.pushed_at !== repository.pushed_at
const defaultBranch = repository.default_branch || 'main'
// Process issues and releases only if data changed
if (dataChanged) {
console.log(`Processing ${isNew ? 'new' : 'updated'} repository data: ${repository.name}`)
// Get issues
const issues = await requestAllWithRetry(`/repos/${USERNAME}/${repository.name}/issues?state=all`)
const issueImageFiles = []
// Loop issues
for (const issue of issues) {
// Download issue images
const issueResult = await downloadImages(
issue.body,
`${repoDir}/images`,
`issue_${issue.id}_{id}`
)
issue.body = issueResult.body
issueImageFiles.push(...issueResult.files)
// Get issue comments
const comments = issue.comments !== 0 ? await requestAllWithRetry(issue.comments_url) : []
// Add issue comments to issues JSON
issue.comments = comments
// Loop issue comments
for (const comment of comments) {
// Download issue comment images
const commentResult = await downloadImages(
comment.body,
`${repoDir}/images`,
`issue_${issue.id}_comment_${comment.id}_{id}`
)
comment.body = commentResult.body
issueImageFiles.push(...commentResult.files)
}
}
// Save issues
writeJSON(`${repoDir}/issues.json`, issues)
// Clean up orphaned issue images
cleanupImages(`${repoDir}/images`, 'issue_', issueImageFiles)
// Get releases
const releases = await requestAllWithRetry(`/repos/${USERNAME}/${repository.name}/releases`)
const releaseImageFiles = []
// Loop releases
for (const release of releases) {
// Download release text images
const releaseResult = await downloadImages(
release.body,
`${repoDir}/images`,
`release_${release.id}_{id}`
)
release.body = releaseResult.body
releaseImageFiles.push(...releaseResult.files)
// Loop release assets
for (const asset of release.assets) {
// Download release assets (skips if file already exists)
downloadFile(
asset.url,
`${repoDir}/releases/${release.tag_name}/${asset.name}`
)
}
}
// Save releases
writeJSON(`${repoDir}/releases.json`, releases)
// Clean up orphaned release images
cleanupImages(`${repoDir}/images`, 'release_', releaseImageFiles)
// Clean up release folders for removed releases
const releasesDir = `${repoDir}/releases`
if (fs.existsSync(releasesDir)) {
const currentTags = new Set(releases.map(r => r.tag_name))
for (const entry of fs.readdirSync(releasesDir)) {
const entryPath = `${releasesDir}/${entry}`
if (fs.statSync(entryPath).isDirectory() && !currentTags.has(entry)) {
console.log(`Removing deleted release: ${repository.name}/${entry}`)
fs.removeSync(entryPath)
}
}
}
} else {
console.log(`Skipping unchanged repository data: ${repository.name}`)
}
// Clone or update git repository only if code changed
if (codeChanged) {
// Remove macOS AppleDouble files from pack directory to avoid "non-monotonic index" errors
shell.exec(`find "${repoPath}/.git/objects/pack" -name '._*' -delete 2>/dev/null || true`)
if (localExists) {
console.log(`Updating git repository: ${repository.name}`)
shell.exec(`git -C "${repoPath}" fetch --all && git -C "${repoPath}" reset --hard "origin/${defaultBranch}"`)
} else {
console.log(`Cloning git repository: ${repository.name}`)
shell.exec(`git clone "https://${TOKEN}@github.com/${USERNAME}/${repository.name}.git" "${repoPath}"`)
}
// Process markdown images (only after code changes since files come from the repo)
const repoFolder = `${repoPath}/`
const imageFolder = `${repoDir}/images/`
const markdownFiles = await glob(`${repoFolder}**/*.{md,MD}`)
const markdownImageFiles = []
for (const markdownFile of markdownFiles) {
// Download markdown images
const baseImagePath = relative(dirname(markdownFile), imageFolder)
const imageFileBasename = markdownFile.replace(repoFolder, '').replace(/\//g, '_').replace(/\.md$/i, '')
let markdownFileContent = fs.readFileSync(markdownFile, { encoding: 'utf8' })
const markdownResult = await downloadImages(
markdownFileContent,
imageFolder,
`markdown_${imageFileBasename}_{id}`,
baseImagePath
)
markdownImageFiles.push(...markdownResult.files)
// Update markdown file
fs.writeFileSync(markdownFile, markdownResult.body)
}
// Clean up orphaned markdown images
cleanupImages(imageFolder, 'markdown_', markdownImageFiles)
} else {
console.log(`Skipping unchanged git repository: ${repository.name}`)
}
// Store metadata for this repo
newRepoMeta[repository.name] = {
updated_at: repository.updated_at,
pushed_at: repository.pushed_at
}
// Save metadata progressively to allow safe interruptions
saveMetadata({
lastBackupAt: new Date().toISOString(),
repositories: { ...repoMeta, ...newRepoMeta },
starred: starredMeta
})
}
// Get user details
const user = await requestJson('/user')
writeJSON(`${folder}/user/user.json`, user)
// Get starred repositories
const starred = await requestAllWithRetry('/user/starred')
writeJSON(`${folder}/user/starred.json`, starred)
// Determine which starred repos were removed
const currentStarredKeys = new Set(starred.map(r => `${r.owner.login}/${r.name}`))
const existingStarredKeys = new Set(Object.keys(starredMeta))
for (const key of existingStarredKeys) {
if (!currentStarredKeys.has(key)) {
console.log(`Removing unstarred repository: ${key}`)
fs.removeSync(`${folder}/starred/${key}`)
// Remove empty owner folder
const ownerDir = `${folder}/starred/${key.split('/')[0]}`
if (fs.existsSync(ownerDir) && fs.readdirSync(ownerDir).length === 0) {
fs.removeSync(ownerDir)
}
}
}
// Clone or update starred repositories
const newStarredMeta = {}
for (const repo of starred) {
const owner = repo.owner.login
const name = repo.name
const key = `${owner}/${name}`
const targetPath = `${folder}/starred/${owner}/${name}`
const defaultBranch = repo.default_branch || 'main'
const prev = starredMeta[key]
const localExists = fs.existsSync(`${targetPath}/.git`)
const isChanged = !prev || !localExists || prev.pushed_at !== repo.pushed_at
if (localExists && isChanged) {
console.log(`Updating starred repository: ${key}`)
shell.exec(`git -C "${targetPath}" fetch --depth 1 origin && git -C "${targetPath}" reset --hard "origin/${defaultBranch}"`)
} else if (!localExists) {
console.log(`Cloning starred repository: ${key}`)
fs.ensureDirSync(targetPath)
shell.exec(`git clone "https://${TOKEN}@github.com/${owner}/${name}.git" --depth 1 "${targetPath}"`)
} else {
console.log(`Skipping unchanged starred repository: ${key}`)
}
newStarredMeta[key] = { pushed_at: repo.pushed_at }
// Save metadata progressively for starred repos
saveMetadata({
lastBackupAt: new Date().toISOString(),
repositories: newRepoMeta,
starred: { ...starredMeta, ...newStarredMeta }
})
}
// Save final complete metadata
saveMetadata({
lastBackupAt: new Date().toISOString(),
repositories: newRepoMeta,
starred: newStarredMeta
})
// Complete script
console.log('Backup completed!')
shell.exit()
} catch (err) {
throw new Error(err)
}
}
// Run the backup
backup()