Skip to content

Commit

Permalink
download repos en masse
Browse files Browse the repository at this point in the history
  • Loading branch information
DryCreations committed May 30, 2021
0 parents commit e106d0d
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GITHUB_CLIENT_ID=
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
/tmp/
Empty file added README.md
Empty file.
52 changes: 52 additions & 0 deletions mod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import AuthFlow from "./modules/auth.js";

import { Select } from "https://deno.land/x/cliffy/prompt/select.ts";
import { config } from "https://deno.land/x/dotenv/mod.ts";

import { download_repos } from './modules/git.js';

config({ safe: true, export: true });

let octokit;

while(true) {
let options = {
'download': {
disabled: octokit === undefined,
name: 'Download Repositories',
action: async () => {
await download_repos(octokit);
}
},
'delete': {
disabled: octokit === undefined,
name: 'Delete Repositories',
action: async () => {
console.log('start grading')
}
},
'auth': {
disabled: false,
name: 'Authenticate',
action: async () => {
octokit = await AuthFlow();
}
},
'exit': {
disabled: false,
name: 'Exit',
action: async () => {
Deno.exit();
}
}
}

const choice = await Select.prompt({
message: "What would you like to do?",
options: Object.entries(options).map(([value, {name, disabled}]) => {
return {value, name, disabled}
}),
});

await options[choice].action();
}
34 changes: 34 additions & 0 deletions modules/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Octokit } from "https://cdn.skypack.dev/octokit?dts";
import { createOAuthDeviceAuth } from "https://cdn.pika.dev/@octokit/auth-oauth-device";

export default async function() {
const octokit = new Octokit({
authStrategy: createOAuthDeviceAuth,
auth: {
clientType: "oauth-app",
clientId: Deno.env.get('GITHUB_CLIENT_ID'),
scopes: ["public_repo"],
async onVerification (verification) {
// verification example
// {
// device_code: "3584d83530557fdd1f46af8289938c8ef79f9dc5",
// user_code: "WDJB-MJHT",
// verification_uri: "https://github.com/login/device",
// expires_in: 900,
// interval: 5,
// };

console.log("Open %s", verification.verification_uri);
console.log("Enter code: %s", verification.user_code);
},
},
});

const {
data: { login },
} = await octokit.rest.users.getAuthenticated();

console.log("Hello, %s", login);

return octokit;
}
88 changes: 88 additions & 0 deletions modules/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Select } from "https://deno.land/x/cliffy/prompt/select.ts";
import { Input } from "https://deno.land/x/cliffy/prompt/input.ts";
import { Toggle } from "https://deno.land/x/cliffy/prompt/toggle.ts";

let cache = {};

export async function download_repos(octokit) {
let is_org = await Select.prompt({
message: "Where are these repos?",
options: [
{value: 'org', name: 'Organization'},
{value: 'user', name: 'User'}
],
}) == 'org';

const name = await Input.prompt(`What is the ${is_org ? 'organization' : 'user'} name?`);

let fetch_repos = true;
let suggestions;

if (cache[name]?.length > 0) {
fetch_repos = await Toggle.prompt("I have recently fetched these repos. Should I fetch them again?");
suggestions = cache[name].map(v => {
return v.name;
})
}

const reg = await Input.prompt({
message: `What would you like to search for?`,
suggestions,
list: suggestions !== undefined,
});

let repos = await get_repos(octokit, is_org, name, reg);

const req = { name: "run" };
const perm = await Deno.permissions.request(req);

if (perm.state == "granted") {
await Promise.all(repos.map(async (r) => {
let dir_name = `./tmp/${r.full_name}`;

const rm = Deno.run({
cmd: ["rm", "-rf", dir_name]
})

await rm.status();

const clone = Deno.run({
cmd: ["git", "clone", r.clone_url, dir_name]
})

return clone.status();
}))

await Deno.permissions.revoke(req)
} else {
console.log('failed to download -- please grant permissions to run scripts')
}

return repos;
}

export async function delete_repos(octokit) {

}

async function get_repos(octokit, is_org, name, reg) {
let err;

let repos = await octokit.paginate(`GET /${is_org ? `orgs` : `users` }/{owner}/repos`, {
owner: name,
per_page: 100
}).catch(e => {
console.log(e);
return [];
})

cache[name] = repos;

if (typeof reg == "string") {
reg = new RegExp(reg)
}

return repos.filter((v) => {
return reg.test(v.name)
})
}
15 changes: 15 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let repos = ['kit-static-error', 'manga-reader'];

await Promise.all(repos.map(async (r) => {
const rm = Deno.run({
cmd: ["rm", "-rf", `./tmp/${r}`]
})

await rm.status();

const clone = Deno.run({
cmd: ["git","clone",`https://github.com/DryCreations/${r}.git`, `./tmp/${r}`]
})

return clone.status();
}))

0 comments on commit e106d0d

Please sign in to comment.