From 8330f93a3da60c31aa086e9b67423f85127f50c5 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sun, 25 Oct 2020 21:10:59 +0100 Subject: [PATCH 1/2] optionally use GitHub access token to prevent rate limiting --- README.md | 1 + src/index.js | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c342a89..0551ced 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ This will a spin up a docker container running infinite which will try to mirror ### Parameters - `GITHUB_USERNAME` name of user or organization which public repos should be mirrored +- `GITHUB_TOKEN` [GitHub personal access token](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token) (optional) - `GITEA_URL` url of your gitea server - `GITEA_TOKEN` token for your gitea user diff --git a/src/index.js b/src/index.js index ce28765..61bb70a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,8 +1,11 @@ -const octokit = require('@octokit/rest')(); +const {Octokit} = require('@octokit/rest'); const request = require('superagent'); -async function getGithubRepositories(username) { +async function getGithubRepositories(username, token) { + const octokit = new Octokit({ + auth: token || null, + }); return octokit.paginate('GET /users/:username/repos', { username: username }) .then(repositories => toRepositoryList(repositories)); } @@ -66,6 +69,7 @@ async function main() { console.error('No GITHUB_USERNAME specified, please specify! Exiting..'); return; } + const githubToken = process.env.GITHUB_TOKEN; const giteaUrl = process.env.GITEA_URL; if (!giteaUrl) { console.error('No GITEA_URL specified, please specify! Exiting..'); @@ -79,7 +83,7 @@ async function main() { } - const githubRepositories = await getGithubRepositories(githubUsername); + const githubRepositories = await getGithubRepositories(githubUsername, githubToken); console.log(`Found ${githubRepositories.length} repositories on github`); const gitea = { From 463c1967b462b6f27b9a95e6f15cd85fa3b5f32a Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sun, 25 Oct 2020 21:11:35 +0100 Subject: [PATCH 2/2] send max 4 requests to Gitea concurrently --- package.json | 1 + src/index.js | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 997175c..f606df6 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "homepage": "https://github.com/jaedle/mirror-to-gitea#readme", "dependencies": { "@octokit/rest": "^16.2.0", + "p-queue": "^6.6.2", "superagent": "^4.0.0" } } \ No newline at end of file diff --git a/src/index.js b/src/index.js index 61bb70a..ae7da2d 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ const {Octokit} = require('@octokit/rest'); const request = require('superagent'); +const {default: PQueue} = require('p-queue'); async function getGithubRepositories(username, token) { @@ -90,13 +91,14 @@ async function main() { url: giteaUrl, token: giteaToken, }; - const giteaUser = await getGiteaUser(gitea); - githubRepositories.forEach( - async repository => { + + const queue = new PQueue({ concurrency: 4 }); + await queue.addAll(githubRepositories.map(repository => { + return async () => { await mirror(repository, gitea, giteaUser); - } - ); + }; + })); } main();