releasesPromo.tsx 13 KB

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