repositoryProjectPathConfigForm.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import {useRef} from 'react';
  2. import pick from 'lodash/pick';
  3. import FieldFromConfig from 'sentry/components/forms/fieldFromConfig';
  4. import Form, {FormProps} from 'sentry/components/forms/form';
  5. import FormModel from 'sentry/components/forms/model';
  6. import {Field} from 'sentry/components/forms/types';
  7. import {t} from 'sentry/locale';
  8. import type {
  9. Integration,
  10. IntegrationRepository,
  11. Organization,
  12. Project,
  13. Repository,
  14. RepositoryProjectPathConfig,
  15. } from 'sentry/types';
  16. import {trackAnalytics} from 'sentry/utils/analytics';
  17. import {sentryNameToOption} from 'sentry/utils/integrationUtil';
  18. import useApi from 'sentry/utils/useApi';
  19. type Props = {
  20. integration: Integration;
  21. onCancel: FormProps['onCancel'];
  22. onSubmitSuccess: FormProps['onSubmitSuccess'];
  23. organization: Organization;
  24. projects: Project[];
  25. repos: Repository[];
  26. existingConfig?: RepositoryProjectPathConfig;
  27. };
  28. function RepositoryProjectPathConfigForm({
  29. existingConfig,
  30. integration,
  31. onCancel,
  32. onSubmitSuccess,
  33. organization,
  34. projects,
  35. repos,
  36. }: Props) {
  37. const api = useApi();
  38. const formRef = useRef(new FormModel());
  39. const repoChoices = repos.map(({name, id}) => ({value: id, label: name}));
  40. /**
  41. * Automatically switch to the default branch for the repo
  42. */
  43. function handleRepoChange(id: string) {
  44. const repo = repos.find(r => r.id === id);
  45. if (!repo) {
  46. return;
  47. }
  48. // Use the integration repo search to get the default branch
  49. api
  50. .requestPromise(
  51. `/organizations/${organization.slug}/integrations/${integration.id}/repos/`,
  52. {query: {search: repo.name}}
  53. )
  54. .then((data: {repos: IntegrationRepository[]}) => {
  55. const {defaultBranch} = data.repos.find(r => r.identifier === repo.name) ?? {};
  56. const isCurrentRepo = formRef.current.getValue('repositoryId') === repo.id;
  57. if (defaultBranch && isCurrentRepo) {
  58. formRef.current.setValue('defaultBranch', defaultBranch);
  59. }
  60. });
  61. }
  62. const formFields: Field[] = [
  63. {
  64. name: 'projectId',
  65. type: 'sentry_project_selector',
  66. required: true,
  67. label: t('Project'),
  68. projects,
  69. },
  70. {
  71. name: 'repositoryId',
  72. type: 'select_async',
  73. required: true,
  74. label: t('Repo'),
  75. placeholder: t('Choose repo'),
  76. url: `/organizations/${organization.slug}/repos/`,
  77. defaultOptions: repoChoices,
  78. onResults: results => results.map(sentryNameToOption),
  79. onChange: handleRepoChange,
  80. },
  81. {
  82. name: 'defaultBranch',
  83. type: 'string',
  84. required: true,
  85. label: t('Branch'),
  86. placeholder: t('Type your branch'),
  87. showHelpInTooltip: true,
  88. help: t(
  89. 'If an event does not have a release tied to a commit, we will use this branch when linking to your source code.'
  90. ),
  91. },
  92. {
  93. name: 'stackRoot',
  94. type: 'string',
  95. required: false,
  96. label: t('Stack Trace Root'),
  97. placeholder: t('Type root path of your stack traces'),
  98. showHelpInTooltip: true,
  99. help: t(
  100. 'Any stack trace starting with this path will be mapped with this rule. An empty string will match all paths.'
  101. ),
  102. },
  103. {
  104. name: 'sourceRoot',
  105. type: 'string',
  106. required: false,
  107. label: t('Source Code Root'),
  108. placeholder: t('Type root path of your source code, e.g. `src/`.'),
  109. showHelpInTooltip: true,
  110. help: t(
  111. 'When a rule matches, the stack trace root is replaced with this path to get the path in your repository. Leaving this empty means replacing the stack trace root with an empty string.'
  112. ),
  113. },
  114. ];
  115. function handlePreSubmit() {
  116. trackAnalytics('integrations.stacktrace_submit_config', {
  117. setup_type: 'manual',
  118. view: 'integration_configuration_detail',
  119. provider: integration.provider.key,
  120. organization,
  121. });
  122. }
  123. const initialData = {
  124. defaultBranch: 'master',
  125. stackRoot: '',
  126. sourceRoot: '',
  127. repositoryId: existingConfig?.repoId,
  128. integrationId: integration.id,
  129. ...pick(existingConfig, ['projectId', 'defaultBranch', 'stackRoot', 'sourceRoot']),
  130. };
  131. // endpoint changes if we are making a new row or updating an existing one
  132. const baseEndpoint = `/organizations/${organization.slug}/code-mappings/`;
  133. const endpoint = existingConfig ? `${baseEndpoint}${existingConfig.id}/` : baseEndpoint;
  134. const apiMethod = existingConfig ? 'PUT' : 'POST';
  135. return (
  136. <Form
  137. onSubmitSuccess={onSubmitSuccess}
  138. onPreSubmit={handlePreSubmit}
  139. initialData={initialData}
  140. apiEndpoint={endpoint}
  141. apiMethod={apiMethod}
  142. model={formRef.current}
  143. onCancel={onCancel}
  144. >
  145. {formFields.map(field => (
  146. <FieldFromConfig
  147. key={field.name}
  148. field={field}
  149. inline={false}
  150. stacked
  151. flexibleControlStateSize
  152. />
  153. ))}
  154. </Form>
  155. );
  156. }
  157. export default RepositoryProjectPathConfigForm;