eventCause.tsx 3.6 KB

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