@shashwatrajs/repo-insights
Version:
GitHub Repository Insights - CLI tool and library to fetch GitHub repo statistics
40 lines (32 loc) • 1.29 kB
JavaScript
const fetch = require('node-fetch');
async function getRepoInsights(owner, repo) {
const base = `https://api.github.com/repos/${owner}/${repo}`;
try {
// Repo Main Data
const repoRes = await fetch(base);
if (repoRes.status === 404) throw new Error('Repository not found');
if (repoRes.status === 403) throw new Error('Rate limit exceeded');
const repoData = await repoRes.json();
// Commits Data (last 30 days)
const since = (new Date(Date.now() - 30*24*60*60*1000)).toISOString();
console.log(`Fetching commits since: ${since}`);
const commitRes = await fetch(`${base}/commits?since=${since}&per_page=100`);
let commits = [];
if (commitRes.ok) commits = await commitRes.json();
let lastCommitDate = null;
if (commits.length > 0 && commits[0].commit && commits[0].commit.committer) {
lastCommitDate = commits[0].commit.committer.date;
}
return {
repo: `${owner}/${repo}`,
stars: repoData.stargazers_count,
forks: repoData.forks_count,
issues_open: repoData.open_issues_count,
commits_last_30_days: commits.length,
last_commit_date: commits?.commit?.committer?.date || null
};
} catch (err) {
return { error: err.message }
}
}
module.exports = { getRepoInsights };