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 {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 {Organization, Project, SentryApp} from 'sentry/types';
  23. import {trackAnalytics} from 'sentry/utils/analytics';
  24. import useApi from 'sentry/utils/useApi';
  25. import useApiRequests from 'sentry/utils/useApiRequests';
  26. const releasesSetupUrl = 'https://docs.sentry.io/product/releases/';
  27. const docsLink = (
  28. <Button external href={releasesSetupUrl}>
  29. {t('Setup')}
  30. </Button>
  31. );
  32. export const RELEASES_TOUR_STEPS: TourStep[] = [
  33. {
  34. title: t('Suspect Commits'),
  35. image: <TourImage src={commitImage} />,
  36. body: (
  37. <TourText>
  38. {t(
  39. 'Sentry suggests which commit caused an issue and who is likely responsible so you can triage.'
  40. )}
  41. </TourText>
  42. ),
  43. actions: docsLink,
  44. },
  45. {
  46. title: t('Release Stats'),
  47. image: <TourImage src={statsImage} />,
  48. body: (
  49. <TourText>
  50. {t(
  51. 'Get an overview of the commits in each release, and which issues were introduced or fixed.'
  52. )}
  53. </TourText>
  54. ),
  55. actions: docsLink,
  56. },
  57. {
  58. title: t('Easily Resolve'),
  59. image: <TourImage src={resolutionImage} />,
  60. body: (
  61. <TourText>
  62. {t(
  63. 'Automatically resolve issues by including the issue number in your commit message.'
  64. )}
  65. </TourText>
  66. ),
  67. actions: docsLink,
  68. },
  69. {
  70. title: t('Deploy Emails'),
  71. image: <TourImage src={emailImage} />,
  72. body: (
  73. <TourText>
  74. {t(
  75. 'Receive email notifications about when your code gets deployed. This can be customized in settings.'
  76. )}
  77. </TourText>
  78. ),
  79. },
  80. ];
  81. type Props = {
  82. organization: Organization;
  83. project: Project;
  84. };
  85. const ReleasesPromo = ({organization, project}: Props) => {
  86. const {data, renderComponent, isLoading} = useApiRequests<{
  87. internalIntegrations: SentryApp[];
  88. }>({
  89. endpoints: [
  90. [
  91. 'internalIntegrations',
  92. `/organizations/${organization.slug}/sentry-apps/`,
  93. {query: {status: 'internal'}},
  94. ],
  95. ],
  96. });
  97. const api = useApi();
  98. const [token, setToken] = useState(null);
  99. const [integrations, setIntegrations] = useState<SentryApp[]>([]);
  100. const [selectedItem, selectItem] = useState<Pick<Item, 'label' | 'value'> | null>(null);
  101. useEffect(() => {
  102. if (!isLoading && data.internalIntegrations) {
  103. setIntegrations(data.internalIntegrations);
  104. }
  105. }, [isLoading, data.internalIntegrations]);
  106. useEffect(() => {
  107. trackAnalytics('releases.quickstart_viewed', {
  108. organization,
  109. project_id: project.id,
  110. });
  111. // eslint-disable-next-line react-hooks/exhaustive-deps
  112. }, []);
  113. const trackQuickstartCopy = useCallback(() => {
  114. trackAnalytics('releases.quickstart_copied', {
  115. organization,
  116. project_id: project.id,
  117. });
  118. }, [organization, project]);
  119. const trackQuickstartCreatedIntegration = useCallback(
  120. (integration: SentryApp) => {
  121. trackAnalytics('releases.quickstart_create_integration.success', {
  122. organization,
  123. project_id: project.id,
  124. integration_uuid: integration.uuid,
  125. });
  126. },
  127. [organization, project]
  128. );
  129. const trackCreateIntegrationModalClose = useCallback(() => {
  130. trackAnalytics('releases.quickstart_create_integration_modal.close', {
  131. organization,
  132. project_id: project.id,
  133. });
  134. }, [organization, project.id]);
  135. const fetchToken = async sentryAppSlug => {
  136. const tokens = await api.requestPromise(`/sentry-apps/${sentryAppSlug}/api-tokens/`);
  137. if (!tokens.length) {
  138. const newToken = await generateToken(sentryAppSlug);
  139. return setToken(newToken);
  140. }
  141. return setToken(tokens[0].token);
  142. };
  143. const generateToken = async (sentryAppSlug: string) => {
  144. const newToken = await api.requestPromise(
  145. `/sentry-apps/${sentryAppSlug}/api-tokens/`,
  146. {
  147. method: 'POST',
  148. }
  149. );
  150. return newToken.token;
  151. };
  152. const renderIntegrationNode = (integration: SentryApp) => {
  153. return {
  154. value: {slug: integration.slug, name: integration.name},
  155. searchKey: `${integration.name}`,
  156. label: (
  157. <MenuItemWrapper data-test-id="integration-option" key={integration.uuid}>
  158. <Label>{integration.name}</Label>
  159. </MenuItemWrapper>
  160. ),
  161. };
  162. };
  163. const codeChunks = useMemo(
  164. () => [
  165. `# Install the cli
  166. curl -sL https://sentry.io/get-cli/ | bash
  167. # Setup configuration values
  168. SENTRY_AUTH_TOKEN=`,
  169. token && selectedItem
  170. ? `${token} # From internal integration: ${selectedItem.value.name}`
  171. : '<click-here-for-your-token>',
  172. `
  173. SENTRY_ORG=${organization.slug}
  174. SENTRY_PROJECT=${project.slug}
  175. VERSION=\`sentry-cli releases propose-version\`
  176. # Workflow to create releases
  177. sentry-cli releases new "$VERSION"
  178. sentry-cli releases set-commits "$VERSION" --auto
  179. sentry-cli releases finalize "$VERSION"`,
  180. ],
  181. [token, selectedItem, organization.slug, project.slug]
  182. );
  183. return renderComponent(
  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. fetchToken(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. fetchToken(value.slug);
  260. trackQuickstartCreatedIntegration(integration);
  261. },
  262. onCancel: () => {
  263. trackCreateIntegrationModalClose();
  264. },
  265. })
  266. }
  267. >
  268. <MenuItemFooterWrapper>
  269. <IconContainer>
  270. <IconAdd color="activeText" isCircled legacySize="14px" />
  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: 600;
  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;