integrationExternalMappings.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import {Fragment, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import Confirm from 'sentry/components/confirm';
  5. import LoadingError from 'sentry/components/loadingError';
  6. import LoadingIndicator from 'sentry/components/loadingIndicator';
  7. import Pagination from 'sentry/components/pagination';
  8. import {PanelTable} from 'sentry/components/panels/panelTable';
  9. import QuestionTooltip from 'sentry/components/questionTooltip';
  10. import {IconAdd, IconArrow, IconDelete} from 'sentry/icons';
  11. import {t, tct} from 'sentry/locale';
  12. import PluginIcon from 'sentry/plugins/components/pluginIcon';
  13. import {space} from 'sentry/styles/space';
  14. import type {
  15. ExternalActorMapping,
  16. ExternalActorMappingOrSuggestion,
  17. ExternalActorSuggestion,
  18. Integration,
  19. } from 'sentry/types/integrations';
  20. import {isExternalActorMapping} from 'sentry/utils/integrationUtil';
  21. import {useApiQuery} from 'sentry/utils/queryClient';
  22. import {capitalize} from 'sentry/utils/string/capitalize';
  23. import {useLocation} from 'sentry/utils/useLocation';
  24. import useOrganization from 'sentry/utils/useOrganization';
  25. import IntegrationExternalMappingForm from './integrationExternalMappingForm';
  26. type CodeOwnersAssociationMappings = {
  27. [projectSlug: string]: {
  28. associations: {
  29. [externalName: string]: string;
  30. };
  31. errors: {
  32. [errorKey: string]: string;
  33. };
  34. };
  35. };
  36. type Props = Pick<
  37. IntegrationExternalMappingForm['props'],
  38. | 'dataEndpoint'
  39. | 'getBaseFormEndpoint'
  40. | 'sentryNamesMapper'
  41. | 'onResults'
  42. | 'defaultOptions'
  43. > & {
  44. integration: Integration;
  45. mappings: ExternalActorMapping[];
  46. onCreate: (mapping?: ExternalActorMappingOrSuggestion) => void;
  47. onDelete: (mapping: ExternalActorMapping) => void;
  48. type: 'team' | 'user';
  49. pageLinks?: string;
  50. };
  51. type LocationQuery = {
  52. cursor?: string;
  53. };
  54. function IntegrationExternalMappings(props: Props) {
  55. const {
  56. integration,
  57. type,
  58. mappings,
  59. pageLinks,
  60. dataEndpoint,
  61. defaultOptions,
  62. onCreate,
  63. onResults,
  64. onDelete,
  65. getBaseFormEndpoint,
  66. sentryNamesMapper,
  67. } = props;
  68. const [newlyAssociatedMappings, setNewlyAssociatedMappings] = useState<
  69. ExternalActorMapping[]
  70. >([]);
  71. const organization = useOrganization();
  72. const location = useLocation<LocationQuery>();
  73. const {cursor} = location.query;
  74. const isFirstPage = cursor ? cursor.split(':')[1] === '0' : true;
  75. const {
  76. data: associationMappings,
  77. isPending,
  78. isError,
  79. refetch,
  80. } = useApiQuery<CodeOwnersAssociationMappings>(
  81. [
  82. `/organizations/${organization.slug}/codeowners-associations/`,
  83. {query: {provider: integration.provider.key}},
  84. ],
  85. {staleTime: 0}
  86. );
  87. if (isPending) {
  88. return <LoadingIndicator />;
  89. }
  90. if (isError) {
  91. return <LoadingError onRetry={refetch} />;
  92. }
  93. const unassociatedMappings = (): ExternalActorSuggestion[] => {
  94. const errorKey = `missing_external_${type}s`;
  95. const unassociatedMappingsSet = Object.values(associationMappings).reduce(
  96. (map, {errors}) => {
  97. return new Set<string>([...map, ...errors[errorKey]!]);
  98. },
  99. new Set<string>()
  100. );
  101. return Array.from(unassociatedMappingsSet).map(externalName => ({externalName}));
  102. };
  103. const allMappings = (): ExternalActorMappingOrSuggestion[] => {
  104. if (!isFirstPage) {
  105. return mappings;
  106. }
  107. const inlineMappings = unassociatedMappings().map(mapping => {
  108. // If this mapping has been changed, replace it with the new version from its change's response
  109. // The new version will be used in IntegrationExternalMappingForm to update the apiMethod and apiEndpoint
  110. const newlyAssociatedMapping = newlyAssociatedMappings.find(
  111. ({externalName}) => externalName === mapping.externalName
  112. );
  113. return newlyAssociatedMapping ?? mapping;
  114. });
  115. return [...inlineMappings, ...mappings];
  116. };
  117. const renderMappingName = (mapping: ExternalActorMappingOrSuggestion) => {
  118. return (
  119. <IntegrationExternalMappingForm
  120. type={type}
  121. integration={integration}
  122. dataEndpoint={dataEndpoint}
  123. getBaseFormEndpoint={getBaseFormEndpoint}
  124. mapping={mapping}
  125. sentryNamesMapper={sentryNamesMapper}
  126. onResults={onResults}
  127. onSubmitSuccess={(newMapping: ExternalActorMapping) => {
  128. setNewlyAssociatedMappings([
  129. ...newlyAssociatedMappings.filter(
  130. map => map.externalName !== newMapping.externalName
  131. ),
  132. newMapping,
  133. ]);
  134. }}
  135. isInline
  136. defaultOptions={defaultOptions}
  137. />
  138. );
  139. };
  140. const renderMappingActions = (mapping: ExternalActorMappingOrSuggestion) => {
  141. const canDelete = organization.access.includes('org:integrations');
  142. return isExternalActorMapping(mapping) ? (
  143. <Confirm
  144. disabled={!canDelete}
  145. onConfirm={() => onDelete(mapping)}
  146. message={t('Are you sure you want to remove this external %s mapping?', type)}
  147. >
  148. <Button
  149. borderless
  150. size="sm"
  151. icon={<IconDelete size="sm" />}
  152. aria-label={t('Remove user mapping')}
  153. title={
  154. canDelete
  155. ? t('Remove user mapping')
  156. : t(
  157. 'You must be an organization owner, manager or admin to delete an external user mapping.'
  158. )
  159. }
  160. />
  161. </Confirm>
  162. ) : (
  163. <QuestionTooltip
  164. title={t('This %s mapping suggestion was generated from a CODEOWNERS file', type)}
  165. size="sm"
  166. />
  167. );
  168. };
  169. return (
  170. <Fragment>
  171. <MappingTable
  172. data-test-id="mapping-table"
  173. isEmpty={!allMappings().length}
  174. emptyMessage={tct('Set up External [type] Mappings.', {type: capitalize(type)})}
  175. headers={[
  176. tct('External [type]', {type}),
  177. <IconArrow key="arrow" direction="right" size="sm" />,
  178. tct('Sentry [type]', {type}),
  179. <AddButton
  180. key="delete-button"
  181. data-test-id="add-mapping-button"
  182. onClick={() => onCreate()}
  183. size="xs"
  184. icon={<IconAdd isCircled />}
  185. >
  186. {tct('Add [type] Mapping', {type})}
  187. </AddButton>,
  188. ]}
  189. >
  190. {allMappings().map((mapping, index) => (
  191. <Fragment key={index}>
  192. <ExternalNameColumn>
  193. <StyledPluginIcon pluginId={integration.provider.key} size={19} />
  194. <span>{mapping.externalName}</span>
  195. </ExternalNameColumn>
  196. <div>
  197. <IconArrow direction="right" size="sm" color="gray300" />
  198. </div>
  199. <ExternalForm>{renderMappingName(mapping)}</ExternalForm>
  200. <div>{renderMappingActions(mapping)}</div>
  201. </Fragment>
  202. ))}
  203. </MappingTable>
  204. <Pagination pageLinks={pageLinks} />
  205. </Fragment>
  206. );
  207. }
  208. export default IntegrationExternalMappings;
  209. const MappingTable = styled(PanelTable)`
  210. overflow: visible;
  211. grid-template-columns: 1fr max-content 1fr 66px;
  212. ${p =>
  213. !p.isEmpty
  214. ? `
  215. > :nth-child(n + 5) {
  216. display: flex;
  217. align-items: center;
  218. padding: ${space(1.5)} ${space(2)};
  219. }
  220. > * {
  221. padding: ${space(1)} ${space(2)};
  222. }
  223. `
  224. : `
  225. > :not(:nth-child(n + 5)) {
  226. padding: ${space(1)} ${space(2)};
  227. }`}
  228. > :nth-child(4n) {
  229. padding-right: ${space(1)};
  230. justify-content: end;
  231. }
  232. `;
  233. const StyledPluginIcon = styled(PluginIcon)`
  234. min-width: ${p => p.size}px;
  235. margin-right: ${space(2)};
  236. `;
  237. const ExternalNameColumn = styled('div')`
  238. font-family: ${p => p.theme.text.familyMono};
  239. `;
  240. const AddButton = styled(Button)`
  241. align-self: end;
  242. `;
  243. const ExternalForm = styled('div')`
  244. width: 100%;
  245. `;