modal.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  4. import ExternalLink from 'sentry/components/links/externalLink';
  5. import {t, tct} from 'sentry/locale';
  6. import {space} from 'sentry/styles/space';
  7. import type {Event, Frame} from 'sentry/types/event';
  8. import type {TagWithTopValues} from 'sentry/types/group';
  9. import type {Organization} from 'sentry/types/organization';
  10. import type {Project} from 'sentry/types/project';
  11. import {uniq} from 'sentry/utils/array/uniq';
  12. import {safeURL} from 'sentry/utils/url/safeURL';
  13. import {useUser} from 'sentry/utils/useUser';
  14. import OwnerInput from 'sentry/views/settings/project/projectOwnership/ownerInput';
  15. type IssueOwnershipResponse = {
  16. autoAssignment: boolean;
  17. dateCreated: string;
  18. fallthrough: boolean;
  19. isActive: boolean;
  20. lastUpdated: string;
  21. raw: string;
  22. };
  23. type Props = DeprecatedAsyncComponent['props'] & {
  24. issueId: string;
  25. onCancel: () => void;
  26. organization: Organization;
  27. project: Project;
  28. eventData?: Event;
  29. };
  30. type State = {
  31. ownership: null | IssueOwnershipResponse;
  32. urlTagData: null | TagWithTopValues;
  33. } & DeprecatedAsyncComponent['state'];
  34. function getFrameSuggestions(eventData?: Event) {
  35. // pull frame data out of exception or the stacktrace
  36. const entry = eventData?.entries?.find(({type}) =>
  37. ['exception', 'stacktrace'].includes(type)
  38. );
  39. let frames: Frame[] = [];
  40. if (entry?.type === 'exception') {
  41. frames = entry?.data?.values?.[0]?.stacktrace?.frames ?? [];
  42. } else if (entry?.type === 'stacktrace') {
  43. frames = entry?.data?.frames ?? [];
  44. }
  45. // Only display in-app frames
  46. frames = frames.filter(frame => frame?.inApp).reverse();
  47. return uniq(frames.map(frame => frame.filename || frame.absPath || ''));
  48. }
  49. /**
  50. * Attempt to remove the origin from a URL
  51. */
  52. function getUrlPath(maybeUrl?: string) {
  53. if (!maybeUrl) {
  54. return '';
  55. }
  56. const parsedURL = safeURL(maybeUrl);
  57. if (!parsedURL) {
  58. return maybeUrl;
  59. }
  60. return `*${parsedURL.pathname}`;
  61. }
  62. function OwnershipSuggestions({
  63. paths,
  64. urls,
  65. eventData,
  66. }: {
  67. paths: string[];
  68. urls: string[];
  69. eventData?: Event;
  70. }) {
  71. const user = useUser();
  72. if (!user.email) {
  73. return null;
  74. }
  75. const pathSuggestion = paths.length ? `path:${paths[0]} ${user.email}` : null;
  76. const urlSuggestion = urls.length ? `url:${getUrlPath(urls[0])} ${user.email}` : null;
  77. const transactionTag = eventData?.tags?.find(({key}) => key === 'transaction');
  78. const transactionSuggestion = transactionTag
  79. ? `tags.transaction:${transactionTag.value} ${user.email}`
  80. : null;
  81. return (
  82. <StyledPre>
  83. # {t('Here’s some suggestions based on this issue')}
  84. <br />
  85. {[pathSuggestion, urlSuggestion, transactionSuggestion]
  86. .filter(x => x)
  87. .map(suggestion => (
  88. <Fragment key={suggestion}>
  89. {suggestion}
  90. <br />
  91. </Fragment>
  92. ))}
  93. </StyledPre>
  94. );
  95. }
  96. class ProjectOwnershipModal extends DeprecatedAsyncComponent<Props, State> {
  97. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  98. const {organization, project, issueId} = this.props;
  99. return [
  100. ['ownership', `/projects/${organization.slug}/${project.slug}/ownership/`],
  101. [
  102. 'urlTagData',
  103. `/issues/${issueId}/tags/url/`,
  104. {},
  105. {
  106. allowError: error =>
  107. // Allow for 404s
  108. error.status === 404,
  109. },
  110. ],
  111. ];
  112. }
  113. renderBody() {
  114. const {ownership, urlTagData} = this.state;
  115. const {eventData, organization, project, onCancel} = this.props;
  116. if (!ownership) {
  117. return null;
  118. }
  119. const urls = urlTagData
  120. ? urlTagData.topValues
  121. .sort((a, b) => a.count - b.count)
  122. .map(i => i.value)
  123. .slice(0, 5)
  124. : [];
  125. const paths = getFrameSuggestions(eventData);
  126. return (
  127. <Fragment>
  128. <Fragment>
  129. <Description>
  130. {tct(
  131. 'Assign issues based on custom rules. To learn more, [docs:read the docs].',
  132. {
  133. docs: (
  134. <ExternalLink href="https://docs.sentry.io/product/issues/issue-owners/" />
  135. ),
  136. }
  137. )}
  138. </Description>
  139. <OwnershipSuggestions paths={paths} urls={urls} eventData={eventData} />
  140. </Fragment>
  141. <OwnerInput
  142. organization={organization}
  143. project={project}
  144. initialText={ownership?.raw || ''}
  145. urls={urls}
  146. paths={paths}
  147. dateUpdated={ownership.lastUpdated}
  148. onCancel={onCancel}
  149. page="issue_details"
  150. />
  151. </Fragment>
  152. );
  153. }
  154. }
  155. const Description = styled('p')`
  156. margin-bottom: ${space(1)};
  157. `;
  158. const StyledPre = styled('pre')`
  159. word-break: break-word;
  160. padding: ${space(2)};
  161. line-height: 1.6;
  162. color: ${p => p.theme.subText};
  163. `;
  164. export default ProjectOwnershipModal;