|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import {execSync} from 'child_process'; |
| 9 | +import {writeFileSync, mkdirSync} from 'fs'; |
| 10 | +import {join, dirname} from 'path'; |
| 11 | +import {fileURLToPath} from 'url'; |
| 12 | + |
| 13 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 14 | +const OUTPUT_DIR = join(__dirname, 'data'); |
| 15 | +const OUTPUT_FILE = join(OUTPUT_DIR, 'commits.json'); |
| 16 | + |
| 17 | +function parseArgs() { |
| 18 | + const args = process.argv.slice(2); |
| 19 | + const vIndex = args.indexOf('-v'); |
| 20 | + if (vIndex === -1 || vIndex + 1 >= args.length) { |
| 21 | + console.error('Usage: node gen-data.mjs -v <version>'); |
| 22 | + console.error('Example: node gen-data.mjs -v 19.2.0'); |
| 23 | + process.exit(1); |
| 24 | + } |
| 25 | + return args[vIndex + 1]; |
| 26 | +} |
| 27 | + |
| 28 | +function resolveTag(version) { |
| 29 | + const tag = version.startsWith('v') ? version : `v${version}`; |
| 30 | + try { |
| 31 | + execSync(`git tag -l "${tag}" | grep -q .`, {stdio: 'pipe'}); |
| 32 | + } catch { |
| 33 | + console.error(`Error: git tag "${tag}" not found.`); |
| 34 | + console.error( |
| 35 | + 'Available recent tags:', |
| 36 | + execSync('git tag --sort=-creatordate | head -5').toString().trim() |
| 37 | + ); |
| 38 | + process.exit(1); |
| 39 | + } |
| 40 | + return tag; |
| 41 | +} |
| 42 | + |
| 43 | +function resolveGitHubUsernames(commits, repo) { |
| 44 | + // Dedupe by author name — only need one commit per unique author |
| 45 | + const authorToHash = new Map(); |
| 46 | + for (const commit of commits) { |
| 47 | + if (!authorToHash.has(commit.author)) { |
| 48 | + authorToHash.set(commit.author, commit.fullHash); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + const authorToUsername = new Map(); |
| 53 | + const entries = Array.from(authorToHash.entries()); |
| 54 | + console.log(`Resolving GitHub usernames for ${entries.length} unique authors...`); |
| 55 | + |
| 56 | + for (const [author, hash] of entries) { |
| 57 | + try { |
| 58 | + const login = execSync( |
| 59 | + `gh api repos/${repo}/commits/${hash} --jq '.author.login'`, |
| 60 | + {stdio: ['pipe', 'pipe', 'pipe'], timeout: 10000} |
| 61 | + ) |
| 62 | + .toString() |
| 63 | + .trim(); |
| 64 | + if (login && login !== 'null') { |
| 65 | + authorToUsername.set(author, login); |
| 66 | + } |
| 67 | + } catch { |
| 68 | + // Silently skip — will fall back to display name |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + console.log(`Resolved ${authorToUsername.size}/${entries.length} usernames.`); |
| 73 | + return authorToUsername; |
| 74 | +} |
| 75 | + |
| 76 | +function getCommits(lastRelease) { |
| 77 | + const listOfCommits = execSync( |
| 78 | + `git log --pretty=format:"%h|%ai|%aN|%ae" ${lastRelease}...` |
| 79 | + ).toString(); |
| 80 | + |
| 81 | + const summary = execSync( |
| 82 | + `git log --pretty=format:"%s" ${lastRelease}...` |
| 83 | + ) |
| 84 | + .toString() |
| 85 | + .split('\n'); |
| 86 | + |
| 87 | + const body = execSync( |
| 88 | + `git log --pretty=format:"%b<!----!>" ${lastRelease}...` |
| 89 | + ) |
| 90 | + .toString() |
| 91 | + .split('<!----!>\n'); |
| 92 | + |
| 93 | + const commits = listOfCommits.split('\n').map((commitMessage, index) => { |
| 94 | + const diffMatch = body[index]?.match(/D\d+/); |
| 95 | + const diff = diffMatch != null && diffMatch[0]; |
| 96 | + const [hash, date, name] = commitMessage.split('|'); |
| 97 | + return { |
| 98 | + hash: hash.slice(0, 7), |
| 99 | + fullHash: hash, |
| 100 | + summary: summary[index], |
| 101 | + message: body[index], |
| 102 | + author: name, |
| 103 | + diff, |
| 104 | + date, |
| 105 | + }; |
| 106 | + }); |
| 107 | + |
| 108 | + return commits; |
| 109 | +} |
| 110 | + |
| 111 | +// Detect the GitHub repo from the git remote |
| 112 | +function getRepo() { |
| 113 | + try { |
| 114 | + const remote = execSync('git remote get-url origin', {stdio: 'pipe'}) |
| 115 | + .toString() |
| 116 | + .trim(); |
| 117 | + const match = remote.match(/github\.com[:/](.+?)(?:\.git)?$/); |
| 118 | + if (match) return match[1]; |
| 119 | + } catch { |
| 120 | + // fall through |
| 121 | + } |
| 122 | + return 'facebook/react'; |
| 123 | +} |
| 124 | + |
| 125 | +const version = parseArgs(); |
| 126 | +const lastRelease = resolveTag(version); |
| 127 | +const commits = getCommits(lastRelease); |
| 128 | +const repo = getRepo(); |
| 129 | +const usernameMap = resolveGitHubUsernames(commits, repo); |
| 130 | + |
| 131 | +// Attach github username to each commit |
| 132 | +for (const commit of commits) { |
| 133 | + const username = usernameMap.get(commit.author); |
| 134 | + if (username) { |
| 135 | + commit.github = username; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +mkdirSync(OUTPUT_DIR, {recursive: true}); |
| 140 | +writeFileSync(OUTPUT_FILE, JSON.stringify({lastRelease, commits}, null, 2)); |
| 141 | + |
| 142 | +console.log( |
| 143 | + `Wrote ${commits.length} commits (since ${lastRelease}) to data/commits.json` |
| 144 | +); |
0 commit comments