code icon Code

Get GitHub Repository Info

Fetch repository metadata and README

Source Code

const [owner, repo] = process.argv.slice(2);

const token = process.env.GITHUB_TOKEN || "PLACEHOLDER_TOKEN";

async function getRepoInfo() {
  console.log(`Fetching repository info for ${owner}/${repo}...`);

  // Fetch repo metadata
  const repoUrl = `https://api.github.com/repos/${owner}/${repo}`;
  const repoResponse = await fetch(repoUrl, {
    headers: {
      Authorization: `Bearer ${token}`,
      Accept: "application/vnd.github+json",
      "X-GitHub-Api-Version": "2022-11-28",
    },
  });

  if (!repoResponse.ok) {
    const error = await repoResponse.json();
    console.error("ERROR: GitHub API request failed.");
    console.error(`  Status: ${repoResponse.status}`);
    console.error(`  Message: ${error.message || repoResponse.statusText}`);
    console.error("  Check that the repository exists and you have access.");
    process.exit(1);
  }

  const repoData = await repoResponse.json();

  // Try to fetch README
  let readme = null;
  try {
    const readmeUrl = `https://api.github.com/repos/${owner}/${repo}/readme`;
    const readmeResponse = await fetch(readmeUrl, {
      headers: {
        Authorization: `Bearer ${token}`,
        Accept: "application/vnd.github.raw+json",
        "X-GitHub-Api-Version": "2022-11-28",
      },
    });
    if (readmeResponse.ok) {
      readme = await readmeResponse.text();
    }
  } catch (e) {
    // README may not exist, that's fine
  }

  const remaining = repoResponse.headers.get("X-RateLimit-Remaining");
  if (remaining && parseInt(remaining) < 100) {
    console.log(`⚠️ Rate limit warning: ${remaining} requests remaining`);
  }

  const result = {
    name: repoData.name,
    full_name: repoData.full_name,
    description: repoData.description,
    html_url: repoData.html_url,
    default_branch: repoData.default_branch,
    language: repoData.language,
    topics: repoData.topics,
    visibility: repoData.visibility,
    open_issues_count: repoData.open_issues_count,
    forks_count: repoData.forks_count,
    stargazers_count: repoData.stargazers_count,
    created_at: repoData.created_at,
    updated_at: repoData.updated_at,
    readme: readme,
  };

  const fs = await import("fs");
  fs.writeFileSync("session/repos.json", JSON.stringify(result, null, 2));

  console.log(`Repository: ${result.full_name}`);
  console.log(`  Description: ${result.description || "(none)"}`);
  console.log(`  Language: ${result.language || "unknown"}`);
  console.log(`  Default branch: ${result.default_branch}`);
  console.log(`  Open issues: ${result.open_issues_count}`);
  console.log(`  Stars: ${result.stargazers_count}`);
  console.log(`  Wrote details to session/repos.json`);
}

try {
  await getRepoInfo();
} catch (error) {
  console.error("ERROR: Unexpected failure getting repository info.");
  console.error(`  ${error.message}`);
  process.exit(1);
}