projectServiceHookDetails.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import {Fragment} from 'react';
  2. import {
  3. addErrorMessage,
  4. addLoadingMessage,
  5. clearIndicators,
  6. } from 'sentry/actionCreators/indicator';
  7. import {Button} from 'sentry/components/button';
  8. import MiniBarChart from 'sentry/components/charts/miniBarChart';
  9. import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  10. import EmptyMessage from 'sentry/components/emptyMessage';
  11. import ErrorBoundary from 'sentry/components/errorBoundary';
  12. import FieldGroup from 'sentry/components/forms/fieldGroup';
  13. import Panel from 'sentry/components/panels/panel';
  14. import PanelAlert from 'sentry/components/panels/panelAlert';
  15. import PanelBody from 'sentry/components/panels/panelBody';
  16. import PanelHeader from 'sentry/components/panels/panelHeader';
  17. import TextCopyInput from 'sentry/components/textCopyInput';
  18. import {t} from 'sentry/locale';
  19. import type {ServiceHook} from 'sentry/types/integrations';
  20. import type {Organization} from 'sentry/types/organization';
  21. import {browserHistory} from 'sentry/utils/browserHistory';
  22. import getDynamicText from 'sentry/utils/getDynamicText';
  23. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  24. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  25. import ServiceHookSettingsForm from 'sentry/views/settings/project/serviceHookSettingsForm';
  26. type Params = {
  27. hookId: string;
  28. projectId: string;
  29. };
  30. type StatsProps = {
  31. organization: Organization;
  32. params: Params;
  33. };
  34. type StatsState = {
  35. stats: {total: number; ts: number}[] | null;
  36. } & DeprecatedAsyncComponent['state'];
  37. class HookStats extends DeprecatedAsyncComponent<StatsProps, StatsState> {
  38. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  39. const until = Math.floor(new Date().getTime() / 1000);
  40. const since = until - 3600 * 24 * 30;
  41. const {organization} = this.props;
  42. const {hookId, projectId} = this.props.params;
  43. return [
  44. [
  45. 'stats',
  46. `/projects/${organization.slug}/${projectId}/hooks/${hookId}/stats/`,
  47. {
  48. query: {
  49. since,
  50. until,
  51. resolution: '1d',
  52. },
  53. },
  54. ],
  55. ];
  56. }
  57. renderBody() {
  58. const {stats} = this.state;
  59. if (stats === null) {
  60. return null;
  61. }
  62. let emptyStats = true;
  63. const series = {
  64. seriesName: t('Events'),
  65. data: stats.map(p => {
  66. if (p.total) {
  67. emptyStats = false;
  68. }
  69. return {
  70. name: p.ts * 1000,
  71. value: p.total,
  72. };
  73. }),
  74. };
  75. return (
  76. <Panel>
  77. <PanelHeader>{t('Events in the last 30 days (by day)')}</PanelHeader>
  78. <PanelBody withPadding>
  79. {!emptyStats ? (
  80. <MiniBarChart
  81. isGroupedByDate
  82. showTimeInTooltip
  83. labelYAxisExtents
  84. series={[series]}
  85. height={150}
  86. />
  87. ) : (
  88. <EmptyMessage
  89. title={t('Nothing recorded in the last 30 days.')}
  90. description={t('Total webhooks fired for this configuration.')}
  91. />
  92. )}
  93. </PanelBody>
  94. </Panel>
  95. );
  96. }
  97. }
  98. type Props = {
  99. organization: Organization;
  100. params: Params;
  101. };
  102. type State = {
  103. hook: ServiceHook | null;
  104. } & DeprecatedAsyncComponent['state'];
  105. export default class ProjectServiceHookDetails extends DeprecatedAsyncComponent<
  106. Props,
  107. State
  108. > {
  109. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  110. const {organization} = this.props;
  111. const {projectId, hookId} = this.props.params;
  112. return [['hook', `/projects/${organization.slug}/${projectId}/hooks/${hookId}/`]];
  113. }
  114. onDelete = () => {
  115. const {organization} = this.props;
  116. const {projectId, hookId} = this.props.params;
  117. addLoadingMessage(t('Saving changes\u2026'));
  118. this.api.request(`/projects/${organization.slug}/${projectId}/hooks/${hookId}/`, {
  119. method: 'DELETE',
  120. success: () => {
  121. clearIndicators();
  122. browserHistory.push(
  123. normalizeUrl(`/settings/${organization.slug}/projects/${projectId}/hooks/`)
  124. );
  125. },
  126. error: () => {
  127. addErrorMessage(t('Unable to remove application. Please try again.'));
  128. },
  129. });
  130. };
  131. renderBody() {
  132. const {organization, params} = this.props;
  133. const {projectId, hookId} = params;
  134. const {hook} = this.state;
  135. if (!hook) {
  136. return null;
  137. }
  138. return (
  139. <Fragment>
  140. <SettingsPageHeader title={t('Service Hook Details')} />
  141. <ErrorBoundary>
  142. <HookStats params={params} organization={organization} />
  143. </ErrorBoundary>
  144. <ServiceHookSettingsForm
  145. organization={organization}
  146. projectId={projectId}
  147. hookId={hookId}
  148. initialData={{
  149. ...hook,
  150. isActive: hook.status !== 'disabled',
  151. }}
  152. />
  153. <Panel>
  154. <PanelHeader>{t('Event Validation')}</PanelHeader>
  155. <PanelBody>
  156. <PanelAlert type="info" showIcon>
  157. Sentry will send the <code>X-ServiceHook-Signature</code> header built using{' '}
  158. <code>HMAC(SHA256, [secret], [payload])</code>. You should always verify
  159. this signature before trusting the information provided in the webhook.
  160. </PanelAlert>
  161. <FieldGroup
  162. label={t('Secret')}
  163. flexibleControlStateSize
  164. inline={false}
  165. help={t('The shared secret used for generating event HMAC signatures.')}
  166. >
  167. <TextCopyInput>
  168. {getDynamicText({
  169. value: hook.secret,
  170. fixed: 'a dynamic secret value',
  171. })}
  172. </TextCopyInput>
  173. </FieldGroup>
  174. </PanelBody>
  175. </Panel>
  176. <Panel>
  177. <PanelHeader>{t('Delete Hook')}</PanelHeader>
  178. <PanelBody>
  179. <FieldGroup
  180. label={t('Delete Hook')}
  181. help={t('Removing this hook is immediate and permanent.')}
  182. >
  183. <div>
  184. <Button priority="danger" onClick={this.onDelete}>
  185. {t('Delete Hook')}
  186. </Button>
  187. </div>
  188. </FieldGroup>
  189. </PanelBody>
  190. </Panel>
  191. </Fragment>
  192. );
  193. }
  194. }