| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import type {Repository} from 'isl-server/src/Repository'; |
| 9 | import type {CommitInfo} from 'isl/src/types'; |
| 10 | |
| 11 | import {relativeDate} from 'isl/src/relativeDate'; |
| 12 | |
| 13 | export function getDiffBlameHoverMarkup(repo: Repository, commit: CommitInfo): string { |
| 14 | const {date, author, title, hash} = commit; |
| 15 | |
| 16 | let diffId = commit.diffId; |
| 17 | if (diffId == null) { |
| 18 | // Hack: Public commits in GitHub-backed repos often don't have a PR number associated. |
| 19 | // Do a best-effort match in the title/description. |
| 20 | // TODO: we should see if we can fix this in sl itself. |
| 21 | const PRnumberRegex = /#(\d{2,}\b)/; // Sure, this misses the first 9 PRs, but also avoids "#1" reason for false positives. |
| 22 | diffId = commit.title.match(PRnumberRegex)?.[1] ?? commit.description.match(PRnumberRegex)?.[1]; |
| 23 | } |
| 24 | |
| 25 | const diffLinkMarkup = |
| 26 | diffId != null |
| 27 | ? `${repo.codeReviewProvider?.getDiffUrlMarkdown(diffId)}` |
| 28 | : (repo.codeReviewProvider?.getCommitHashUrlMarkdown(commit.hash) ?? hash.slice(0, 12)); |
| 29 | |
| 30 | // Though the ISL UI knows the schema for commit messages and can parse things out, |
| 31 | // here in the vscode extension we don't really know the schema. Let's just dump the whole commit message. |
| 32 | return ( |
| 33 | `**${author}** - ${diffLinkMarkup} (${relativeDate(date, {})})\n\n` + |
| 34 | `**${title}**\n\n\n${commit.description}` |
| 35 | ); |
| 36 | } |
| 37 | |