releaseIssues.tsx 13 KB

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