addCodeOwnerModal.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. import {type Dispatch, Fragment, type SetStateAction, useCallback, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  4. import type {ModalRenderProps} from 'sentry/actionCreators/modal';
  5. import {Alert} from 'sentry/components/alert';
  6. import {Button, LinkButton} from 'sentry/components/button';
  7. import SelectField from 'sentry/components/forms/fields/selectField';
  8. import Form from 'sentry/components/forms/form';
  9. import Link from 'sentry/components/links/link';
  10. import LoadingError from 'sentry/components/loadingError';
  11. import LoadingIndicator from 'sentry/components/loadingIndicator';
  12. import Panel from 'sentry/components/panels/panel';
  13. import PanelBody from 'sentry/components/panels/panelBody';
  14. import {IconCheckmark, IconNot} from 'sentry/icons';
  15. import {t, tct} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import type {
  18. CodeOwner,
  19. CodeownersFile,
  20. Integration,
  21. RepositoryProjectPathConfig,
  22. } from 'sentry/types/integrations';
  23. import type {Organization} from 'sentry/types/organization';
  24. import type {Project} from 'sentry/types/project';
  25. import {getIntegrationIcon} from 'sentry/utils/integrationUtil';
  26. import {
  27. fetchMutation,
  28. useApiQuery,
  29. useMutation,
  30. type UseMutationResult,
  31. } from 'sentry/utils/queryClient';
  32. import type RequestError from 'sentry/utils/requestError/requestError';
  33. import useApi from 'sentry/utils/useApi';
  34. type Props = {
  35. organization: Organization;
  36. project: Project;
  37. onSave?: (data: CodeOwner) => void;
  38. } & ModalRenderProps;
  39. type TCodeownersPayload = {codeMappingId: string | null; raw: string};
  40. type TCodeownersData = CodeOwner;
  41. type TCodeownersError = RequestError;
  42. type TCodeownersVariables = [TCodeownersPayload];
  43. type TCodeownersContext = unknown;
  44. export default function AddCodeOwnerModal({
  45. organization,
  46. Header,
  47. Body,
  48. Footer,
  49. project,
  50. onSave,
  51. closeModal,
  52. }: Props) {
  53. const {
  54. data: codeMappings,
  55. isPending: isCodeMappingsPending,
  56. isError: isCodeMappingsError,
  57. } = useApiQuery<RepositoryProjectPathConfig[]>(
  58. [
  59. `/organizations/${organization.slug}/code-mappings/`,
  60. {query: {project: project.id}},
  61. ],
  62. {staleTime: Infinity}
  63. );
  64. const {
  65. data: integrations,
  66. isPending: isIntegrationsPending,
  67. isError: isIntegrationsError,
  68. } = useApiQuery<Integration[]>(
  69. [
  70. `/organizations/${organization.slug}/integrations/`,
  71. {query: {features: ['codeowners']}},
  72. ],
  73. {staleTime: Infinity}
  74. );
  75. const [codeMappingId, setCodeMappingId] = useState<string | null>(null);
  76. const {data: codeownersFile} = useApiQuery<CodeownersFile>(
  77. [`/organizations/${organization.slug}/code-mappings/${codeMappingId}/codeowners/`],
  78. {staleTime: Infinity, enabled: Boolean(codeMappingId)}
  79. );
  80. const api = useApi({
  81. persistInFlight: false,
  82. });
  83. const mutation = useMutation<
  84. TCodeownersData,
  85. TCodeownersError,
  86. TCodeownersVariables,
  87. TCodeownersContext
  88. >({
  89. mutationFn: ([payload]: TCodeownersVariables) => {
  90. return fetchMutation(api)([
  91. 'POST',
  92. `/projects/${organization.slug}/${project.slug}/codeowners/`,
  93. {},
  94. payload,
  95. ]);
  96. },
  97. onSuccess: d => {
  98. const codeMapping = codeMappings?.find(
  99. mapping => mapping.id === codeMappingId?.toString()
  100. );
  101. onSave?.({...d, codeMapping});
  102. closeModal();
  103. },
  104. onError: err => {
  105. if (err.responseJSON && !('raw' in err.responseJSON)) {
  106. addErrorMessage(
  107. Object.values(err.responseJSON ?? {})
  108. .flat()
  109. .join(' ')
  110. );
  111. }
  112. },
  113. gcTime: 0,
  114. });
  115. const addFile = useCallback(() => {
  116. if (codeownersFile) {
  117. mutation.mutate([{codeMappingId, raw: codeownersFile.raw}]);
  118. }
  119. }, [codeMappingId, codeownersFile, mutation]);
  120. if (isCodeMappingsPending || isIntegrationsPending) {
  121. return <LoadingIndicator />;
  122. }
  123. if (isCodeMappingsError || isIntegrationsError) {
  124. return <LoadingError />;
  125. }
  126. return (
  127. <Fragment>
  128. <Header closeButton>{t('Add Code Owner File')}</Header>
  129. <Body>
  130. {codeMappings.length ? (
  131. <ApplyCodeMappings
  132. codeMappingId={codeMappingId}
  133. codeMappings={codeMappings}
  134. codeownersFile={codeownersFile}
  135. mutation={mutation}
  136. organization={organization}
  137. setCodeMappingId={setCodeMappingId}
  138. />
  139. ) : (
  140. <LinkCodeOwners integrations={integrations} organization={organization} />
  141. )}
  142. </Body>
  143. <Footer>
  144. <Button
  145. disabled={codeownersFile ? false : true}
  146. aria-label={t('Add File')}
  147. priority="primary"
  148. onClick={addFile}
  149. >
  150. {t('Add File')}
  151. </Button>
  152. </Footer>
  153. </Fragment>
  154. );
  155. }
  156. function ApplyCodeMappings({
  157. codeMappingId,
  158. codeMappings,
  159. codeownersFile,
  160. mutation,
  161. organization,
  162. setCodeMappingId,
  163. }: {
  164. codeMappingId: string | null;
  165. codeMappings: RepositoryProjectPathConfig[];
  166. codeownersFile: CodeownersFile | undefined;
  167. mutation: UseMutationResult<CodeOwner, RequestError, TCodeownersVariables, unknown>;
  168. organization: Organization;
  169. setCodeMappingId: Dispatch<SetStateAction<string | null>>;
  170. }) {
  171. const baseUrl = `/settings/${organization.slug}/integrations/`;
  172. return (
  173. <Form apiMethod="POST" apiEndpoint="/code-mappings/" hideFooter initialData={{}}>
  174. <StyledSelectField
  175. name="codeMappingId"
  176. label={t('Apply an existing code mapping')}
  177. options={codeMappings.map((cm: RepositoryProjectPathConfig) => ({
  178. value: cm.id,
  179. label: `Repo Name: ${cm.repoName}, Stack Trace Root: ${cm.stackRoot}, Source Code Root: ${cm.sourceRoot}`,
  180. }))}
  181. onChange={setCodeMappingId}
  182. required
  183. inline={false}
  184. flexibleControlStateSize
  185. stacked
  186. />
  187. <FileResult>
  188. {codeownersFile ? (
  189. <SourceFile codeownersFile={codeownersFile} />
  190. ) : (
  191. <NoSourceFile />
  192. )}
  193. {mutation.isError && mutation.error.responseJSON?.raw ? (
  194. <ErrorMessage
  195. baseUrl={baseUrl}
  196. codeMappingId={codeMappingId}
  197. codeMappings={codeMappings}
  198. errorJSON={mutation.error.responseJSON as {raw?: string}}
  199. />
  200. ) : null}
  201. </FileResult>
  202. </Form>
  203. );
  204. }
  205. function LinkCodeOwners({
  206. integrations,
  207. organization,
  208. }: {
  209. integrations: Integration[];
  210. organization: Organization;
  211. }) {
  212. const baseUrl = `/settings/${organization.slug}/integrations/`;
  213. if (integrations.length) {
  214. return (
  215. <Fragment>
  216. <div>
  217. {t(
  218. "Configure code mapping to add your CODEOWNERS file. Select the integration you'd like to use for mapping:"
  219. )}
  220. </div>
  221. <IntegrationsList>
  222. {integrations.map(integration => (
  223. <LinkButton
  224. key={integration.id}
  225. to={`${baseUrl}${integration.provider.key}/${integration.id}/?tab=codeMappings&referrer=add-codeowners`}
  226. >
  227. {getIntegrationIcon(integration.provider.key)}
  228. <IntegrationName>{integration.name}</IntegrationName>
  229. </LinkButton>
  230. ))}
  231. </IntegrationsList>
  232. </Fragment>
  233. );
  234. }
  235. return (
  236. <Fragment>
  237. <div>{t('Install a GitHub or GitLab integration to use this feature.')}</div>
  238. <Container style={{paddingTop: space(2)}}>
  239. <LinkButton priority="primary" size="sm" to={baseUrl}>
  240. Setup Integration
  241. </LinkButton>
  242. </Container>
  243. </Fragment>
  244. );
  245. }
  246. function SourceFile({codeownersFile}: {codeownersFile: CodeownersFile}) {
  247. return (
  248. <Panel>
  249. <SourceFileBody>
  250. <IconCheckmark size="md" isCircled color="green200" />
  251. {codeownersFile.filepath}
  252. <LinkButton size="sm" href={codeownersFile.html_url} external>
  253. {t('Preview File')}
  254. </LinkButton>
  255. </SourceFileBody>
  256. </Panel>
  257. );
  258. }
  259. function NoSourceFile() {
  260. return (
  261. <Panel>
  262. <NoSourceFileBody>
  263. <IconNot size="md" color="red200" />
  264. {t('No codeowner file found.')}
  265. </NoSourceFileBody>
  266. </Panel>
  267. );
  268. }
  269. function ErrorMessage({
  270. baseUrl,
  271. codeMappingId,
  272. codeMappings,
  273. errorJSON,
  274. }: {
  275. baseUrl: string;
  276. codeMappingId: string | null;
  277. codeMappings: RepositoryProjectPathConfig[];
  278. errorJSON: {raw?: string} | null;
  279. }) {
  280. const codeMapping = codeMappings.find(mapping => mapping.id === codeMappingId);
  281. const errActors = errorJSON?.raw?.[0]!.split('\n').map((el, i) => <p key={i}>{el}</p>);
  282. return (
  283. <Alert type="error" showIcon>
  284. {errActors}
  285. {codeMapping && (
  286. <p>
  287. {tct(
  288. 'Configure [userMappingsLink:User Mappings] or [teamMappingsLink:Team Mappings] for any missing associations.',
  289. {
  290. userMappingsLink: (
  291. <Link
  292. to={`${baseUrl}${codeMapping.provider?.key ?? ''}/${codeMapping.integrationId ?? ''}/?tab=userMappings&referrer=add-codeowners`}
  293. />
  294. ),
  295. teamMappingsLink: (
  296. <Link
  297. to={`${baseUrl}${codeMapping.provider?.key ?? ''}/${codeMapping.integrationId ?? ''}/?tab=teamMappings&referrer=add-codeowners`}
  298. />
  299. ),
  300. }
  301. )}
  302. </p>
  303. )}
  304. {tct(
  305. '[addAndSkip:Add and Skip Missing Associations] will add your codeowner file and skip any rules that having missing associations. You can add associations later for any skipped rules.',
  306. {addAndSkip: <strong>Add and Skip Missing Associations</strong>}
  307. )}
  308. </Alert>
  309. );
  310. }
  311. const StyledSelectField = styled(SelectField)`
  312. border-bottom: None;
  313. padding-right: 16px;
  314. `;
  315. const FileResult = styled('div')`
  316. width: inherit;
  317. `;
  318. const NoSourceFileBody = styled(PanelBody)`
  319. display: grid;
  320. padding: 12px;
  321. grid-template-columns: 30px 1fr;
  322. align-items: center;
  323. `;
  324. const SourceFileBody = styled(PanelBody)`
  325. display: grid;
  326. padding: 12px;
  327. grid-template-columns: 30px 1fr 100px;
  328. align-items: center;
  329. `;
  330. const IntegrationsList = styled('div')`
  331. display: grid;
  332. gap: ${space(1)};
  333. justify-items: center;
  334. margin-top: ${space(2)};
  335. `;
  336. const IntegrationName = styled('p')`
  337. padding-left: 10px;
  338. `;
  339. const Container = styled('div')`
  340. display: flex;
  341. justify-content: center;
  342. `;