groupActivityItem.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. import {Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import moment from 'moment-timezone';
  4. import CommitLink from 'sentry/components/commitLink';
  5. import {DateTime} from 'sentry/components/dateTime';
  6. import Duration from 'sentry/components/duration';
  7. import ExternalLink from 'sentry/components/links/externalLink';
  8. import Link from 'sentry/components/links/link';
  9. import PullRequestLink from 'sentry/components/pullRequestLink';
  10. import Version from 'sentry/components/version';
  11. import VersionHoverCard from 'sentry/components/versionHoverCard';
  12. import {t, tct, tn} from 'sentry/locale';
  13. import type {
  14. GroupActivity,
  15. GroupActivityAssigned,
  16. GroupActivitySetEscalating,
  17. GroupActivitySetIgnored,
  18. } from 'sentry/types/group';
  19. import {GroupActivityType} from 'sentry/types/group';
  20. import type {Organization, Team} from 'sentry/types/organization';
  21. import type {Project} from 'sentry/types/project';
  22. import type {User} from 'sentry/types/user';
  23. import useOrganization from 'sentry/utils/useOrganization';
  24. import {isSemverRelease} from 'sentry/utils/versions/isSemverRelease';
  25. export default function getGroupActivityItem(
  26. activity: GroupActivity,
  27. organization: Organization,
  28. project: Project,
  29. author: React.ReactNode,
  30. teams: Team[]
  31. ) {
  32. const issuesLink = `/organizations/${organization.slug}/issues/`;
  33. function getIgnoredMessage(data: GroupActivitySetIgnored['data']): {
  34. message: JSX.Element | string | null;
  35. title: JSX.Element | string;
  36. } {
  37. if (data.ignoreDuration) {
  38. return {
  39. title: t('Archived'),
  40. message: tct('by [author] for [duration]', {
  41. author,
  42. duration: <Duration seconds={data.ignoreDuration * 60} />,
  43. }),
  44. };
  45. }
  46. if (data.ignoreCount && data.ignoreWindow) {
  47. return {
  48. title: t('Archived'),
  49. message: tct('by [author] until it happens [count] time(s) in [duration]', {
  50. author,
  51. count: data.ignoreCount,
  52. duration: <Duration seconds={data.ignoreWindow * 60} />,
  53. }),
  54. };
  55. }
  56. if (data.ignoreCount) {
  57. return {
  58. title: t('Archived'),
  59. message: tct('by [author] until it happens [count] time(s)', {
  60. author,
  61. count: data.ignoreCount,
  62. }),
  63. };
  64. }
  65. if (data.ignoreUserCount && data.ignoreUserWindow) {
  66. return {
  67. title: t('Archived'),
  68. message: tct('by [author] until it affects [count] user(s) in [duration]', {
  69. author,
  70. count: data.ignoreUserCount,
  71. duration: <Duration seconds={data.ignoreUserWindow * 60} />,
  72. }),
  73. };
  74. }
  75. if (data.ignoreUserCount) {
  76. return {
  77. title: t('Archived'),
  78. message: tct('by [author] until it affects [count] user(s)', {
  79. author,
  80. count: data.ignoreUserCount,
  81. }),
  82. };
  83. }
  84. if (data.ignoreUntil) {
  85. return {
  86. title: t('Archived'),
  87. message: tct('by [author] until [date]', {
  88. author,
  89. date: <DateTime date={data.ignoreUntil} />,
  90. }),
  91. };
  92. }
  93. if (data.ignoreUntilEscalating) {
  94. return {
  95. title: t('Archived'),
  96. message: tct('by [author] until it escalates', {
  97. author,
  98. }),
  99. };
  100. }
  101. return {
  102. title: t('Archived'),
  103. message: tct('by [author] forever', {
  104. author,
  105. }),
  106. };
  107. }
  108. function getAssignedMessage(assignedActivity: GroupActivityAssigned) {
  109. const {data} = assignedActivity;
  110. let assignee: string | User | undefined = undefined;
  111. if (data.assigneeType === 'team') {
  112. const team = teams.find(({id}) => id === data.assignee);
  113. // TODO: could show a loading indicator if the team is loading
  114. assignee = team ? `#${team.slug}` : '<unknown-team>';
  115. } else if (activity.user && data.assignee === activity.user.id) {
  116. assignee = t('themselves');
  117. } else if (data.assigneeType === 'user' && data.assigneeEmail) {
  118. assignee = data.assigneeEmail;
  119. } else {
  120. assignee = t('an unknown user');
  121. }
  122. const isAutoAssigned = [
  123. 'projectOwnership',
  124. 'codeowners',
  125. 'suspectCommitter',
  126. ].includes(data.integration as string);
  127. const integrationName: Record<
  128. NonNullable<GroupActivityAssigned['data']['integration']>,
  129. string
  130. > = {
  131. msteams: t('Microsoft Teams'),
  132. slack: t('Slack'),
  133. projectOwnership: t('Ownership Rule'),
  134. codeowners: t('Codeowners Rule'),
  135. suspectCommitter: t('Suspect Commit'),
  136. };
  137. return {
  138. title: isAutoAssigned ? t('Auto-Assigned') : t('Assigned'),
  139. message: tct('by [author] to [assignee]. [assignedReason]', {
  140. author,
  141. assignee,
  142. assignedReason: data.integration && integrationName[data.integration] && (
  143. <CodeWrapper>
  144. {t('Assigned via %s', integrationName[data.integration])}
  145. {data.rule && (
  146. <Fragment>
  147. : <StyledRuleSpan>{data.rule}</StyledRuleSpan>
  148. </Fragment>
  149. )}
  150. </CodeWrapper>
  151. ),
  152. }),
  153. };
  154. }
  155. function getEscalatingMessage(data: GroupActivitySetEscalating['data']): {
  156. message: JSX.Element | string | null;
  157. title: JSX.Element | string;
  158. } {
  159. if (data.forecast) {
  160. return {
  161. title: t('Escalated'),
  162. message: tct('by [author] because over [forecast] [event] happened in an hour', {
  163. author,
  164. forecast: data.forecast,
  165. event: data.forecast === 1 ? 'event' : 'events',
  166. }),
  167. };
  168. }
  169. if (data.expired_snooze) {
  170. if (data.expired_snooze.count && data.expired_snooze.window) {
  171. return {
  172. title: t('Escalated'),
  173. message: tct('by [author] because [count] [event] happened in [duration]', {
  174. author,
  175. count: data.expired_snooze.count,
  176. event: data.expired_snooze.count === 1 ? 'event' : 'events',
  177. duration: <Duration seconds={data.expired_snooze.window * 60} />,
  178. }),
  179. };
  180. }
  181. if (data.expired_snooze.count) {
  182. return {
  183. title: t('Escalated'),
  184. message: tct('by [author] because [count] [event] happened', {
  185. author,
  186. count: data.expired_snooze.count,
  187. event: data.expired_snooze.count === 1 ? 'event' : 'events',
  188. }),
  189. };
  190. }
  191. if (data.expired_snooze.user_count && data.expired_snooze.user_window) {
  192. return {
  193. title: t('Escalated'),
  194. message: tct('by [author] because [count] [user] affected in [duration]', {
  195. author,
  196. count: data.expired_snooze.user_count,
  197. user: data.expired_snooze.user_count === 1 ? 'user was' : 'users were',
  198. duration: <Duration seconds={data.expired_snooze.user_window * 60} />,
  199. }),
  200. };
  201. }
  202. if (data.expired_snooze.user_count) {
  203. return {
  204. title: t('Escalated'),
  205. message: tct('by [author] because [count] [user] affected', {
  206. author,
  207. count: data.expired_snooze.user_count,
  208. user: data.expired_snooze.user_count === 1 ? 'user was' : 'users were',
  209. }),
  210. };
  211. }
  212. if (data.expired_snooze.until) {
  213. return {
  214. title: t('Escalated'),
  215. message: tct('by [author] because [date] passed', {
  216. author,
  217. date: <DateTime date={data.expired_snooze.until} />,
  218. }),
  219. };
  220. }
  221. }
  222. return {
  223. title: t('Escalated'),
  224. message: tct('by [author]', {author}),
  225. }; // should not reach this
  226. }
  227. function renderContent(): {
  228. message: JSX.Element | string | null;
  229. title: JSX.Element | string;
  230. } {
  231. switch (activity.type) {
  232. case GroupActivityType.NOTE:
  233. return {
  234. title: tct('[author]', {author}),
  235. message: activity.data.text,
  236. };
  237. case GroupActivityType.SET_RESOLVED:
  238. let resolvedMessage: JSX.Element;
  239. if ('integration_id' in activity.data && activity.data.integration_id) {
  240. resolvedMessage = tct('by [author] via [integration]', {
  241. integration: (
  242. <Link
  243. to={`/settings/${organization.slug}/integrations/${activity.data.provider_key}/${activity.data.integration_id}/`}
  244. >
  245. {activity.data.provider}
  246. </Link>
  247. ),
  248. author,
  249. });
  250. } else {
  251. resolvedMessage = tct('by [author]', {author});
  252. }
  253. return {
  254. title: t('Resolved'),
  255. message: resolvedMessage,
  256. };
  257. case GroupActivityType.SET_RESOLVED_BY_AGE:
  258. return {
  259. title: t('Resolved'),
  260. message: tct('by [author] due to inactivity', {
  261. author,
  262. }),
  263. };
  264. case GroupActivityType.SET_RESOLVED_IN_RELEASE:
  265. // Resolved in the next release
  266. if ('current_release_version' in activity.data) {
  267. const currentVersion = activity.data.current_release_version;
  268. return {
  269. title: t('Resolved'),
  270. message: tct('by [author] in releases greater than [version] [semver]', {
  271. author,
  272. version: <ActivityRelease project={project} version={currentVersion} />,
  273. semver: isSemverRelease(currentVersion) ? t('(semver)') : t('(non-semver)'),
  274. }),
  275. };
  276. }
  277. const version = activity.data.version;
  278. return {
  279. title: t('Resolved'),
  280. message: version
  281. ? tct('by [author] in [version] [semver]', {
  282. author,
  283. version: <ActivityRelease project={project} version={version} />,
  284. semver: isSemverRelease(version) ? t('(semver)') : t('(non-semver)'),
  285. })
  286. : tct('by [author] in the upcoming release', {
  287. author,
  288. }),
  289. };
  290. case GroupActivityType.SET_RESOLVED_IN_COMMIT:
  291. const deployedReleases = (activity.data.commit?.releases || [])
  292. .filter(r => r.dateReleased !== null)
  293. .sort(
  294. (a, b) => moment(a.dateReleased).valueOf() - moment(b.dateReleased).valueOf()
  295. );
  296. if (deployedReleases.length === 1 && activity.data.commit) {
  297. return {
  298. title: t('Resolved'),
  299. message: tct(
  300. 'by [author] in [version]. This commit was released in [release]',
  301. {
  302. author,
  303. version: (
  304. <CommitLink
  305. inline
  306. commitId={activity.data.commit.id}
  307. repository={activity.data.commit.repository}
  308. />
  309. ),
  310. release: (
  311. <ActivityRelease
  312. project={project}
  313. version={deployedReleases[0].version}
  314. />
  315. ),
  316. }
  317. ),
  318. };
  319. }
  320. if (deployedReleases.length > 1 && activity.data.commit) {
  321. return {
  322. title: t('Resolved'),
  323. message: tct(
  324. 'by [author] in [version]. This commit was released in [release] and [otherCount] others',
  325. {
  326. author,
  327. otherCount: deployedReleases.length - 1,
  328. version: (
  329. <CommitLink
  330. inline
  331. commitId={activity.data.commit.id}
  332. repository={activity.data.commit.repository}
  333. />
  334. ),
  335. release: (
  336. <ActivityRelease
  337. project={project}
  338. version={deployedReleases[0].version}
  339. />
  340. ),
  341. }
  342. ),
  343. };
  344. }
  345. if (activity.data.commit) {
  346. return {
  347. title: t('Resolved'),
  348. message: tct('by [author] in [commit]', {
  349. author,
  350. commit: (
  351. <CommitLink
  352. inline
  353. commitId={activity.data.commit.id}
  354. repository={activity.data.commit.repository}
  355. />
  356. ),
  357. }),
  358. };
  359. }
  360. return {
  361. title: t('Resolved'),
  362. message: tct('by [author] in a commit', {author}),
  363. };
  364. case GroupActivityType.SET_RESOLVED_IN_PULL_REQUEST: {
  365. const {data} = activity;
  366. const {pullRequest} = data;
  367. return {
  368. title: t('Pull Request Created'),
  369. message: tct(' by [author]: [pullRequest]', {
  370. author,
  371. pullRequest: pullRequest ? (
  372. <PullRequestLink
  373. inline
  374. pullRequest={pullRequest}
  375. repository={pullRequest.repository}
  376. />
  377. ) : (
  378. t('PR not available')
  379. ),
  380. }),
  381. };
  382. }
  383. case GroupActivityType.SET_UNRESOLVED: {
  384. // TODO(nisanthan): Remove after migrating records to SET_ESCALATING
  385. const {data} = activity;
  386. if ('forecast' in data && data.forecast) {
  387. return {
  388. title: t('Escalated'),
  389. message: tct(
  390. ' by [author] because over [forecast] [event] happened in an hour',
  391. {
  392. author,
  393. forecast: data.forecast,
  394. event: data.forecast === 1 ? 'event' : 'events',
  395. }
  396. ),
  397. };
  398. }
  399. if ('integration_id' in data && data.integration_id) {
  400. return {
  401. title: t('Unresolved'),
  402. message: tct('by [author] via [integration]', {
  403. integration: (
  404. <Link
  405. to={`/settings/${organization.slug}/integrations/${data.provider_key}/${data.integration_id}/`}
  406. >
  407. {data.provider}
  408. </Link>
  409. ),
  410. author,
  411. }),
  412. };
  413. }
  414. return {
  415. title: t('Unresolved'),
  416. message: tct('by [author]', {author}),
  417. };
  418. }
  419. case GroupActivityType.SET_IGNORED: {
  420. const {data} = activity;
  421. return getIgnoredMessage(data);
  422. }
  423. case GroupActivityType.SET_PUBLIC:
  424. return {
  425. title: t('Made Public'),
  426. message: tct('by [author]', {author}),
  427. };
  428. case GroupActivityType.SET_PRIVATE:
  429. return {
  430. title: t('Made Private'),
  431. message: tct('by [author]', {author}),
  432. };
  433. case GroupActivityType.SET_REGRESSION: {
  434. const {data} = activity;
  435. let subtext: React.ReactNode = null;
  436. if (data.version && data.resolved_in_version && 'follows_semver' in data) {
  437. subtext = (
  438. <Subtext>
  439. {tct(
  440. '[regressionVersion] is greater than or equal to [resolvedVersion] compared via [comparison]',
  441. {
  442. regressionVersion: (
  443. <ActivityRelease project={project} version={data.version} />
  444. ),
  445. resolvedVersion: (
  446. <ActivityRelease
  447. project={project}
  448. version={data.resolved_in_version}
  449. />
  450. ),
  451. comparison: data.follows_semver ? t('semver') : t('release date'),
  452. }
  453. )}
  454. </Subtext>
  455. );
  456. }
  457. return {
  458. title: t('Regressed'),
  459. message: data.version
  460. ? tct('by [author] in [version]. [subtext]', {
  461. author,
  462. version: <ActivityRelease project={project} version={data.version} />,
  463. subtext,
  464. })
  465. : tct('by [author]', {
  466. author,
  467. subtext,
  468. }),
  469. };
  470. }
  471. case GroupActivityType.CREATE_ISSUE: {
  472. const {data} = activity;
  473. return {
  474. title: t('Created Issue'),
  475. message: tct('by [author] on [provider] titled [title]', {
  476. author,
  477. provider: data.provider,
  478. title: <ExternalLink href={data.location}>{data.title}</ExternalLink>,
  479. }),
  480. };
  481. }
  482. case GroupActivityType.MERGE:
  483. return {
  484. title: t('Merged'),
  485. message: tn(
  486. '%1$s issue into this issue by %2$s',
  487. '%1$s issues into this issue by %2$s',
  488. activity.data.issues.length,
  489. author
  490. ),
  491. };
  492. case GroupActivityType.UNMERGE_SOURCE: {
  493. const {data} = activity;
  494. const {destination, fingerprints} = data;
  495. return {
  496. title: t('Unmerged'),
  497. message: tn(
  498. '%1$s fingerprint to %3$s by %2$s',
  499. '%1$s fingerprints to %3$s by %2$s',
  500. fingerprints.length,
  501. author,
  502. destination ? (
  503. <Link
  504. to={`${issuesLink}${destination.id}?referrer=group-activity-unmerged-source`}
  505. >
  506. {destination.shortId}
  507. </Link>
  508. ) : (
  509. t('a group')
  510. )
  511. ),
  512. };
  513. }
  514. case GroupActivityType.UNMERGE_DESTINATION: {
  515. const {data} = activity;
  516. const {source, fingerprints} = data;
  517. return {
  518. title: t('Unmerged'),
  519. message: tn(
  520. '%1$s fingerprint to %3$s by %2$s',
  521. '%1$s fingerprints to %3$s by %2$s',
  522. fingerprints.length,
  523. author,
  524. source ? (
  525. <Link
  526. to={`${issuesLink}${source.id}?referrer=group-activity-unmerged-destination`}
  527. >
  528. {source.shortId}
  529. </Link>
  530. ) : (
  531. t('a group')
  532. )
  533. ),
  534. };
  535. }
  536. case GroupActivityType.FIRST_SEEN:
  537. if (activity.data.priority) {
  538. return {
  539. title: t('First Seen'),
  540. message: tct('Marked as [priority] priority', {
  541. priority: activity.data.priority,
  542. }),
  543. };
  544. }
  545. return {
  546. title: t('First Seen'),
  547. message: null,
  548. };
  549. case GroupActivityType.ASSIGNED: {
  550. return getAssignedMessage(activity);
  551. }
  552. case GroupActivityType.UNASSIGNED:
  553. return {
  554. title: t('Unassigned'),
  555. message: tct('by [author]', {author}),
  556. };
  557. case GroupActivityType.REPROCESS: {
  558. const {data} = activity;
  559. const {oldGroupId, eventCount} = data;
  560. return {
  561. title: t('Resprocessed Events'),
  562. message: tct('by [author]. [new-events]', {
  563. author,
  564. ['new-events']: (
  565. <Link
  566. to={`/organizations/${organization.slug}/issues/?query=reprocessing.original_issue_id:${oldGroupId}&referrer=group-activity-reprocesses`}
  567. >
  568. {tn('See %s new event', 'See %s new events', eventCount)}
  569. </Link>
  570. ),
  571. }),
  572. };
  573. }
  574. case GroupActivityType.MARK_REVIEWED: {
  575. return {
  576. title: t('Reviewed'),
  577. message: tct('by [author]', {
  578. author,
  579. }),
  580. };
  581. }
  582. case GroupActivityType.AUTO_SET_ONGOING: {
  583. return {
  584. title: t('Marked as Ongoing'),
  585. message: activity.data?.afterDays
  586. ? tct('automatically by [author] after [afterDays] days', {
  587. author,
  588. afterDays: activity.data.afterDays,
  589. })
  590. : tct('automatically by [author]', {
  591. author,
  592. }),
  593. };
  594. }
  595. case GroupActivityType.SET_ESCALATING: {
  596. return getEscalatingMessage(activity.data);
  597. }
  598. case GroupActivityType.SET_PRIORITY: {
  599. const {data} = activity;
  600. switch (data.reason) {
  601. case 'escalating':
  602. return {
  603. title: t('Priority Updated'),
  604. message: tct('by [author] to be [priority] after it escalated', {
  605. author,
  606. priority: data.priority,
  607. }),
  608. };
  609. case 'ongoing':
  610. return {
  611. title: t('Priority Updated'),
  612. message: tct(
  613. 'by [author] to be [priority] after it was marked as ongoing',
  614. {author, priority: data.priority}
  615. ),
  616. };
  617. default:
  618. return {
  619. title: t('Priority Updated'),
  620. message: tct('by [author] to be [priority]', {
  621. author,
  622. priority: data.priority,
  623. }),
  624. };
  625. }
  626. }
  627. case GroupActivityType.DELETED_ATTACHMENT:
  628. return {
  629. title: t('Attachment Deleted'),
  630. message: tct('by [author]', {author}),
  631. };
  632. default:
  633. return {title: '', message: ''}; // should never hit (?)
  634. }
  635. }
  636. return renderContent();
  637. }
  638. function ActivityRelease({project, version}: {project: Project; version: string}) {
  639. const organization = useOrganization();
  640. return (
  641. <VersionHoverCard
  642. organization={organization}
  643. projectSlug={project.slug}
  644. releaseVersion={version}
  645. >
  646. <ReleaseVersion version={version} projectId={project.id} />
  647. </VersionHoverCard>
  648. );
  649. }
  650. const Subtext = styled('div')`
  651. font-size: ${p => p.theme.fontSizeSmall};
  652. `;
  653. const CodeWrapper = styled('div')`
  654. overflow-wrap: anywhere;
  655. font-size: ${p => p.theme.fontSizeSmall};
  656. `;
  657. const StyledRuleSpan = styled('span')`
  658. font-family: ${p => p.theme.text.familyMono};
  659. `;
  660. const ReleaseVersion = styled(Version)`
  661. color: ${p => p.theme.gray300};
  662. text-decoration: underline;
  663. `;