index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. import {createContext} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import isEqual from 'lodash/isEqual';
  6. import pick from 'lodash/pick';
  7. import {Alert} from 'sentry/components/alert';
  8. import AsyncComponent from 'sentry/components/asyncComponent';
  9. import * as Layout from 'sentry/components/layouts/thirds';
  10. import LoadingIndicator from 'sentry/components/loadingIndicator';
  11. import NoProjectMessage from 'sentry/components/noProjectMessage';
  12. import PageFiltersContainer from 'sentry/components/organizations/pageFilters/container';
  13. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  14. import PickProjectToContinue from 'sentry/components/pickProjectToContinue';
  15. import {PAGE_URL_PARAM, URL_PARAM} from 'sentry/constants/pageFilters';
  16. import {IconInfo} from 'sentry/icons';
  17. import {t} from 'sentry/locale';
  18. import {space} from 'sentry/styles/space';
  19. import {
  20. Deploy,
  21. Organization,
  22. PageFilters,
  23. ReleaseMeta,
  24. ReleaseProject,
  25. ReleaseWithHealth,
  26. SessionApiResponse,
  27. SessionFieldWithOperation,
  28. } from 'sentry/types';
  29. import {formatVersion} from 'sentry/utils/formatters';
  30. import withRouteAnalytics, {
  31. WithRouteAnalyticsProps,
  32. } from 'sentry/utils/routeAnalytics/withRouteAnalytics';
  33. import routeTitleGen from 'sentry/utils/routeTitle';
  34. import {getCount} from 'sentry/utils/sessions';
  35. import withOrganization from 'sentry/utils/withOrganization';
  36. import withPageFilters from 'sentry/utils/withPageFilters';
  37. import AsyncView from 'sentry/views/asyncView';
  38. import {getReleaseBounds, ReleaseBounds, searchReleaseVersion} from '../utils';
  39. import ReleaseHeader from './header/releaseHeader';
  40. type ReleaseContextType = {
  41. deploys: Deploy[];
  42. hasHealthData: boolean;
  43. project: Required<ReleaseProject>;
  44. refetchData: () => void;
  45. release: ReleaseWithHealth;
  46. releaseBounds: ReleaseBounds;
  47. releaseMeta: ReleaseMeta;
  48. };
  49. const ReleaseContext = createContext<ReleaseContextType>({} as ReleaseContextType);
  50. type RouteParams = {
  51. orgId: string;
  52. release: string;
  53. };
  54. type Props = RouteComponentProps<RouteParams, {}> &
  55. WithRouteAnalyticsProps & {
  56. organization: Organization;
  57. releaseMeta: ReleaseMeta;
  58. selection: PageFilters;
  59. };
  60. type State = {
  61. deploys: Deploy[];
  62. release: ReleaseWithHealth;
  63. sessions: SessionApiResponse | null;
  64. } & AsyncView['state'];
  65. class ReleasesDetail extends AsyncView<Props, State> {
  66. shouldReload = true;
  67. getTitle() {
  68. const {params, organization, selection} = this.props;
  69. const {release} = this.state;
  70. // The release details page will always have only one project selected
  71. const project = release?.projects.find(p => p.id === selection.projects[0]);
  72. return routeTitleGen(
  73. t('Release %s', formatVersion(params.release)),
  74. organization.slug,
  75. false,
  76. project?.slug
  77. );
  78. }
  79. getDefaultState() {
  80. return {
  81. ...super.getDefaultState(),
  82. deploys: [],
  83. sessions: null,
  84. };
  85. }
  86. componentDidUpdate(prevProps: Props, prevState: State) {
  87. const {organization, params, location} = this.props;
  88. if (
  89. prevProps.params.release !== params.release ||
  90. prevProps.organization.slug !== organization.slug ||
  91. !isEqual(
  92. this.pickLocationQuery(prevProps.location),
  93. this.pickLocationQuery(location)
  94. )
  95. ) {
  96. super.componentDidUpdate(prevProps, prevState);
  97. }
  98. }
  99. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  100. const {organization, location, params, releaseMeta} = this.props;
  101. const basePath = `/organizations/${organization.slug}/releases/${encodeURIComponent(
  102. params.release
  103. )}/`;
  104. const endpoints: ReturnType<AsyncView['getEndpoints']> = [
  105. [
  106. 'release',
  107. basePath,
  108. {
  109. query: {
  110. adoptionStages: 1,
  111. ...normalizeDateTimeParams(this.pickLocationQuery(location)),
  112. },
  113. },
  114. ],
  115. ];
  116. if (releaseMeta.deployCount > 0) {
  117. endpoints.push(['deploys', `${basePath}deploys/`]);
  118. }
  119. // Used to figure out if the release has any health data
  120. endpoints.push([
  121. 'sessions',
  122. `/organizations/${organization.slug}/sessions/`,
  123. {
  124. query: {
  125. project: location.query.project,
  126. environment: location.query.environment ?? [],
  127. query: searchReleaseVersion(params.release),
  128. field: 'sum(session)',
  129. statsPeriod: '90d',
  130. interval: '1d',
  131. },
  132. },
  133. {
  134. allowError: error => error.status === 400,
  135. },
  136. ]);
  137. return endpoints;
  138. }
  139. pickLocationQuery(location: Location) {
  140. return pick(location.query, [
  141. ...Object.values(URL_PARAM),
  142. ...Object.values(PAGE_URL_PARAM),
  143. ]);
  144. }
  145. renderError(...args) {
  146. const possiblyWrongProject = Object.values(this.state.errors).find(
  147. e => e?.status === 404 || e?.status === 403
  148. );
  149. if (possiblyWrongProject) {
  150. return (
  151. <Layout.Page>
  152. <Alert type="error" showIcon>
  153. {t('This release may not be in your selected project.')}
  154. </Alert>
  155. </Layout.Page>
  156. );
  157. }
  158. return super.renderError(...args);
  159. }
  160. renderLoading() {
  161. return (
  162. <Layout.Page>
  163. <LoadingIndicator />
  164. </Layout.Page>
  165. );
  166. }
  167. renderBody() {
  168. const {organization, location, selection, releaseMeta} = this.props;
  169. const {release, deploys, sessions, reloading} = this.state;
  170. const project = release?.projects.find(p => p.id === selection.projects[0]);
  171. const releaseBounds = getReleaseBounds(release);
  172. if (!project || !release) {
  173. if (reloading) {
  174. return <LoadingIndicator />;
  175. }
  176. return null;
  177. }
  178. return (
  179. <Layout.Page>
  180. <NoProjectMessage organization={organization}>
  181. <ReleaseHeader
  182. location={location}
  183. organization={organization}
  184. release={release}
  185. project={project}
  186. releaseMeta={releaseMeta}
  187. refetchData={this.fetchData}
  188. />
  189. <ReleaseContext.Provider
  190. value={{
  191. release,
  192. project,
  193. deploys,
  194. releaseMeta,
  195. refetchData: this.fetchData,
  196. hasHealthData:
  197. getCount(sessions?.groups, SessionFieldWithOperation.SESSIONS) > 0,
  198. releaseBounds,
  199. }}
  200. >
  201. {this.props.children}
  202. </ReleaseContext.Provider>
  203. </NoProjectMessage>
  204. </Layout.Page>
  205. );
  206. }
  207. }
  208. type ReleasesDetailContainerProps = Omit<Props, 'releaseMeta'>;
  209. type ReleasesDetailContainerState = {
  210. releaseMeta: ReleaseMeta | null;
  211. } & AsyncComponent['state'];
  212. class ReleasesDetailContainer extends AsyncComponent<
  213. ReleasesDetailContainerProps,
  214. ReleasesDetailContainerState
  215. > {
  216. shouldReload = true;
  217. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  218. const {organization, params} = this.props;
  219. // fetch projects this release belongs to
  220. return [
  221. [
  222. 'releaseMeta',
  223. `/organizations/${organization.slug}/releases/${encodeURIComponent(
  224. params.release
  225. )}/meta/`,
  226. ],
  227. ];
  228. }
  229. componentDidMount() {
  230. this.removeGlobalDateTimeFromUrl();
  231. this.props.setRouteAnalyticsParams({release: this.props.params.release});
  232. }
  233. componentDidUpdate(
  234. prevProps: ReleasesDetailContainerProps,
  235. prevState: ReleasesDetailContainerState
  236. ) {
  237. const {organization, params} = this.props;
  238. this.removeGlobalDateTimeFromUrl();
  239. if (
  240. prevProps.params.release !== params.release ||
  241. prevProps.organization.slug !== organization.slug
  242. ) {
  243. this.props.setRouteAnalyticsParams({release: this.props.params.release});
  244. super.componentDidUpdate(prevProps, prevState);
  245. }
  246. }
  247. removeGlobalDateTimeFromUrl() {
  248. const {router, location} = this.props;
  249. const {start, end, statsPeriod, utc, ...restQuery} = location.query;
  250. if (start || end || statsPeriod || utc) {
  251. router.replace({
  252. ...location,
  253. query: restQuery,
  254. });
  255. }
  256. }
  257. renderError(...args) {
  258. const has404Errors = Object.values(this.state.errors).find(e => e?.status === 404);
  259. if (has404Errors) {
  260. // This catches a 404 coming from the release endpoint and displays a custom error message.
  261. return (
  262. <Layout.Page withPadding>
  263. <Alert type="error" showIcon>
  264. {t('This release could not be found.')}
  265. </Alert>
  266. </Layout.Page>
  267. );
  268. }
  269. return super.renderError(...args);
  270. }
  271. isProjectMissingInUrl() {
  272. const projectId = this.props.location.query.project;
  273. return !projectId || typeof projectId !== 'string';
  274. }
  275. renderLoading() {
  276. return (
  277. <Layout.Page>
  278. <LoadingIndicator />
  279. </Layout.Page>
  280. );
  281. }
  282. renderProjectsFooterMessage() {
  283. return (
  284. <ProjectsFooterMessage>
  285. <IconInfo size="xs" /> {t('Only projects with this release are visible.')}
  286. </ProjectsFooterMessage>
  287. );
  288. }
  289. renderBody() {
  290. const {organization, params, router} = this.props;
  291. const {releaseMeta} = this.state;
  292. if (!releaseMeta) {
  293. return null;
  294. }
  295. const {projects} = releaseMeta;
  296. if (this.isProjectMissingInUrl()) {
  297. return (
  298. <PickProjectToContinue
  299. projects={projects.map(({id, slug}) => ({
  300. id: String(id),
  301. slug,
  302. }))}
  303. router={router}
  304. nextPath={{
  305. pathname: `/organizations/${organization.slug}/releases/${encodeURIComponent(
  306. params.release
  307. )}/`,
  308. }}
  309. noProjectRedirectPath={`/organizations/${organization.slug}/releases/`}
  310. />
  311. );
  312. }
  313. return (
  314. <PageFiltersContainer
  315. shouldForceProject={projects.length === 1}
  316. forceProject={
  317. projects.length === 1 ? {...projects[0], id: String(projects[0].id)} : undefined
  318. }
  319. specificProjectSlugs={projects.map(p => p.slug)}
  320. >
  321. <ReleasesDetail {...this.props} releaseMeta={releaseMeta} />
  322. </PageFiltersContainer>
  323. );
  324. }
  325. }
  326. const ProjectsFooterMessage = styled('div')`
  327. display: grid;
  328. align-items: center;
  329. grid-template-columns: min-content 1fr;
  330. gap: ${space(1)};
  331. `;
  332. export {ReleaseContext, ReleasesDetailContainer};
  333. export default withRouteAnalytics(
  334. withPageFilters(withOrganization(ReleasesDetailContainer))
  335. );