eventCause.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import {Fragment, JSXElementConstructor, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import flatMap from 'lodash/flatMap';
  4. import uniqBy from 'lodash/uniqBy';
  5. import {CommitRowProps} from 'sentry/components/commitRow';
  6. import {CauseHeader, DataSection} from 'sentry/components/events/styles';
  7. import {Panel} from 'sentry/components/panels';
  8. import {IconAdd, IconSubtract} from 'sentry/icons';
  9. import {t} from 'sentry/locale';
  10. import space from 'sentry/styles/space';
  11. import {AvatarProject, Commit, Group, IssueCategory} from 'sentry/types';
  12. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  13. import useRouteAnalyticsParams from 'sentry/utils/routeAnalytics/useRouteAnalyticsParams';
  14. import useCommitters from 'sentry/utils/useCommitters';
  15. import useOrganization from 'sentry/utils/useOrganization';
  16. interface Props {
  17. commitRow: JSXElementConstructor<CommitRowProps>;
  18. eventId: string;
  19. project: AvatarProject;
  20. group?: Group;
  21. }
  22. function EventCause({group, eventId, project, commitRow: CommitRow}: Props) {
  23. const organization = useOrganization();
  24. const [isExpanded, setIsExpanded] = useState(false);
  25. const {committers, fetching} = useCommitters({
  26. eventId,
  27. projectSlug: project.slug,
  28. });
  29. useRouteAnalyticsParams({
  30. fetching,
  31. num_suspect_commits: committers.length,
  32. });
  33. function getUniqueCommitsWithAuthors() {
  34. // Get a list of commits with author information attached
  35. const commitsWithAuthors = flatMap(committers, ({commits, author}) =>
  36. commits.map(commit => ({
  37. ...commit,
  38. author,
  39. }))
  40. );
  41. // Remove duplicate commits
  42. return uniqBy(commitsWithAuthors, commit => commit.id);
  43. }
  44. if (!committers.length) {
  45. return null;
  46. }
  47. const handlePullRequestClick = () => {
  48. trackAdvancedAnalyticsEvent('issue_details.suspect_commits.pull_request_clicked', {
  49. organization,
  50. project_id: parseInt(project.id as string, 10),
  51. group_id: parseInt(group?.id as string, 10),
  52. issue_category: group?.issueCategory ?? IssueCategory.ERROR,
  53. });
  54. };
  55. const handleCommitClick = (commit: Commit) => {
  56. trackAdvancedAnalyticsEvent('issue_details.suspect_commits.commit_clicked', {
  57. organization,
  58. project_id: parseInt(project.id as string, 10),
  59. group_id: parseInt(group?.id as string, 10),
  60. issue_category: group?.issueCategory ?? IssueCategory.ERROR,
  61. has_pull_request: commit.pullRequest?.id !== undefined,
  62. });
  63. };
  64. const commits = getUniqueCommitsWithAuthors();
  65. return (
  66. <DataSection>
  67. <CauseHeader>
  68. <h3 data-test-id="event-cause">
  69. {t('Suspect Commits')} ({commits.length})
  70. </h3>
  71. {commits.length > 1 && (
  72. <ExpandButton onClick={() => setIsExpanded(!isExpanded)}>
  73. {isExpanded ? (
  74. <Fragment>
  75. {t('Show less')} <IconSubtract isCircled size="md" />
  76. </Fragment>
  77. ) : (
  78. <Fragment>
  79. {t('Show more')} <IconAdd isCircled size="md" />
  80. </Fragment>
  81. )}
  82. </ExpandButton>
  83. )}
  84. </CauseHeader>
  85. <Panel>
  86. {commits.slice(0, isExpanded ? 100 : 1).map(commit => (
  87. <CommitRow
  88. key={commit.id}
  89. commit={commit}
  90. onCommitClick={handleCommitClick}
  91. onPullRequestClick={handlePullRequestClick}
  92. />
  93. ))}
  94. </Panel>
  95. </DataSection>
  96. );
  97. }
  98. const ExpandButton = styled('button')`
  99. display: flex;
  100. align-items: center;
  101. gap: ${space(0.5)};
  102. `;
  103. export default EventCause;