All files / publisher/utils formatGitCommit.tsx

100% Statements 13/13
100% Branches 22/22
100% Functions 3/3
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87            20x 6x       14x 2x     12x                         19x 11x       8x       4x     4x                                         13x 13x   13x                                            
/**
 * Formats a git commit ID for display (truncates to 7 characters)
 * @param commitId - The full git commit ID
 * @returns The truncated commit ID or null if invalid
 */
export function formatCommitId(commitId?: string | null): string | null {
  if (!commitId || typeof commitId !== "string") {
    return null;
  }
 
  // Git commit IDs should be at least 7 characters long
  if (commitId.length < 7) {
    return null;
  }
 
  return commitId.substring(0, 7);
}
 
/**
 * Creates a GitHub commit URL from repository and commit ID
 * @param githubRepository - The GitHub repository in format "owner/repo"
 * @param commitId - The git commit ID
 * @returns The GitHub commit URL or null if invalid
 */
export function createGitHubCommitUrl(
  githubRepository?: string | null,
  commitId?: string | null,
): string | null {
  if (!githubRepository || !commitId) {
    return null;
  }
 
  // Validate repository format (should be "owner/repo")
  if (
    !githubRepository.includes("/") ||
    githubRepository.split("/").length !== 2
  ) {
    return null;
  }
 
  return `https://github.com/${githubRepository}/commit/${commitId}`;
}
 
/**
 * React component for displaying a git commit ID with optional GitHub link
 * @param commitId - The git commit ID
 * @param githubRepository - The GitHub repository in format "owner/repo"
 * @param showLink - Whether to show as a clickable link (default: true)
 * @returns JSX element or null if no commit ID
 */
interface GitCommitLinkProps {
  commitId?: string | null;
  githubRepository?: string | null;
  showLink?: boolean;
}
 
export function GitCommitLink({
  commitId,
  githubRepository,
  showLink = true,
}: GitCommitLinkProps): React.JSX.Element {
  const formattedCommitId = formatCommitId(commitId);
  const commitUrl = createGitHubCommitUrl(githubRepository, commitId);
 
  return !formattedCommitId ? (
    <span
      className="p-text--code"
      aria-label="No commit data available for this build"
    >
      -
    </span>
  ) : !showLink || !githubRepository || !commitUrl ? (
    <span className="p-text--code">{formattedCommitId}</span>
  ) : (
    <a
      href={commitUrl}
      target="_blank"
      rel="noopener noreferrer"
      className="p-text--code"
      title={`View commit ${formattedCommitId} on GitHub`}
      aria-label={`View commit ${formattedCommitId} on GitHub`}
    >
      {formattedCommitId}
    </a>
  );
}