releaseIssues.tsx 13 KB

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