autofixSetupModal.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import {Fragment, useEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import {
  5. type AutofixSetupRepoDefinition,
  6. type AutofixSetupResponse,
  7. useAutofixSetup,
  8. } from 'sentry/components/events/autofix/useAutofixSetup';
  9. import {GuidedSteps} from 'sentry/components/guidedSteps/guidedSteps';
  10. import ExternalLink from 'sentry/components/links/externalLink';
  11. import LoadingError from 'sentry/components/loadingError';
  12. import LoadingIndicator from 'sentry/components/loadingIndicator';
  13. import {IconCheckmark, IconGithub} from 'sentry/icons';
  14. import {t, tct} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import type {Integration} from 'sentry/types/integrations';
  17. import {trackAnalytics} from 'sentry/utils/analytics';
  18. import {useApiQuery} from 'sentry/utils/queryClient';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. function AutofixIntegrationStep({autofixSetup}: {autofixSetup: AutofixSetupResponse}) {
  21. if (autofixSetup.integration.ok) {
  22. return (
  23. <Fragment>
  24. {tct('The GitHub integration is already installed, [link: view in settings].', {
  25. link: <ExternalLink href={`/settings/integrations/github/`} />,
  26. })}
  27. <GuidedSteps.ButtonWrapper>
  28. <GuidedSteps.NextButton />
  29. </GuidedSteps.ButtonWrapper>
  30. </Fragment>
  31. );
  32. }
  33. if (autofixSetup.integration.reason === 'integration_inactive') {
  34. return (
  35. <Fragment>
  36. <p>
  37. {tct(
  38. 'The GitHub integration has been installed but is not active. Navigate to the [integration settings page] and enable it to continue.',
  39. {
  40. link: <ExternalLink href={`/settings/integrations/github/`} />,
  41. }
  42. )}
  43. </p>
  44. <p>
  45. {tct(
  46. 'Once enabled, come back to this page. For more information related to installing the GitHub integration, read the [link:documentation].',
  47. {
  48. link: (
  49. <ExternalLink href="https://docs.sentry.io/product/integrations/source-code-mgmt/github/" />
  50. ),
  51. }
  52. )}
  53. </p>
  54. <GuidedSteps.ButtonWrapper>
  55. <ExternalLink href="/settings/integrations/github/">
  56. <Button size="sm" priority="primary">
  57. {t('Set up Integration')}
  58. </Button>
  59. </ExternalLink>
  60. <GuidedSteps.NextButton />
  61. </GuidedSteps.ButtonWrapper>
  62. </Fragment>
  63. );
  64. }
  65. return (
  66. <Fragment>
  67. <p>
  68. {t(
  69. 'Install the GitHub integration on the integration settings page and clicking the "Install" button. Follow the steps provided.'
  70. )}
  71. </p>
  72. <p>
  73. {tct(
  74. 'Once installed, come back to this page. For more information related to installing the GitHub integration, read the [link:documentation].',
  75. {
  76. link: (
  77. <ExternalLink href="https://docs.sentry.io/product/integrations/source-code-mgmt/github/" />
  78. ),
  79. }
  80. )}
  81. </p>
  82. <GuidedSteps.ButtonWrapper>
  83. <ExternalLink href="/settings/integrations/github/">
  84. <Button size="sm" priority="primary">
  85. {t('Set up Integration')}
  86. </Button>
  87. </ExternalLink>
  88. <GuidedSteps.NextButton />
  89. </GuidedSteps.ButtonWrapper>
  90. </Fragment>
  91. );
  92. }
  93. function AutofixCodeMappingStep() {
  94. const organization = useOrganization();
  95. const {data: integrationConfigurations} = useApiQuery<Integration[]>(
  96. [
  97. `/organizations/${organization.slug}/integrations/?provider_key=github&includeConfig=0`,
  98. ],
  99. {
  100. staleTime: Infinity,
  101. }
  102. );
  103. const configurationId = integrationConfigurations?.at(0)?.id;
  104. const url = `/settings/integrations/github/${configurationId ? configurationId + '/?tab=codeMappings' : ''}`;
  105. return (
  106. <Fragment>
  107. <p>
  108. {t(
  109. 'Set up code mappings for the Github repositories you want to run Autofix on for this project.'
  110. )}
  111. </p>
  112. <GuidedSteps.ButtonWrapper>
  113. <GuidedSteps.BackButton />
  114. <ExternalLink href={url}>
  115. <Button size="sm" priority="primary">
  116. {configurationId ? t('Configure Code Mappings') : t('Configure Integration')}
  117. </Button>
  118. </ExternalLink>
  119. </GuidedSteps.ButtonWrapper>
  120. </Fragment>
  121. );
  122. }
  123. export function GitRepoLink({repo}: {repo: AutofixSetupRepoDefinition}) {
  124. if (repo.provider === 'github' || repo.provider.split(':')[1] === 'github') {
  125. return (
  126. <RepoLinkItem>
  127. <GithubLink>
  128. <IconGithub size="sm" />
  129. <span>
  130. {repo.owner}/{repo.name}
  131. </span>
  132. </GithubLink>
  133. {repo.ok ? <IconCheckmark color="success" isCircled /> : null}
  134. </RepoLinkItem>
  135. );
  136. }
  137. return (
  138. <li>
  139. {repo.owner}/{repo.name}
  140. </li>
  141. );
  142. }
  143. function AutofixSetupSteps({autofixSetup}: {autofixSetup: AutofixSetupResponse}) {
  144. return (
  145. <GuidedSteps>
  146. <GuidedSteps.Step
  147. stepKey="integration"
  148. title={t('Install the GitHub Integration')}
  149. isCompleted={
  150. autofixSetup.integration.ok ||
  151. autofixSetup.integration.reason === 'integration_no_code_mappings'
  152. }
  153. >
  154. <AutofixIntegrationStep autofixSetup={autofixSetup} />
  155. </GuidedSteps.Step>
  156. <GuidedSteps.Step
  157. stepKey="codeMappings"
  158. title={t('Set up Code Mappings')}
  159. isCompleted={autofixSetup.integration.ok}
  160. >
  161. <AutofixCodeMappingStep />
  162. </GuidedSteps.Step>
  163. </GuidedSteps>
  164. );
  165. }
  166. export function AutofixSetupContent({
  167. projectId,
  168. groupId,
  169. }: {
  170. groupId: string;
  171. projectId: string;
  172. }) {
  173. const organization = useOrganization();
  174. const {data, isPending, isError} = useAutofixSetup(
  175. {groupId},
  176. // Want to check setup status whenever the user comes back to the tab
  177. {refetchOnWindowFocus: true}
  178. );
  179. useEffect(() => {
  180. if (!data) {
  181. return;
  182. }
  183. trackAnalytics('autofix.setup_modal_viewed', {
  184. groupId,
  185. projectId,
  186. organization,
  187. setup_gen_ai_consent: data.genAIConsent.ok,
  188. setup_integration: data.integration.ok,
  189. setup_write_integration: data.githubWriteIntegration.ok,
  190. });
  191. }, [data, groupId, organization, projectId]);
  192. if (isPending) {
  193. return <LoadingIndicator />;
  194. }
  195. if (isError) {
  196. return <LoadingError message={t('Failed to fetch Autofix setup progress.')} />;
  197. }
  198. return (
  199. <Fragment>
  200. <Divider />
  201. <Header>Set up Autofix</Header>
  202. <p>
  203. Sentry's AI-enabled Autofix uses all of the contextual data surrounding this error
  204. to work with you to find the root cause and create a fix.
  205. </p>
  206. <p>To use Autofix, please follow the instructions below.</p>
  207. <AutofixSetupSteps autofixSetup={data} />
  208. </Fragment>
  209. );
  210. }
  211. export const AutofixSetupDone = styled('div')`
  212. position: relative;
  213. display: flex;
  214. align-items: center;
  215. justify-content: center;
  216. flex-direction: column;
  217. padding: 40px;
  218. font-size: ${p => p.theme.fontSizeLarge};
  219. `;
  220. const Header = styled('p')`
  221. font-size: ${p => p.theme.fontSizeLarge};
  222. font-weight: ${p => p.theme.fontWeightBold};
  223. margin-bottom: ${space(2)};
  224. margin-top: ${space(2)};
  225. `;
  226. const RepoLinkItem = styled('li')`
  227. display: flex;
  228. align-items: center;
  229. gap: ${space(0.5)};
  230. `;
  231. const GithubLink = styled('div')`
  232. display: flex;
  233. align-items: center;
  234. gap: ${space(0.5)};
  235. `;
  236. const Divider = styled('div')`
  237. margin: ${space(3)} 0;
  238. border-bottom: 2px solid ${p => p.theme.gray100};
  239. `;