Created
June 8, 2025 12:53
-
-
Save sdwvit/d657bfda2aee63267dbc39b3f074ca5a to your computer and use it in GitHub Desktop.
Fetch grid to chart dependency version map using github api / git history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https'); | |
const fetch = require('node-fetch'); | |
// Function to fetch commit SHAs for a specific file URL from GitHub API with pagination support | |
async function getCommits(fileUrl, owner, repo) { | |
const apiUrlTemplate = `https://api.github.com/repos/${owner}/${repo}/commits?path=${fileUrl}&per_page=100&page={page}`; | |
let allCommits = []; | |
let page = 1; | |
let hasMorePages = true; | |
while (hasMorePages) { | |
const apiUrl = apiUrlTemplate.replace('{page}', page); | |
try { | |
const response = await fetch(apiUrl); | |
if (!response.ok) throw new Error(`Error fetching commits: ${response.statusText}`); | |
const data = await response.json(); | |
// Check for more pages | |
hasMorePages = data.length === 100; | |
allCommits = allCommits.concat(data.map(commit => commit.sha)); | |
console.log(`Fetched page ${page} of commits`); | |
page++; | |
} catch (error) { | |
console.error('Error fetching commits:', error); | |
throw error; | |
} | |
} | |
return allCommits; | |
} | |
// Function to get package.json content at a specific commit SHA using GitHub API | |
async function fetchPackageJson(owner, repo, sha, filePath = 'packages/ag-grid-enterprise/package.json') { | |
const apiUrl = `https://raw.githubusercontent.com/${owner}/${repo}/${sha}/${filePath}`; | |
try { | |
const response = await fetch(apiUrl); | |
if (!response.ok) throw new Error(`Error fetching package.json at commit ${sha}: Network response was not ok`); | |
return await response.text(); | |
} catch (error) { | |
console.error(error.message); | |
throw error; | |
} | |
} | |
// Function to extract non-beta versions of ag-charts-community from both dependencies and optionalDependencies | |
function extractNonBetaVersions(pkgJson) { | |
const dependencySources = [ | |
pkgJson.optionalDependencies || {}, | |
pkgJson.dependencies || {} | |
]; | |
let version; | |
for (const source of dependencySources) { | |
if (source['ag-charts-community'] && !/-beta.+$/.test(source['ag-charts-community'])) { | |
version = source['ag-charts-community']; | |
break; | |
} | |
} | |
return version ? [version] : []; | |
} | |
function storePkgJsonCache(pkgJson, sha) { | |
const fs = require('fs'); | |
const path = require('path'); | |
const cacheFilePath = path.join(__dirname, 'pkg-json-cache.json'); | |
const cache = { [sha]: pkgJson }; | |
if (fs.existsSync(cacheFilePath)) { | |
Object.assign(cache, JSON.parse(fs.readFileSync(cacheFilePath, 'utf8'))); | |
} | |
fs.writeFileSync(cacheFilePath, JSON.stringify(cache, null, 2)); | |
console.log(`Package.json ${pkgJson.version} cached at ${cacheFilePath}`); | |
} | |
function loadPkgJsonCache(sha) { | |
const fs = require('fs'); | |
const path = require('path'); | |
const cacheFilePath = path.join(__dirname, 'pkg-json-cache.json'); | |
if (fs.existsSync(cacheFilePath)) { | |
const data = fs.readFileSync(cacheFilePath, 'utf8'); | |
const cache = JSON.parse(data); | |
return JSON.stringify(cache[sha]) || null; | |
} else { | |
console.warn(`Cache file not found at ${cacheFilePath}`); | |
return null; | |
} | |
} | |
function storeCommitsCache(commits) { | |
const fs = require('fs'); | |
const path = require('path'); | |
const cacheFilePath = path.join(__dirname, 'commits-cache.json'); | |
fs.writeFileSync(cacheFilePath, JSON.stringify(commits, null, 2)); | |
console.log(`Commits cache stored at ${cacheFilePath}`); | |
} | |
function loadCommitsCache() { | |
const fs = require('fs'); | |
const path = require('path'); | |
const cacheFilePath = path.join(__dirname, 'commits-cache.json'); | |
if (fs.existsSync(cacheFilePath)) { | |
const data = fs.readFileSync(cacheFilePath, 'utf8'); | |
return JSON.parse(data); | |
} else { | |
console.warn(`Cache file not found at ${cacheFilePath}`); | |
return null; | |
} | |
} | |
// Main function to execute the script | |
async function main() { | |
const owner = 'ag-grid'; | |
const repo = 'ag-grid'; | |
const filePath = 'packages/ag-grid-enterprise/package.json'; | |
// Fetch commit SHAs for the file path | |
const commits = loadCommitsCache() || await getCommits(filePath, owner, repo); | |
storeCommitsCache(commits); | |
let versionDependencyMap = new Map(); | |
for (const sha of commits) { | |
try { | |
const pkgJsonStr = loadPkgJsonCache(sha) || await fetchPackageJson(owner, repo, sha); | |
const pkgJson = JSON.parse(pkgJsonStr); | |
storePkgJsonCache(pkgJson, sha); | |
const versions = extractNonBetaVersions(pkgJson); | |
if (versionDependencyMap.has(pkgJson.version)) { | |
continue; | |
} | |
if (versions.length > 0) { | |
console.log(`Adding version ${pkgJson.version}->${versions[0]}`); | |
versionDependencyMap.set(pkgJson.version, versions[0]); | |
} | |
} catch (error) { | |
console.error(`Error processing commit ${sha}:`, error.message); | |
} | |
} | |
console.log('Mapping between ag-grid versions and non-beta ag-charts-community versions:', versionDependencyMap); | |
} | |
// Run the script | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment