Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
74 changes: 74 additions & 0 deletions app/config-schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Ajv2020 from "ajv/dist/2020.js";

const toggleCommandSchema = {
additionalProperties: false,
properties: {
enabled: {
type: "boolean",
},
},
required: ["enabled"],
type: "object",
};

const labelCommandSchema = {
additionalProperties: false,
properties: {
allowedLabels: {
items: {
type: "string",
},
type: "array",
},
enabled: {
type: "boolean",
},
},
required: ["allowedLabels", "enabled"],
type: "object",
};

export const configSchema = {
$schema: "https://json-schema.org/draft/2020-12/schema",
additionalProperties: false,
properties: {
commands: {
additionalProperties: false,
properties: {
close: toggleCommandSchema,
label: labelCommandSchema,
removeLabel: labelCommandSchema,
reopen: toggleCommandSchema,
reviewer: toggleCommandSchema,
transfer: toggleCommandSchema,
},
required: [
"close",
"label",
"removeLabel",
"reopen",
"reviewer",
"transfer",
],
type: "object",
},
},
required: ["commands"],
type: "object",
};

const validator = new Ajv2020({
allErrors: true,
}).compile(configSchema);

export function validateConfig(config) {
if (validator(config)) {
return;
}

throw new Error(
validator.errors
.map((error) => `${error.instancePath || "/"} ${error.message}`)
.join("; "),
);
}
39 changes: 39 additions & 0 deletions app/config-schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { validateConfig } from "./config-schema.js";
import { defaultConfig } from "./default-config.js";

describe("config-schema", () => {
test("default config is valid", () => {
expect(() => validateConfig(defaultConfig)).not.toThrow();
});

test("enabled label config with allowed labels is valid", () => {
const config = structuredClone(defaultConfig);
config.commands.label.enabled = true;
config.commands.label.allowedLabels = ["bug", "help wanted"];

expect(() => validateConfig(config)).not.toThrow();
});

test("throws when enabled is not a boolean", () => {
const config = structuredClone(defaultConfig);
config.commands.close.enabled = "true";

expect(() => validateConfig(config)).toThrow("/commands/close/enabled");
});

test("throws when allowedLabels is not an array", () => {
const config = structuredClone(defaultConfig);
config.commands.label.allowedLabels = "bug";

expect(() => validateConfig(config)).toThrow(
"/commands/label/allowedLabels",
);
});

test("throws when command config includes unknown properties", () => {
const config = structuredClone(defaultConfig);
config.commands.transfer.target = "repo";

expect(() => validateConfig(config)).toThrow("/commands/transfer");
});
});
4 changes: 2 additions & 2 deletions app/router.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { addReaction, reportError } from "./github.js";
import { getAuthToken } from "./auth.js";
import { validateConfig } from "./config-schema.js";
import { defaultConfig } from "./default-config.js";

import { Octokit } from "@octokit/core";
Expand Down Expand Up @@ -44,14 +45,13 @@ export async function router(auth, id, payload, verbose) {
}

try {
// TODO validate against schema
// noinspection JSUnusedGlobalSymbols
const { config } = await octokit.config.get({
owner: payload.repository.owner.login,
repo: payload.repository.name,
path: ".github/comment-ops.yml",
defaults: (configs) => deepmerge.all([defaultConfig, ...configs]),
});
validateConfig(config);

for (const command of commands) {
const result = command.enabled(config);
Expand Down
Loading
Loading