index.tsx 11 KB

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