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