projectServiceHookDetails.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import {Fragment, useState} 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 EmptyMessage from 'sentry/components/emptyMessage';
  10. import ErrorBoundary from 'sentry/components/errorBoundary';
  11. import FieldGroup from 'sentry/components/forms/fieldGroup';
  12. import LoadingError from 'sentry/components/loadingError';
  13. import LoadingIndicator from 'sentry/components/loadingIndicator';
  14. import Panel from 'sentry/components/panels/panel';
  15. import PanelAlert from 'sentry/components/panels/panelAlert';
  16. import PanelBody from 'sentry/components/panels/panelBody';
  17. import PanelHeader from 'sentry/components/panels/panelHeader';
  18. import TextCopyInput from 'sentry/components/textCopyInput';
  19. import {t} from 'sentry/locale';
  20. import type {ServiceHook} from 'sentry/types/integrations';
  21. import {useApiQuery, useMutation} from 'sentry/utils/queryClient';
  22. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  23. import useApi from 'sentry/utils/useApi';
  24. import {useNavigate} from 'sentry/utils/useNavigate';
  25. import useOrganization from 'sentry/utils/useOrganization';
  26. import {useParams} from 'sentry/utils/useParams';
  27. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  28. import ServiceHookSettingsForm from 'sentry/views/settings/project/serviceHookSettingsForm';
  29. function HookStats() {
  30. const organization = useOrganization();
  31. const {hookId, projectId} = useParams<{hookId: string; projectId: string}>();
  32. const [until] = useState(() => Math.floor(new Date().getTime() / 1000));
  33. const since = until - 3600 * 24 * 30;
  34. const {
  35. data: stats,
  36. isPending,
  37. isError,
  38. refetch,
  39. } = useApiQuery<Array<{total: number; ts: number}>>(
  40. [
  41. `/projects/${organization.slug}/${projectId}/hooks/${hookId}/stats/`,
  42. {
  43. query: {
  44. since,
  45. until,
  46. resolution: '1d',
  47. },
  48. },
  49. ],
  50. {staleTime: 0}
  51. );
  52. if (isPending) {
  53. return <LoadingIndicator />;
  54. }
  55. if (isError) {
  56. return <LoadingError onRetry={refetch} />;
  57. }
  58. if (stats === null) {
  59. return null;
  60. }
  61. let emptyStats = true;
  62. const series = {
  63. seriesName: t('Events'),
  64. data: stats.map(p => {
  65. if (p.total) {
  66. emptyStats = false;
  67. }
  68. return {
  69. name: p.ts * 1000,
  70. value: p.total,
  71. };
  72. }),
  73. };
  74. return (
  75. <Panel>
  76. <PanelHeader>{t('Events in the last 30 days (by day)')}</PanelHeader>
  77. <PanelBody withPadding>
  78. {!emptyStats ? (
  79. <MiniBarChart
  80. isGroupedByDate
  81. showTimeInTooltip
  82. labelYAxisExtents
  83. series={[series]}
  84. height={150}
  85. />
  86. ) : (
  87. <EmptyMessage
  88. title={t('Nothing recorded in the last 30 days.')}
  89. description={t('Total webhooks fired for this configuration.')}
  90. />
  91. )}
  92. </PanelBody>
  93. </Panel>
  94. );
  95. }
  96. export default function ProjectServiceHookDetails() {
  97. const organization = useOrganization();
  98. const {hookId, projectId} = useParams<{hookId: string; projectId: string}>();
  99. const api = useApi({persistInFlight: true});
  100. const navigate = useNavigate();
  101. const {
  102. data: hook,
  103. isPending,
  104. isError,
  105. refetch,
  106. } = useApiQuery<ServiceHook>(
  107. [`/projects/${organization.slug}/${projectId}/hooks/${hookId}/`],
  108. {staleTime: 0}
  109. );
  110. const deleteMutation = useMutation({
  111. mutationFn: () => {
  112. return api.requestPromise(
  113. `/projects/${organization.slug}/${projectId}/hooks/${hookId}/`,
  114. {
  115. method: 'DELETE',
  116. }
  117. );
  118. },
  119. onMutate: () => {
  120. addLoadingMessage(t('Saving changes\u2026'));
  121. },
  122. onSuccess: () => {
  123. clearIndicators();
  124. navigate(
  125. normalizeUrl(`/settings/${organization.slug}/projects/${projectId}/hooks/`)
  126. );
  127. },
  128. onError: () => {
  129. addErrorMessage(t('Unable to remove application. Please try again.'));
  130. },
  131. });
  132. if (isPending) {
  133. return <LoadingIndicator />;
  134. }
  135. if (isError) {
  136. return <LoadingError onRetry={refetch} />;
  137. }
  138. if (!hook) {
  139. return null;
  140. }
  141. return (
  142. <Fragment>
  143. <SettingsPageHeader title={t('Service Hook Details')} />
  144. <ErrorBoundary>
  145. <HookStats />
  146. </ErrorBoundary>
  147. <ServiceHookSettingsForm
  148. organization={organization}
  149. projectId={projectId}
  150. hookId={hookId}
  151. initialData={{
  152. ...hook,
  153. isActive: hook.status !== 'disabled',
  154. }}
  155. />
  156. <Panel>
  157. <PanelHeader>{t('Event Validation')}</PanelHeader>
  158. <PanelBody>
  159. <PanelAlert type="info" showIcon>
  160. Sentry will send the <code>X-ServiceHook-Signature</code> header built using{' '}
  161. <code>HMAC(SHA256, [secret], [payload])</code>. You should always verify this
  162. signature before trusting the information provided in the webhook.
  163. </PanelAlert>
  164. <FieldGroup
  165. label={t('Secret')}
  166. flexibleControlStateSize
  167. inline={false}
  168. help={t('The shared secret used for generating event HMAC signatures.')}
  169. >
  170. <TextCopyInput>{hook.secret}</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={() => deleteMutation.mutate()}>
  183. {t('Delete Hook')}
  184. </Button>
  185. </div>
  186. </FieldGroup>
  187. </PanelBody>
  188. </Panel>
  189. </Fragment>
  190. );
  191. }