integrationExternalMappings.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import {Fragment} from 'react';
  2. import {withRouter, WithRouterProps} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import capitalize from 'lodash/capitalize';
  5. import Access from 'sentry/components/acl/access';
  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 IntegrationExternalMappingForm from 'sentry/components/integrationExternalMappingForm';
  11. import Pagination from 'sentry/components/pagination';
  12. import {Panel, PanelBody, PanelHeader, PanelItem} from 'sentry/components/panels';
  13. import Tooltip from 'sentry/components/tooltip';
  14. import {IconAdd, IconArrow, IconEllipsis, IconQuestion} from 'sentry/icons';
  15. import {t, tct} from 'sentry/locale';
  16. import PluginIcon from 'sentry/plugins/components/pluginIcon';
  17. import space from 'sentry/styles/space';
  18. import {
  19. ExternalActorMapping,
  20. ExternalActorMappingOrSuggestion,
  21. ExternalActorSuggestion,
  22. Integration,
  23. Organization,
  24. } from 'sentry/types';
  25. import {getIntegrationIcon, isExternalActorMapping} from 'sentry/utils/integrationUtil';
  26. import EmptyMessage from 'sentry/views/settings/components/emptyMessage';
  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, hasAccess: boolean) {
  110. const {
  111. type,
  112. getBaseFormEndpoint,
  113. integration,
  114. dataEndpoint,
  115. sentryNamesMapper,
  116. onResults,
  117. defaultOptions,
  118. } = this.props;
  119. const mappingName = isExternalActorMapping(mapping) ? mapping.sentryName : '';
  120. return hasAccess ? (
  121. <IntegrationExternalMappingForm
  122. type={type}
  123. integration={integration}
  124. dataEndpoint={dataEndpoint}
  125. getBaseFormEndpoint={getBaseFormEndpoint}
  126. mapping={mapping}
  127. sentryNamesMapper={sentryNamesMapper}
  128. onResults={onResults}
  129. onSubmitSuccess={(newMapping: ExternalActorMapping) => {
  130. this.setState({
  131. newlyAssociatedMappings: [
  132. ...this.state.newlyAssociatedMappings.filter(
  133. map => map.externalName !== newMapping.externalName
  134. ),
  135. newMapping as ExternalActorMapping,
  136. ],
  137. });
  138. }}
  139. isInline
  140. defaultOptions={defaultOptions}
  141. />
  142. ) : (
  143. mappingName
  144. );
  145. }
  146. renderMappingOptions(mapping: ExternalActorMappingOrSuggestion, hasAccess: boolean) {
  147. const {type, onDelete} = this.props;
  148. return isExternalActorMapping(mapping) ? (
  149. <Tooltip
  150. title={t(
  151. 'You must be an organization owner, manager or admin to make changes to an external user mapping.'
  152. )}
  153. disabled={hasAccess}
  154. >
  155. <DropdownLink
  156. anchorRight
  157. customTitle={
  158. <Button
  159. borderless
  160. size="small"
  161. icon={<IconEllipsisVertical size="sm" />}
  162. disabled={!hasAccess}
  163. aria-label={t('Actions')}
  164. data-test-id="mapping-option"
  165. />
  166. }
  167. >
  168. <MenuItemActionLink
  169. shouldConfirm
  170. message={t(`Are you sure you want to remove this external ${type} mapping?`)}
  171. disabled={!hasAccess}
  172. onAction={() => onDelete(mapping)}
  173. title={t(`Delete External ${capitalize(type)}`)}
  174. data-test-id="delete-mapping-button"
  175. >
  176. <RedText>{t('Delete')}</RedText>
  177. </MenuItemActionLink>
  178. </DropdownLink>
  179. </Tooltip>
  180. ) : (
  181. <Tooltip
  182. title={t(`This ${type} mapping suggestion was generated from a CODEOWNERS file`)}
  183. >
  184. <Button
  185. disabled
  186. borderless
  187. size="small"
  188. icon={<IconQuestion size="sm" />}
  189. aria-label={t(
  190. `This ${type} mapping suggestion was generated from a CODEOWNERS file`
  191. )}
  192. data-test-id="suggestion-option"
  193. />
  194. </Tooltip>
  195. );
  196. }
  197. renderBody() {
  198. const {integration, type, onCreate, pageLinks} = this.props;
  199. return (
  200. <Fragment>
  201. <Panel>
  202. <PanelHeader disablePadding hasButtons>
  203. <HeaderLayout>
  204. <ExternalNameColumn header>
  205. {tct('External [type]', {type})}
  206. </ExternalNameColumn>
  207. <ArrowColumn>
  208. <IconArrow direction="right" size="md" />
  209. </ArrowColumn>
  210. <SentryNameColumn>{tct('Sentry [type]', {type})}</SentryNameColumn>
  211. <Access access={['org:integrations']}>
  212. {({hasAccess}) => (
  213. <ButtonColumn>
  214. <Tooltip
  215. title={tct(
  216. 'You must be an organization owner, manager or admin to edit or remove a [type] mapping.',
  217. {type}
  218. )}
  219. disabled={hasAccess}
  220. >
  221. <AddButton
  222. data-test-id="add-mapping-button"
  223. onClick={() => onCreate()}
  224. size="xsmall"
  225. icon={<IconAdd size="xs" isCircled />}
  226. disabled={!hasAccess}
  227. >
  228. <ButtonText>{tct('Add [type] Mapping', {type})}</ButtonText>
  229. </AddButton>
  230. </Tooltip>
  231. </ButtonColumn>
  232. )}
  233. </Access>
  234. </HeaderLayout>
  235. </PanelHeader>
  236. <PanelBody data-test-id="mapping-table">
  237. {!this.allMappings.length && (
  238. <EmptyMessage
  239. icon={getIntegrationIcon(integration.provider.key, 'lg')}
  240. data-test-id="empty-message"
  241. >
  242. {tct('Set up External [type] Mappings.', {type: capitalize(type)})}
  243. </EmptyMessage>
  244. )}
  245. {this.allMappings.map((mapping, index) => (
  246. <Access access={['org:integrations']} key={index}>
  247. {({hasAccess}) => (
  248. <ConfigPanelItem>
  249. <Layout>
  250. <ExternalNameColumn>
  251. <StyledPluginIcon pluginId={integration.provider.key} size={19} />
  252. <span>{mapping.externalName}</span>
  253. </ExternalNameColumn>
  254. <ArrowColumn>
  255. <IconArrow direction="right" size="md" />
  256. </ArrowColumn>
  257. <SentryNameColumn>
  258. {this.renderMappingName(mapping, hasAccess)}
  259. </SentryNameColumn>
  260. <ButtonColumn>
  261. {this.renderMappingOptions(mapping, hasAccess)}
  262. </ButtonColumn>
  263. </Layout>
  264. </ConfigPanelItem>
  265. )}
  266. </Access>
  267. ))}
  268. </PanelBody>
  269. </Panel>
  270. <Pagination pageLinks={pageLinks} />
  271. </Fragment>
  272. );
  273. }
  274. }
  275. export default withRouter(IntegrationExternalMappings);
  276. const AddButton = styled(Button)`
  277. text-transform: capitalize;
  278. height: inherit;
  279. `;
  280. const ButtonText = styled('div')`
  281. white-space: break-spaces;
  282. `;
  283. const Layout = styled('div')`
  284. display: grid;
  285. grid-column-gap: ${space(1)};
  286. padding: ${space(1)};
  287. width: 100%;
  288. align-items: center;
  289. grid-template-columns: 2.25fr 50px 2.75fr 100px;
  290. grid-template-areas: 'external-name arrow sentry-name button';
  291. `;
  292. const HeaderLayout = styled(Layout)`
  293. align-items: center;
  294. padding: 0 ${space(1)} 0 ${space(2)};
  295. text-transform: uppercase;
  296. `;
  297. const ConfigPanelItem = styled(PanelItem)`
  298. padding: 0 ${space(1)};
  299. `;
  300. const IconEllipsisVertical = styled(IconEllipsis)`
  301. transform: rotate(90deg);
  302. `;
  303. const StyledPluginIcon = styled(PluginIcon)`
  304. min-width: ${p => p.size}px;
  305. margin-right: ${space(2)};
  306. `;
  307. // Columns below
  308. const Column = styled('span')`
  309. overflow: hidden;
  310. overflow-wrap: break-word;
  311. `;
  312. const ExternalNameColumn = styled(Column)<{header?: boolean}>`
  313. grid-area: external-name;
  314. display: flex;
  315. align-items: center;
  316. font-family: ${p => (p.header ? 'inherit' : p.theme.text.familyMono)};
  317. `;
  318. const ArrowColumn = styled(Column)`
  319. grid-area: arrow;
  320. `;
  321. const SentryNameColumn = styled(Column)`
  322. grid-area: sentry-name;
  323. overflow: visible;
  324. `;
  325. const ButtonColumn = styled(Column)`
  326. grid-area: button;
  327. text-align: right;
  328. overflow: visible;
  329. `;
  330. const RedText = styled('span')`
  331. color: ${p => p.theme.red300};
  332. `;