releasesPromo.tsx 13 KB

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