Created
October 28, 2021 17:44
-
-
Save JohannSuarez/b359afd6b276eac8883cd703a078707b to your computer and use it in GitHub Desktop.
Iterate Through a Github Org and Download Each Repo ( Assuming has private repo access )
This file contains 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
/* | |
IMPORTANT: | |
You must have a .env file in the same directory as this script | |
that contains your personal access token. | |
Get one here: https://github.com/settings/tokens | |
In the .env , put your access token like so: | |
GH_TOKEN='ACCESS_TOKEN' | |
No quotation marks. | |
*/ | |
require('dotenv').config() | |
const { Octokit } = require("@octokit/rest"); | |
const { exec } = require("child_process"); | |
const util = require('util'); | |
const octokit = new Octokit({ auth: process.env.GH_TOKEN }); | |
// Asynchronous version of exec. This way, we don't download the repos one by one. | |
// Multiple child processes are spawned. | |
const execa = util.promisify(require('child_process').exec); | |
repo_list = []; | |
// If running this script, clean up the org_repos if it exists. | |
exec("rm -rf org_repos", (error, stdout, stderr) => { | |
if (error) { | |
console.log(`error: ${error.message}`); | |
return; | |
} | |
if (stderr) { | |
console.log(`stderr: ${stderr}`); | |
return; | |
} | |
console.log(`${stdout}`); | |
}); | |
async function back_dat_up() { | |
// Use pagination because we'll need two calls. | |
// Each call can have max 100 results. | |
// We have 113 repos. | |
const response = await | |
octokit | |
.paginate( | |
"GET /orgs/{org}/repos", | |
{ org: "longtailfinancial", per_page: 30 }, | |
(response) => response.data.map((repo) => repo.full_name) | |
) | |
.then((repo_names) => { | |
return repo_names | |
}); | |
return response | |
} | |
back_dat_up().then(call_result => { | |
console.log("Repo count: " + call_result.length); | |
repo_list = call_result; | |
console.log(repo_list); | |
// For this to be automated, you must be have a registered SSH key to your Github. | |
// Otherwise, you will be prompted to enter your Username and Password over a hundred times. | |
for (const repo of repo_list) { | |
execa('mkdir -p org_repos'); | |
command_string = `cd org_repos && git clone [email protected]:${repo}.git`; | |
console.log(`Cloning: ${repo}`); | |
execa(command_string); | |
} | |
}); | |
\\ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment