Skip to content

Commit

Permalink
defaulted min_approvals to 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Bullrich authored Jul 26, 2023
1 parent a0d02a7 commit 2b6a7b4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/file/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { validate } from "@eng-automation/js";
import Joi from "joi";

import { ActionLogger } from "../github/types";
import { BasicRule, ConfigurationFile, Rule } from "./types";
import { BasicRule, ConfigurationFile, DebugRule, Rule, RuleTypes } from "./types";

/** For the users or team schema. Will be recycled A LOT
* Remember to add `.xor("users", "teams")` to force one of the two to be picked up
Expand All @@ -22,7 +22,8 @@ const ruleSchema = Joi.object<Rule & { type: string }>().keys({
include: Joi.array().items(Joi.string()).required(),
exclude: Joi.array().items(Joi.string()).optional().allow(null),
}),
type: Joi.string().required(),
// TODO: Add test with invalid type
type: Joi.string().valid(RuleTypes.Basic, RuleTypes.Debug).required(),
});

/** General Configuration schema.
Expand All @@ -38,7 +39,8 @@ export const generalSchema = Joi.object<ConfigurationFile>().keys({
* This rule is quite simple as it only has the min_approvals field and the required reviewers
*/
export const basicRuleSchema = Joi.object<BasicRule>()
.keys({ min_approvals: Joi.number().empty(1), ...reviewersObj })
// TODO: Add test with negative numbers
.keys({ min_approvals: Joi.number().min(1).default(1), ...reviewersObj })
.xor("users", "teams");

/**
Expand All @@ -54,19 +56,19 @@ export const validateConfig = (config: ConfigurationFile): ConfigurationFile | n
message: "Configuration file is invalid",
});

for (const rule of validatedConfig.rules) {
for (let i = 0; i < validatedConfig.rules.length; i++) {
const rule = validatedConfig.rules[i];
const { name, type } = rule;
const message = `Configuration for rule '${rule.name}' is invalid`;
if (type === "basic") {
validate<BasicRule>(rule, basicRuleSchema, { message });
validatedConfig.rules[i] = validate<BasicRule>(rule, basicRuleSchema, { message });
} else if (type === "debug") {
validate<Rule>(rule, ruleSchema, { message });
validatedConfig.rules[i] = validate<DebugRule>(rule, ruleSchema, { message });
} else {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new Error(`Rule ${name} has an invalid type: ${type}`);
}
}

return validatedConfig;
};

Expand Down
22 changes: 22 additions & 0 deletions src/test/runner/basicRule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,26 @@ describe("Basic rule parsing", () => {
`);
await expect(runner.getConfigFile("")).rejects.toThrowError('"value" must contain at least one of [users, teams]');
});

test("should default min_approvals to 1", async () => {
api.getConfigFile.mockResolvedValue(`
rules:
- name: Test review
condition:
include:
- '.*'
exclude:
- 'example'
type: basic
users:
- user-example
`);
const config = await runner.getConfigFile("");
const [rule] = config.rules;
if (rule.type === "basic") {
expect(rule.min_approvals).toEqual(1);
} else {
throw new Error(`Rule type ${rule.type} is invalid`);
}
});
});

0 comments on commit 2b6a7b4

Please sign in to comment.