8000 Fix/get-repositories by camipozas · Pull Request #7 · camipozas/cordyceps · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix/get-repositories #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"init": "tsc --init",
"dev": "ts-node-dev --respawn src/index.ts",
"build": "tsc",
"lint": "eslint . --ext .ts",
"format": "prettier --write \"**/*.ts\""
"format": "prettier --write \"**/*.ts\"",
"test": "jest"
},
"repository": "git+https://github.com/camipozas/get-org-repo.git",
"author": "camipozas <cpozasg1103@gmail.com>",
Expand All @@ -23,6 +23,7 @@
"yarn": "1.22.19"
},
"devDependencies": {
"@types/jest": "^29.5.0",
"@types/node": "^18.14.2",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
Expand All @@ -32,6 +33,7 @@
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.5.0",
"prettier": "^2.8.7",
"ts-node-dev": "^2.0.0",
"typescript": "^4.9.5"
Expand Down
2 changes: 1 addition & 1 deletion src/clone-repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const cloneRepositories = async (repoNames: string[]) => {
const localPath = path.join(env.HOME, env.FOLDER, repoName);

if (fs.existsSync(localPath)) {
log(chalk.yellow(`❌ ${repoName} already exists in ${localPath}`));
log(chalk.red(`❌ ${repoName} already exists in`) + chalk.yellow(` 📁 ${localPath}`));
} else {
await git.clone(repoUrl, localPath);
log(chalk.green(`✅ Cloned ${repoName}`) + chalk.yellow(` 📁 ${localPath}`));
Expand Down
57 changes: 48 additions & 9 deletions src/get-repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { env } from './env/env';

const log = console.log;

interface Repo {
name: string;
}

/**
* It makes a request to the GitHub API to get a list of all the repositories in the organization, and
* then filters out the ones that don't start with "your-filter-prefix-" and is not empty.
* @returns An array of strings.
* It gets the number of pages of repositories in the GitHub organization
* @returns The number of pages of repositories in the GitHub organization.
*/
export const getAllRepositories = async () => {
export const getPages = async () => {
const response = await request('GET /orgs/{org}/repos', {
org: env.GITHUB_ORG,
headers: {
Expand All @@ -20,13 +23,49 @@ export const getAllRepositories = async () => {
per_page: 100,
});

const repoData = response.data;
if (!Array.isArray(repoData) || repoData.length === 0) {
return [];
const link = response.headers.link;
if (!link) {
return 1;
}

const pages = link.split(',').find((item) => item.includes('rel="last"'));
if (!pages) {
return 1;
}

const lastPage = pages.split(';')[0];
const lastPageNumber = lastPage.split('&page=')[1].split('>')[0];
return Number(lastPageNumber);
};

/**
* It makes a request to the GitHub API to get a list of all the repositories in the organization, and
* returns an array of the names of those repositories
* @param {number} pages - number - The number of pages of repositories to fetch.
* @returns An array of strings
*/
export const getAllRepositories = async (pages: number): Promise<string[]> => {
const repos: string[] = [];

for (let page = 1; page <= pages; page++) {
const response = await request('GET /orgs/{org}/repos', {
org: env.GITHUB_ORG,
headers: {
authorization: `token ${env.GITHUB_TOKEN}`,
},
per_page: 100,
page,
});

const repoData = response.data;
if (!Array.isArray(repoData) || repoData.length === 0) {
return [];
}

repos.push(...repoData.map((repo: Repo) => repo.name));
}

const repoNames = repoData.map((repo) => repo.name);
return repoNames.filter((repoName: string) => repoName.startsWith('mach-'));
return repos;
};

/**
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import chalk from 'chalk';

import { getAllRepositories, accessibleRepos } from './get-repositories';
import { getPages, getAllRepositories, accessibleRepos } from './get-repositories';
import { cloneRepositories } from './clone-repositories';
import { repoStatus } from './repo-status';

/**
* We get the number of pages of repositories, get all the repositories, filter out the ones we can't
* access, clone the ones we can, and then check the status of the clones
*/
const main = async () => {
const log = console.log;
log(chalk.blue.bold('🚀 Create your own GitHub organization backup script 🚀'));
const allRepos = await getAllRepositories();
const pages = await getPages();
log(chalk.magenta.bold(`🚀 There are ${pages} pages of repositories 🚀`));
const allRepos = await getAllRepositories(pages);
const accessible = await accessibleRepos(allRepos);
await cloneRepositories(accessible);
await repoStatus(accessible);
Expand Down
9 changes: 6 additions & 3 deletions src/repo-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ export const repoStatus = async (repoNames: string[]) => {
log(chalk.blue.bold('🚀 Checking repository status 🚀'));

for (const repoName of repoNames) {
const localPath = path.join(env.HOME, env.FOLDER, repoName);
const status = await git.status();
const repoPath = path.join(env.HOME, env.FOLDER, repoName);
await git.cwd(repoPath);
const { behind } = await git.status();

if (status.behind > 0) {
if (behind === 0) {
log(chalk.magenta(`🐛 ${repoName} is up to date`));
} else {
try {
await git.pull();
log(chalk.green(`🐛 Pulled latest changes for ${repoName}`));
Expand Down
Loading
0