Skip to content

Commit

Permalink
add new configuration parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jaedle committed Sep 24, 2024
1 parent f33094b commit 71afe18
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 1 deletion.
5 changes: 4 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
"linter": {
"enabled": true,
"rules": {
"recommended": true
"recommended": true,
"performance": {
"noDelete": "off"
}
}
},
"javascript": {
Expand Down
44 changes: 44 additions & 0 deletions src/configuration.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const mustReadEnv = (variable) => {
const val = process.env[variable];
if (val === undefined || val.length === 0) {
throw new Error();
}

return val;
};

function readBoolean(variable) {
return process.env[variable] === "true" || process.env[variable] === "1";
}

function readInt(variable) {
if (process.env[variable] === undefined) {
return undefined;
}

return Number.parseInt(process.env[variable]);
}

export function configuration() {
const defaultDelay = 3600;
const config = {
github: {
username: mustReadEnv("GITHUB_USERNAME"),
token: process.env.GITHUB_TOKEN,
skipForks: readBoolean("SKIP_FORKS"),
privateRepositories: readBoolean("MIRROR_PRIVATE_REPOSITORIES"),
},
gitea: {
url: mustReadEnv("GITEA_URL"),
token: mustReadEnv("GITEA_TOKEN"),
},
dryRun: readBoolean("DRY_RUN"),
delay: readInt("DELAY") ?? defaultDelay,
};

if (config.github.privateRepositories && config.github.token === undefined) {
throw new Error();
}

return config;
}
182 changes: 182 additions & 0 deletions src/configuration.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import { configuration } from "./configuration.mjs";

const aGithubUsername = "A_GITHUB_USERNAME";
const aGiteaUrl = "https://gitea.url";
const aGiteaToken = "secret-gitea-token";
const aGithubToken = "a-github-token";

const variables = [
"DELAY",
"DRY_RUN",
"GITEA_TOKEN",
"GITEA_URL",
"GITHUB_TOKEN",
"GITHUB_USERNAME",
"MIRROR_PRIVATE_REPOSITORIES",
"SKIP_FORKS",
];

function provideMandatoryVariables() {
process.env.GITHUB_USERNAME = aGithubUsername;
process.env.GITEA_URL = aGiteaUrl;
process.env.GITEA_TOKEN = aGiteaToken;
}

const defaultDelay = 3600;
describe("configuration", () => {
beforeEach(() => {
for (const variable of variables) {
delete process.env[variable];
}
});

it("reads configuration with default values", () => {
process.env.GITHUB_USERNAME = aGithubUsername;
process.env.GITEA_URL = aGiteaUrl;
process.env.GITEA_TOKEN = aGiteaToken;

const config = configuration();

expect(config.github.username).toEqual(aGithubUsername);
expect(config.github.token).toBeUndefined();
expect(config.github.skipForks).toEqual(false);

expect(config.gitea.url).toEqual(aGiteaUrl);
expect(config.gitea.token).toEqual(aGiteaToken);

expect(config.delay).toEqual(defaultDelay);
});

it("requires gitea url", () => {
provideMandatoryVariables();
delete process.env.GITEA_URL;

expect(() => configuration()).toThrow();
});

it("requires gitea token", () => {
provideMandatoryVariables();
delete process.env.GITEA_TOKEN;

expect(() => configuration()).toThrow();
});

it("requires github username", () => {
provideMandatoryVariables();
delete process.env.GITHUB_USERNAME;

expect(() => configuration()).toThrow();
});

it("reads github token", () => {
provideMandatoryVariables();
process.env.GITHUB_TOKEN = aGithubToken;

const config = configuration();

expect(config.github.token).toEqual(aGithubToken);
});

describe("dry run flag", () => {
it("treats true as true", () => {
provideMandatoryVariables();
process.env.DRY_RUN = "true";

const config = configuration();

expect(config.dryRun).toEqual(true);
});

it("treats 1 as true", () => {
provideMandatoryVariables();
process.env.DRY_RUN = "1";

const config = configuration();

expect(config.dryRun).toEqual(true);
});

it("treats missing flag as false", () => {
provideMandatoryVariables();

const config = configuration();

expect(config.dryRun).toEqual(false);
});
});

describe("skip forks flag", () => {
it("treats true as true", () => {
provideMandatoryVariables();
process.env.SKIP_FORKS = "true";

const config = configuration();

expect(config.github.skipForks).toEqual(true);
});

it("treats 1 as true", () => {
provideMandatoryVariables();
process.env.SKIP_FORKS = "1";

const config = configuration();

expect(config.github.skipForks).toEqual(true);
});

it("treats missing flag as false", () => {
provideMandatoryVariables();

const config = configuration();

expect(config.github.skipForks).toEqual(false);
});
});

describe("mirror private repositories flag", () => {
it("treats true as true", () => {
provideMandatoryVariables();
process.env.GITHUB_TOKEN = aGithubToken;
process.env.MIRROR_PRIVATE_REPOSITORIES = "true";

const config = configuration();

expect(config.github.privateRepositories).toEqual(true);
});

it("treats 1 as true", () => {
provideMandatoryVariables();
process.env.GITHUB_TOKEN = aGithubToken;

process.env.MIRROR_PRIVATE_REPOSITORIES = "1";

const config = configuration();

expect(config.github.privateRepositories).toEqual(true);
});

it("treats missing flag as false", () => {
provideMandatoryVariables();

const config = configuration();

expect(config.github.privateRepositories).toEqual(false);
});
});

it("requires a github token on private repository mirroring", () => {
provideMandatoryVariables();
process.env.MIRROR_PRIVATE_REPOSITORIES = "true";

expect(() => configuration()).toThrow();
});

it("parses delay", () => {
provideMandatoryVariables();
process.env.DELAY = "1200";

const config = configuration();

expect(config.delay).toEqual(1200);
});
});

0 comments on commit 71afe18

Please sign in to comment.