projectReleaseTracking.tsx 8.1 KB

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