index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 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 {
  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 DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  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. } & 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(['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. } & DeprecatedAsyncComponent['state'];
  212. class ReleasesDetailContainer extends DeprecatedAsyncComponent<
  213. ReleasesDetailContainerProps,
  214. ReleasesDetailContainerState
  215. > {
  216. shouldReload = true;
  217. getEndpoints(): ReturnType<DeprecatedAsyncComponent['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. super.componentDidMount();
  231. this.removeGlobalDateTimeFromUrl();
  232. this.props.setRouteAnalyticsParams({release: this.props.params.release});
  233. }
  234. componentDidUpdate(
  235. prevProps: ReleasesDetailContainerProps,
  236. prevState: ReleasesDetailContainerState
  237. ) {
  238. const {organization, params} = this.props;
  239. this.removeGlobalDateTimeFromUrl();
  240. if (
  241. prevProps.params.release !== params.release ||
  242. prevProps.organization.slug !== organization.slug
  243. ) {
  244. this.props.setRouteAnalyticsParams({release: this.props.params.release});
  245. super.componentDidUpdate(prevProps, prevState);
  246. }
  247. }
  248. removeGlobalDateTimeFromUrl() {
  249. const {router, location} = this.props;
  250. const {start, end, statsPeriod, utc, ...restQuery} = location.query;
  251. if (start || end || statsPeriod || utc) {
  252. router.replace({
  253. ...location,
  254. query: restQuery,
  255. });
  256. }
  257. }
  258. renderError(...args) {
  259. const has404Errors = Object.values(this.state.errors).find(e => e?.status === 404);
  260. if (has404Errors) {
  261. // This catches a 404 coming from the release endpoint and displays a custom error message.
  262. return (
  263. <Layout.Page withPadding>
  264. <Alert type="error" showIcon>
  265. {t('This release could not be found.')}
  266. </Alert>
  267. </Layout.Page>
  268. );
  269. }
  270. return super.renderError(...args);
  271. }
  272. isProjectMissingInUrl() {
  273. const projectId = this.props.location.query.project;
  274. return !projectId || typeof projectId !== 'string';
  275. }
  276. renderLoading() {
  277. return (
  278. <Layout.Page>
  279. <LoadingIndicator />
  280. </Layout.Page>
  281. );
  282. }
  283. renderProjectsFooterMessage() {
  284. return (
  285. <ProjectsFooterMessage>
  286. <IconInfo size="xs" /> {t('Only projects with this release are visible.')}
  287. </ProjectsFooterMessage>
  288. );
  289. }
  290. renderBody() {
  291. const {organization, params, router} = this.props;
  292. const {releaseMeta} = this.state;
  293. if (!releaseMeta) {
  294. return null;
  295. }
  296. const {projects} = releaseMeta;
  297. if (this.isProjectMissingInUrl()) {
  298. return (
  299. <PickProjectToContinue
  300. projects={projects.map(({id, slug}) => ({
  301. id: String(id),
  302. slug,
  303. }))}
  304. router={router}
  305. nextPath={{
  306. pathname: `/organizations/${organization.slug}/releases/${encodeURIComponent(
  307. params.release
  308. )}/`,
  309. }}
  310. noProjectRedirectPath={`/organizations/${organization.slug}/releases/`}
  311. />
  312. );
  313. }
  314. return (
  315. <PageFiltersContainer
  316. shouldForceProject={projects.length === 1}
  317. forceProject={
  318. projects.length === 1 ? {...projects[0], id: String(projects[0].id)} : undefined
  319. }
  320. specificProjectSlugs={projects.map(p => p.slug)}
  321. >
  322. <ReleasesDetail {...this.props} releaseMeta={releaseMeta} />
  323. </PageFiltersContainer>
  324. );
  325. }
  326. }
  327. const ProjectsFooterMessage = styled('div')`
  328. display: grid;
  329. align-items: center;
  330. grid-template-columns: min-content 1fr;
  331. gap: ${space(1)};
  332. `;
  333. export {ReleaseContext, ReleasesDetailContainer};
  334. export default withRouteAnalytics(
  335. withPageFilters(withOrganization(ReleasesDetailContainer))
  336. );