projectServiceHookDetails.tsx 5.9 KB

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