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:
翅膀 committed 2026-10-23 23:21:50 +08:00
1 parent e44fc60c55
commit aca870374f
3 files changed
+32 -12

No files matched your search

+5 -1
View File
@@ -8,6 +8,7 @@ import {
type T,
buildMessage,
repoBaseUrl,
shortRef,
} from "./helpers";
/** Shared "branch/tag + commit" fields for deployment events. */
@@ -21,7 +22,10 @@ function addDeploymentRefFields(
const isTag = deployment.ref.startsWith("refs/tags/");
fields.push({
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,
});
}
+18 -6
View File
@@ -67,30 +67,42 @@ export function commitLink(baseUrl: string | undefined, sha: string, short?: str
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 {
const clean = branch.replace("refs/heads/", "").replace("refs/tags/", "");
const clean = branch;
const display = label ?? clean;
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 {
const clean = tag.replace("refs/tags/", "");
const clean = tag;
const display = label ?? clean;
return baseUrl
? `[\`${display}\`](${baseUrl}/releases/tag/${encodeRefPath(clean)})`
: `\`${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(
baseUrl: string | undefined,
ref: string,
type: "branch" | "tag",
): string | undefined {
if (!baseUrl) return undefined;
const clean = ref.replace("refs/heads/", "").replace("refs/tags/", "");
const clean = ref;
return type === "tag"
? `${baseUrl}/releases/tag/${encodeRefPath(clean)}`
: `${baseUrl}/tree/${encodeRefPath(clean)}`;
+9 -5
View File
@@ -9,6 +9,7 @@ import {
type T,
buildMessage,
repoBaseUrl,
shortRef,
} from "./helpers";
export function formatPush(
@@ -20,7 +21,8 @@ export function formatPush(
): NeutralMessage {
const rawRef = (payload.ref as string) ?? "";
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<{
id?: string;
message?: string;
@@ -52,7 +54,7 @@ export function formatPush(
type: refType,
ref,
}),
url: refUrl(baseUrl, rawRef, refType),
url: refUrl(baseUrl, shortRef(rawRef), refType),
color: GITHUB_COLORS.delete,
},
t,
@@ -65,7 +67,7 @@ export function formatPush(
// instead of a confusing "0 commits pushed".
if (created && count === 0) {
const refType = isTagPush ? "tag" : "branch";
const cleanRef = rawRef.replace("refs/heads/", "").replace("refs/tags/", "");
const cleanRef = shortRef(rawRef);
return buildMessage(
{
author,
@@ -75,7 +77,7 @@ export function formatPush(
type: refType,
ref: cleanRef,
}),
url: refUrl(baseUrl, cleanRef, refType),
url: refUrl(baseUrl, shortRef(rawRef), refType),
color: GITHUB_COLORS.create,
},
t,
@@ -151,7 +153,9 @@ export function formatPush(
count,
s: count !== 1 ? "s" : "",
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,
color: GITHUB_COLORS.push,