Skip to content

Commit

Permalink
Merge pull request #40 from gentlementlegen/fix/configuration
Browse files Browse the repository at this point in the history
fix: configuration load
  • Loading branch information
molecula451 authored May 18, 2024
2 parents b063d2f + fe08f22 commit bfda111
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/bun-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ on:

env:
NODE_ENV: "test"
WEBHOOK_SECRET: ${{ secrets.WEBHOOK_SECRET }}
APP_ID: ${{ secrets.APP_ID }}
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}

jobs:
testing:
permissions: write-all
runs-on: ubuntu-latest
steps:
- uses: oven-sh/setup-bun@v1
- uses: actions/setup-node@v4
with:
node-version: '20.10.0'
Expand Down
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
12 changes: 6 additions & 6 deletions src/github/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Value } from "@sinclair/typebox/value";
import { GitHubContext } from "../github-context";
import { generateConfiguration } from "@ubiquibot/configuration";
import YAML from "yaml";
import { GitHubContext } from "../github-context";
import { expressionRegex } from "../types/plugin";
import { configSchema, PluginConfiguration } from "../types/plugin-configuration";
import { eventNames } from "../types/webhook-events";
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<PluginConfiguration> {
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 config;
}

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();
});
});
});

0 comments on commit bfda111

Please sign in to comment.