diff --git a/src/index.js b/src/index.js index 9aefbf6..1ea9277 100644 --- a/src/index.js +++ b/src/index.js @@ -3,18 +3,25 @@ const request = require('superagent'); const {default: PQueue} = require('p-queue'); -async function getGithubRepositories(username, token) { +async function getGithubRepositories(username, token, mirrorPrivateRepositories) { const octokit = new Octokit({ auth: token || null, }); const publicRepositoriesWithForks = await octokit.paginate('GET /users/:username/repos', { username: username }) - .then(repositories => toRepositoryList(repositories)); + .then(repositories => toRepositoryList(repositories)); - const allRepositoriesWithoutForks = await octokit.paginate('GET /search/repositories?q=user:{username}', { username : username }) + let allRepositoriesWithoutForks; + if(mirrorPrivateRepositories === 'true'){ + allRepositoriesWithoutForks = await octokit.paginate('GET /search/repositories?q=user:{username}', { username : username }) .then(repositories => toRepositoryList(repositories)); + } - return filterDuplicates(allRepositoriesWithoutForks.concat(publicRepositoriesWithForks)); + if(mirrorPrivateRepositories === 'true'){ + return filterDuplicates(allRepositoriesWithoutForks.concat(publicRepositoriesWithForks)); + }else{ + return publicRepositoriesWithForks; + } } function toRepositoryList(repositories) { @@ -57,7 +64,7 @@ function mirrorOnGitea(repository, gitea, giteaUser, githubToken) { request.post(`${gitea.url}/api/v1/repos/migrate`) .set('Authorization', 'token ' + gitea.token) .send({ - auth_token: githubToken, + auth_token: githubToken || null, clone_addr: repository.url, mirror: true, repo_name: repository.name, @@ -91,19 +98,26 @@ async function main() { } const githubToken = process.env.GITHUB_TOKEN; const giteaUrl = process.env.GITEA_URL; + if (!giteaUrl) { console.error('No GITEA_URL specified, please specify! Exiting..'); return; } - const giteaToken = process.env.GITEA_TOKEN; + const giteaToken = process.env.GITEA_TOKE; if (!giteaToken) { console.error('No GITEA_TOKEN specified, please specify! Exiting..'); return; } + const mirrorPrivateRepositories = process.env.MIRROR_PRIVATE_REPOSITORIES; + if(mirrorPrivateRepositories === 'true' && !githubToken){ + console.error('MIRROR_PRIVATE_REPOSITORIES was set to true but no GITHUB_TOKEN was specified, please specify! Exiting..') + return; + } - const githubRepositories = await getGithubRepositories(githubUsername, githubToken); + + const githubRepositories = await getGithubRepositories(githubUsername, githubToken, mirrorPrivateRepositories); console.log(`Found ${githubRepositories.length} repositories on github`); const gitea = {