mirror of
https://github.com/status-im/github-webhook-filter.git
synced 2025-02-16 23:36:34 +00:00
feat: add hideTags
option
This commit is contained in:
parent
7ed47af8bc
commit
2a651bd3f9
@ -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"),
|
||||
|
@ -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}`);
|
||||
}
|
||||
|
3
lib/util.ts
Normal file
3
lib/util.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export function parseBool(s: string): boolean {
|
||||
return ["1", "true", "on", "y", "yes"].includes(s.toLowerCase());
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user