mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-20 17:13:49 +00:00
feat(filters): add bilingual suggestions and value validation
Catalog forge events, actions, payload paths and known values with searchable English and Chinese descriptions. Match-value validation mirrors the server and only rejects inputs that cannot match.
This commit is contained in:
1 parent
7aa36f4dc2
commit
a3e9ab26a7
4 files changed
+1397
-1
No files matched your search
@@ -44,7 +44,8 @@ app/ # Vue 3 UI (Nuxt app dir)
|
||||
├── composables/ # useI18n, useToasts, useConfirm (promise-based confirm), useGroups, useGroupRoutes, useFilterNode (AST helpers), useFragments, useLogs,
|
||||
│ # useAudit, useInvites, useWebhook
|
||||
├── types.ts # shared client types (Route, Group, Filter, ...)
|
||||
└── utils/legal.ts # terms/privacy HTML bodies (zh/en)
|
||||
└── utils/ # legal.ts (terms/privacy HTML bodies, zh/en), format.ts,
|
||||
# filter-catalog.ts (bilingual filter suggestions + value/path validators)
|
||||
server/ # Nitro server
|
||||
├── routes/ # H3 handlers: /health, /webhook[/:groupId], /discord/interactions, /telegram/webhook,
|
||||
│ # /auth/github*, /admin/{login,logout,invite,api/**}, /api/{comment,merge,close,react,richheader}
|
||||
|
||||
@@ -283,6 +283,14 @@ const en: Dict = {
|
||||
"routeEditor.errPath": "Field filters need a path",
|
||||
"routeEditor.errValues": "Add at least one value",
|
||||
"routeEditor.errGroupEmpty": "Group needs at least one condition",
|
||||
"valid.tooLong": "Patterns longer than 200 characters never match",
|
||||
"valid.regex": "Invalid regular expression",
|
||||
"valid.number": "Comparison operators need a number",
|
||||
"valid.event": "Event names use letters, digits and _ (or * ? wildcards, /regex/)",
|
||||
"valid.eventName": "Event names use letters, digits and _",
|
||||
"valid.branch": "Branch names cannot contain spaces or ~ ^ : \\ [",
|
||||
"valid.branchLiteral":
|
||||
"This operator compares text as-is: branch names never contain * ? [ ~ ^ : \\ or spaces",
|
||||
"fragmentPreset.prBot": "PR by a bot",
|
||||
"fragmentPreset.dependabot": "Dependabot PR",
|
||||
"fragmentPreset.releasePublished": "Release published",
|
||||
@@ -694,6 +702,13 @@ const zh: Dict = {
|
||||
"routeEditor.errPath": "字段过滤需要路径",
|
||||
"routeEditor.errValues": "至少需要一个值",
|
||||
"routeEditor.errGroupEmpty": "分组至少需要一个条件",
|
||||
"valid.tooLong": "超过 200 个字符的模式永远不会匹配",
|
||||
"valid.regex": "无效的正则表达式",
|
||||
"valid.number": "比较运算需要数字",
|
||||
"valid.event": "事件名只能包含字母、数字和 _(也可用 * ? 通配符或 /正则/)",
|
||||
"valid.eventName": "事件名只能包含字母、数字和 _",
|
||||
"valid.branch": "分支名不能包含空格或 ~ ^ : \\ [",
|
||||
"valid.branchLiteral": "该运算符按字面比较:分支名不会包含 * ? [ ~ ^ : \\ 或空格",
|
||||
"fragmentPreset.prBot": "机器人提交的 PR",
|
||||
"fragmentPreset.dependabot": "Dependabot PR",
|
||||
"fragmentPreset.releasePublished": "发布 Release",
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,152 @@
|
||||
import { describe, it, expect } from "bun:test";
|
||||
import {
|
||||
actionOptions,
|
||||
eventOptions,
|
||||
fieldPathOptions,
|
||||
fieldValueOptions,
|
||||
isArrayFieldPath,
|
||||
validateFilterValue,
|
||||
valueOptions,
|
||||
} from "../app/utils/filter-catalog";
|
||||
|
||||
function values(opts: { value: string }[]): string[] {
|
||||
return opts.map((o) => o.value);
|
||||
}
|
||||
|
||||
describe("filter catalog options", () => {
|
||||
it("lists every event once, formatted events first and Gitea-only events last", () => {
|
||||
const opts = eventOptions("en");
|
||||
const names = values(opts);
|
||||
expect(new Set(names).size).toBe(names.length);
|
||||
expect(names[0]).toBe("push");
|
||||
expect(names).toContain("custom");
|
||||
expect(names).toContain("merge_group");
|
||||
expect(names.slice(-3)).toEqual(["pull_request_approved", "pull_request_rejected", "wiki"]);
|
||||
});
|
||||
|
||||
it("describes every option in both languages and shows the active one", () => {
|
||||
for (const locale of ["en", "zh"] as const) {
|
||||
for (const opt of [
|
||||
...eventOptions(locale),
|
||||
...actionOptions(locale, []),
|
||||
...fieldPathOptions(locale, []),
|
||||
]) {
|
||||
expect(opt.hint).toBeTruthy();
|
||||
expect(opt.keywords?.length).toBeGreaterThanOrEqual(2);
|
||||
expect(opt.keywords).toContain(opt.hint!);
|
||||
}
|
||||
}
|
||||
const push = eventOptions("zh").find((o) => o.value === "push")!;
|
||||
expect(push.hint).toBe("推送提交到分支或标签");
|
||||
expect(push.keywords).toContain("Commits pushed to a branch or tag");
|
||||
});
|
||||
|
||||
it("puts the actions of the route's events first without duplicates", () => {
|
||||
const opts = actionOptions("en", ["pull_request"]);
|
||||
const names = values(opts);
|
||||
expect(new Set(names).size).toBe(names.length);
|
||||
expect(opts[0]!.group).toBe("For the selected events");
|
||||
expect(names.indexOf("synchronize")).toBeLessThan(names.indexOf("published"));
|
||||
// Actions stay searchable by the events that send them.
|
||||
expect(opts.find((o) => o.value === "published")!.keywords).toContain("release");
|
||||
});
|
||||
|
||||
it("offers the full action list when no event is known", () => {
|
||||
const opts = actionOptions("en", []);
|
||||
expect(opts.every((o) => o.group === undefined)).toBe(true);
|
||||
expect(values(opts)).toContain("label_updated");
|
||||
});
|
||||
|
||||
it("ranks field paths of the route's events first", () => {
|
||||
const opts = fieldPathOptions("en", ["workflow_run"]);
|
||||
const names = values(opts);
|
||||
expect(new Set(names).size).toBe(names.length);
|
||||
expect(opts[0]!.value.startsWith("workflow_run.")).toBe(true);
|
||||
expect(opts.find((o) => o.value === "sender.login")!.group).toBe("All events");
|
||||
});
|
||||
|
||||
it("knows the values of enumerated fields", () => {
|
||||
expect(values(fieldValueOptions("en", "workflow_run.conclusion"))).toContain("failure");
|
||||
expect(values(fieldValueOptions("en", " pull_request.draft "))).toEqual(["true", "false"]);
|
||||
expect(fieldValueOptions("en", "comment.body")).toEqual([]);
|
||||
for (const path of ["review.state", "deployment_status.state", "alert.rule.severity"]) {
|
||||
expect(fieldValueOptions("zh", path).every((o) => !!o.hint)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it("suggests values per filter type and none for free-form ones", () => {
|
||||
expect(values(valueOptions("en", "actor", "eq", "", []))).toContain("*[bot]");
|
||||
expect(values(valueOptions("en", "branch", "eq", "", []))).toContain("main");
|
||||
expect(valueOptions("en", "repo", "eq", "", [])).toEqual([]);
|
||||
expect(valueOptions("en", "keyword", "eq", "", [])).toEqual([]);
|
||||
});
|
||||
|
||||
it("ignores typed event names that are not catalog entries", () => {
|
||||
for (const name of ["constructor", "__proto__", "toString"]) {
|
||||
const opts = actionOptions("en", [name]);
|
||||
expect(opts.every((o) => typeof o.value === "string")).toBe(true);
|
||||
expect(opts.every((o) => o.group === undefined)).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("validateFilterValue", () => {
|
||||
it("accepts event names, globs and regexes", () => {
|
||||
expect(validateFilterValue("event", "eq", "pull_request")).toBeNull();
|
||||
expect(validateFilterValue("event", "eq", "pull_request*")).toBeNull();
|
||||
expect(validateFilterValue("event", "eq", "/^issue/")).toBeNull();
|
||||
expect(validateFilterValue("event", "eq", "pull request")).toBe("valid.event");
|
||||
expect(validateFilterValue("event", "eq", "/(/")).toBe("valid.regex");
|
||||
});
|
||||
|
||||
it("keeps repository and actor values free-form (custom webhooks carry any string)", () => {
|
||||
expect(validateFilterValue("repo", "eq", "acme/widget")).toBeNull();
|
||||
expect(validateFilterValue("repo", "eq", "monitoring/production cluster")).toBeNull();
|
||||
expect(validateFilterValue("actor", "eq", "*[bot]*")).toBeNull();
|
||||
expect(validateFilterValue("actor", "eq", "CI Agent")).toBeNull();
|
||||
});
|
||||
|
||||
it("checks branches and numbers", () => {
|
||||
expect(validateFilterValue("branch", "eq", "release/*")).toBeNull();
|
||||
expect(validateFilterValue("branch", "eq", "refs/tags/v*")).toBeNull();
|
||||
// code_scanning_alert carries the full ref, so refs/heads/... can match.
|
||||
expect(validateFilterValue("branch", "eq", "refs/heads/main")).toBeNull();
|
||||
expect(validateFilterValue("branch", "eq", "a:b")).toBe("valid.branch");
|
||||
expect(validateFilterValue("branch", "eq", "feature x")).toBe("valid.branch");
|
||||
expect(validateFilterValue("branch", "eq", "fix\tbug")).toBe("valid.branch");
|
||||
// Git allows non-ASCII whitespace inside refs.
|
||||
expect(validateFilterValue("branch", "eq", "release/版本\u30001")).toBeNull();
|
||||
expect(validateFilterValue("branch", "eq", "topic\u00a0a")).toBeNull();
|
||||
// `[` is literal for every operator; substring operators read `*`/`?` literally too.
|
||||
expect(validateFilterValue("branch", "eq", "release/[0-9]")).toBe("valid.branch");
|
||||
expect(validateFilterValue("branch", "eq", "/^release\\/[0-9]+$/")).toBeNull();
|
||||
expect(validateFilterValue("branch", "startsWith", "release/")).toBeNull();
|
||||
expect(validateFilterValue("branch", "startsWith", "release/*")).toBe("valid.branchLiteral");
|
||||
expect(validateFilterValue("branch", "contains", "a b")).toBe("valid.branchLiteral");
|
||||
expect(validateFilterValue("repo", "contains", "a*b")).toBeNull();
|
||||
expect(validateFilterValue("field", "gte", "10")).toBeNull();
|
||||
expect(validateFilterValue("field", "lt", "ten")).toBe("valid.number");
|
||||
});
|
||||
|
||||
it("treats the regex operator as a raw pattern and keyword as free text", () => {
|
||||
expect(validateFilterValue("repo", "regex", "^acme/")).toBeNull();
|
||||
expect(validateFilterValue("repo", "regex", "(")).toBe("valid.regex");
|
||||
expect(validateFilterValue("keyword", "exists", "any text")).toBeNull();
|
||||
expect(validateFilterValue("keyword", "eq", "/[/")).toBe("valid.regex");
|
||||
expect(validateFilterValue("action", "eq", "Deploy Docs")).toBeNull();
|
||||
});
|
||||
|
||||
it("rejects patterns the server would never match", () => {
|
||||
expect(validateFilterValue("event", "eq", "a*".repeat(101))).toBe("valid.tooLong");
|
||||
expect(validateFilterValue("field", "regex", "a".repeat(201))).toBe("valid.tooLong");
|
||||
});
|
||||
});
|
||||
|
||||
describe("isArrayFieldPath", () => {
|
||||
it("flags suggested paths that end on an array", () => {
|
||||
expect(isArrayFieldPath("commits.modified")).toBe(true);
|
||||
expect(isArrayFieldPath(" repository.topics ")).toBe(true);
|
||||
expect(isArrayFieldPath("commits.message")).toBe(false);
|
||||
expect(isArrayFieldPath("build status")).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user