Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: configuration load #40

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
100755 → 100644
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"worker": "wrangler dev --env dev --port 8787",
"proxy": "tsx src/proxy.ts",
"knip": "knip --config .github/knip.ts",
"knip-ci": "knip --no-exit-code --reporter json --config .github/knip.ts"
"knip-ci": "knip --no-exit-code --reporter json --config .github/knip.ts",
"test": "bun test"
},
"keywords": [
"typescript",
Expand Down
8 changes: 4 additions & 4 deletions src/github/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { BotConfig, generateConfiguration } from "@ubiquibot/configuration";

const UBIQUIBOT_CONFIG_FULL_PATH = ".github/.ubiquibot-config.yml";

export async function getConfig(context: GitHubContext): Promise<BotConfig | null> {
export async function getConfig(context: GitHubContext): Promise<BotConfig> {
const payload = context.payload;
const defaultConfiguration = generateConfiguration();
if (!("repository" in payload) || !payload.repository) {
Expand All @@ -29,13 +29,13 @@ export async function getConfig(context: GitHubContext): Promise<BotConfig | nul
try {
config = Value.Decode(configSchema, Value.Default(configSchema, _repoConfig));
} catch (error) {
console.error("Error decoding config", error);
return null;
console.error("Error decoding config, will use default.", error);
return defaultConfiguration;
}

checkPluginChains(config);

return generateConfiguration(config as BotConfig);
return generateConfiguration(Value.Default(configSchema, _repoConfig) as BotConfig);
gentlementlegen marked this conversation as resolved.
Show resolved Hide resolved
}

function checkPluginChains(config: PluginConfiguration) {
Expand Down
76 changes: 75 additions & 1 deletion tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ mock.module("@octokit/webhooks", () => ({
Webhooks: WebhooksMocked,
}));

const issueOpened = "issues.opened";

class WebhooksMocked {
constructor(_: unknown) {}
verifyAndReceive(_: unknown) {
Expand All @@ -21,6 +23,9 @@ class WebhooksMocked {
}

import { config } from "dotenv";
import { GitHubContext } from "../src/github/github-context";
import { GitHubEventHandler } from "../src/github/github-event-handler";
import { getConfig } from "../src/github/utils/config";
import worker from "../src/worker";
import { server } from "./__mocks__/node";

Expand Down Expand Up @@ -53,7 +58,7 @@ describe("Worker tests", () => {
it("Should start a worker", async () => {
const req = new Request("http://localhost:8080", {
headers: {
"x-github-event": "issues.opened",
"x-github-event": issueOpened,
"x-github-delivery": "1",
"x-hub-signature-256": "123456",
},
Expand All @@ -66,4 +71,73 @@ describe("Worker tests", () => {
});
expect(res.status).toEqual(200);
});

describe("Configuration tests", () => {
it("Should generate a default configuration when no repo is defined", async () => {
const cfg = await getConfig({
key: issueOpened,
name: issueOpened,
id: "",
payload: {
repository: "",
},
octokit: {},
eventHandler: {} as GitHubEventHandler,
} as unknown as GitHubContext);
expect(cfg).toBeTruthy();
});
it("Should generate a default configuration when the target repo does not contain one", async () => {
const cfg = await getConfig({
key: issueOpened,
name: issueOpened,
id: "",
payload: {
repository: {
owner: { login: "ubiquity" },
name: "ubiquibot-kernel",
},
} as unknown as GitHubContext<"issues.closed">["payload"],
octokit: {
rest: {
repos: {
getContent() {
return { data: null };
},
},
},
},
eventHandler: {} as GitHubEventHandler,
} as unknown as GitHubContext);
expect(cfg).toBeTruthy();
});
it("Should merge the configuration when found", async () => {
const cfg = await getConfig({
key: issueOpened,
name: issueOpened,
id: "",
payload: {
repository: {
owner: { login: "ubiquity" },
name: "conversation-rewards",
},
} as unknown as GitHubContext<"issues.closed">["payload"],
octokit: {
rest: {
repos: {
getContent() {
return {
data: `
incentives:
enabled: false`,
};
},
},
},
},
eventHandler: {} as GitHubEventHandler,
} as unknown as GitHubContext);
expect(cfg).toBeTruthy();
expect(cfg.incentives.enabled).toBeFalse();
});
});
});
Loading