mirror of
https://github.com/Elysola/WebHooker.git
synced 2026-11-21 01:23:40 +00:00
fix(formatter): strip only leading ref prefixes
A short branch or tag may itself contain refs/heads or refs/tags. Strip one leading full-ref prefix at the caller and keep short names unchanged when generating labels and links.
This commit is contained in:
1 parent
e44fc60c55
commit
aca870374f
3 files changed
+32
-12
No files matched your search
@@ -8,6 +8,7 @@ import {
|
|||||||
type T,
|
type T,
|
||||||
buildMessage,
|
buildMessage,
|
||||||
repoBaseUrl,
|
repoBaseUrl,
|
||||||
|
shortRef,
|
||||||
} from "./helpers";
|
} from "./helpers";
|
||||||
|
|
||||||
/** Shared "branch/tag + commit" fields for deployment events. */
|
/** Shared "branch/tag + commit" fields for deployment events. */
|
||||||
@@ -21,7 +22,10 @@ function addDeploymentRefFields(
|
|||||||
const isTag = deployment.ref.startsWith("refs/tags/");
|
const isTag = deployment.ref.startsWith("refs/tags/");
|
||||||
fields.push({
|
fields.push({
|
||||||
name: t("fields.branch_tag"),
|
name: t("fields.branch_tag"),
|
||||||
value: isTag ? tagLink(baseUrl, deployment.ref) : branchLink(baseUrl, deployment.ref),
|
// A deployment's ref may be a full ref: its one leading prefix goes.
|
||||||
|
value: isTag
|
||||||
|
? tagLink(baseUrl, shortRef(deployment.ref))
|
||||||
|
: branchLink(baseUrl, shortRef(deployment.ref)),
|
||||||
inline: true,
|
inline: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,30 +67,42 @@ export function commitLink(baseUrl: string | undefined, sha: string, short?: str
|
|||||||
return baseUrl ? `[\`${label}\`](${baseUrl}/commit/${encodeRefPath(sha)})` : `\`${label}\``;
|
return baseUrl ? `[\`${label}\`](${baseUrl}/commit/${encodeRefPath(sha)})` : `\`${label}\``;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Inline code + hyperlink for a branch (or bare ref), e.g. [`main`](.../tree/main). */
|
/**
|
||||||
|
* A ref's short name: only one leading `refs/heads/` or `refs/tags/` goes —
|
||||||
|
* a branch may itself contain such a path (`feature/refs/tags/test`).
|
||||||
|
*/
|
||||||
|
export function shortRef(ref: string): string {
|
||||||
|
return ref.replace(/^refs\/(?:heads|tags)\//, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inline code + hyperlink for a branch, e.g. [`main`](.../tree/main). Takes
|
||||||
|
* the short name, as written (a branch may well be called `refs/heads/x`);
|
||||||
|
* a full ref is shortened by the caller (`shortRef`).
|
||||||
|
*/
|
||||||
export function branchLink(baseUrl: string | undefined, branch: string, label?: string): string {
|
export function branchLink(baseUrl: string | undefined, branch: string, label?: string): string {
|
||||||
const clean = branch.replace("refs/heads/", "").replace("refs/tags/", "");
|
const clean = branch;
|
||||||
const display = label ?? clean;
|
const display = label ?? clean;
|
||||||
return baseUrl ? `[\`${display}\`](${baseUrl}/tree/${encodeRefPath(clean)})` : `\`${display}\``;
|
return baseUrl ? `[\`${display}\`](${baseUrl}/tree/${encodeRefPath(clean)})` : `\`${display}\``;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Inline code + hyperlink for a tag, e.g. [`v1.0`](.../releases/tag/v1.0). */
|
/** Inline code + hyperlink for a tag (its short name), e.g. [`v1.0`](.../releases/tag/v1.0). */
|
||||||
export function tagLink(baseUrl: string | undefined, tag: string, label?: string): string {
|
export function tagLink(baseUrl: string | undefined, tag: string, label?: string): string {
|
||||||
const clean = tag.replace("refs/tags/", "");
|
const clean = tag;
|
||||||
const display = label ?? clean;
|
const display = label ?? clean;
|
||||||
return baseUrl
|
return baseUrl
|
||||||
? `[\`${display}\`](${baseUrl}/releases/tag/${encodeRefPath(clean)})`
|
? `[\`${display}\`](${baseUrl}/releases/tag/${encodeRefPath(clean)})`
|
||||||
: `\`${display}\``;
|
: `\`${display}\``;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** URL for a branch or tag, when the repository base URL is available. */
|
/** URL for a branch or tag (its short name), when the repository base URL is available. */
|
||||||
export function refUrl(
|
export function refUrl(
|
||||||
baseUrl: string | undefined,
|
baseUrl: string | undefined,
|
||||||
ref: string,
|
ref: string,
|
||||||
type: "branch" | "tag",
|
type: "branch" | "tag",
|
||||||
): string | undefined {
|
): string | undefined {
|
||||||
if (!baseUrl) return undefined;
|
if (!baseUrl) return undefined;
|
||||||
const clean = ref.replace("refs/heads/", "").replace("refs/tags/", "");
|
const clean = ref;
|
||||||
return type === "tag"
|
return type === "tag"
|
||||||
? `${baseUrl}/releases/tag/${encodeRefPath(clean)}`
|
? `${baseUrl}/releases/tag/${encodeRefPath(clean)}`
|
||||||
: `${baseUrl}/tree/${encodeRefPath(clean)}`;
|
: `${baseUrl}/tree/${encodeRefPath(clean)}`;
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
type T,
|
type T,
|
||||||
buildMessage,
|
buildMessage,
|
||||||
repoBaseUrl,
|
repoBaseUrl,
|
||||||
|
shortRef,
|
||||||
} from "./helpers";
|
} from "./helpers";
|
||||||
|
|
||||||
export function formatPush(
|
export function formatPush(
|
||||||
@@ -20,7 +21,8 @@ export function formatPush(
|
|||||||
): NeutralMessage {
|
): NeutralMessage {
|
||||||
const rawRef = (payload.ref as string) ?? "";
|
const rawRef = (payload.ref as string) ?? "";
|
||||||
const isTagPush = rawRef.startsWith("refs/tags/");
|
const isTagPush = rawRef.startsWith("refs/tags/");
|
||||||
const ref = rawRef.replace("refs/heads/", "").replace("refs/tags/", "tag: ");
|
// One prefix, read once: a branch may itself be named `refs/tags/x`.
|
||||||
|
const ref = rawRef.startsWith("refs/tags/") ? `tag: ${shortRef(rawRef)}` : shortRef(rawRef);
|
||||||
const commits = (payload.commits ?? []) as Array<{
|
const commits = (payload.commits ?? []) as Array<{
|
||||||
id?: string;
|
id?: string;
|
||||||
message?: string;
|
message?: string;
|
||||||
@@ -52,7 +54,7 @@ export function formatPush(
|
|||||||
type: refType,
|
type: refType,
|
||||||
ref,
|
ref,
|
||||||
}),
|
}),
|
||||||
url: refUrl(baseUrl, rawRef, refType),
|
url: refUrl(baseUrl, shortRef(rawRef), refType),
|
||||||
color: GITHUB_COLORS.delete,
|
color: GITHUB_COLORS.delete,
|
||||||
},
|
},
|
||||||
t,
|
t,
|
||||||
@@ -65,7 +67,7 @@ export function formatPush(
|
|||||||
// instead of a confusing "0 commits pushed".
|
// instead of a confusing "0 commits pushed".
|
||||||
if (created && count === 0) {
|
if (created && count === 0) {
|
||||||
const refType = isTagPush ? "tag" : "branch";
|
const refType = isTagPush ? "tag" : "branch";
|
||||||
const cleanRef = rawRef.replace("refs/heads/", "").replace("refs/tags/", "");
|
const cleanRef = shortRef(rawRef);
|
||||||
return buildMessage(
|
return buildMessage(
|
||||||
{
|
{
|
||||||
author,
|
author,
|
||||||
@@ -75,7 +77,7 @@ export function formatPush(
|
|||||||
type: refType,
|
type: refType,
|
||||||
ref: cleanRef,
|
ref: cleanRef,
|
||||||
}),
|
}),
|
||||||
url: refUrl(baseUrl, cleanRef, refType),
|
url: refUrl(baseUrl, shortRef(rawRef), refType),
|
||||||
color: GITHUB_COLORS.create,
|
color: GITHUB_COLORS.create,
|
||||||
},
|
},
|
||||||
t,
|
t,
|
||||||
@@ -151,7 +153,9 @@ export function formatPush(
|
|||||||
count,
|
count,
|
||||||
s: count !== 1 ? "s" : "",
|
s: count !== 1 ? "s" : "",
|
||||||
repo: repo ?? t("common.repository"),
|
repo: repo ?? t("common.repository"),
|
||||||
ref: isTagPush ? tagLink(baseUrl, rawRef, ref) : branchLink(baseUrl, rawRef, ref),
|
ref: isTagPush
|
||||||
|
? tagLink(baseUrl, shortRef(rawRef), ref)
|
||||||
|
: branchLink(baseUrl, shortRef(rawRef), ref),
|
||||||
}),
|
}),
|
||||||
url: compareUrl,
|
url: compareUrl,
|
||||||
color: GITHUB_COLORS.push,
|
color: GITHUB_COLORS.push,
|
||||||
|
|||||||
Reference in new issue
Block a user