projectServiceHookDetails.tsx 5.8 KB

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