From 2a651bd3f9b0c95a7f91cae484e3a7bc2cf197a3 Mon Sep 17 00:00:00 2001 From: shiftinv Date: Sat, 3 Sep 2022 01:14:28 +0200 Subject: [PATCH] feat: add `hideTags` option --- lib/config.ts | 4 +++- lib/handler.ts | 34 +++++++++++++++++++++++++--------- lib/util.ts | 3 +++ 3 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 lib/util.ts diff --git a/lib/config.ts b/lib/config.ts index bc49b28..d8e4e48 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -1,3 +1,5 @@ +import * as util from "./util.ts"; + function get(key: string, def?: string): string { const value = Deno.env.get(key) ?? def; if (value !== undefined) { @@ -7,7 +9,7 @@ function get(key: string, def?: string): string { } export default { - debug: !!parseInt(get("DEBUG", "0")), + debug: util.parseBool(get("DEBUG", "0")), hostname: get("HOSTNAME", "127.0.0.1"), port: parseInt(get("PORT", "8080")), signKey: get("SIGN_KEY"), diff --git a/lib/handler.ts b/lib/handler.ts index 13e1f4a..9a73683 100644 --- a/lib/handler.ts +++ b/lib/handler.ts @@ -1,19 +1,31 @@ -import { verify } from "./crypto.ts"; import { http, log } from "../deps.ts"; +import { verify } from "./crypto.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 = json.sender?.login?.toLowerCase() || ""; - if (["coveralls[bot]", "netlify[bot]", "pre-commit-ci[bot]"].some((n) => login.includes(n))) { + 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 branchMatch = /^refs\/heads\/(.*)$/.exec(json.ref); - if ( - event === "push" && branchMatch && - config.allowBranches && !config.allowBranches.includes(branchMatch[1]) - ) { - return `branch '${branchMatch[1]}' not in ${JSON.stringify(config.allowBranches)}`; + 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; @@ -37,6 +49,7 @@ async function sendWebhook( interface UrlConfig { allowBranches?: string[]; + hideTags?: boolean; } function getUrlConfig(params: URLSearchParams): UrlConfig { @@ -48,6 +61,9 @@ function getUrlConfig(params: URLSearchParams): UrlConfig { case "allowBranches": config.allowBranches = value.split(","); break; + case "hideTags": + config.hideTags = util.parseBool(value); + break; default: throw http.createHttpError(418, `Unknown config option: ${key}`); } diff --git a/lib/util.ts b/lib/util.ts new file mode 100644 index 0000000..7fc7393 --- /dev/null +++ b/lib/util.ts @@ -0,0 +1,3 @@ +export function parseBool(s: string): boolean { + return ["1", "true", "on", "y", "yes"].includes(s.toLowerCase()); +}