projectServiceHookDetails.tsx 6.2 KB

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