index.tsx 11 KB

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