feat: add hideTags option

This commit is contained in:
shiftinv 2022-09-03 01:14:28 +02:00
parent 7ed47af8bc
commit 2a651bd3f9
3 changed files with 31 additions and 10 deletions

View File

@ -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"),

View File

@ -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
View File

@ -0,0 +1,3 @@
export function parseBool(s: string): boolean {
return ["1", "true", "on", "y", "yes"].includes(s.toLowerCase());
}