integrationExternalMappings.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. import {Fragment} from 'react';
  2. // eslint-disable-next-line no-restricted-imports
  3. import {withRouter, WithRouterProps} from 'react-router';
  4. import styled from '@emotion/styled';
  5. import capitalize from 'lodash/capitalize';
  6. import MenuItemActionLink from 'sentry/components/actions/menuItemActionLink';
  7. import AsyncComponent from 'sentry/components/asyncComponent';
  8. import Button from 'sentry/components/button';
  9. import DropdownLink from 'sentry/components/dropdownLink';
  10. import EmptyMessage from 'sentry/components/emptyMessage';
  11. import IntegrationExternalMappingForm from 'sentry/components/integrationExternalMappingForm';
  12. import Pagination from 'sentry/components/pagination';
  13. import {Panel, PanelBody, PanelHeader, PanelItem} from 'sentry/components/panels';
  14. import Tooltip from 'sentry/components/tooltip';
  15. import {IconAdd, IconArrow, IconEllipsis, IconQuestion} from 'sentry/icons';
  16. import {t, tct} from 'sentry/locale';
  17. import PluginIcon from 'sentry/plugins/components/pluginIcon';
  18. import space from 'sentry/styles/space';
  19. import {
  20. ExternalActorMapping,
  21. ExternalActorMappingOrSuggestion,
  22. ExternalActorSuggestion,
  23. Integration,
  24. Organization,
  25. } from 'sentry/types';
  26. import {getIntegrationIcon, isExternalActorMapping} from 'sentry/utils/integrationUtil';
  27. type CodeOwnersAssociationMappings = {
  28. [projectSlug: string]: {
  29. associations: {
  30. [externalName: string]: string;
  31. };
  32. errors: {
  33. [errorKey: string]: string;
  34. };
  35. };
  36. };
  37. type Props = AsyncComponent['props'] &
  38. WithRouterProps &
  39. Pick<
  40. IntegrationExternalMappingForm['props'],
  41. | 'dataEndpoint'
  42. | 'getBaseFormEndpoint'
  43. | 'sentryNamesMapper'
  44. | 'onResults'
  45. | 'defaultOptions'
  46. > & {
  47. integration: Integration;
  48. mappings: ExternalActorMapping[];
  49. onCreate: (mapping?: ExternalActorMappingOrSuggestion) => void;
  50. onDelete: (mapping: ExternalActorMapping) => void;
  51. organization: Organization;
  52. type: 'team' | 'user';
  53. pageLinks?: string;
  54. };
  55. type State = AsyncComponent['state'] & {
  56. associationMappings: CodeOwnersAssociationMappings;
  57. newlyAssociatedMappings: ExternalActorMapping[];
  58. };
  59. class IntegrationExternalMappings extends AsyncComponent<Props, State> {
  60. getDefaultState(): State {
  61. return {
  62. ...super.getDefaultState(),
  63. associationMappings: {},
  64. newlyAssociatedMappings: [],
  65. };
  66. }
  67. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  68. const {organization, integration} = this.props;
  69. return [
  70. [
  71. 'associationMappings',
  72. `/organizations/${organization.slug}/codeowners-associations/`,
  73. {query: {provider: integration.provider.key}},
  74. ],
  75. ];
  76. }
  77. get isFirstPage(): boolean {
  78. const {cursor} = this.props.location.query;
  79. return cursor ? cursor?.split(':')[1] === '0' : true;
  80. }
  81. get unassociatedMappings(): ExternalActorSuggestion[] {
  82. const {type} = this.props;
  83. const {associationMappings} = this.state;
  84. const errorKey = `missing_external_${type}s`;
  85. const unassociatedMappings = Object.values(associationMappings).reduce(
  86. (map, {errors}) => {
  87. return new Set<string>([...map, ...errors[errorKey]]);
  88. },
  89. new Set<string>()
  90. );
  91. return Array.from(unassociatedMappings).map(externalName => ({externalName}));
  92. }
  93. get allMappings(): ExternalActorMappingOrSuggestion[] {
  94. const {mappings} = this.props;
  95. if (!this.isFirstPage) {
  96. return mappings;
  97. }
  98. const {newlyAssociatedMappings} = this.state;
  99. const inlineMappings = this.unassociatedMappings.map(mapping => {
  100. // If this mapping has been changed, replace it with the new version from its change's response
  101. // The new version will be used in IntegrationExternalMappingForm to update the apiMethod and apiEndpoint
  102. const newlyAssociatedMapping = newlyAssociatedMappings.find(
  103. ({externalName}) => externalName === mapping.externalName
  104. );
  105. return newlyAssociatedMapping ?? mapping;
  106. });
  107. return [...inlineMappings, ...mappings];
  108. }
  109. renderMappingName(mapping: ExternalActorMappingOrSuggestion) {
  110. const {
  111. type,
  112. getBaseFormEndpoint,
  113. integration,
  114. dataEndpoint,
  115. sentryNamesMapper,
  116. onResults,
  117. defaultOptions,
  118. } = this.props;
  119. return (
  120. <IntegrationExternalMappingForm
  121. type={type}
  122. integration={integration}
  123. dataEndpoint={dataEndpoint}
  124. getBaseFormEndpoint={getBaseFormEndpoint}
  125. mapping={mapping}
  126. sentryNamesMapper={sentryNamesMapper}
  127. onResults={onResults}
  128. onSubmitSuccess={(newMapping: ExternalActorMapping) => {
  129. this.setState({
  130. newlyAssociatedMappings: [
  131. ...this.state.newlyAssociatedMappings.filter(
  132. map => map.externalName !== newMapping.externalName
  133. ),
  134. newMapping as ExternalActorMapping,
  135. ],
  136. });
  137. }}
  138. isInline
  139. defaultOptions={defaultOptions}
  140. />
  141. );
  142. }
  143. renderMappingOptions(mapping: ExternalActorMappingOrSuggestion) {
  144. const {type, onDelete, organization} = this.props;
  145. const canDelete = organization.access.includes('org:integrations');
  146. return isExternalActorMapping(mapping) ? (
  147. <Tooltip
  148. title={t(
  149. 'You must be an organization owner, manager or admin to delete an external user mapping.'
  150. )}
  151. disabled={canDelete}
  152. >
  153. <DropdownLink
  154. anchorRight
  155. disabled={!canDelete}
  156. customTitle={
  157. <Button
  158. borderless
  159. size="sm"
  160. icon={<IconEllipsisVertical size="sm" />}
  161. aria-label={t('Actions')}
  162. data-test-id="mapping-option"
  163. disabled={!canDelete}
  164. />
  165. }
  166. >
  167. <MenuItemActionLink
  168. shouldConfirm
  169. message={t('Are you sure you want to remove this external %s mapping?', type)}
  170. onAction={() => onDelete(mapping)}
  171. aria-label={t('Delete External %s', capitalize(type))}
  172. data-test-id="delete-mapping-button"
  173. >
  174. <RedText>{t('Delete')}</RedText>
  175. </MenuItemActionLink>
  176. </DropdownLink>
  177. </Tooltip>
  178. ) : (
  179. <Tooltip
  180. title={t('This %s mapping suggestion was generated from a CODEOWNERS file', type)}
  181. >
  182. <Button
  183. disabled
  184. borderless
  185. size="sm"
  186. icon={<IconQuestion size="sm" />}
  187. aria-label={t(
  188. `This %s mapping suggestion was generated from a CODEOWNERS file`,
  189. type
  190. )}
  191. data-test-id="suggestion-option"
  192. />
  193. </Tooltip>
  194. );
  195. }
  196. renderBody() {
  197. const {integration, type, onCreate, pageLinks} = this.props;
  198. return (
  199. <Fragment>
  200. <Panel>
  201. <PanelHeader disablePadding hasButtons>
  202. <HeaderLayout>
  203. <ExternalNameColumn header>
  204. {tct('External [type]', {type})}
  205. </ExternalNameColumn>
  206. <ArrowColumn>
  207. <IconArrow direction="right" size="md" />
  208. </ArrowColumn>
  209. <SentryNameColumn>{tct('Sentry [type]', {type})}</SentryNameColumn>
  210. <ButtonColumn>
  211. <AddButton
  212. data-test-id="add-mapping-button"
  213. onClick={() => onCreate()}
  214. size="xs"
  215. icon={<IconAdd size="xs" isCircled />}
  216. >
  217. <ButtonText>{tct('Add [type] Mapping', {type})}</ButtonText>
  218. </AddButton>
  219. </ButtonColumn>
  220. </HeaderLayout>
  221. </PanelHeader>
  222. <PanelBody data-test-id="mapping-table">
  223. {!this.allMappings.length && (
  224. <EmptyMessage
  225. icon={getIntegrationIcon(integration.provider.key, 'lg')}
  226. data-test-id="empty-message"
  227. >
  228. {tct('Set up External [type] Mappings.', {type: capitalize(type)})}
  229. </EmptyMessage>
  230. )}
  231. {this.allMappings.map((mapping, index) => (
  232. <ConfigPanelItem key={index}>
  233. <Layout>
  234. <ExternalNameColumn>
  235. <StyledPluginIcon pluginId={integration.provider.key} size={19} />
  236. <span>{mapping.externalName}</span>
  237. </ExternalNameColumn>
  238. <ArrowColumn>
  239. <IconArrow direction="right" size="md" />
  240. </ArrowColumn>
  241. <SentryNameColumn>{this.renderMappingName(mapping)}</SentryNameColumn>
  242. <ButtonColumn>{this.renderMappingOptions(mapping)}</ButtonColumn>
  243. </Layout>
  244. </ConfigPanelItem>
  245. ))}
  246. </PanelBody>
  247. </Panel>
  248. <Pagination pageLinks={pageLinks} />
  249. </Fragment>
  250. );
  251. }
  252. }
  253. export default withRouter(IntegrationExternalMappings);
  254. const AddButton = styled(Button)`
  255. text-transform: capitalize;
  256. height: inherit;
  257. `;
  258. const ButtonText = styled('div')`
  259. white-space: break-spaces;
  260. `;
  261. const Layout = styled('div')`
  262. display: grid;
  263. grid-column-gap: ${space(1)};
  264. padding: ${space(1)};
  265. width: 100%;
  266. align-items: center;
  267. grid-template-columns: 2.25fr 50px 2.75fr 100px;
  268. grid-template-areas: 'external-name arrow sentry-name button';
  269. `;
  270. const HeaderLayout = styled(Layout)`
  271. align-items: center;
  272. padding: 0 ${space(1)} 0 ${space(2)};
  273. text-transform: uppercase;
  274. `;
  275. const ConfigPanelItem = styled(PanelItem)`
  276. padding: 0 ${space(1)};
  277. `;
  278. const IconEllipsisVertical = styled(IconEllipsis)`
  279. transform: rotate(90deg);
  280. `;
  281. const StyledPluginIcon = styled(PluginIcon)`
  282. min-width: ${p => p.size}px;
  283. margin-right: ${space(2)};
  284. `;
  285. // Columns below
  286. const Column = styled('span')`
  287. overflow: hidden;
  288. overflow-wrap: break-word;
  289. `;
  290. const ExternalNameColumn = styled(Column)<{header?: boolean}>`
  291. grid-area: external-name;
  292. display: flex;
  293. align-items: center;
  294. font-family: ${p => (p.header ? 'inherit' : p.theme.text.familyMono)};
  295. `;
  296. const ArrowColumn = styled(Column)`
  297. grid-area: arrow;
  298. `;
  299. const SentryNameColumn = styled(Column)`
  300. grid-area: sentry-name;
  301. overflow: visible;
  302. `;
  303. const ButtonColumn = styled(Column)`
  304. grid-area: button;
  305. text-align: right;
  306. overflow: visible;
  307. `;
  308. const RedText = styled('span')`
  309. color: ${p => p.theme.red300};
  310. `;