|
| 1 | +const axios = require('axios'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +const config = require('./config'); |
| 6 | + |
| 7 | +const username = config.github_username; |
| 8 | +const URL = `${config.api_url}/users/${username}/repos`; |
| 9 | +const OUT_FILE = 'src/repos.json'; |
| 10 | + |
| 11 | +async function fetchRepos(url) { |
| 12 | + const repos = []; |
| 13 | + let page = 1; |
| 14 | + |
| 15 | + while (true) { |
| 16 | + try { |
| 17 | + const res = await axios.get(url, { |
| 18 | + headers: { |
| 19 | + Accept: 'application/vnd.github.v3+json', |
| 20 | + Authorization: `token ${config.access_token}`, |
| 21 | + }, |
| 22 | + params: { |
| 23 | + page, |
| 24 | + }, |
| 25 | + }); |
| 26 | + |
| 27 | + if (res.data.length === 0) { |
| 28 | + break; |
| 29 | + } |
| 30 | + |
| 31 | + const forkedRepos = res.data |
| 32 | + .filter((repo) => repo.fork) |
| 33 | + .map((repo) => repo.full_name); |
| 34 | + console.log(`[Page ${page}] Found ${forkedRepos.length} forked repo(s):`); |
| 35 | + console.log(forkedRepos.map((repo) => `- ${repo}`).join('\n') + '\n'); |
| 36 | + repos.push(...forkedRepos); |
| 37 | + page++; |
| 38 | + } catch (err) { |
| 39 | + console.error(`Error fetching page ${page}: ${err}`); |
| 40 | + break; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return repos; |
| 45 | +} |
| 46 | + |
| 47 | +fetchRepos(URL).then((result) => { |
| 48 | + console.log('Forked repos found:', result.length); |
| 49 | + console.log(result.map((repo) => `- ${repo}`).join('\n')); |
| 50 | + console.log(); |
| 51 | + fs.writeFileSync(OUT_FILE, JSON.stringify(result, null, 2)); |
| 52 | + console.log( |
| 53 | + `Wrote forked repos into "${path.join(process.cwd(), OUT_FILE)}"`, |
| 54 | + ); |
| 55 | +}); |
0 commit comments