projectReleaseTracking.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  2. import {hasEveryAccess} from 'sentry/components/acl/access';
  3. import {Alert} from 'sentry/components/alert';
  4. import AutoSelectText from 'sentry/components/autoSelectText';
  5. import {Button} from 'sentry/components/button';
  6. import Confirm from 'sentry/components/confirm';
  7. import FieldGroup from 'sentry/components/forms/fieldGroup';
  8. import ExternalLink from 'sentry/components/links/externalLink';
  9. import LoadingError from 'sentry/components/loadingError';
  10. import LoadingIndicator from 'sentry/components/loadingIndicator';
  11. import Panel from 'sentry/components/panels/panel';
  12. import PanelBody from 'sentry/components/panels/panelBody';
  13. import PanelHeader from 'sentry/components/panels/panelHeader';
  14. import PluginList from 'sentry/components/pluginList';
  15. import SentryDocumentTitle from 'sentry/components/sentryDocumentTitle';
  16. import TextCopyInput from 'sentry/components/textCopyInput';
  17. import {t, tct} from 'sentry/locale';
  18. import type {Plugin} from 'sentry/types/integrations';
  19. import type {Organization} from 'sentry/types/organization';
  20. import type {Project} from 'sentry/types/project';
  21. import getDynamicText from 'sentry/utils/getDynamicText';
  22. import {
  23. type ApiQueryKey,
  24. setApiQueryData,
  25. useApiQuery,
  26. useQueryClient,
  27. } from 'sentry/utils/queryClient';
  28. import useApi from 'sentry/utils/useApi';
  29. import {useParams} from 'sentry/utils/useParams';
  30. import withPlugins from 'sentry/utils/withPlugins';
  31. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  32. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  33. type Props = {
  34. organization: Organization;
  35. plugins: {loading: boolean; plugins: Plugin[]};
  36. project: Project;
  37. };
  38. type TokenResponse = {
  39. token: string;
  40. webhookUrl: string;
  41. };
  42. const TOKEN_PLACEHOLDER = 'YOUR_TOKEN';
  43. const WEBHOOK_PLACEHOLDER = 'YOUR_WEBHOOK_URL';
  44. const placeholderData = {
  45. token: TOKEN_PLACEHOLDER,
  46. webhookUrl: WEBHOOK_PLACEHOLDER,
  47. };
  48. function getReleaseTokenQueryKey(
  49. organization: Organization,
  50. projectId: string
  51. ): ApiQueryKey {
  52. return [`/projects/${organization.slug}/${projectId}/releases/token/`];
  53. }
  54. function ProjectReleaseTracking({organization, project, plugins}: Props) {
  55. const api = useApi({persistInFlight: true});
  56. const {projectId} = useParams<{projectId: string}>();
  57. const queryClient = useQueryClient();
  58. const {
  59. data: releaseTokenData = placeholderData,
  60. isFetching,
  61. isError,
  62. error,
  63. } = useApiQuery<TokenResponse>(getReleaseTokenQueryKey(organization, projectId), {
  64. staleTime: 0,
  65. retry: false,
  66. });
  67. const handleRegenerateToken = () => {
  68. api.request(`/projects/${organization.slug}/${projectId}/releases/token/`, {
  69. method: 'POST',
  70. data: {project: projectId},
  71. success: data => {
  72. setApiQueryData<TokenResponse>(
  73. queryClient,
  74. getReleaseTokenQueryKey(organization, projectId),
  75. data
  76. );
  77. addSuccessMessage(
  78. t(
  79. 'Your deploy token has been regenerated. You will need to update any existing deploy hooks.'
  80. )
  81. );
  82. },
  83. error: () => {
  84. addErrorMessage(t('Unable to regenerate deploy token, please try again'));
  85. },
  86. });
  87. };
  88. function getReleaseWebhookIntructions() {
  89. return (
  90. 'curl ' +
  91. releaseTokenData?.webhookUrl +
  92. ' \\' +
  93. '\n ' +
  94. '-X POST \\' +
  95. '\n ' +
  96. "-H 'Content-Type: application/json' \\" +
  97. '\n ' +
  98. '-d \'{"version": "abcdefg"}\''
  99. );
  100. }
  101. if (isError && error?.status !== 403) {
  102. return <LoadingError />;
  103. }
  104. // Using isFetching instead of isPending to avoid showing loading indicator when 403
  105. if (isFetching || plugins.loading) {
  106. return <LoadingIndicator />;
  107. }
  108. const pluginList = plugins.plugins.filter(
  109. (p: Plugin) => p.type === 'release-tracking' && p.hasConfiguration
  110. );
  111. const hasWrite = hasEveryAccess(['project:write'], {organization, project});
  112. return (
  113. <div>
  114. <SentryDocumentTitle title={t('Releases')} projectSlug={project.slug} />
  115. <SettingsPageHeader title={t('Release Tracking')} />
  116. <TextBlock>
  117. {t(
  118. 'Configure release tracking for this project to automatically record new releases of your application.'
  119. )}
  120. </TextBlock>
  121. {!hasWrite && (
  122. <Alert type="warning">
  123. {t(
  124. 'You do not have sufficient permissions to access Release tokens, placeholders are displayed below.'
  125. )}
  126. </Alert>
  127. )}
  128. <Panel>
  129. <PanelHeader>{t('Client Configuration')}</PanelHeader>
  130. <PanelBody withPadding>
  131. <p>
  132. {tct(
  133. 'Start by binding the [release] attribute in your application, take a look at [link] to see how to configure this for the SDK you are using.',
  134. {
  135. link: (
  136. <ExternalLink href="https://docs.sentry.io/platform-redirect/?next=/configuration/releases/">
  137. our docs
  138. </ExternalLink>
  139. ),
  140. release: <code>release</code>,
  141. }
  142. )}
  143. </p>
  144. <p>
  145. {t(
  146. "This will annotate each event with the version of your application, as well as automatically create a release entity in the system the first time it's seen."
  147. )}
  148. </p>
  149. <p>
  150. {t(
  151. 'In addition you may configure a release hook (or use our API) to push a release and include additional metadata with it.'
  152. )}
  153. </p>
  154. </PanelBody>
  155. </Panel>
  156. <Panel>
  157. <PanelHeader>{t('Deploy Token')}</PanelHeader>
  158. <PanelBody>
  159. <FieldGroup
  160. label={t('Token')}
  161. help={t('A unique secret which is used to generate deploy hook URLs')}
  162. >
  163. <TextCopyInput>{releaseTokenData.token}</TextCopyInput>
  164. </FieldGroup>
  165. <FieldGroup
  166. label={t('Regenerate Token')}
  167. help={t(
  168. 'If a service becomes compromised, you should regenerate the token and re-configure any deploy hooks with the newly generated URL.'
  169. )}
  170. >
  171. <div>
  172. <Confirm
  173. disabled={!hasWrite}
  174. priority="danger"
  175. onConfirm={handleRegenerateToken}
  176. message={t(
  177. 'Are you sure you want to regenerate your token? Your current token will no longer be usable.'
  178. )}
  179. >
  180. <Button priority="danger">{t('Regenerate Token')}</Button>
  181. </Confirm>
  182. </div>
  183. </FieldGroup>
  184. </PanelBody>
  185. </Panel>
  186. <Panel>
  187. <PanelHeader>{t('Webhook')}</PanelHeader>
  188. <PanelBody withPadding>
  189. <p>
  190. {t(
  191. 'If you simply want to integrate with an existing system, sometimes its easiest just to use a webhook.'
  192. )}
  193. </p>
  194. <AutoSelectText>
  195. <pre>{releaseTokenData?.webhookUrl}</pre>
  196. </AutoSelectText>
  197. <p>
  198. {t(
  199. 'The release webhook accepts the same parameters as the "Create a new Release" API endpoint.'
  200. )}
  201. </p>
  202. {getDynamicText({
  203. value: (
  204. <AutoSelectText>
  205. <pre>{getReleaseWebhookIntructions()}</pre>
  206. </AutoSelectText>
  207. ),
  208. fixed: (
  209. <pre>
  210. {`curl __WEBHOOK_URL__ \\
  211. -X POST \\
  212. -H 'Content-Type: application/json' \\
  213. -d \'{"version": "abcdefg"}\'`}
  214. </pre>
  215. ),
  216. })}
  217. </PanelBody>
  218. </Panel>
  219. <PluginList organization={organization} project={project} pluginList={pluginList} />
  220. <Panel>
  221. <PanelHeader>{t('API')}</PanelHeader>
  222. <PanelBody withPadding>
  223. <p>
  224. {t(
  225. 'You can notify Sentry when you release new versions of your application via our HTTP API.'
  226. )}
  227. </p>
  228. <p>
  229. {tct('See the [link:releases documentation] for more information.', {
  230. link: <ExternalLink href="https://docs.sentry.io/workflow/releases/" />,
  231. })}
  232. </p>
  233. </PanelBody>
  234. </Panel>
  235. </div>
  236. );
  237. }
  238. export default withPlugins(ProjectReleaseTracking);
  239. // Export for tests
  240. export {ProjectReleaseTracking};