releaseIssues.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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([
  165. `${IssuesQuery.ALL}:${version}`,
  166. 'is:unresolved',
  167. ]).formatString(),
  168. },
  169. };
  170. case IssuesType.RESOLVED:
  171. return {
  172. path: `/organizations/${organization.slug}/releases/${encodeURIComponent(
  173. version
  174. )}/resolved/`,
  175. queryParams: {...queryParams, query: ''},
  176. };
  177. case IssuesType.UNHANDLED:
  178. return {
  179. path: `/organizations/${organization.slug}/issues/`,
  180. queryParams: {
  181. ...queryParams,
  182. query: new MutableSearch([
  183. `${IssuesQuery.ALL}:${version}`,
  184. IssuesQuery.UNHANDLED,
  185. 'is:unresolved',
  186. ]).formatString(),
  187. },
  188. };
  189. case IssuesType.REGRESSED:
  190. return {
  191. path: `/organizations/${organization.slug}/issues/`,
  192. queryParams: {
  193. ...queryParams,
  194. query: new MutableSearch([
  195. `${IssuesQuery.REGRESSED}:${version}`,
  196. ]).formatString(),
  197. },
  198. };
  199. case IssuesType.NEW:
  200. default:
  201. return {
  202. path: `/organizations/${organization.slug}/issues/`,
  203. queryParams: {
  204. ...queryParams,
  205. query: new MutableSearch([
  206. `${IssuesQuery.NEW}:${version}`,
  207. 'is:unresolved',
  208. ]).formatString(),
  209. },
  210. };
  211. }
  212. }
  213. async fetchIssuesCount() {
  214. const {api, organization, version} = this.props;
  215. const issueCountEndpoint = this.getIssueCountEndpoint();
  216. const resolvedEndpoint = `/organizations/${
  217. organization.slug
  218. }/releases/${encodeURIComponent(version)}/resolved/`;
  219. try {
  220. await Promise.all([
  221. api.requestPromise(issueCountEndpoint),
  222. api.requestPromise(resolvedEndpoint),
  223. ]).then(([issueResponse, resolvedResponse]) => {
  224. this.setState({
  225. count: {
  226. all: issueResponse[`${IssuesQuery.ALL}:"${version}" is:unresolved`] || 0,
  227. new: issueResponse[`${IssuesQuery.NEW}:"${version}" is:unresolved`] || 0,
  228. resolved: resolvedResponse.length,
  229. unhandled:
  230. issueResponse[
  231. `${IssuesQuery.UNHANDLED} ${IssuesQuery.ALL}:"${version}" is:unresolved`
  232. ] || 0,
  233. regressed: issueResponse[`${IssuesQuery.REGRESSED}:"${version}"`] || 0,
  234. },
  235. });
  236. });
  237. } catch {
  238. // do nothing
  239. }
  240. }
  241. getIssueCountEndpoint() {
  242. const {organization, version, location, releaseBounds} = this.props;
  243. const issuesCountPath = `/organizations/${organization.slug}/issues-count/`;
  244. const params = [
  245. `${IssuesQuery.NEW}:"${version}" is:unresolved`,
  246. `${IssuesQuery.ALL}:"${version}" is:unresolved`,
  247. `${IssuesQuery.UNHANDLED} ${IssuesQuery.ALL}:"${version}" is:unresolved`,
  248. `${IssuesQuery.REGRESSED}:"${version}"`,
  249. ];
  250. const queryParams = params.map(param => param);
  251. const queryParameters = {
  252. ...getReleaseParams({
  253. location,
  254. releaseBounds,
  255. }),
  256. query: queryParams,
  257. };
  258. return `${issuesCountPath}?${qs.stringify(queryParameters)}`;
  259. }
  260. handleIssuesTypeSelection = (issuesType: IssuesType) => {
  261. const {location} = this.props;
  262. const issuesTypeQuery =
  263. issuesType === IssuesType.ALL
  264. ? IssuesType.ALL
  265. : issuesType === IssuesType.NEW
  266. ? IssuesType.NEW
  267. : issuesType === IssuesType.RESOLVED
  268. ? IssuesType.RESOLVED
  269. : issuesType === IssuesType.UNHANDLED
  270. ? IssuesType.UNHANDLED
  271. : issuesType === IssuesType.REGRESSED
  272. ? IssuesType.REGRESSED
  273. : '';
  274. const to = {
  275. ...location,
  276. query: {
  277. ...location.query,
  278. issuesType: issuesTypeQuery,
  279. },
  280. };
  281. browserHistory.replace(to);
  282. this.setState({issuesType});
  283. };
  284. handleFetchSuccess = (groupListState, onCursor) => {
  285. this.setState({pageLinks: groupListState.pageLinks, onCursor});
  286. };
  287. renderEmptyMessage = () => {
  288. const {location, releaseBounds} = this.props;
  289. const {issuesType} = this.state;
  290. const isEntireReleasePeriod =
  291. !location.query.pageStatsPeriod && !location.query.pageStart;
  292. const {statsPeriod} = getReleaseParams({
  293. location,
  294. releaseBounds,
  295. });
  296. const selectedTimePeriod = statsPeriod ? DEFAULT_RELATIVE_PERIODS[statsPeriod] : null;
  297. const displayedPeriod = selectedTimePeriod
  298. ? selectedTimePeriod.toLowerCase()
  299. : t('given timeframe');
  300. return (
  301. <EmptyState>
  302. {issuesType === IssuesType.NEW
  303. ? isEntireReleasePeriod
  304. ? t('No new issues in this release.')
  305. : tct('No new issues for the [timePeriod].', {
  306. timePeriod: displayedPeriod,
  307. })
  308. : null}
  309. {issuesType === IssuesType.UNHANDLED
  310. ? isEntireReleasePeriod
  311. ? t('No unhandled issues in this release.')
  312. : tct('No unhandled issues for the [timePeriod].', {
  313. timePeriod: displayedPeriod,
  314. })
  315. : null}
  316. {issuesType === IssuesType.REGRESSED
  317. ? isEntireReleasePeriod
  318. ? t('No regressed issues in this release.')
  319. : tct('No regressed issues for the [timePeriod].', {
  320. timePeriod: displayedPeriod,
  321. })
  322. : null}
  323. {issuesType === IssuesType.RESOLVED && t('No resolved issues in this release.')}
  324. {issuesType === IssuesType.ALL
  325. ? isEntireReleasePeriod
  326. ? t('No issues in this release')
  327. : tct('No issues for the [timePeriod].', {
  328. timePeriod: displayedPeriod,
  329. })
  330. : null}
  331. </EmptyState>
  332. );
  333. };
  334. render() {
  335. const {issuesType, count, pageLinks, onCursor} = this.state;
  336. const {organization, queryFilterDescription, withChart} = this.props;
  337. const {path, queryParams} = this.getIssuesEndpoint();
  338. const issuesTypes = [
  339. {value: IssuesType.ALL, label: t('All Issues'), issueCount: count.all},
  340. {value: IssuesType.NEW, label: t('New Issues'), issueCount: count.new},
  341. {
  342. value: IssuesType.UNHANDLED,
  343. label: t('Unhandled'),
  344. issueCount: count.unhandled,
  345. },
  346. {
  347. value: IssuesType.REGRESSED,
  348. label: t('Regressed'),
  349. issueCount: count.regressed,
  350. },
  351. {
  352. value: IssuesType.RESOLVED,
  353. label: t('Resolved'),
  354. issueCount: count.resolved,
  355. },
  356. ];
  357. return (
  358. <Fragment>
  359. <ControlsWrapper>
  360. <StyledButtonBar active={issuesType} merged>
  361. {issuesTypes.map(({value, label, issueCount}) => (
  362. <Button
  363. key={value}
  364. barId={value}
  365. size="xs"
  366. onClick={() => this.handleIssuesTypeSelection(value)}
  367. data-test-id={`filter-${value}`}
  368. >
  369. {label}
  370. <QueryCount count={issueCount} max={99} hideParens hideIfEmpty={false} />
  371. </Button>
  372. ))}
  373. </StyledButtonBar>
  374. <OpenInButtonBar gap={1}>
  375. <Button to={this.getIssuesUrl()} size="xs" data-test-id="issues-button">
  376. {t('Open in Issues')}
  377. </Button>
  378. <StyledPagination pageLinks={pageLinks} onCursor={onCursor} size="xs" />
  379. </OpenInButtonBar>
  380. </ControlsWrapper>
  381. <div data-test-id="release-wrapper">
  382. <GroupList
  383. orgId={organization.slug}
  384. endpointPath={path}
  385. queryParams={queryParams}
  386. query=""
  387. canSelectGroups={false}
  388. queryFilterDescription={queryFilterDescription}
  389. withChart={withChart}
  390. narrowGroups
  391. renderEmptyMessage={this.renderEmptyMessage}
  392. withPagination={false}
  393. onFetchSuccess={this.handleFetchSuccess}
  394. />
  395. </div>
  396. </Fragment>
  397. );
  398. }
  399. }
  400. const ControlsWrapper = styled('div')`
  401. display: flex;
  402. flex-wrap: wrap;
  403. align-items: center;
  404. justify-content: space-between;
  405. @media (max-width: ${p => p.theme.breakpoints.small}) {
  406. display: block;
  407. ${ButtonGrid} {
  408. overflow: auto;
  409. }
  410. }
  411. `;
  412. const OpenInButtonBar = styled(ButtonBar)`
  413. margin: ${space(1)} 0;
  414. `;
  415. const StyledButtonBar = styled(ButtonBar)`
  416. grid-template-columns: repeat(4, 1fr);
  417. ${ButtonLabel} {
  418. white-space: nowrap;
  419. gap: ${space(0.5)};
  420. span:last-child {
  421. color: ${p => p.theme.buttonCount};
  422. }
  423. }
  424. .active {
  425. ${ButtonLabel} {
  426. span:last-child {
  427. color: ${p => p.theme.buttonCountActive};
  428. }
  429. }
  430. }
  431. `;
  432. const StyledPagination = styled(Pagination)`
  433. margin: 0;
  434. `;
  435. export default withApi(withOrganization(ReleaseIssues));