[READ-ONLY] Mirror of https://github.com/shuuji3/github-contribution-recorder. 馃摴 A tool to record GitHub activities (commits, issues, PRs, comments) into a local SQLite database.
1.9 kB
57 lines
1import { spawn } from 'child_process'
2import * as fs from 'fs'
3import * as path from 'path'
4import { config } from './config.ts'
5
6const author = config.recordAuthor
7
8function getAllFiles(dirPath: string, arrayOfFiles: string[] = []): string[] {
9 const files = fs.readdirSync(dirPath)
10 files.forEach((file) => {
11 const fullPath = path.join(dirPath, file)
12 if (fs.statSync(fullPath).isDirectory()) {
13 arrayOfFiles = getAllFiles(fullPath, arrayOfFiles)
14 } else if (file.endsWith('.json')) {
15 arrayOfFiles.push(fullPath)
16 }
17 })
18 return arrayOfFiles
19}
20
21const files = getAllFiles('records').sort()
22
23const git = spawn('git', ['-C', 'records', 'fast-import'])
24
25git.stdout.on('data', (data) => console.log(`git: ${data}`))
26git.stderr.on('data', (data) => console.error(`git error: ${data}`))
27
28git.on('close', (code) => {
29 console.log(`fast-import exited with code ${code}`)
30})
31
32let commitCount = 0
33for (const file of files) {
34 const filename = path.basename(file)
35 // filename format: YYYY-MM-DD-HHmmss-repo-type-slug.json
36 const parts = filename.split('-')
37 const dateStr = `${parts[0]}-${parts[1]}-${parts[2]}`
38 const timePart = parts[3]
39 const timeStr = `${timePart.substring(0, 2)}:${timePart.substring(2, 4)}:${timePart.substring(4, 6)}`
40 const timestamp = Math.floor(new Date(`${dateStr}T${timeStr}Z`).getTime() / 1000)
41
42 const content = fs.readFileSync(file, 'utf8')
43 const relativePath = path.relative('records', file)
44
45 const streamData =
46 `commit refs/heads/main\n` +
47 `committer ${author.name} <${author.email}> ${timestamp} +0000\n` +
48 `data <<EOF\nRecord activity: ${filename}\nEOF\n` +
49 `M 100644 inline ${relativePath}\n` +
50 `data ${Buffer.byteLength(content)}\n` +
51 `${content}\n`
52
53 git.stdin.write(streamData)
54 commitCount++
55}
56git.stdin.end()
57console.log(`Streamed ${commitCount} commits to git fast-import.`)