diff --git a/package-lock.json b/package-lock.json index 84ef74f..d6904ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "ISC", "dependencies": { "@octokit/rest": "^18.0.12", + "js-yaml": "^4.1.0", "p-queue": "^6.6.2", "superagent": "^4.0.0" } @@ -134,6 +135,11 @@ "@octokit/openapi-types": "^12.11.0" } }, + "node_modules/argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" + }, "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -286,6 +292,17 @@ "node": ">=0.10.0" } }, + "node_modules/js-yaml": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", + "dependencies": { + "argparse": "^2.0.1" + }, + "bin": { + "js-yaml": "bin/js-yaml.js" + } + }, "node_modules/methods": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", @@ -633,6 +650,11 @@ "@octokit/openapi-types": "^12.11.0" } }, + "argparse": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", + "integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==" + }, "asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", @@ -746,6 +768,14 @@ "resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-5.0.0.tgz", "integrity": "sha512-VRSzKkbMm5jMDoKLbltAkFQ5Qr7VDiTFGXxYFXXowVj387GeGNOCsOH6Msy00SGZ3Fp84b1Naa1psqgcCIEP5Q==" }, + "js-yaml": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", + "integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==", + "requires": { + "argparse": "^2.0.1" + } + }, "methods": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", diff --git a/package.json b/package.json index 6f7a4b6..f359797 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "homepage": "https://github.com/jaedle/mirror-to-gitea#readme", "dependencies": { "@octokit/rest": "^18.0.12", + "js-yaml": "^4.1.0", "p-queue": "^6.6.2", "superagent": "^4.0.0" } diff --git a/src/index.js b/src/index.js index 0ff8d0d..ddd0bea 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,8 @@ const {Octokit} = require('@octokit/rest'); const request = require('superagent'); const {default: PQueue} = require('p-queue'); +const yaml = require('js-yaml'); +const fs = require('fs'); async function getGithubRepositories(username, token, mirrorPrivateRepositories, isOrg) { @@ -119,19 +121,8 @@ async function mirror(repository, gitea, giteaUser, githubToken, giteaOwner) { await ensureOrg(gitea, giteaOwner,() => mirrorOnGitea(repository, gitea, giteaUser, githubToken, giteaOwner)); } -async function main() { - const githubUsernameAll = process.env.GITHUB_USERNAME.split(':'); - const githubUsername = githubUsernameAll[0] - if (!githubUsername) { - console.error('No GITHUB_USERNAME specified, please specify! Exiting..'); - return; - } - - const isOrg = githubUsernameAll.length > 0 && githubUsernameAll[1] === "org" - - const githubToken = process.env.GITHUB_TOKEN; +async function createMirrorsOnGites() { const giteaUrl = process.env.GITEA_URL; - if (!giteaUrl) { console.error('No GITEA_URL specified, please specify! Exiting..'); return; @@ -142,16 +133,8 @@ async function main() { 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, mirrorPrivateRepositories, isOrg); - console.log(`Found ${githubRepositories.length} repositories on github`); - + + const githubToken = process.env.GITHUB_TOKEN; const gitea = { url: giteaUrl, token: giteaToken, @@ -164,6 +147,77 @@ async function main() { await mirror(repository, gitea, giteaUser, githubToken, githubUsername); }; })); + } -main(); +async function singleOrg() { + const githubUsernameAll = process.env.GITHUB_USERNAME.split(':'); + const githubUsername = githubUsernameAll[0] + if (!githubUsername) { + console.error('No GITHUB_USERNAME specified, please specify! Exiting..'); + return; + } + + const isOrg = githubUsernameAll.length > 0 && githubUsernameAll[1] === "org" + + const githubToken = process.env.GITHUB_TOKEN; + + 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, mirrorPrivateRepositories, isOrg); + console.log(`Found ${githubRepositories.length} repositories on github`); + await createMirrorsOnGitea(githubRepositories); + +} + +async function yamlOrg() { + + const yamlPath = process.env.YAML_URL; + if (!yamlPath) { + console.error('No YAML_URL specified, please specify! Exiting..'); + return; + } + + var yamlContent = await fetch(yamlPath) + .then(resp => resp.text()) + + var doc = yaml.load(yamlContent); + + const githubToken = process.env.GITHUB_TOKEN; + + const repos = [] + for(var org of doc.orgs) { + console.log(`Fetching org ${org}...`) + const githubRepositories = await getGithubRepositories(org, githubToken, false, true); + console.log(`\tFound ${githubRepositories.length} repositories on github`); + repos.push(...githubRepositories) + + } + + await createMirrorsOnGitea(repos); + +} + +let mode = "single"; +const modeconfig = process.env.MIRROR_MODE; +if (modeconfig ) { + mode = modeconfig; +} + +if(mode === "single") { + console.log("Running in single mode.") + singleOrg(); +} +else if(mode === "yaml") { + console.log("running in yaml mode") + yamlOrg(); +} + + + + +