projectReleaseTracking.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  21. const TOKEN_PLACEHOLDER = 'YOUR_TOKEN';
  22. const WEBHOOK_PLACEHOLDER = 'YOUR_WEBHOOK_URL';
  23. import {hasEveryAccess} from 'sentry/components/acl/access';
  24. type Props = {
  25. organization: Organization;
  26. plugins: {loading: boolean; plugins: Plugin[]};
  27. project: Project;
  28. } & RouteComponentProps<{projectId: string}, {}>;
  29. type State = {
  30. data: {
  31. token: string;
  32. webhookUrl: string;
  33. } | null;
  34. } & AsyncView['state'];
  35. const placeholderData = {
  36. token: TOKEN_PLACEHOLDER,
  37. webhookUrl: WEBHOOK_PLACEHOLDER,
  38. };
  39. class ProjectReleaseTracking extends AsyncView<Props, State> {
  40. getTitle() {
  41. const {projectId} = this.props.params;
  42. return routeTitleGen(t('Releases'), projectId, false);
  43. }
  44. getEndpoints(): ReturnType<AsyncView['getEndpoints']> {
  45. const {organization} = this.props;
  46. const {projectId} = this.props.params;
  47. // Allow 403s
  48. return [
  49. [
  50. 'data',
  51. `/projects/${organization.slug}/${projectId}/releases/token/`,
  52. {},
  53. {allowError: err => err && err.status === 403},
  54. ],
  55. ];
  56. }
  57. handleRegenerateToken = () => {
  58. const {organization} = this.props;
  59. const {projectId} = this.props.params;
  60. this.api.request(`/projects/${organization.slug}/${projectId}/releases/token/`, {
  61. method: 'POST',
  62. data: {project: projectId},
  63. success: data => {
  64. this.setState({
  65. data: {
  66. token: data.token,
  67. webhookUrl: data.webhookUrl,
  68. },
  69. });
  70. addSuccessMessage(
  71. t(
  72. 'Your deploy token has been regenerated. You will need to update any existing deploy hooks.'
  73. )
  74. );
  75. },
  76. error: () => {
  77. addErrorMessage(t('Unable to regenerate deploy token, please try again'));
  78. },
  79. });
  80. };
  81. getReleaseWebhookIntructions() {
  82. const {webhookUrl} = this.state.data || placeholderData;
  83. return (
  84. 'curl ' +
  85. webhookUrl +
  86. ' \\' +
  87. '\n ' +
  88. '-X POST \\' +
  89. '\n ' +
  90. "-H 'Content-Type: application/json' \\" +
  91. '\n ' +
  92. '-d \'{"version": "abcdefg"}\''
  93. );
  94. }
  95. renderBody() {
  96. const {organization, project, plugins} = this.props;
  97. const hasWrite = hasEveryAccess(['project:write'], {organization, project});
  98. if (plugins.loading) {
  99. return <LoadingIndicator />;
  100. }
  101. const pluginList = plugins.plugins.filter(
  102. (p: Plugin) => p.type === 'release-tracking' && p.hasConfiguration
  103. );
  104. let {token, webhookUrl} = this.state.data || placeholderData;
  105. token = getDynamicText({value: token, fixed: '__TOKEN__'});
  106. webhookUrl = getDynamicText({value: webhookUrl, fixed: '__WEBHOOK_URL__'});
  107. return (
  108. <div>
  109. <SettingsPageHeader title={t('Release Tracking')} />
  110. <TextBlock>
  111. {t(
  112. 'Configure release tracking for this project to automatically record new releases of your application.'
  113. )}
  114. </TextBlock>
  115. {!hasWrite && (
  116. <Alert type="warning">
  117. {t(
  118. 'You do not have sufficient permissions to access Release tokens, placeholders are displayed below.'
  119. )}
  120. </Alert>
  121. )}
  122. <Panel>
  123. <PanelHeader>{t('Client Configuration')}</PanelHeader>
  124. <PanelBody withPadding>
  125. <p>
  126. {tct(
  127. '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.',
  128. {
  129. link: (
  130. <ExternalLink href="https://docs.sentry.io/platform-redirect/?next=/configuration/releases/">
  131. our docs
  132. </ExternalLink>
  133. ),
  134. release: <code>release</code>,
  135. }
  136. )}
  137. </p>
  138. <p>
  139. {t(
  140. "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."
  141. )}
  142. </p>
  143. <p>
  144. {t(
  145. 'In addition you may configure a release hook (or use our API) to push a release and include additional metadata with it.'
  146. )}
  147. </p>
  148. </PanelBody>
  149. </Panel>
  150. <Panel>
  151. <PanelHeader>{t('Deploy Token')}</PanelHeader>
  152. <PanelBody>
  153. <FieldGroup
  154. label={t('Token')}
  155. help={t('A unique secret which is used to generate deploy hook URLs')}
  156. >
  157. <TextCopyInput>{token}</TextCopyInput>
  158. </FieldGroup>
  159. <FieldGroup
  160. label={t('Regenerate Token')}
  161. help={t(
  162. 'If a service becomes compromised, you should regenerate the token and re-configure any deploy hooks with the newly generated URL.'
  163. )}
  164. >
  165. <div>
  166. <Confirm
  167. disabled={!hasWrite}
  168. priority="danger"
  169. onConfirm={this.handleRegenerateToken}
  170. message={t(
  171. 'Are you sure you want to regenerate your token? Your current token will no longer be usable.'
  172. )}
  173. >
  174. <Button priority="danger">{t('Regenerate Token')}</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};