projectReleaseTracking.tsx 8.6 KB

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