|
| 1 | +export type ProjectTerminalLabelInput = { |
| 2 | + readonly containerName?: string | undefined |
| 3 | + readonly displayName: string |
| 4 | + readonly repoRef: string |
| 5 | + readonly repoUrl: string |
| 6 | +} |
| 7 | + |
| 8 | +const decimalDigitsPattern = /^\d+$/u |
| 9 | + |
| 10 | +const stripGitSuffix = (value: string): string => value.endsWith(".git") ? value.slice(0, -4) : value |
| 11 | + |
| 12 | +const readPathPart = (value: string | undefined): string | null => { |
| 13 | + const trimmed = value?.trim() ?? "" |
| 14 | + return trimmed.length > 0 ? trimmed : null |
| 15 | +} |
| 16 | + |
| 17 | +const splitGitHubRemotePath = (repoUrl: string): ReadonlyArray<string> | null => { |
| 18 | + const trimmed = repoUrl.trim() |
| 19 | + const httpsPrefix = "https://github.com/" |
| 20 | + const sshUrlPrefix = "ssh://git@github.com/" |
| 21 | + const sshScpPrefix = "git@github.com:" |
| 22 | + if (trimmed.startsWith(httpsPrefix)) { |
| 23 | + return trimmed.slice(httpsPrefix.length).split("/").filter((part) => part.length > 0) |
| 24 | + } |
| 25 | + if (trimmed.startsWith(sshUrlPrefix)) { |
| 26 | + return trimmed.slice(sshUrlPrefix.length).split("/").filter((part) => part.length > 0) |
| 27 | + } |
| 28 | + if (trimmed.startsWith(sshScpPrefix)) { |
| 29 | + return trimmed.slice(sshScpPrefix.length).split("/").filter((part) => part.length > 0) |
| 30 | + } |
| 31 | + return null |
| 32 | +} |
| 33 | + |
| 34 | +const githubRepositoryPath = (repoUrl: string): string | null => { |
| 35 | + const parts = splitGitHubRemotePath(repoUrl) |
| 36 | + const owner = readPathPart(parts?.[0]) |
| 37 | + const repoRaw = readPathPart(parts?.[1]) |
| 38 | + if (owner === null || repoRaw === null) { |
| 39 | + return null |
| 40 | + } |
| 41 | + return `${owner}/${stripGitSuffix(repoRaw)}` |
| 42 | +} |
| 43 | + |
| 44 | +const sourceUrlForContext = (repoUrl: string, path: string): string | null => { |
| 45 | + const repoPath = githubRepositoryPath(repoUrl) |
| 46 | + return repoPath === null ? null : `https://github.com/${repoPath}/${path}` |
| 47 | +} |
| 48 | + |
| 49 | +const renderIssueContext = (repoUrl: string, issueId: string): string => { |
| 50 | + const issueUrl = sourceUrlForContext(repoUrl, `issues/${issueId}`) |
| 51 | + return issueUrl === null ? `issue #${issueId}` : issueUrl |
| 52 | +} |
| 53 | + |
| 54 | +const renderPullRequestContext = (repoUrl: string, pullRequestId: string): string => { |
| 55 | + const pullRequestUrl = sourceUrlForContext(repoUrl, `pull/${pullRequestId}`) |
| 56 | + return pullRequestUrl === null ? `PR #${pullRequestId}` : pullRequestUrl |
| 57 | +} |
| 58 | + |
| 59 | +const renderMergeRequestContext = (mergeRequestId: string): string => `MR #${mergeRequestId}` |
| 60 | + |
| 61 | +const renderSourceContext = (repoUrl: string, repoRef: string): string => { |
| 62 | + const trimmedUrl = repoUrl.trim() |
| 63 | + const trimmedRef = repoRef.trim() |
| 64 | + if (trimmedUrl.length === 0) { |
| 65 | + return trimmedRef.length === 0 || trimmedRef === "main" ? "" : trimmedRef |
| 66 | + } |
| 67 | + return trimmedRef.length === 0 || trimmedRef === "main" |
| 68 | + ? trimmedUrl |
| 69 | + : `${trimmedUrl} (${trimmedRef})` |
| 70 | +} |
| 71 | + |
| 72 | +const parseWrappedNumericRef = (value: string, prefix: string, suffix: string): string | null => { |
| 73 | + if (!value.startsWith(prefix) || !value.endsWith(suffix)) { |
| 74 | + return null |
| 75 | + } |
| 76 | + const id = value.slice(prefix.length, value.length - suffix.length) |
| 77 | + return decimalDigitsPattern.test(id) ? id : null |
| 78 | +} |
| 79 | + |
| 80 | +const renderWorkspaceContext = ( |
| 81 | + repoUrl: string, |
| 82 | + repoRef: string |
| 83 | +): string => { |
| 84 | + const issueId = parseWrappedNumericRef(repoRef, "issue-", "") |
| 85 | + if (issueId !== null) { |
| 86 | + return renderIssueContext(repoUrl, issueId) |
| 87 | + } |
| 88 | + const pullRequestId = parseWrappedNumericRef(repoRef, "refs/pull/", "/head") |
| 89 | + if (pullRequestId !== null) { |
| 90 | + return renderPullRequestContext(repoUrl, pullRequestId) |
| 91 | + } |
| 92 | + const mergeRequestId = parseWrappedNumericRef(repoRef, "refs/merge-requests/", "/head") |
| 93 | + if (mergeRequestId !== null) { |
| 94 | + return renderMergeRequestContext(mergeRequestId) |
| 95 | + } |
| 96 | + return renderSourceContext(repoUrl, repoRef) |
| 97 | +} |
| 98 | + |
| 99 | +const appendNonEmpty = (parts: ReadonlyArray<string>, value: string): ReadonlyArray<string> => { |
| 100 | + const trimmed = value.trim() |
| 101 | + return trimmed.length === 0 ? parts : [...parts, trimmed] |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Builds the terminal-facing project label with source link and container identity. |
| 106 | + * |
| 107 | + * @param project - Project identity returned by the docker-git API. |
| 108 | + * @returns A deterministic label for SSH terminal headers and ready messages. |
| 109 | + * |
| 110 | + * @pure true |
| 111 | + * @effect none |
| 112 | + * @invariant GitHub issue/PR refs prefer canonical source URLs; labels preserve non-empty containerName. |
| 113 | + * @precondition project.displayName identifies the repository or fallback project label. |
| 114 | + * @postcondition result contains workspace source link/context and non-empty containerName when present. |
| 115 | + * @complexity O(n) where n = |repoUrl| + |repoRef| |
| 116 | + * @throws Never |
| 117 | + */ |
| 118 | +// CHANGE: keep SSH terminal labels to source link/context plus container identity |
| 119 | +// WHY: verbose repository + issue text duplicates the source URL and crowds the terminal header |
| 120 | +// QUOTE(ТЗ): "ссылки и название контейнера будет предостаточно" |
| 121 | +// REF: issue-370 |
| 122 | +// SOURCE: n/a |
| 123 | +// FORMAT THEOREM: forall p: label(p) contains context(repoUrl(p), repoRef(p)) or containerName(p) |
| 124 | +// PURITY: CORE |
| 125 | +// EFFECT: none |
| 126 | +// INVARIANT: issue-* -> issue context; refs/pull/*/head -> PR context; containerName is preserved when non-empty |
| 127 | +// COMPLEXITY: O(n) |
| 128 | +export const projectTerminalLabel = (project: ProjectTerminalLabelInput): string => { |
| 129 | + const withContext = appendNonEmpty([], renderWorkspaceContext(project.repoUrl, project.repoRef)) |
| 130 | + const containerName = project.containerName?.trim() ?? "" |
| 131 | + const withContainer = containerName.length === 0 |
| 132 | + ? withContext |
| 133 | + : appendNonEmpty(withContext, `container ${containerName}`) |
| 134 | + if (withContainer.length > 0) { |
| 135 | + return withContainer.join(" | ") |
| 136 | + } |
| 137 | + const displayName = project.displayName.trim() |
| 138 | + return displayName.length === 0 ? project.repoUrl.trim() : displayName |
| 139 | +} |
0 commit comments