integrationExternalMappings.tsx 11 KB

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