Skip to content

Commit

Permalink
add ability to pull from yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticroentgen committed Mar 21, 2024
1 parent e4a1e50 commit a594816
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 23 deletions.
30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
100 changes: 77 additions & 23 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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();
}





0 comments on commit a594816

Please sign in to comment.