mirror of
https://github.com/amalgamated-tools/mirror-to-gitea.git
synced 2025-12-23 22:18:05 -05:00
migrate to new configuration parser
This commit is contained in:
@@ -11,4 +11,6 @@ docker container run \
|
||||
-e GITHUB_USERNAME="$GITHUB_USERNAME" \
|
||||
-e GITEA_URL="$GITEA_URL" \
|
||||
-e GITEA_TOKEN="$GITEA_TOKEN" \
|
||||
-e MIRROR_PRIVATE_REPOSITORIES="true" \
|
||||
-e DRY_RUN="true" \
|
||||
jaedle/mirror-to-gitea:development
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const mustReadEnv = (variable) => {
|
||||
const val = process.env[variable];
|
||||
if (val === undefined || val.length === 0) {
|
||||
throw new Error();
|
||||
throw new Error(`invalid configuration, please provide ${variable}`);
|
||||
}
|
||||
|
||||
return val;
|
||||
@@ -37,7 +37,9 @@ export function configuration() {
|
||||
};
|
||||
|
||||
if (config.github.privateRepositories && config.github.token === undefined) {
|
||||
throw new Error();
|
||||
throw new Error(
|
||||
"invalid configuration, mirroring private repositories requires setting GITHUB_TOKEN",
|
||||
);
|
||||
}
|
||||
|
||||
return config;
|
||||
|
||||
@@ -51,21 +51,27 @@ describe("configuration", () => {
|
||||
provideMandatoryVariables();
|
||||
delete process.env.GITEA_URL;
|
||||
|
||||
expect(() => configuration()).toThrow();
|
||||
expect(() => configuration()).toThrow(
|
||||
new Error("invalid configuration, please provide GITEA_URL"),
|
||||
);
|
||||
});
|
||||
|
||||
it("requires gitea token", () => {
|
||||
provideMandatoryVariables();
|
||||
delete process.env.GITEA_TOKEN;
|
||||
|
||||
expect(() => configuration()).toThrow();
|
||||
expect(() => configuration()).toThrow(
|
||||
new Error("invalid configuration, please provide GITEA_TOKEN"),
|
||||
);
|
||||
});
|
||||
|
||||
it("requires github username", () => {
|
||||
provideMandatoryVariables();
|
||||
delete process.env.GITHUB_USERNAME;
|
||||
|
||||
expect(() => configuration()).toThrow();
|
||||
expect(() => configuration()).toThrow(
|
||||
new Error("invalid configuration, please provide GITHUB_USERNAME"),
|
||||
);
|
||||
});
|
||||
|
||||
it("reads github token", () => {
|
||||
@@ -168,7 +174,11 @@ describe("configuration", () => {
|
||||
provideMandatoryVariables();
|
||||
process.env.MIRROR_PRIVATE_REPOSITORIES = "true";
|
||||
|
||||
expect(() => configuration()).toThrow();
|
||||
expect(() => configuration()).toThrow(
|
||||
new Error(
|
||||
"invalid configuration, mirroring private repositories requires setting GITHUB_TOKEN",
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it("parses delay", () => {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Octokit } from "@octokit/rest";
|
||||
import PQueue from "p-queue";
|
||||
import * as request from "superagent";
|
||||
import { configuration } from "./configuration.mjs";
|
||||
|
||||
async function getGithubRepositories(
|
||||
username,
|
||||
@@ -117,58 +118,37 @@ async function mirror(repository, gitea, giteaUser, githubToken, dryRun) {
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const githubUsername = process.env.GITHUB_USERNAME;
|
||||
if (!githubUsername) {
|
||||
console.error("No GITHUB_USERNAME specified, please specify! Exiting..");
|
||||
return;
|
||||
let config;
|
||||
try {
|
||||
config = configuration();
|
||||
} catch (e) {
|
||||
console.error("invalid configuration", e);
|
||||
process.exit(1);
|
||||
}
|
||||
const mirrorForks = !["1", "true"].includes(process.env.SKIP_FORKS);
|
||||
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;
|
||||
if (!giteaToken) {
|
||||
console.error("No GITEA_TOKEN specified, please specify! Exiting..");
|
||||
return;
|
||||
}
|
||||
|
||||
const mirrorPrivateRepositories = ["1", "true"].includes(
|
||||
process.env.MIRROR_PRIVATE_REPOSITORIES,
|
||||
);
|
||||
if (mirrorPrivateRepositories && !githubToken) {
|
||||
console.error(
|
||||
"MIRROR_PRIVATE_REPOSITORIES was set to true but no GITHUB_TOKEN was specified, please specify! Exiting..",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const dryRun = ["1", "true"].includes(process.env.DRY_RUN);
|
||||
|
||||
console.log("Starting with the following configuration:");
|
||||
console.log(` - GITHUB_USERNAME: ${githubUsername}`);
|
||||
console.log(` - GITHUB_TOKEN: ${githubToken ? "****" : ""}`);
|
||||
console.log(` - GITEA_URL: ${giteaUrl}`);
|
||||
console.log(` - GITEA_TOKEN: ${giteaToken ? "****" : ""}`);
|
||||
console.log(` - MIRROR_PRIVATE_REPOSITORIES: ${mirrorPrivateRepositories}`);
|
||||
console.log(` - SKIP_FORKS: ${!mirrorForks}`);
|
||||
console.log(` - DRY_RUN: ${dryRun}`);
|
||||
console.log(` - GITHUB_USERNAME: ${config.github.username}`);
|
||||
console.log(` - GITHUB_TOKEN: ${config.github.token ? "****" : ""}`);
|
||||
console.log(
|
||||
` - MIRROR_PRIVATE_REPOSITORIES: ${config.github.privateRepositories}`,
|
||||
);
|
||||
console.log(` - GITEA_URL: ${config.gitea.url}`);
|
||||
console.log(` - GITEA_TOKEN: ${config.gitea.token ? "****" : ""}`);
|
||||
console.log(` - SKIP_FORKS: ${config.github.skipForks}`);
|
||||
console.log(` - DRY_RUN: ${config.dryRun}`);
|
||||
|
||||
const githubRepositories = await getGithubRepositories(
|
||||
githubUsername,
|
||||
githubToken,
|
||||
mirrorPrivateRepositories,
|
||||
mirrorForks,
|
||||
config.github.username,
|
||||
config.github.token,
|
||||
config.github.privateRepositories,
|
||||
!config.github.skipForks,
|
||||
);
|
||||
|
||||
console.log(`Found ${githubRepositories.length} repositories on github`);
|
||||
|
||||
const gitea = {
|
||||
url: giteaUrl,
|
||||
token: giteaToken,
|
||||
url: config.gitea.url,
|
||||
token: config.gitea.token,
|
||||
};
|
||||
const giteaUser = await getGiteaUser(gitea);
|
||||
|
||||
@@ -176,7 +156,13 @@ async function main() {
|
||||
await queue.addAll(
|
||||
githubRepositories.map((repository) => {
|
||||
return async () => {
|
||||
await mirror(repository, gitea, giteaUser, githubToken, dryRun);
|
||||
await mirror(
|
||||
repository,
|
||||
gitea,
|
||||
giteaUser,
|
||||
config.github.token,
|
||||
config.dryRun,
|
||||
);
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user