Skip to content

Commit 9095c5a

Browse files
committed
feat: add allowBots filter parameter
1 parent 816f756 commit 9095c5a

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ Additional options can be configured per URL:
2929
- This is case-sensitive and supports regex
3030
- Only full matches are considered, i.e. no substrings; `abc.*xyz` is equivalent to
3131
`/^(abc.*xyz)$/`
32+
- `allowBots`: Forward events from bots
33+
- Accepts a bool (`1`, `0`, `true`, `false`) or a regex (see `allowBranches` for supported values)
34+
- If unset, ignores events only from common CI bots[^1], unless the branch is explicitly allowed
3235
- `hideTags`: Ignore tag updates
3336
- `commentBurstLimit`: Ignore burst PR review comments in a short timespan, only showing the first x
3437
comments per review
38+
39+
[^1]: `coveralls[bot]`, `netlify[bot]`, `pre-commit-ci[bot]`, `dependabot[bot]`

src/filter.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { getAndIncrementKV } from "./kv.ts";
33
import { UrlConfig } from "./types.d.ts";
44
import { wildcardMatch } from "./util.ts";
55

6+
const COMMON_CI_BOTS = ["coveralls[bot]", "netlify[bot]", "pre-commit-ci[bot]", "dependabot[bot]"];
7+
68
export default async function filter(
79
headers: Record<string, string>,
810
json: any,
@@ -101,15 +103,19 @@ export default async function filter(
101103
}
102104
}
103105

104-
// ignore bots
105-
if (
106-
!isExplicitlyAllowedBranch && // show bot pushes on allowed branches
107-
login &&
108-
["coveralls[bot]", "netlify[bot]", "pre-commit-ci[bot]", "dependabot[bot]"].some((n) =>
109-
login.includes(n)
110-
)
111-
) {
112-
return "bot";
106+
if (login && login.endsWith("[bot]")) {
107+
if (config.allowBots !== undefined) {
108+
// ignore bot if matching
109+
if (!wildcardMatch(config.allowBots, login.slice(0, -5))) {
110+
return `bot '${login}' does not match ${JSON.stringify(config.allowBots)}`;
111+
}
112+
} else if (
113+
// ignore some CI bots if not explicitly allowed
114+
!isExplicitlyAllowedBranch && // show bot pushes on allowed branches
115+
COMMON_CI_BOTS.some((n) => login.includes(n))
116+
) {
117+
return "bot";
118+
}
113119
}
114120

115121
return null;

src/handler.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ function getUrlConfig(params: Record<string, string>): UrlConfig {
4242
case "allowBranches":
4343
config.allowBranches = value;
4444
break;
45+
case "allowBots": {
46+
const bool = parseBool(value, false);
47+
if (bool === undefined) {
48+
config.allowBots = value;
49+
} else {
50+
config.allowBots = bool ? ".*" : "";
51+
}
52+
break;
53+
}
4554
case "hideTags":
4655
config.hideTags = parseBool(value);
4756
break;

src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface UrlConfig {
22
allowBranches?: string;
3+
allowBots?: string;
34
hideTags?: boolean;
45
commentBurstLimit?: number;
56
}

src/util.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import type { Context } from "@hono/hono";
22

3-
export function parseBool(s: string): boolean {
4-
return ["1", "true", "on", "y", "yes"].includes(s.toLowerCase());
3+
export function parseBool(s: string, strict: false): boolean | undefined;
4+
export function parseBool(s: string, strict?: true): boolean;
5+
export function parseBool(s: string, strict: boolean = true): boolean | undefined {
6+
s = s.toLowerCase();
7+
if (["1", "true", "on", "y", "yes"].includes(s)) return true;
8+
else if (["0", "false", "off", "n", "no"].includes(s)) return false;
9+
10+
if (strict) throw new Error(`invalid bool value: ${s}`);
11+
return undefined;
512
}
613

714
export function sleep(ms: number): Promise<void> {

0 commit comments

Comments
 (0)