integrationExternalMappings.tsx 11 KB

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