sentryFunctionDetails.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import {useEffect, useState} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import Editor from '@monaco-editor/react';
  4. import {
  5. addErrorMessage,
  6. addLoadingMessage,
  7. addSuccessMessage,
  8. } from 'sentry/actionCreators/indicator';
  9. import Feature from 'sentry/components/acl/feature';
  10. import AsyncComponent from 'sentry/components/asyncComponent';
  11. import Form from 'sentry/components/forms/form';
  12. import JsonForm from 'sentry/components/forms/jsonForm';
  13. import FormModel from 'sentry/components/forms/model';
  14. import {Field} from 'sentry/components/forms/types';
  15. import {Panel, PanelBody, PanelHeader} from 'sentry/components/panels';
  16. import {t} from 'sentry/locale';
  17. import {SentryFunction} from 'sentry/types';
  18. import SentryFunctionEnvironmentVariables from './sentryFunctionsEnvironmentVariables';
  19. import SentryFunctionSubscriptions from './sentryFunctionSubscriptions';
  20. function transformData(data: Record<string, any>) {
  21. const events: string[] = [];
  22. if (data.onIssue) {
  23. events.push('issue');
  24. }
  25. if (data.onError) {
  26. events.push('error');
  27. }
  28. if (data.onComment) {
  29. events.push('comment');
  30. }
  31. delete data.onIssue;
  32. delete data.onError;
  33. delete data.onComment;
  34. data.events = events;
  35. const envVariables: EnvVariable[] = [];
  36. let i = 0;
  37. while (data[`env-variable-name-${i}`]) {
  38. if (data[`env-variable-value-${i}`]) {
  39. envVariables.push({
  40. name: data[`env-variable-name-${i}`],
  41. value: data[`env-variable-value-${i}`],
  42. });
  43. }
  44. delete data[`env-variable-name-${i}`];
  45. delete data[`env-variable-value-${i}`];
  46. i++;
  47. }
  48. data.envVariables = envVariables;
  49. const {...output} = data;
  50. return output;
  51. }
  52. type Props = {
  53. sentryFunction?: SentryFunction;
  54. } & WrapperProps;
  55. type EnvVariable = {
  56. name: string;
  57. value: string;
  58. };
  59. const formFields: Field[] = [
  60. {
  61. name: 'name',
  62. type: 'string',
  63. required: true,
  64. placeholder: 'e.g. My Sentry Function',
  65. label: 'Name',
  66. help: 'Human readable name of your Sentry Function',
  67. },
  68. {
  69. name: 'author',
  70. type: 'string',
  71. placeholder: 'e.g. Acme Software',
  72. label: 'Author',
  73. help: 'The company or person who built and maintains this Sentry Function.',
  74. },
  75. {
  76. name: 'overview',
  77. type: 'string',
  78. placeholder: 'e.g. This Sentry Function does something useful',
  79. label: 'Overview',
  80. help: 'A short description of your Sentry Function.',
  81. },
  82. ];
  83. function SentryFunctionDetails(props: Props) {
  84. const [form] = useState(() => new FormModel({transformData}));
  85. const {orgId, functionSlug} = props.params;
  86. const {sentryFunction} = props;
  87. const method = functionSlug ? 'PUT' : 'POST';
  88. let endpoint = `/organizations/${orgId}/functions/`;
  89. if (functionSlug) {
  90. endpoint += `${functionSlug}/`;
  91. }
  92. const defaultCode = sentryFunction
  93. ? sentryFunction.code
  94. : `exports.yourFunction = (req, res) => {
  95. let message = req.query.message || req.body.message || 'Hello World!';
  96. console.log('Query: ' + req.query);
  97. console.log('Body: ' + req.body);
  98. res.status(200).send(message);
  99. };`;
  100. const [events, setEvents] = useState(sentryFunction?.events || []);
  101. useEffect(() => {
  102. form.setValue('onIssue', events.includes('issue'));
  103. form.setValue('onError', events.includes('error'));
  104. form.setValue('onComment', events.includes('comment'));
  105. }, [form, events]);
  106. const [envVariables, setEnvVariables] = useState(
  107. sentryFunction?.env_variables?.length
  108. ? sentryFunction?.env_variables
  109. : [{name: '', value: ''}]
  110. );
  111. const handleSubmitError = err => {
  112. let errorMessage = t('Unknown Error');
  113. if (err.status >= 400 && err.status < 500) {
  114. errorMessage = err?.responseJSON.detail ?? errorMessage;
  115. }
  116. addErrorMessage(errorMessage);
  117. };
  118. const handleSubmitSuccess = data => {
  119. addSuccessMessage(t('Sentry Function successfully saved.', data.name));
  120. const baseUrl = `/settings/${orgId}/developer-settings/sentry-functions/`;
  121. const url = `${baseUrl}${data.slug}/`;
  122. if (sentryFunction) {
  123. addSuccessMessage(t('%s successfully saved.', data.name));
  124. } else {
  125. addSuccessMessage(t('%s successfully created.', data.name));
  126. }
  127. browserHistory.push(url);
  128. };
  129. function handleEditorChange(value, _event) {
  130. form.setValue('code', value);
  131. }
  132. return (
  133. <div>
  134. <Feature features={['organizations:sentry-functions']}>
  135. <h2>
  136. {sentryFunction ? t('Editing Sentry Function') : t('Create Sentry Function')}
  137. </h2>
  138. <Form
  139. apiMethod={method}
  140. apiEndpoint={endpoint}
  141. model={form}
  142. onPreSubmit={() => {
  143. addLoadingMessage(t('Saving changes..'));
  144. }}
  145. initialData={{
  146. code: defaultCode,
  147. events,
  148. envVariables,
  149. ...props.sentryFunction,
  150. }}
  151. onSubmitError={handleSubmitError}
  152. onSubmitSuccess={handleSubmitSuccess}
  153. >
  154. <JsonForm forms={[{title: t('Sentry Function Details'), fields: formFields}]} />
  155. <Panel>
  156. <PanelHeader>{t('Webhooks')}</PanelHeader>
  157. <PanelBody>
  158. <SentryFunctionSubscriptions events={events} setEvents={setEvents} />
  159. </PanelBody>
  160. </Panel>
  161. <Panel>
  162. <SentryFunctionEnvironmentVariables
  163. envVariables={envVariables}
  164. setEnvVariables={setEnvVariables}
  165. />
  166. </Panel>
  167. <Panel>
  168. <PanelHeader>{t('Write your Code Below')}</PanelHeader>
  169. <PanelBody>
  170. <Editor
  171. height="40vh"
  172. theme="light"
  173. defaultLanguage="javascript"
  174. defaultValue={defaultCode}
  175. onChange={handleEditorChange}
  176. options={{
  177. minimap: {
  178. enabled: false,
  179. },
  180. scrollBeyondLastLine: false,
  181. }}
  182. />
  183. </PanelBody>
  184. </Panel>
  185. </Form>
  186. </Feature>
  187. </div>
  188. );
  189. }
  190. type WrapperState = {
  191. sentryFunction?: SentryFunction;
  192. } & AsyncComponent['state'];
  193. type WrapperProps = {
  194. params: {orgId: string; functionSlug?: string};
  195. } & AsyncComponent['props'];
  196. class SentryFunctionsWrapper extends AsyncComponent<WrapperProps, WrapperState> {
  197. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  198. const {functionSlug, orgId} = this.props.params;
  199. if (functionSlug) {
  200. return [['sentryFunction', `/organizations/${orgId}/functions/${functionSlug}/`]];
  201. }
  202. return [];
  203. }
  204. renderBody() {
  205. return (
  206. <SentryFunctionDetails sentryFunction={this.state.sentryFunction} {...this.props} />
  207. );
  208. }
  209. }
  210. export default SentryFunctionsWrapper;