integrationExternalMappings.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 ${type} mapping?`)}
  170. onAction={() => onDelete(mapping)}
  171. aria-label={t(`Delete External ${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 ${type} mapping suggestion was generated from a CODEOWNERS file`)}
  181. >
  182. <Button
  183. disabled
  184. borderless
  185. size="sm"
  186. icon={<IconQuestion size="sm" />}
  187. aria-label={t(
  188. `This ${type} mapping suggestion was generated from a CODEOWNERS file`
  189. )}
  190. data-test-id="suggestion-option"
  191. />
  192. </Tooltip>
  193. );
  194. }
  195. renderBody() {
  196. const {integration, type, onCreate, pageLinks} = this.props;
  197. return (
  198. <Fragment>
  199. <Panel>
  200. <PanelHeader disablePadding hasButtons>
  201. <HeaderLayout>
  202. <ExternalNameColumn header>
  203. {tct('External [type]', {type})}
  204. </ExternalNameColumn>
  205. <ArrowColumn>
  206. <IconArrow direction="right" size="md" />
  207. </ArrowColumn>
  208. <SentryNameColumn>{tct('Sentry [type]', {type})}</SentryNameColumn>
  209. <ButtonColumn>
  210. <AddButton
  211. data-test-id="add-mapping-button"
  212. onClick={() => onCreate()}
  213. size="xs"
  214. icon={<IconAdd size="xs" isCircled />}
  215. >
  216. <ButtonText>{tct('Add [type] Mapping', {type})}</ButtonText>
  217. </AddButton>
  218. </ButtonColumn>
  219. </HeaderLayout>
  220. </PanelHeader>
  221. <PanelBody data-test-id="mapping-table">
  222. {!this.allMappings.length && (
  223. <EmptyMessage
  224. icon={getIntegrationIcon(integration.provider.key, 'lg')}
  225. data-test-id="empty-message"
  226. >
  227. {tct('Set up External [type] Mappings.', {type: capitalize(type)})}
  228. </EmptyMessage>
  229. )}
  230. {this.allMappings.map((mapping, index) => (
  231. <ConfigPanelItem key={index}>
  232. <Layout>
  233. <ExternalNameColumn>
  234. <StyledPluginIcon pluginId={integration.provider.key} size={19} />
  235. <span>{mapping.externalName}</span>
  236. </ExternalNameColumn>
  237. <ArrowColumn>
  238. <IconArrow direction="right" size="md" />
  239. </ArrowColumn>
  240. <SentryNameColumn>{this.renderMappingName(mapping)}</SentryNameColumn>
  241. <ButtonColumn>{this.renderMappingOptions(mapping)}</ButtonColumn>
  242. </Layout>
  243. </ConfigPanelItem>
  244. ))}
  245. </PanelBody>
  246. </Panel>
  247. <Pagination pageLinks={pageLinks} />
  248. </Fragment>
  249. );
  250. }
  251. }
  252. export default withRouter(IntegrationExternalMappings);
  253. const AddButton = styled(Button)`
  254. text-transform: capitalize;
  255. height: inherit;
  256. `;
  257. const ButtonText = styled('div')`
  258. white-space: break-spaces;
  259. `;
  260. const Layout = styled('div')`
  261. display: grid;
  262. grid-column-gap: ${space(1)};
  263. padding: ${space(1)};
  264. width: 100%;
  265. align-items: center;
  266. grid-template-columns: 2.25fr 50px 2.75fr 100px;
  267. grid-template-areas: 'external-name arrow sentry-name button';
  268. `;
  269. const HeaderLayout = styled(Layout)`
  270. align-items: center;
  271. padding: 0 ${space(1)} 0 ${space(2)};
  272. text-transform: uppercase;
  273. `;
  274. const ConfigPanelItem = styled(PanelItem)`
  275. padding: 0 ${space(1)};
  276. `;
  277. const IconEllipsisVertical = styled(IconEllipsis)`
  278. transform: rotate(90deg);
  279. `;
  280. const StyledPluginIcon = styled(PluginIcon)`
  281. min-width: ${p => p.size}px;
  282. margin-right: ${space(2)};
  283. `;
  284. // Columns below
  285. const Column = styled('span')`
  286. overflow: hidden;
  287. overflow-wrap: break-word;
  288. `;
  289. const ExternalNameColumn = styled(Column)<{header?: boolean}>`
  290. grid-area: external-name;
  291. display: flex;
  292. align-items: center;
  293. font-family: ${p => (p.header ? 'inherit' : p.theme.text.familyMono)};
  294. `;
  295. const ArrowColumn = styled(Column)`
  296. grid-area: arrow;
  297. `;
  298. const SentryNameColumn = styled(Column)`
  299. grid-area: sentry-name;
  300. overflow: visible;
  301. `;
  302. const ButtonColumn = styled(Column)`
  303. grid-area: button;
  304. text-align: right;
  305. overflow: visible;
  306. `;
  307. const RedText = styled('span')`
  308. color: ${p => p.theme.red300};
  309. `;