releaseIssues.tsx 13 KB

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