projectReleaseTracking.tsx 8.2 KB

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