releasesPromo.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. import {useCallback, useEffect, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import commitImage from 'sentry-images/spot/releases-tour-commits.svg';
  4. import emailImage from 'sentry-images/spot/releases-tour-email.svg';
  5. import resolutionImage from 'sentry-images/spot/releases-tour-resolution.svg';
  6. import statsImage from 'sentry-images/spot/releases-tour-stats.svg';
  7. import {openCreateReleaseIntegration} from 'sentry/actionCreators/modal';
  8. import Access from 'sentry/components/acl/access';
  9. import {Button} from 'sentry/components/button';
  10. import {CodeSnippet} from 'sentry/components/codeSnippet';
  11. import DropdownAutoComplete from 'sentry/components/dropdownAutoComplete';
  12. import type {Item} from 'sentry/components/dropdownAutoComplete/types';
  13. import Link from 'sentry/components/links/link';
  14. import LoadingIndicator from 'sentry/components/loadingIndicator';
  15. import type {TourStep} from 'sentry/components/modals/featureTourModal';
  16. import {TourImage, TourText} from 'sentry/components/modals/featureTourModal';
  17. import Panel from 'sentry/components/panels/panel';
  18. import TextOverflow from 'sentry/components/textOverflow';
  19. import {Tooltip} from 'sentry/components/tooltip';
  20. import {IconAdd} from 'sentry/icons';
  21. import {t} from 'sentry/locale';
  22. import {space} from 'sentry/styles/space';
  23. import type {
  24. NewInternalAppApiToken,
  25. Organization,
  26. Project,
  27. SentryApp,
  28. } from 'sentry/types';
  29. import {trackAnalytics} from 'sentry/utils/analytics';
  30. import {useApiQuery} from 'sentry/utils/queryClient';
  31. import useApi from 'sentry/utils/useApi';
  32. const releasesSetupUrl = 'https://docs.sentry.io/product/releases/';
  33. const docsLink = (
  34. <Button external href={releasesSetupUrl}>
  35. {t('Setup')}
  36. </Button>
  37. );
  38. export const RELEASES_TOUR_STEPS: TourStep[] = [
  39. {
  40. title: t('Suspect Commits'),
  41. image: <TourImage src={commitImage} />,
  42. body: (
  43. <TourText>
  44. {t(
  45. 'Sentry suggests which commit caused an issue and who is likely responsible so you can triage.'
  46. )}
  47. </TourText>
  48. ),
  49. actions: docsLink,
  50. },
  51. {
  52. title: t('Release Stats'),
  53. image: <TourImage src={statsImage} />,
  54. body: (
  55. <TourText>
  56. {t(
  57. 'Get an overview of the commits in each release, and which issues were introduced or fixed.'
  58. )}
  59. </TourText>
  60. ),
  61. actions: docsLink,
  62. },
  63. {
  64. title: t('Easily Resolve'),
  65. image: <TourImage src={resolutionImage} />,
  66. body: (
  67. <TourText>
  68. {t(
  69. 'Automatically resolve issues by including the issue number in your commit message.'
  70. )}
  71. </TourText>
  72. ),
  73. actions: docsLink,
  74. },
  75. {
  76. title: t('Deploy Emails'),
  77. image: <TourImage src={emailImage} />,
  78. body: (
  79. <TourText>
  80. {t(
  81. 'Receive email notifications about when your code gets deployed. This can be customized in settings.'
  82. )}
  83. </TourText>
  84. ),
  85. },
  86. ];
  87. type Props = {
  88. organization: Organization;
  89. project: Project;
  90. };
  91. function ReleasesPromo({organization, project}: Props) {
  92. const {data, isLoading} = useApiQuery<SentryApp[]>(
  93. [`/organizations/${organization.slug}/sentry-apps/`, {query: {status: 'internal'}}],
  94. {
  95. staleTime: 0,
  96. }
  97. );
  98. const api = useApi();
  99. const [token, setToken] = useState<string | null>(null);
  100. const [integrations, setIntegrations] = useState<SentryApp[]>([]);
  101. const [selectedItem, selectItem] = useState<Pick<Item, 'label' | 'value'> | null>(null);
  102. useEffect(() => {
  103. if (!isLoading && data) {
  104. setIntegrations(data);
  105. }
  106. }, [isLoading, data]);
  107. useEffect(() => {
  108. trackAnalytics('releases.quickstart_viewed', {
  109. organization,
  110. project_id: project.id,
  111. });
  112. // eslint-disable-next-line react-hooks/exhaustive-deps
  113. }, []);
  114. const trackQuickstartCopy = useCallback(() => {
  115. trackAnalytics('releases.quickstart_copied', {
  116. organization,
  117. project_id: project.id,
  118. });
  119. }, [organization, project]);
  120. const trackQuickstartCreatedIntegration = useCallback(
  121. (integration: SentryApp) => {
  122. trackAnalytics('releases.quickstart_create_integration.success', {
  123. organization,
  124. project_id: project.id,
  125. integration_uuid: integration.uuid,
  126. });
  127. },
  128. [organization, project]
  129. );
  130. const trackCreateIntegrationModalClose = useCallback(() => {
  131. trackAnalytics('releases.quickstart_create_integration_modal.close', {
  132. organization,
  133. project_id: project.id,
  134. });
  135. }, [organization, project.id]);
  136. const generateAndSetNewToken = async (sentryAppSlug: string) => {
  137. const newToken = await generateToken(sentryAppSlug);
  138. return setToken(newToken);
  139. };
  140. const generateToken = async (sentryAppSlug: string) => {
  141. const newToken: NewInternalAppApiToken = await api.requestPromise(
  142. `/sentry-apps/${sentryAppSlug}/api-tokens/`,
  143. {
  144. method: 'POST',
  145. }
  146. );
  147. return newToken.token;
  148. };
  149. const renderIntegrationNode = (integration: SentryApp) => {
  150. return {
  151. value: {slug: integration.slug, name: integration.name},
  152. searchKey: `${integration.name}`,
  153. label: (
  154. <MenuItemWrapper data-test-id="integration-option" key={integration.uuid}>
  155. <Label>{integration.name}</Label>
  156. </MenuItemWrapper>
  157. ),
  158. };
  159. };
  160. const codeChunks = useMemo(
  161. () => [
  162. `# Install the cli
  163. curl -sL https://sentry.io/get-cli/ | bash
  164. # Setup configuration values
  165. SENTRY_AUTH_TOKEN=`,
  166. token && selectedItem
  167. ? `${token} # From internal integration: ${selectedItem.value.name}`
  168. : '<click-here-for-your-token>',
  169. `
  170. SENTRY_ORG=${organization.slug}
  171. SENTRY_PROJECT=${project.slug}
  172. VERSION=\`sentry-cli releases propose-version\`
  173. # Workflow to create releases
  174. sentry-cli releases new "$VERSION"
  175. sentry-cli releases set-commits "$VERSION" --auto
  176. sentry-cli releases finalize "$VERSION"`,
  177. ],
  178. [token, selectedItem, organization.slug, project.slug]
  179. );
  180. if (isLoading) {
  181. return <LoadingIndicator />;
  182. }
  183. return (
  184. <Panel>
  185. <Container>
  186. <ContainerHeader>
  187. <h3>{t('Set up Releases')}</h3>
  188. <Button priority="default" size="sm" href={releasesSetupUrl} external>
  189. {t('Full Documentation')}
  190. </Button>
  191. </ContainerHeader>
  192. <p>
  193. {t(
  194. 'Find which release caused an issue, apply source maps, and get notified about your deploys.'
  195. )}
  196. </p>
  197. <p>
  198. {t(
  199. 'Add the following commands to your CI config when you deploy your application.'
  200. )}
  201. </p>
  202. <CodeSnippetWrapper>
  203. <CodeSnippet
  204. dark
  205. language="bash"
  206. hideCopyButton={!token || !selectedItem}
  207. onCopy={trackQuickstartCopy}
  208. >
  209. {codeChunks.join('')}
  210. </CodeSnippet>
  211. <CodeSnippetOverlay className="prism-dark language-bash">
  212. <CodeSnippetOverlaySpan>{codeChunks[0]}</CodeSnippetOverlaySpan>
  213. <CodeSnippetDropdownWrapper>
  214. <CodeSnippetDropdown
  215. minWidth={300}
  216. maxHeight={400}
  217. onOpen={e => {
  218. // This can be called multiple times and does not always have `event`
  219. e?.stopPropagation();
  220. }}
  221. items={[
  222. {
  223. label: <GroupHeader>{t('Available Integrations')}</GroupHeader>,
  224. id: 'available-integrations',
  225. items: (integrations || []).map(renderIntegrationNode),
  226. },
  227. ]}
  228. alignMenu="left"
  229. onSelect={({label, value}) => {
  230. selectItem({label, value});
  231. generateAndSetNewToken(value.slug);
  232. }}
  233. itemSize="small"
  234. searchPlaceholder={t('Select Internal Integration')}
  235. menuFooter={
  236. <Access access={['org:integrations']}>
  237. {({hasAccess}) => (
  238. <Tooltip
  239. title={t(
  240. 'You must be an organization owner, manager or admin to create an integration.'
  241. )}
  242. disabled={hasAccess}
  243. >
  244. <CreateIntegrationLink
  245. to=""
  246. data-test-id="create-release-integration"
  247. disabled={!hasAccess}
  248. onClick={() =>
  249. openCreateReleaseIntegration({
  250. organization,
  251. project,
  252. onCreateSuccess: (integration: SentryApp) => {
  253. setIntegrations([integration, ...integrations]);
  254. const {label, value} = renderIntegrationNode(integration);
  255. selectItem({
  256. label,
  257. value,
  258. });
  259. generateAndSetNewToken(value.slug);
  260. trackQuickstartCreatedIntegration(integration);
  261. },
  262. onCancel: () => {
  263. trackCreateIntegrationModalClose();
  264. },
  265. })
  266. }
  267. >
  268. <MenuItemFooterWrapper>
  269. <IconContainer>
  270. <IconAdd color="activeText" isCircled size="sm" />
  271. </IconContainer>
  272. <Label>{t('Create New Integration')}</Label>
  273. </MenuItemFooterWrapper>
  274. </CreateIntegrationLink>
  275. </Tooltip>
  276. )}
  277. </Access>
  278. }
  279. disableLabelPadding
  280. emptyHidesInput
  281. >
  282. {() => <CodeSnippetOverlaySpan>{codeChunks[1]}</CodeSnippetOverlaySpan>}
  283. </CodeSnippetDropdown>
  284. </CodeSnippetDropdownWrapper>
  285. <CodeSnippetOverlaySpan>{codeChunks[2]}</CodeSnippetOverlaySpan>
  286. </CodeSnippetOverlay>
  287. </CodeSnippetWrapper>
  288. </Container>
  289. </Panel>
  290. );
  291. }
  292. const Container = styled('div')`
  293. padding: ${space(3)};
  294. `;
  295. const ContainerHeader = styled('div')`
  296. display: flex;
  297. justify-content: space-between;
  298. align-items: center;
  299. margin-bottom: ${space(3)};
  300. min-height: 32px;
  301. h3 {
  302. margin: 0;
  303. }
  304. @media (max-width: ${p => p.theme.breakpoints.small}) {
  305. flex-direction: column;
  306. align-items: flex-start;
  307. h3 {
  308. margin-bottom: ${space(2)};
  309. }
  310. }
  311. `;
  312. const CodeSnippetWrapper = styled('div')`
  313. position: relative;
  314. `;
  315. /**
  316. * CodeSnippet stringifies all inner children (due to Prism code highlighting), so we
  317. * can't put CodeSnippetDropdown inside of it. Instead, we can render a pre wrap
  318. * containing the same code (without Prism highlighting) with CodeSnippetDropdown in the
  319. * middle and overlay it on top of CodeSnippet.
  320. */
  321. const CodeSnippetOverlay = styled('pre')`
  322. position: absolute;
  323. top: 0;
  324. bottom: 0;
  325. left: 0;
  326. right: 0;
  327. z-index: 2;
  328. margin-bottom: 0;
  329. pointer-events: none;
  330. && {
  331. background: transparent;
  332. }
  333. `;
  334. /**
  335. * Invisible code span overlaid on top of the highlighted code. Exists only to
  336. * properly position <CodeSnippetDropdown /> inside <CodeSnippetOverlay />.
  337. */
  338. const CodeSnippetOverlaySpan = styled('span')`
  339. visibility: hidden;
  340. `;
  341. const CodeSnippetDropdownWrapper = styled('span')`
  342. /* Re-enable pointer events (disabled by CodeSnippetOverlay) */
  343. pointer-events: initial;
  344. `;
  345. const CodeSnippetDropdown = styled(DropdownAutoComplete)`
  346. position: absolute;
  347. font-family: ${p => p.theme.text.family};
  348. border: none;
  349. border-radius: 4px;
  350. width: 300px;
  351. `;
  352. const GroupHeader = styled('div')`
  353. font-size: ${p => p.theme.fontSizeSmall};
  354. font-family: ${p => p.theme.text.family};
  355. font-weight: ${p => p.theme.fontWeightBold};
  356. margin: ${space(1)} 0;
  357. color: ${p => p.theme.subText};
  358. line-height: ${p => p.theme.fontSizeSmall};
  359. text-align: left;
  360. `;
  361. const CreateIntegrationLink = styled(Link)`
  362. color: ${p => (p.disabled ? p.theme.disabled : p.theme.textColor)};
  363. `;
  364. const MenuItemWrapper = styled('div')<{
  365. disabled?: boolean;
  366. py?: number;
  367. }>`
  368. cursor: ${p => (p.disabled ? 'not-allowed' : 'pointer')};
  369. display: flex;
  370. align-items: center;
  371. font-family: ${p => p.theme.text.family};
  372. font-size: 13px;
  373. ${p =>
  374. typeof p.py !== 'undefined' &&
  375. `
  376. padding-top: ${p.py};
  377. padding-bottom: ${p.py};
  378. `};
  379. `;
  380. const MenuItemFooterWrapper = styled(MenuItemWrapper)`
  381. padding: ${space(0.25)} ${space(1)};
  382. border-top: 1px solid ${p => p.theme.innerBorder};
  383. background-color: ${p => p.theme.tag.highlight.background};
  384. color: ${p => p.theme.active};
  385. :hover {
  386. color: ${p => p.theme.activeHover};
  387. svg {
  388. fill: ${p => p.theme.activeHover};
  389. }
  390. }
  391. `;
  392. const IconContainer = styled('div')`
  393. display: flex;
  394. align-items: center;
  395. justify-content: center;
  396. width: 24px;
  397. height: 24px;
  398. flex-shrink: 0;
  399. `;
  400. const Label = styled(TextOverflow)`
  401. margin-left: 6px;
  402. `;
  403. export default ReleasesPromo;