Skip to content

Commit

Permalink
fix: simplify commentType generation and default multipliers
Browse files Browse the repository at this point in the history
Refactor commentType to use stringLiteralUnion
  • Loading branch information
gentlementlegen committed Nov 17, 2024
1 parent 9cc7c4a commit 0b0243e
Showing 1 changed file with 82 additions and 65 deletions.
147 changes: 82 additions & 65 deletions src/configuration/formatting-evaluator-config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { Static, Type } from "@sinclair/typebox";
import { Static, TLiteral, TUnion, Type } from "@sinclair/typebox";
import { CommentAssociation, CommentKind, CommentType } from "./comment-types";

export const commentType = Type.Union(
type IntoStringLiteralUnion<T> = { [K in keyof T]: T[K] extends string ? TLiteral<T[K]> : never };

function stringLiteralUnion<T extends string[]>(values: [...T]): TUnion<IntoStringLiteralUnion<T>> {
const literals = values.map((value) => Type.Literal(value));
return Type.Union(literals) as TUnion<IntoStringLiteralUnion<T>>;
}

export const commentType = stringLiteralUnion(
Object.keys(CommentKind).flatMap((kind) =>
Object.keys(CommentAssociation).map((association) => Type.Literal(`${kind}_${association}` as CommentType))
)
Object.keys(CommentAssociation).map((association) => `${kind}_${association}`)
) as CommentType[]
);

export const wordRegex = /\b\w+\b/;
Expand Down Expand Up @@ -55,68 +62,78 @@ export const formattingEvaluatorConfigurationType = Type.Object(
/**
* Multipliers applied to different parts of the comment body content
*/
multipliers: Type.Array(
Type.Object({
role: Type.Array(commentType, { minItems: 1 }),
multiplier: Type.Number(),
rewards: rewardsType,
multipliers: Type.Transform(
Type.Array(
Type.Object({
role: Type.Array(commentType, { minItems: 1 }),
multiplier: Type.Number(),
rewards: rewardsType,
}),
{
default: [],
}
)
)
.Decode((value) => {
if (!value?.length) {
return [
{
role: ["ISSUE_SPECIFICATION"] as CommentType[],
multiplier: 1,
rewards: { wordValue: 0.1, html: htmlType.default },
},
{
role: ["ISSUE_AUTHOR"] as CommentType[],
multiplier: 1,
rewards: { wordValue: 0.2, html: htmlType.default },
},
{
role: ["ISSUE_ASSIGNEE"] as CommentType[],
multiplier: 1,
rewards: { wordValue: 0.1, html: htmlType.default },
},
{
role: ["ISSUE_COLLABORATOR"] as CommentType[],
multiplier: 1,
rewards: { wordValue: 0.1, html: htmlType.default },
},
{
role: ["ISSUE_CONTRIBUTOR"] as CommentType[],
multiplier: 0.25,
rewards: { wordValue: 0.1, html: htmlType.default },
},
{
role: ["PULL_SPECIFICATION"] as CommentType[],
multiplier: 0,
rewards: { wordValue: 0, html: htmlType.default },
},
{
role: ["PULL_AUTHOR"] as CommentType[],
multiplier: 0,
rewards: { wordValue: 0.2, html: htmlType.default },
},
{
role: ["PULL_ASSIGNEE"] as CommentType[],
multiplier: 1,
rewards: { wordValue: 0.1, html: htmlType.default },
},
{
role: ["PULL_COLLABORATOR"] as CommentType[],
multiplier: 1,
rewards: { wordValue: 0.1, html: htmlType.default },
},
{
role: ["PULL_CONTRIBUTOR"] as CommentType[],
multiplier: 0.25,
rewards: { wordValue: 0.1, html: htmlType.default },
},
];
}
return value;
})
.Encode((value) => {
return value;
}),
{
minItems: 1,
default: [
{
role: ["ISSUE_SPECIFICATION"],
multiplier: 1,
rewards: { wordValue: 0.1 },
},
{
role: ["ISSUE_AUTHOR"],
multiplier: 1,
rewards: { wordValue: 0.2 },
},
{
role: ["ISSUE_ASSIGNEE"],
multiplier: 0,
rewards: { wordValue: 0 },
},
{
role: ["ISSUE_COLLABORATOR"],
multiplier: 1,
rewards: { wordValue: 0.1 },
},
{
role: ["ISSUE_CONTRIBUTOR"],
multiplier: 0.25,
rewards: { wordValue: 0.1 },
},
{
role: ["PULL_SPECIFICATION"],
multiplier: 0,
rewards: { wordValue: 0 },
},
{
role: ["PULL_AUTHOR"],
multiplier: 2,
rewards: { wordValue: 0.2 },
},
{
role: ["PULL_ASSIGNEE"],
multiplier: 1,
rewards: { wordValue: 0.1 },
},
{
role: ["PULL_COLLABORATOR"],
multiplier: 1,
rewards: { wordValue: 0.1 },
},
{
role: ["PULL_CONTRIBUTOR"],
multiplier: 0.25,
rewards: { wordValue: 0.1 },
},
],
}
),
wordCountExponent: Type.Number({ default: 0.85 }),
},
{ default: {} }
Expand Down

0 comments on commit 0b0243e

Please sign in to comment.