Skip to content

Instantly share code, notes, and snippets.

@lukekarrys
Created May 1, 2021 03:06
Show Gist options
  • Save lukekarrys/d5573f557072dd799f56476279393d69 to your computer and use it in GitHub Desktop.
Save lukekarrys/d5573f557072dd799f56476279393d69 to your computer and use it in GitHub Desktop.
Get all repos from a GitHub org with instructions to then clone them
const { Octokit } = require("@octokit/core");
const fs = require("fs").promises;
const octokit = new Octokit({
auth: "GH_REPO_TOKEN",
});
const ORG = process.argv[2];
const PER_PAGE = 100;
const getAll = async () => {
try {
return require(`./${ORG}_repos.json`);
} catch (e) {}
let lastCount = 0;
let all = [];
do {
const { data } = await octokit.request("GET /orgs/{org}/repos", {
org: ORG,
per_page: PER_PAGE,
page: all.length / PER_PAGE + 1,
});
lastCount = data.length;
all = [...all, ...data];
} while (lastCount === PER_PAGE);
return all;
};
const main = async () => {
const all = await getAll();
await fs.writeFile(`./${ORG}_repos.json`, JSON.stringify(all, null, 2));
await fs.writeFile(
`./${ORG}_repos.txt`,
all.map((d) => d.ssh_url).join("\n") + "\n"
);
};
main()
.then(() =>
console.log("Now run: while read p; do git clone $p; done <ORG_repos.txt")
)
.catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment