From 1738aa0f67dcebd2589715c00f27cca27e8a29a0 Mon Sep 17 00:00:00 2001 From: shiftinv Date: Sat, 3 Sep 2022 01:35:09 +0200 Subject: [PATCH] chore: move filter code into separate module --- lib/filter.ts | 30 ++++++++++++++++++++++++++++++ lib/handler.ts | 37 ++----------------------------------- lib/types.d.ts | 4 ++++ 3 files changed, 36 insertions(+), 35 deletions(-) create mode 100644 lib/filter.ts create mode 100644 lib/types.d.ts diff --git a/lib/filter.ts b/lib/filter.ts new file mode 100644 index 0000000..c5420c2 --- /dev/null +++ b/lib/filter.ts @@ -0,0 +1,30 @@ +import { UrlConfig } from "./types.d.ts"; + +export default function filter(headers: Headers, json: any, config: UrlConfig): string | null { + const event = headers.get("x-github-event"); + const login: string | undefined = json.sender?.login?.toLowerCase(); + if ( + login && + ["coveralls[bot]", "netlify[bot]", "pre-commit-ci[bot]"].some((n) => login.includes(n)) + ) { + return "bot"; + } + + const refMatch = /^refs\/([^\/]+)\/(.+)$/.exec(json.ref); + if (event === "push" && refMatch) { + // check if branch is allowed + if ( + refMatch[0] == "heads" && config.allowBranches !== undefined && + !config.allowBranches.includes(refMatch[1]) + ) { + return `branch '${refMatch[1]}' not in ${JSON.stringify(config.allowBranches)}`; + } + + // check if it's a tag + if (refMatch[0] == "tags" && config.hideTags === true) { + return "tag"; + } + } + + return null; +} diff --git a/lib/handler.ts b/lib/handler.ts index 9a73683..d9e4ee4 100644 --- a/lib/handler.ts +++ b/lib/handler.ts @@ -1,36 +1,8 @@ import { http, log } from "../deps.ts"; import { verify } from "./crypto.ts"; +import filterWebhook from "./filter.ts"; import * as util from "./util.ts"; -function filter(headers: Headers, json: any, config: UrlConfig): string | null { - const event = headers.get("x-github-event"); - const login: string | undefined = json.sender?.login?.toLowerCase(); - if ( - login && - ["coveralls[bot]", "netlify[bot]", "pre-commit-ci[bot]"].some((n) => login.includes(n)) - ) { - return "bot"; - } - - const refMatch = /^refs\/([^\/]+)\/(.+)$/.exec(json.ref); - if (event === "push" && refMatch) { - // check if branch is allowed - if ( - refMatch[0] == "heads" && config.allowBranches !== undefined && - !config.allowBranches.includes(refMatch[1]) - ) { - return `branch '${refMatch[1]}' not in ${JSON.stringify(config.allowBranches)}`; - } - - // check if it's a tag - if (refMatch[0] == "tags" && config.hideTags === true) { - return "tag"; - } - } - - return null; -} - async function sendWebhook( id: string, token: string, @@ -47,11 +19,6 @@ async function sendWebhook( return await fetch(req); } -interface UrlConfig { - allowBranches?: string[]; - hideTags?: boolean; -} - function getUrlConfig(params: URLSearchParams): UrlConfig { const config: UrlConfig = {}; for (const [key, value] of params) { @@ -95,7 +62,7 @@ export default async function handle(req: Request): Promise { const json = JSON.parse(data); // do the thing - const filterReason = filter(req.headers, json, urlConfig); + const filterReason = filterWebhook(req.headers, json, urlConfig); if (filterReason !== null) { return new Response(`Ignored by webhook filter (reason: ${filterReason})`, { status: 203 }); } diff --git a/lib/types.d.ts b/lib/types.d.ts new file mode 100644 index 0000000..eddd154 --- /dev/null +++ b/lib/types.d.ts @@ -0,0 +1,4 @@ +interface UrlConfig { + allowBranches?: string[]; + hideTags?: boolean; +}