integrationExternalTeamMappings.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import {Fragment} from 'react';
  2. import type {WithRouterProps} from 'react-router';
  3. import uniqBy from 'lodash/uniqBy';
  4. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  5. import {openModal} from 'sentry/actionCreators/modal';
  6. import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  7. import {t} from 'sentry/locale';
  8. import type {
  9. ExternalActorMapping,
  10. ExternalActorMappingOrSuggestion,
  11. Integration,
  12. } from 'sentry/types/integrations';
  13. import type {Organization, Team} from 'sentry/types/organization';
  14. import {sentryNameToOption} from 'sentry/utils/integrationUtil';
  15. import withOrganization from 'sentry/utils/withOrganization';
  16. // eslint-disable-next-line no-restricted-imports
  17. import withSentryRouter from 'sentry/utils/withSentryRouter';
  18. import IntegrationExternalMappingForm from './integrationExternalMappingForm';
  19. import IntegrationExternalMappings from './integrationExternalMappings';
  20. type Props = DeprecatedAsyncComponent['props'] &
  21. WithRouterProps & {
  22. integration: Integration;
  23. organization: Organization;
  24. };
  25. type State = DeprecatedAsyncComponent['state'] & {
  26. initialResults: Team[];
  27. queryResults: {
  28. // For inline forms, the mappingKey will be the external name (since multiple will be rendered at one time)
  29. // For the modal form, the mappingKey will be this.modalMappingKey (since only one modal form is rendered at any time)
  30. [mappingKey: string]: Team[];
  31. };
  32. teams: Team[];
  33. };
  34. class IntegrationExternalTeamMappings extends DeprecatedAsyncComponent<Props, State> {
  35. getDefaultState() {
  36. return {
  37. ...super.getDefaultState(),
  38. teams: [],
  39. initialResults: [],
  40. queryResults: {},
  41. };
  42. }
  43. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  44. const {organization, location} = this.props;
  45. return [
  46. // We paginate on this query, since we're filtering by hasExternalTeams:true
  47. [
  48. 'teams',
  49. `/organizations/${organization.slug}/teams/`,
  50. {query: {...location?.query, query: 'hasExternalTeams:true'}},
  51. ],
  52. // We use this query as defaultOptions to reduce identical API calls
  53. ['initialResults', `/organizations/${organization.slug}/teams/`],
  54. ];
  55. }
  56. handleDelete = async (mapping: ExternalActorMapping) => {
  57. try {
  58. const {organization} = this.props;
  59. const {teams} = this.state;
  60. const team = teams.find(item => item.id === mapping.teamId);
  61. if (!team) {
  62. throw new Error('Cannot find correct team slug.');
  63. }
  64. const endpoint = `/teams/${organization.slug}/${team.slug}/external-teams/${mapping.id}/`;
  65. await this.api.requestPromise(endpoint, {
  66. method: 'DELETE',
  67. });
  68. // remove config and update state
  69. addSuccessMessage(t('Deletion successful'));
  70. this.fetchData();
  71. } catch {
  72. // no 4xx errors should happen on delete
  73. addErrorMessage(t('An error occurred'));
  74. }
  75. };
  76. handleSubmitSuccess = () => {
  77. this.fetchData();
  78. };
  79. get mappings() {
  80. const {integration} = this.props;
  81. const {teams} = this.state;
  82. const externalTeamMappings = teams.reduce<ExternalActorMapping[]>((acc, team) => {
  83. const {externalTeams} = team;
  84. acc.push(
  85. ...externalTeams
  86. .filter(externalTeam => externalTeam.provider === integration.provider.key)
  87. .map(externalTeam => ({...externalTeam, sentryName: team.slug}))
  88. );
  89. return acc;
  90. }, []);
  91. return externalTeamMappings.sort((a, b) => parseInt(a.id, 10) - parseInt(b.id, 10));
  92. }
  93. modalMappingKey = '__MODAL_RESULTS__';
  94. get dataEndpoint() {
  95. const {organization} = this.props;
  96. return `/organizations/${organization.slug}/teams/`;
  97. }
  98. get defaultTeamOptions() {
  99. const {initialResults} = this.state;
  100. return this.sentryNamesMapper(initialResults).map(sentryNameToOption);
  101. }
  102. getBaseFormEndpoint(mapping?: ExternalActorMappingOrSuggestion) {
  103. if (!mapping) {
  104. return '';
  105. }
  106. const {organization} = this.props;
  107. const {queryResults, initialResults} = this.state;
  108. const fieldResults =
  109. queryResults[mapping.externalName] ?? queryResults[this.modalMappingKey];
  110. const team =
  111. // First, search for the team in the query results...
  112. fieldResults?.find(item => item.id === mapping.teamId) ??
  113. // Then in the initial results, if nothing was found.
  114. initialResults?.find(item => item.id === mapping.teamId);
  115. return `/teams/${organization.slug}/${team?.slug ?? ''}/external-teams/`;
  116. }
  117. sentryNamesMapper(teams: Team[]) {
  118. return teams.map(({id, slug}) => ({id, name: slug}));
  119. }
  120. /**
  121. * This method combines the results from searches made on a form dropping repeated entries
  122. * that have identical 'id's. This is because we need the result of the search query when
  123. * the user submits to get the team slug, but it won't always be the last query they've made.
  124. *
  125. * If they search (but not select) after making a selection, and we didn't keep a running collection of results,
  126. * we wouldn't have the team to generate the endpoint from.
  127. */
  128. combineResultsById = (resultList1, resultList2) => {
  129. return uniqBy([...resultList1, ...resultList2], 'id');
  130. };
  131. handleResults = (results, mappingKey?: string) => {
  132. if (mappingKey) {
  133. const {queryResults} = this.state;
  134. this.setState({
  135. queryResults: {
  136. ...queryResults,
  137. // Ensure we always have a team to pull the slug from
  138. [mappingKey]: this.combineResultsById(results, queryResults[mappingKey] ?? []),
  139. },
  140. });
  141. }
  142. };
  143. openModal = (mapping?: ExternalActorMappingOrSuggestion) => {
  144. const {integration} = this.props;
  145. openModal(({Body, Header, closeModal}) => (
  146. <Fragment>
  147. <Header closeButton>{t('Configure External Team Mapping')}</Header>
  148. <Body>
  149. <IntegrationExternalMappingForm
  150. type="team"
  151. integration={integration}
  152. dataEndpoint={this.dataEndpoint}
  153. getBaseFormEndpoint={map => this.getBaseFormEndpoint(map)}
  154. defaultOptions={this.defaultTeamOptions}
  155. mapping={mapping}
  156. mappingKey={this.modalMappingKey}
  157. sentryNamesMapper={this.sentryNamesMapper}
  158. onCancel={closeModal}
  159. onResults={this.handleResults}
  160. onSubmitSuccess={() => {
  161. this.handleSubmitSuccess();
  162. closeModal();
  163. }}
  164. />
  165. </Body>
  166. </Fragment>
  167. ));
  168. };
  169. renderBody() {
  170. const {integration, organization} = this.props;
  171. const {teamsPageLinks} = this.state;
  172. return (
  173. <IntegrationExternalMappings
  174. type="team"
  175. integration={integration}
  176. organization={organization}
  177. mappings={this.mappings}
  178. dataEndpoint={this.dataEndpoint}
  179. getBaseFormEndpoint={mapping => this.getBaseFormEndpoint(mapping)}
  180. defaultOptions={this.defaultTeamOptions}
  181. sentryNamesMapper={this.sentryNamesMapper}
  182. onCreate={this.openModal}
  183. onDelete={this.handleDelete}
  184. pageLinks={teamsPageLinks}
  185. onResults={this.handleResults}
  186. />
  187. );
  188. }
  189. }
  190. export default withSentryRouter(withOrganization(IntegrationExternalTeamMappings));