index.tsx 11 KB

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