commitAuthorBreakdown.tsx 3.8 KB

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