commitAuthorBreakdown.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import styled from '@emotion/styled';
  2. import UserAvatar from 'sentry/components/avatar/userAvatar';
  3. import {Button} from 'sentry/components/button';
  4. import Collapsible from 'sentry/components/collapsible';
  5. import LoadingError from 'sentry/components/loadingError';
  6. import LoadingIndicator from 'sentry/components/loadingIndicator';
  7. import * as SidebarSection from 'sentry/components/sidebarSection';
  8. import {t, tn} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import type {Commit, User} from 'sentry/types';
  11. import {percent} from 'sentry/utils';
  12. import {userDisplayName} from 'sentry/utils/formatters';
  13. import {useApiQuery} from 'sentry/utils/queryClient';
  14. type GroupedAuthorCommits = {
  15. [key: string]: {author: User | undefined; commitCount: number};
  16. };
  17. type Props = {
  18. orgId: string;
  19. projectSlug: string;
  20. version: string;
  21. };
  22. function CommitAuthorBreakdown({orgId, projectSlug, version}: Props) {
  23. const commitsEndpoint = `/projects/${orgId}/${projectSlug}/releases/${encodeURIComponent(
  24. version
  25. )}/commits/`;
  26. const {
  27. data: commits,
  28. isLoading,
  29. isError,
  30. } = useApiQuery<Commit[]>([commitsEndpoint], {staleTime: 0});
  31. if (isLoading) {
  32. return <LoadingIndicator />;
  33. }
  34. if (isError) {
  35. return <LoadingError />;
  36. }
  37. function getDisplayPercent(authorCommitCount: number) {
  38. if (commits) {
  39. const calculatedPercent = Math.round(percent(authorCommitCount, commits.length));
  40. return `${calculatedPercent < 1 ? '<1' : calculatedPercent}%`;
  41. }
  42. return '';
  43. }
  44. // group commits by author
  45. const groupedAuthorCommits = commits?.reduce<GroupedAuthorCommits>(
  46. (authorCommitsAccumulator, commit) => {
  47. const email = commit.author?.email ?? 'unknown';
  48. if (authorCommitsAccumulator.hasOwnProperty(email)) {
  49. authorCommitsAccumulator[email].commitCount += 1;
  50. } else {
  51. authorCommitsAccumulator[email] = {
  52. commitCount: 1,
  53. author: commit.author,
  54. };
  55. }
  56. return authorCommitsAccumulator;
  57. },
  58. {}
  59. );
  60. // sort authors by number of commits
  61. const sortedAuthorsByNumberOfCommits = Object.values(groupedAuthorCommits).sort(
  62. (a, b) => b.commitCount - a.commitCount
  63. );
  64. if (!sortedAuthorsByNumberOfCommits.length) {
  65. return null;
  66. }
  67. return (
  68. <SidebarSection.Wrap>
  69. <SidebarSection.Title>{t('Commit Author Breakdown')}</SidebarSection.Title>
  70. <SidebarSection.Content>
  71. <Collapsible
  72. expandButton={({onExpand, numberOfHiddenItems}) => (
  73. <Button priority="link" onClick={onExpand}>
  74. {tn(
  75. 'Show %s collapsed author',
  76. 'Show %s collapsed authors',
  77. numberOfHiddenItems
  78. )}
  79. </Button>
  80. )}
  81. >
  82. {sortedAuthorsByNumberOfCommits.map(({commitCount, author}, index) => (
  83. <AuthorLine key={author?.email ?? index}>
  84. <UserAvatar user={author} size={20} hasTooltip />
  85. <AuthorName>{userDisplayName(author || {}, false)}</AuthorName>
  86. <Commits>{tn('%s commit', '%s commits', commitCount)}</Commits>
  87. <Percent>{getDisplayPercent(commitCount)}</Percent>
  88. </AuthorLine>
  89. ))}
  90. </Collapsible>
  91. </SidebarSection.Content>
  92. </SidebarSection.Wrap>
  93. );
  94. }
  95. const AuthorLine = styled('div')`
  96. display: inline-grid;
  97. grid-template-columns: 30px 2fr 1fr 40px;
  98. width: 100%;
  99. margin-bottom: ${space(1)};
  100. font-size: ${p => p.theme.fontSizeMedium};
  101. `;
  102. const AuthorName = styled('div')`
  103. color: ${p => p.theme.textColor};
  104. ${p => p.theme.overflowEllipsis};
  105. `;
  106. const Commits = styled('div')`
  107. color: ${p => p.theme.subText};
  108. text-align: right;
  109. `;
  110. const Percent = styled('div')`
  111. min-width: 40px;
  112. text-align: right;
  113. `;
  114. export default CommitAuthorBreakdown;