mirror of
https://github.com/ReCloudStudio/WebHooker.git
synced 2026-09-22 16:11:29 +00:00
feat: add formatters for workflow_job, status, deployment, ping
This commit is contained in:
parent
fc2e2c2efa
commit
1fc3991332
6 changed files with 292 additions and 6 deletions
|
|
@ -48,6 +48,14 @@ function extractBranch(event: WebhookEvent): string | undefined {
|
||||||
const suite = event.payload.check_suite as { head_branch?: string } | undefined;
|
const suite = event.payload.check_suite as { head_branch?: string } | undefined;
|
||||||
return suite?.head_branch;
|
return suite?.head_branch;
|
||||||
}
|
}
|
||||||
|
if (event.event === "workflow_job") {
|
||||||
|
const job = event.payload.workflow_job as { head_branch?: string } | undefined;
|
||||||
|
return job?.head_branch;
|
||||||
|
}
|
||||||
|
if (event.event === "deployment") {
|
||||||
|
const deployment = event.payload.deployment as { ref?: string } | undefined;
|
||||||
|
return deployment?.ref?.replace("refs/heads/", "");
|
||||||
|
}
|
||||||
if (event.event === "commit_comment") {
|
if (event.event === "commit_comment") {
|
||||||
const comment = event.payload.comment as { position?: number | null } | undefined;
|
const comment = event.payload.comment as { position?: number | null } | undefined;
|
||||||
if (comment?.position != null) {
|
if (comment?.position != null) {
|
||||||
|
|
|
||||||
|
|
@ -81,6 +81,77 @@ export function formatCheckSuite(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function formatStatus(
|
||||||
|
payload: Record<string, unknown>,
|
||||||
|
repo: string | undefined,
|
||||||
|
author: NeutralAuthor,
|
||||||
|
t: T,
|
||||||
|
showEmoji: boolean,
|
||||||
|
): NeutralMessage {
|
||||||
|
const state = (payload.state as string) ?? "pending";
|
||||||
|
const context = (payload.context as string) ?? "";
|
||||||
|
const description = payload.description as string | undefined;
|
||||||
|
const targetUrl = payload.target_url as string | undefined;
|
||||||
|
const sha = payload.sha as string | undefined;
|
||||||
|
|
||||||
|
const emoji = state === "success" ? "✅" : state === "failure" || state === "error" ? "❌" : "⏳";
|
||||||
|
const em = (e: string): string => emojiPrefix(e, showEmoji);
|
||||||
|
const colorKey =
|
||||||
|
state === "success"
|
||||||
|
? "check_run_success"
|
||||||
|
: state === "failure" || state === "error"
|
||||||
|
? "check_run_failure"
|
||||||
|
: "check_run_other";
|
||||||
|
|
||||||
|
const fields: Array<{ name: string; value: string; inline?: boolean }> = [];
|
||||||
|
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.status"),
|
||||||
|
value: `${em(emoji)}${state}`,
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (context) {
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.context"),
|
||||||
|
value: context,
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sha) {
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.commit"),
|
||||||
|
value: `\`${sha.slice(0, 7)}\``,
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (description) {
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.description"),
|
||||||
|
value: description,
|
||||||
|
inline: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return buildMessage(
|
||||||
|
{
|
||||||
|
author,
|
||||||
|
title: t("events.status.title", {
|
||||||
|
repo: repo ?? t("common.repository"),
|
||||||
|
context: context || t("common.unknown"),
|
||||||
|
state,
|
||||||
|
}),
|
||||||
|
url: targetUrl,
|
||||||
|
color: GITHUB_COLORS[colorKey],
|
||||||
|
fields,
|
||||||
|
},
|
||||||
|
t,
|
||||||
|
repo,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function formatCheckRun(
|
export function formatCheckRun(
|
||||||
payload: Record<string, unknown>,
|
payload: Record<string, unknown>,
|
||||||
repo: string | undefined,
|
repo: string | undefined,
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,77 @@ import type { NeutralMessage, NeutralAuthor } from "../types";
|
||||||
import { GITHUB_COLORS } from "./colors";
|
import { GITHUB_COLORS } from "./colors";
|
||||||
import { emojiPrefix, type T, buildMessage } from "./helpers";
|
import { emojiPrefix, type T, buildMessage } from "./helpers";
|
||||||
|
|
||||||
|
export function formatDeployment(
|
||||||
|
payload: Record<string, unknown>,
|
||||||
|
repo: string | undefined,
|
||||||
|
author: NeutralAuthor,
|
||||||
|
t: T,
|
||||||
|
showEmoji: boolean,
|
||||||
|
): NeutralMessage {
|
||||||
|
const deployment = payload.deployment as {
|
||||||
|
environment?: string;
|
||||||
|
ref?: string;
|
||||||
|
sha?: string;
|
||||||
|
description?: string;
|
||||||
|
html_url?: string;
|
||||||
|
statuses_url?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const emoji = "🚀";
|
||||||
|
const em = (e: string): string => emojiPrefix(e, showEmoji);
|
||||||
|
const env = deployment.environment ?? t("common.unknown");
|
||||||
|
const shortSha = deployment.sha?.slice(0, 7) ?? "???????";
|
||||||
|
const fields: Array<{ name: string; value: string; inline?: boolean }> = [];
|
||||||
|
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.status"),
|
||||||
|
value: `${em(emoji)}created`,
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.environment"),
|
||||||
|
value: env,
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (deployment.ref) {
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.branch_tag"),
|
||||||
|
value: `\`${deployment.ref}\``,
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (deployment.sha) {
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.commit"),
|
||||||
|
value: `\`${shortSha}\``,
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (deployment.description) {
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.description"),
|
||||||
|
value: deployment.description,
|
||||||
|
inline: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return buildMessage(
|
||||||
|
{
|
||||||
|
author,
|
||||||
|
title: t("events.deployment.title", { repo: repo ?? t("common.repository"), env, state: "created" }),
|
||||||
|
url: deployment.html_url ?? deployment.statuses_url,
|
||||||
|
color: GITHUB_COLORS.deployment_pending,
|
||||||
|
fields,
|
||||||
|
},
|
||||||
|
t,
|
||||||
|
repo,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function formatDeploymentStatus(
|
export function formatDeploymentStatus(
|
||||||
payload: Record<string, unknown>,
|
payload: Record<string, unknown>,
|
||||||
repo: string | undefined,
|
repo: string | undefined,
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,13 @@ import { formatPullRequest } from "./pull-request";
|
||||||
import { formatPullRequestReview, formatPullRequestReviewComment } from "./review";
|
import { formatPullRequestReview, formatPullRequestReviewComment } from "./review";
|
||||||
import { formatIssues } from "./issues";
|
import { formatIssues } from "./issues";
|
||||||
import { formatIssueComment } from "./comments";
|
import { formatIssueComment } from "./comments";
|
||||||
import { formatWorkflowRun } from "./workflow";
|
import { formatWorkflowRun, formatWorkflowJob } from "./workflow";
|
||||||
import { formatRelease } from "./release";
|
import { formatRelease } from "./release";
|
||||||
import { formatCreate, formatDelete } from "./create";
|
import { formatCreate, formatDelete } from "./create";
|
||||||
import { formatStar, formatFork } from "./repo";
|
import { formatStar, formatFork } from "./repo";
|
||||||
import { formatCheckRun, formatCheckSuite } from "./check";
|
import { formatCheckRun, formatCheckSuite, formatStatus } from "./check";
|
||||||
import { formatCommitComment } from "./commit-comment";
|
import { formatCommitComment } from "./commit-comment";
|
||||||
import { formatDeploymentStatus } from "./deployment";
|
import { formatDeployment, formatDeploymentStatus } from "./deployment";
|
||||||
import { formatMember } from "./member";
|
import { formatMember } from "./member";
|
||||||
import { formatLabel } from "./label";
|
import { formatLabel } from "./label";
|
||||||
import { formatMilestone } from "./milestone";
|
import { formatMilestone } from "./milestone";
|
||||||
|
|
@ -20,6 +20,7 @@ import { formatDiscussion, formatDiscussionComment } from "./discussion";
|
||||||
import { formatRepository } from "./repository";
|
import { formatRepository } from "./repository";
|
||||||
import { formatCodeScanningAlert, formatDependabotAlert } from "./security";
|
import { formatCodeScanningAlert, formatDependabotAlert } from "./security";
|
||||||
import { formatGeneric } from "./generic";
|
import { formatGeneric } from "./generic";
|
||||||
|
import { formatPing } from "./ping";
|
||||||
|
|
||||||
export type { T } from "./helpers";
|
export type { T } from "./helpers";
|
||||||
export { formatPush } from "./push";
|
export { formatPush } from "./push";
|
||||||
|
|
@ -27,13 +28,13 @@ export { formatPullRequest } from "./pull-request";
|
||||||
export { formatPullRequestReview, formatPullRequestReviewComment } from "./review";
|
export { formatPullRequestReview, formatPullRequestReviewComment } from "./review";
|
||||||
export { formatIssues } from "./issues";
|
export { formatIssues } from "./issues";
|
||||||
export { formatIssueComment } from "./comments";
|
export { formatIssueComment } from "./comments";
|
||||||
export { formatWorkflowRun } from "./workflow";
|
export { formatWorkflowRun, formatWorkflowJob } from "./workflow";
|
||||||
export { formatRelease } from "./release";
|
export { formatRelease } from "./release";
|
||||||
export { formatCreate, formatDelete } from "./create";
|
export { formatCreate, formatDelete } from "./create";
|
||||||
export { formatStar, formatFork } from "./repo";
|
export { formatStar, formatFork } from "./repo";
|
||||||
export { formatCheckRun, formatCheckSuite } from "./check";
|
export { formatCheckRun, formatCheckSuite, formatStatus } from "./check";
|
||||||
export { formatCommitComment } from "./commit-comment";
|
export { formatCommitComment } from "./commit-comment";
|
||||||
export { formatDeploymentStatus } from "./deployment";
|
export { formatDeployment, formatDeploymentStatus } from "./deployment";
|
||||||
export { formatMember } from "./member";
|
export { formatMember } from "./member";
|
||||||
export { formatLabel } from "./label";
|
export { formatLabel } from "./label";
|
||||||
export { formatMilestone } from "./milestone";
|
export { formatMilestone } from "./milestone";
|
||||||
|
|
@ -41,6 +42,7 @@ export { formatDiscussion, formatDiscussionComment } from "./discussion";
|
||||||
export { formatRepository } from "./repository";
|
export { formatRepository } from "./repository";
|
||||||
export { formatCodeScanningAlert, formatDependabotAlert } from "./security";
|
export { formatCodeScanningAlert, formatDependabotAlert } from "./security";
|
||||||
export { formatGeneric } from "./generic";
|
export { formatGeneric } from "./generic";
|
||||||
|
export { formatPing } from "./ping";
|
||||||
|
|
||||||
export function formatEvent(
|
export function formatEvent(
|
||||||
route: Route,
|
route: Route,
|
||||||
|
|
@ -77,6 +79,14 @@ export function formatEvent(
|
||||||
return formatIssueComment(payload, repo, author, t, showEmoji);
|
return formatIssueComment(payload, repo, author, t, showEmoji);
|
||||||
case "workflow_run":
|
case "workflow_run":
|
||||||
return formatWorkflowRun(payload, repo, author, t, showEmoji);
|
return formatWorkflowRun(payload, repo, author, t, showEmoji);
|
||||||
|
case "workflow_job":
|
||||||
|
return formatWorkflowJob(payload, repo, author, t, showEmoji);
|
||||||
|
case "status":
|
||||||
|
return formatStatus(payload, repo, author, t, showEmoji);
|
||||||
|
case "deployment":
|
||||||
|
return formatDeployment(payload, repo, author, t, showEmoji);
|
||||||
|
case "ping":
|
||||||
|
return formatPing(payload, repo, author, t, showEmoji);
|
||||||
case "release":
|
case "release":
|
||||||
return formatRelease(payload, repo, author, t, showEmoji);
|
return formatRelease(payload, repo, author, t, showEmoji);
|
||||||
case "create":
|
case "create":
|
||||||
|
|
|
||||||
36
src/formatters/ping.ts
Normal file
36
src/formatters/ping.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
import type { NeutralMessage, NeutralAuthor } from "../types";
|
||||||
|
import { GITHUB_COLORS } from "./colors";
|
||||||
|
import { type T, buildMessage } from "./helpers";
|
||||||
|
|
||||||
|
export function formatPing(
|
||||||
|
payload: Record<string, unknown>,
|
||||||
|
repo: string | undefined,
|
||||||
|
author: NeutralAuthor,
|
||||||
|
t: T,
|
||||||
|
_showEmoji: boolean,
|
||||||
|
): NeutralMessage {
|
||||||
|
const zen = payload.zen as string | undefined;
|
||||||
|
const hookId = payload.hook_id as number | undefined;
|
||||||
|
|
||||||
|
const fields: Array<{ name: string; value: string; inline?: boolean }> = [];
|
||||||
|
|
||||||
|
if (hookId != null) {
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.details"),
|
||||||
|
value: String(hookId),
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return buildMessage(
|
||||||
|
{
|
||||||
|
author,
|
||||||
|
title: t("events.ping.title", { repo: repo ?? t("common.repository") }),
|
||||||
|
color: GITHUB_COLORS.default,
|
||||||
|
description: zen,
|
||||||
|
fields,
|
||||||
|
},
|
||||||
|
t,
|
||||||
|
repo,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,96 @@ import type { NeutralMessage, NeutralAuthor } from "../types";
|
||||||
import { GITHUB_COLORS, WORKFLOW_CONCLUSION_EMOJI } from "./colors";
|
import { GITHUB_COLORS, WORKFLOW_CONCLUSION_EMOJI } from "./colors";
|
||||||
import { emojiPrefix, type T, buildMessage } from "./helpers";
|
import { emojiPrefix, type T, buildMessage } from "./helpers";
|
||||||
|
|
||||||
|
export function formatWorkflowJob(
|
||||||
|
payload: Record<string, unknown>,
|
||||||
|
repo: string | undefined,
|
||||||
|
author: NeutralAuthor,
|
||||||
|
t: T,
|
||||||
|
showEmoji: boolean,
|
||||||
|
): NeutralMessage {
|
||||||
|
const job = payload.workflow_job as {
|
||||||
|
name?: string;
|
||||||
|
status?: string;
|
||||||
|
conclusion?: string | null;
|
||||||
|
head_branch?: string;
|
||||||
|
head_sha?: string;
|
||||||
|
html_url?: string;
|
||||||
|
workflow_name?: string;
|
||||||
|
run_id?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
const status =
|
||||||
|
job.status === "queued"
|
||||||
|
? "queued"
|
||||||
|
: job.status === "in_progress"
|
||||||
|
? "running"
|
||||||
|
: (job.conclusion ?? "pending");
|
||||||
|
const emoji = WORKFLOW_CONCLUSION_EMOJI[status] ?? "⏳";
|
||||||
|
const em = (e: string): string => emojiPrefix(e, showEmoji);
|
||||||
|
const colorKey =
|
||||||
|
status === "success"
|
||||||
|
? "workflow_run_success"
|
||||||
|
: status === "failure"
|
||||||
|
? "workflow_run_failure"
|
||||||
|
: "workflow_run_other";
|
||||||
|
|
||||||
|
const fields: Array<{ name: string; value: string; inline?: boolean }> = [];
|
||||||
|
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.status"),
|
||||||
|
value: `${em(emoji)}${status}`,
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (job.name) {
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.job"),
|
||||||
|
value: job.name,
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (job.workflow_name) {
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.workflow"),
|
||||||
|
value: job.workflow_name,
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (job.head_branch) {
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.branch"),
|
||||||
|
value: `\`${job.head_branch}\``,
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (job.head_sha) {
|
||||||
|
fields.push({
|
||||||
|
name: t("fields.commit"),
|
||||||
|
value: `\`${job.head_sha.slice(0, 7)}\``,
|
||||||
|
inline: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return buildMessage(
|
||||||
|
{
|
||||||
|
author,
|
||||||
|
title: t("events.workflow_job.title", {
|
||||||
|
repo: repo ?? t("common.repository"),
|
||||||
|
name: job.name ?? "Job",
|
||||||
|
conclusion: status,
|
||||||
|
}),
|
||||||
|
url: job.html_url,
|
||||||
|
color: GITHUB_COLORS[colorKey],
|
||||||
|
fields,
|
||||||
|
},
|
||||||
|
t,
|
||||||
|
repo,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function formatWorkflowRun(
|
export function formatWorkflowRun(
|
||||||
payload: Record<string, unknown>,
|
payload: Record<string, unknown>,
|
||||||
repo: string | undefined,
|
repo: string | undefined,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue