repositoryProjectPathConfigForm.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import pick from 'lodash/pick';
  2. import {FieldFromConfig} from 'sentry/components/forms';
  3. import Form, {FormProps} from 'sentry/components/forms/form';
  4. import {Field} from 'sentry/components/forms/types';
  5. import {t} from 'sentry/locale';
  6. import {
  7. Integration,
  8. Organization,
  9. Project,
  10. Repository,
  11. RepositoryProjectPathConfig,
  12. } from 'sentry/types';
  13. import {
  14. sentryNameToOption,
  15. trackIntegrationAnalytics,
  16. } from 'sentry/utils/integrationUtil';
  17. type Props = {
  18. integration: Integration;
  19. onCancel: FormProps['onCancel'];
  20. onSubmitSuccess: FormProps['onSubmitSuccess'];
  21. organization: Organization;
  22. projects: Project[];
  23. repos: Repository[];
  24. existingConfig?: RepositoryProjectPathConfig;
  25. };
  26. function RepositoryProjectPathConfigForm({
  27. existingConfig,
  28. integration,
  29. onCancel,
  30. onSubmitSuccess,
  31. organization,
  32. projects,
  33. repos,
  34. }: Props) {
  35. const orgSlug = organization.slug;
  36. const repoChoices = repos.map(({name, id}) => ({value: id, label: name}));
  37. const formFields: Field[] = [
  38. {
  39. name: 'projectId',
  40. type: 'sentry_project_selector',
  41. required: true,
  42. label: t('Project'),
  43. projects,
  44. },
  45. {
  46. name: 'repositoryId',
  47. type: 'select_async',
  48. required: true,
  49. label: t('Repo'),
  50. placeholder: t('Choose repo'),
  51. url: `/organizations/${orgSlug}/repos/`,
  52. defaultOptions: repoChoices,
  53. onResults: results => results.map(sentryNameToOption),
  54. },
  55. {
  56. name: 'defaultBranch',
  57. type: 'string',
  58. required: true,
  59. label: t('Branch'),
  60. placeholder: t('Type your branch'),
  61. showHelpInTooltip: true,
  62. help: t(
  63. 'If an event does not have a release tied to a commit, we will use this branch when linking to your source code.'
  64. ),
  65. },
  66. {
  67. name: 'stackRoot',
  68. type: 'string',
  69. required: false,
  70. label: t('Stack Trace Root'),
  71. placeholder: t('Type root path of your stack traces'),
  72. showHelpInTooltip: true,
  73. help: t(
  74. 'Any stack trace starting with this path will be mapped with this rule. An empty string will match all paths.'
  75. ),
  76. },
  77. {
  78. name: 'sourceRoot',
  79. type: 'string',
  80. required: false,
  81. label: t('Source Code Root'),
  82. placeholder: t('Type root path of your source code, e.g. `src/`.'),
  83. showHelpInTooltip: true,
  84. help: t(
  85. '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.'
  86. ),
  87. },
  88. ];
  89. function handlePreSubmit() {
  90. trackIntegrationAnalytics('integrations.stacktrace_submit_config', {
  91. setup_type: 'manual',
  92. view: 'integration_configuration_detail',
  93. provider: integration.provider.key,
  94. organization,
  95. });
  96. }
  97. const initialData = {
  98. defaultBranch: 'master',
  99. stackRoot: '',
  100. sourceRoot: '',
  101. repositoryId: existingConfig?.repoId,
  102. integrationId: integration.id,
  103. ...pick(existingConfig, ['projectId', 'defaultBranch', 'stackRoot', 'sourceRoot']),
  104. };
  105. // endpoint changes if we are making a new row or updating an existing one
  106. const baseEndpoint = `/organizations/${organization.slug}/code-mappings/`;
  107. const endpoint = existingConfig ? `${baseEndpoint}${existingConfig.id}/` : baseEndpoint;
  108. const apiMethod = existingConfig ? 'PUT' : 'POST';
  109. return (
  110. <Form
  111. onSubmitSuccess={onSubmitSuccess}
  112. onPreSubmit={handlePreSubmit}
  113. initialData={initialData}
  114. apiEndpoint={endpoint}
  115. apiMethod={apiMethod}
  116. onCancel={onCancel}
  117. >
  118. {formFields.map(field => (
  119. <FieldFromConfig
  120. key={field.name}
  121. field={field}
  122. inline={false}
  123. stacked
  124. flexibleControlStateSize
  125. />
  126. ))}
  127. </Form>
  128. );
  129. }
  130. export default RepositoryProjectPathConfigForm;