releaseIssues.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import isEqual from 'lodash/isEqual';
  5. import * as qs from 'query-string';
  6. import type {Client} from 'sentry/api';
  7. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  8. import {Button} from 'sentry/components/button';
  9. import ButtonBar from 'sentry/components/buttonBar';
  10. import GroupList from 'sentry/components/issues/groupList';
  11. import Pagination from 'sentry/components/pagination';
  12. import QueryCount from 'sentry/components/queryCount';
  13. import {SegmentedControl} from 'sentry/components/segmentedControl';
  14. import {DEFAULT_RELATIVE_PERIODS} from 'sentry/constants';
  15. import {t, tct} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import type {Organization} from 'sentry/types/organization';
  18. import {browserHistory} from 'sentry/utils/browserHistory';
  19. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  20. import withApi from 'sentry/utils/withApi';
  21. import withOrganization from 'sentry/utils/withOrganization';
  22. import {IssueSortOptions} from 'sentry/views/issueList/utils';
  23. import type {ReleaseBounds} from '../../utils';
  24. import {getReleaseParams} from '../../utils';
  25. import EmptyState from '../commitsAndFiles/emptyState';
  26. enum IssuesType {
  27. NEW = 'new',
  28. UNHANDLED = 'unhandled',
  29. REGRESSED = 'regressed',
  30. RESOLVED = 'resolved',
  31. ALL = 'all',
  32. }
  33. const issuesQuery: Record<IssuesType, string> = {
  34. [IssuesType.NEW]: 'first-release',
  35. [IssuesType.UNHANDLED]: 'error.handled:0',
  36. [IssuesType.REGRESSED]: 'regressed_in_release',
  37. [IssuesType.RESOLVED]: 'is:resolved',
  38. [IssuesType.ALL]: 'release',
  39. };
  40. type IssuesQueryParams = {
  41. limit: number;
  42. query: string;
  43. sort: string;
  44. };
  45. const defaultProps = {
  46. withChart: false,
  47. };
  48. type Props = {
  49. api: Client;
  50. location: Location;
  51. organization: Organization;
  52. releaseBounds: ReleaseBounds;
  53. version: string;
  54. queryFilterDescription?: string;
  55. } & Partial<typeof defaultProps>;
  56. type State = {
  57. count: {
  58. all: number | null;
  59. new: number | null;
  60. regressed: number | null;
  61. resolved: number | null;
  62. unhandled: number | null;
  63. };
  64. onCursor?: () => void;
  65. pageLinks?: string;
  66. };
  67. class ReleaseIssues extends Component<Props, State> {
  68. static defaultProps = defaultProps;
  69. state: State = this.getInitialState();
  70. getInitialState() {
  71. return {
  72. count: {
  73. new: null,
  74. all: null,
  75. resolved: null,
  76. unhandled: null,
  77. regressed: null,
  78. },
  79. };
  80. }
  81. componentDidMount() {
  82. this.fetchIssuesCount();
  83. }
  84. componentDidUpdate(prevProps: Props) {
  85. if (
  86. !isEqual(
  87. getReleaseParams({
  88. location: this.props.location,
  89. releaseBounds: this.props.releaseBounds,
  90. }),
  91. getReleaseParams({
  92. location: prevProps.location,
  93. releaseBounds: prevProps.releaseBounds,
  94. })
  95. )
  96. ) {
  97. this.fetchIssuesCount();
  98. }
  99. }
  100. getActiveIssuesType(): IssuesType {
  101. const query = (this.props.location.query?.issuesType as string) ?? '';
  102. return Object.values<string>(IssuesType).includes(query)
  103. ? (query as IssuesType)
  104. : IssuesType.NEW;
  105. }
  106. getIssuesUrl() {
  107. const {version, organization} = this.props;
  108. const issuesType = this.getActiveIssuesType();
  109. const {queryParams} = this.getIssuesEndpoint();
  110. const query = new MutableSearch([]);
  111. switch (issuesType) {
  112. case IssuesType.NEW:
  113. query.setFilterValues('firstRelease', [version]);
  114. break;
  115. case IssuesType.UNHANDLED:
  116. query.setFilterValues('release', [version]);
  117. query.setFilterValues('error.handled', ['0']);
  118. break;
  119. case IssuesType.REGRESSED:
  120. query.setFilterValues('regressed_in_release', [version]);
  121. break;
  122. case IssuesType.RESOLVED:
  123. case IssuesType.ALL:
  124. default:
  125. query.setFilterValues('release', [version]);
  126. }
  127. return {
  128. pathname: `/organizations/${organization.slug}/issues/`,
  129. query: {
  130. ...queryParams,
  131. limit: undefined,
  132. cursor: undefined,
  133. query: query.formatString(),
  134. },
  135. };
  136. }
  137. getIssuesEndpoint(): {path: string; queryParams: IssuesQueryParams} {
  138. const {version, organization, location, releaseBounds} = this.props;
  139. const issuesType = this.getActiveIssuesType();
  140. const queryParams = {
  141. ...getReleaseParams({
  142. location,
  143. releaseBounds,
  144. }),
  145. limit: 10,
  146. sort: IssueSortOptions.FREQ,
  147. groupStatsPeriod: 'auto',
  148. };
  149. switch (issuesType) {
  150. case IssuesType.ALL:
  151. return {
  152. path: `/organizations/${organization.slug}/issues/`,
  153. queryParams: {
  154. ...queryParams,
  155. query: new MutableSearch([
  156. `${issuesQuery.all}:${version}`,
  157. 'is:unresolved',
  158. ]).formatString(),
  159. },
  160. };
  161. case IssuesType.RESOLVED:
  162. return {
  163. path: `/organizations/${organization.slug}/releases/${encodeURIComponent(
  164. version
  165. )}/resolved/`,
  166. queryParams: {...queryParams, query: ''},
  167. };
  168. case IssuesType.UNHANDLED:
  169. return {
  170. path: `/organizations/${organization.slug}/issues/`,
  171. queryParams: {
  172. ...queryParams,
  173. query: new MutableSearch([
  174. `${issuesQuery.all}:${version}`,
  175. issuesQuery.unhandled,
  176. 'is:unresolved',
  177. ]).formatString(),
  178. },
  179. };
  180. case IssuesType.REGRESSED:
  181. return {
  182. path: `/organizations/${organization.slug}/issues/`,
  183. queryParams: {
  184. ...queryParams,
  185. query: new MutableSearch([
  186. `${issuesQuery.regressed}:${version}`,
  187. ]).formatString(),
  188. },
  189. };
  190. case IssuesType.NEW:
  191. default:
  192. return {
  193. path: `/organizations/${organization.slug}/issues/`,
  194. queryParams: {
  195. ...queryParams,
  196. query: new MutableSearch([
  197. `${issuesQuery.new}:${version}`,
  198. 'is:unresolved',
  199. ]).formatString(),
  200. },
  201. };
  202. }
  203. }
  204. async fetchIssuesCount() {
  205. const {api, organization, version} = this.props;
  206. const issueCountEndpoint = this.getIssueCountEndpoint();
  207. const resolvedEndpoint = `/organizations/${
  208. organization.slug
  209. }/releases/${encodeURIComponent(version)}/resolved/`;
  210. try {
  211. await Promise.all([
  212. api.requestPromise(issueCountEndpoint),
  213. api.requestPromise(resolvedEndpoint),
  214. ]).then(([issueResponse, resolvedResponse]) => {
  215. this.setState({
  216. count: {
  217. all: issueResponse[`${issuesQuery.all}:"${version}" is:unresolved`] || 0,
  218. new: issueResponse[`${issuesQuery.new}:"${version}" is:unresolved`] || 0,
  219. resolved: resolvedResponse.length,
  220. unhandled:
  221. issueResponse[
  222. `${issuesQuery.unhandled} ${issuesQuery.all}:"${version}" is:unresolved`
  223. ] || 0,
  224. regressed: issueResponse[`${issuesQuery.regressed}:"${version}"`] || 0,
  225. },
  226. });
  227. });
  228. } catch {
  229. // do nothing
  230. }
  231. }
  232. getIssueCountEndpoint() {
  233. const {organization, version, location, releaseBounds} = this.props;
  234. const issuesCountPath = `/organizations/${organization.slug}/issues-count/`;
  235. const params = [
  236. `${issuesQuery.new}:"${version}" is:unresolved`,
  237. `${issuesQuery.all}:"${version}" is:unresolved`,
  238. `${issuesQuery.unhandled} ${issuesQuery.all}:"${version}" is:unresolved`,
  239. `${issuesQuery.regressed}:"${version}"`,
  240. ];
  241. const queryParams = params.map(param => param);
  242. const queryParameters = {
  243. ...getReleaseParams({
  244. location,
  245. releaseBounds,
  246. }),
  247. query: queryParams,
  248. };
  249. return `${issuesCountPath}?${qs.stringify(queryParameters)}`;
  250. }
  251. handleIssuesTypeSelection = (issuesType: IssuesType) => {
  252. const {location} = this.props;
  253. browserHistory.replace({
  254. ...location,
  255. query: {
  256. ...location.query,
  257. issuesType,
  258. },
  259. });
  260. };
  261. handleFetchSuccess = (groupListState, onCursor) => {
  262. this.setState({pageLinks: groupListState.pageLinks, onCursor});
  263. };
  264. renderEmptyMessage = () => {
  265. const {location, releaseBounds} = this.props;
  266. const issuesType = this.getActiveIssuesType();
  267. const isEntireReleasePeriod =
  268. !location.query.pageStatsPeriod && !location.query.pageStart;
  269. const {statsPeriod} = getReleaseParams({
  270. location,
  271. releaseBounds,
  272. });
  273. const selectedTimePeriod = statsPeriod ? DEFAULT_RELATIVE_PERIODS[statsPeriod] : null;
  274. const displayedPeriod = selectedTimePeriod
  275. ? selectedTimePeriod.toLowerCase()
  276. : t('given timeframe');
  277. return (
  278. <EmptyState>
  279. {issuesType === IssuesType.NEW
  280. ? isEntireReleasePeriod
  281. ? t('No new issues in this release.')
  282. : tct('No new issues for the [timePeriod].', {
  283. timePeriod: displayedPeriod,
  284. })
  285. : null}
  286. {issuesType === IssuesType.UNHANDLED
  287. ? isEntireReleasePeriod
  288. ? t('No unhandled issues in this release.')
  289. : tct('No unhandled issues for the [timePeriod].', {
  290. timePeriod: displayedPeriod,
  291. })
  292. : null}
  293. {issuesType === IssuesType.REGRESSED
  294. ? isEntireReleasePeriod
  295. ? t('No regressed issues in this release.')
  296. : tct('No regressed issues for the [timePeriod].', {
  297. timePeriod: displayedPeriod,
  298. })
  299. : null}
  300. {issuesType === IssuesType.RESOLVED && t('No resolved issues in this release.')}
  301. {issuesType === IssuesType.ALL
  302. ? isEntireReleasePeriod
  303. ? t('No issues in this release')
  304. : tct('No issues for the [timePeriod].', {
  305. timePeriod: displayedPeriod,
  306. })
  307. : null}
  308. </EmptyState>
  309. );
  310. };
  311. render() {
  312. const {count, pageLinks, onCursor} = this.state;
  313. const issuesType = this.getActiveIssuesType();
  314. const {organization, queryFilterDescription, withChart, version} = this.props;
  315. const {path, queryParams} = this.getIssuesEndpoint();
  316. const issuesTypes = [
  317. {value: IssuesType.ALL, label: t('All Issues'), issueCount: count.all},
  318. {value: IssuesType.NEW, label: t('New Issues'), issueCount: count.new},
  319. {
  320. value: IssuesType.UNHANDLED,
  321. label: t('Unhandled'),
  322. issueCount: count.unhandled,
  323. },
  324. {
  325. value: IssuesType.REGRESSED,
  326. label: t('Regressed'),
  327. issueCount: count.regressed,
  328. },
  329. {
  330. value: IssuesType.RESOLVED,
  331. label: t('Resolved'),
  332. issueCount: count.resolved,
  333. },
  334. ];
  335. return (
  336. <Fragment>
  337. <ControlsWrapper>
  338. <GuideAnchor target="release_states">
  339. <SegmentedControl
  340. aria-label={t('Issue type')}
  341. size="xs"
  342. value={issuesType}
  343. onChange={key => this.handleIssuesTypeSelection(key)}
  344. >
  345. {issuesTypes.map(({value, label, issueCount}) => (
  346. <SegmentedControl.Item key={value} textValue={label}>
  347. {label}&nbsp;
  348. <QueryCount
  349. count={issueCount}
  350. max={99}
  351. hideParens
  352. hideIfEmpty={false}
  353. />
  354. </SegmentedControl.Item>
  355. ))}
  356. </SegmentedControl>
  357. </GuideAnchor>
  358. <OpenInButtonBar gap={1}>
  359. <Button to={this.getIssuesUrl()} size="xs">
  360. {t('Open in Issues')}
  361. </Button>
  362. <StyledPagination pageLinks={pageLinks} onCursor={onCursor} size="xs" />
  363. </OpenInButtonBar>
  364. </ControlsWrapper>
  365. <div data-test-id="release-wrapper">
  366. <GroupList
  367. orgSlug={organization.slug}
  368. endpointPath={path}
  369. queryParams={queryParams}
  370. query={`release:${version}`}
  371. canSelectGroups={false}
  372. queryFilterDescription={queryFilterDescription}
  373. withChart={withChart}
  374. narrowGroups
  375. renderEmptyMessage={this.renderEmptyMessage}
  376. withPagination={false}
  377. onFetchSuccess={this.handleFetchSuccess}
  378. source="release"
  379. />
  380. </div>
  381. </Fragment>
  382. );
  383. }
  384. }
  385. const ControlsWrapper = styled('div')`
  386. display: flex;
  387. flex-wrap: wrap;
  388. align-items: center;
  389. justify-content: space-between;
  390. @media (max-width: ${p => p.theme.breakpoints.small}) {
  391. display: block;
  392. }
  393. `;
  394. const OpenInButtonBar = styled(ButtonBar)`
  395. margin: ${space(1)} 0;
  396. `;
  397. const StyledPagination = styled(Pagination)`
  398. margin: 0;
  399. `;
  400. export default withApi(withOrganization(ReleaseIssues));