chore: move filter code into separate module
This commit is contained in:
parent
e8a9272368
commit
1738aa0f67
|
@ -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;
|
||||
}
|
|
@ -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<Response> {
|
|||
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 });
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
interface UrlConfig {
|
||||
allowBranches?: string[];
|
||||
hideTags?: boolean;
|
||||
}
|
Loading…
Reference in New Issue