releasesPromo.tsx 13 KB

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