index.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import {closeModal, openEditOwnershipRules, openModal} from 'sentry/actionCreators/modal';
  4. import Access, {hasEveryAccess} from 'sentry/components/acl/access';
  5. import {Button} from 'sentry/components/button';
  6. import ButtonBar from 'sentry/components/buttonBar';
  7. import ErrorBoundary from 'sentry/components/errorBoundary';
  8. import Form from 'sentry/components/forms/form';
  9. import JsonForm from 'sentry/components/forms/jsonForm';
  10. import ExternalLink from 'sentry/components/links/externalLink';
  11. import {IconEdit} from 'sentry/icons';
  12. import {t, tct} from 'sentry/locale';
  13. import type {CodeOwner, IssueOwnership, Organization, Project} from 'sentry/types';
  14. import routeTitleGen from 'sentry/utils/routeTitle';
  15. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  16. import SettingsPageHeader from 'sentry/views/settings/components/settingsPageHeader';
  17. import TextBlock from 'sentry/views/settings/components/text/textBlock';
  18. import PermissionAlert from 'sentry/views/settings/project/permissionAlert';
  19. import AddCodeOwnerModal from 'sentry/views/settings/project/projectOwnership/addCodeOwnerModal';
  20. import {CodeOwnerErrors} from 'sentry/views/settings/project/projectOwnership/codeownerErrors';
  21. import {CodeOwnerFileTable} from 'sentry/views/settings/project/projectOwnership/codeOwnerFileTable';
  22. import {OwnershipRulesTable} from 'sentry/views/settings/project/projectOwnership/ownershipRulesTable';
  23. type Props = {
  24. organization: Organization;
  25. project: Project;
  26. } & RouteComponentProps<{projectId: string}, {}>;
  27. type State = {
  28. codeowners?: CodeOwner[];
  29. ownership?: null | IssueOwnership;
  30. } & DeprecatedAsyncView['state'];
  31. class ProjectOwnership extends DeprecatedAsyncView<Props, State> {
  32. getOwnershipTitle() {
  33. return t('Ownership Rules');
  34. }
  35. getTitle() {
  36. const {project} = this.props;
  37. return routeTitleGen(this.getOwnershipTitle(), project.slug, false);
  38. }
  39. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  40. const {organization, project} = this.props;
  41. const endpoints: ReturnType<DeprecatedAsyncView['getEndpoints']> = [
  42. ['ownership', `/projects/${organization.slug}/${project.slug}/ownership/`],
  43. ];
  44. if (organization.features.includes('integrations-codeowners')) {
  45. endpoints.push([
  46. 'codeowners',
  47. `/projects/${organization.slug}/${project.slug}/codeowners/`,
  48. {query: {expand: ['codeMapping', 'ownershipSyntax']}},
  49. ]);
  50. }
  51. return endpoints;
  52. }
  53. handleAddCodeOwner = () => {
  54. openModal(modalProps => (
  55. <AddCodeOwnerModal
  56. {...modalProps}
  57. organization={this.props.organization}
  58. project={this.props.project}
  59. onSave={this.handleCodeOwnerAdded}
  60. />
  61. ));
  62. };
  63. getPlaceholder() {
  64. return `#example usage
  65. path:src/example/pipeline/* person@sentry.io #infra
  66. module:com.module.name.example #sdks
  67. url:http://example.com/settings/* #product
  68. tags.sku_class:enterprise #enterprise`;
  69. }
  70. handleOwnershipSave = (ownership: IssueOwnership) => {
  71. this.setState(prevState => ({
  72. ...prevState,
  73. ownership,
  74. }));
  75. closeModal();
  76. };
  77. handleCodeOwnerAdded = (data: CodeOwner) => {
  78. const {codeowners} = this.state;
  79. const newCodeowners = [data, ...(codeowners || [])];
  80. this.setState({codeowners: newCodeowners});
  81. };
  82. handleCodeOwnerDeleted = (data: CodeOwner) => {
  83. const {codeowners} = this.state;
  84. const newCodeowners = (codeowners || []).filter(
  85. codeowner => codeowner.id !== data.id
  86. );
  87. this.setState({codeowners: newCodeowners});
  88. };
  89. handleCodeOwnerUpdated = (data: CodeOwner) => {
  90. const codeowners = this.state.codeowners || [];
  91. const index = codeowners.findIndex(item => item.id === data.id);
  92. this.setState({
  93. codeowners: [...codeowners.slice(0, index), data, ...codeowners.slice(index + 1)],
  94. });
  95. };
  96. renderBody() {
  97. const {project, organization} = this.props;
  98. const {ownership, codeowners} = this.state;
  99. const disabled = !hasEveryAccess(['project:write'], {organization, project});
  100. const editOwnershipRulesDisabled = !hasEveryAccess(['project:read'], {
  101. organization,
  102. project,
  103. });
  104. const hasCodeowners = organization.features?.includes('integrations-codeowners');
  105. return (
  106. <Fragment>
  107. <SettingsPageHeader
  108. title={this.getOwnershipTitle()}
  109. action={
  110. <ButtonBar gap={1}>
  111. {hasCodeowners && (
  112. <Access access={['org:integrations']} project={project}>
  113. {({hasAccess}) => (
  114. <Button
  115. onClick={this.handleAddCodeOwner}
  116. size="sm"
  117. data-test-id="add-codeowner-button"
  118. disabled={!hasAccess}
  119. >
  120. {t('Import CODEOWNERS')}
  121. </Button>
  122. )}
  123. </Access>
  124. )}
  125. <Button
  126. type="button"
  127. size="sm"
  128. icon={<IconEdit />}
  129. priority="primary"
  130. onClick={() =>
  131. openEditOwnershipRules({
  132. organization,
  133. project,
  134. ownership: ownership!,
  135. onSave: this.handleOwnershipSave,
  136. })
  137. }
  138. disabled={!!ownership && editOwnershipRulesDisabled}
  139. >
  140. {t('Edit Rules')}
  141. </Button>
  142. </ButtonBar>
  143. }
  144. />
  145. <TextBlock>
  146. {tct(
  147. `Auto-assign issues to users and teams. To learn more, [link:read the docs].`,
  148. {
  149. link: (
  150. <ExternalLink href="https://docs.sentry.io/product/error-monitoring/issue-owners/" />
  151. ),
  152. }
  153. )}
  154. </TextBlock>
  155. <PermissionAlert
  156. access={!editOwnershipRulesDisabled ? ['project:read'] : ['project:write']}
  157. project={project}
  158. />
  159. <CodeOwnerErrors
  160. orgSlug={organization.slug}
  161. projectSlug={project.slug}
  162. codeowners={codeowners ?? []}
  163. />
  164. {ownership && (
  165. <ErrorBoundary mini>
  166. <OwnershipRulesTable
  167. projectRules={ownership.schema?.rules ?? []}
  168. codeowners={codeowners ?? []}
  169. />
  170. </ErrorBoundary>
  171. )}
  172. <PermissionAlert project={project} />
  173. {hasCodeowners && (
  174. <CodeOwnerFileTable
  175. project={project}
  176. codeowners={codeowners ?? []}
  177. onDelete={this.handleCodeOwnerDeleted}
  178. onUpdate={this.handleCodeOwnerUpdated}
  179. disabled={disabled}
  180. />
  181. )}
  182. {ownership && (
  183. <Form
  184. apiEndpoint={`/projects/${organization.slug}/${project.slug}/ownership/`}
  185. apiMethod="PUT"
  186. saveOnBlur
  187. initialData={{
  188. fallthrough: ownership.fallthrough,
  189. autoAssignment: ownership.autoAssignment,
  190. codeownersAutoSync: ownership.codeownersAutoSync,
  191. }}
  192. hideFooter
  193. >
  194. <JsonForm
  195. forms={[
  196. {
  197. title: t('Issue Owners'),
  198. fields: [
  199. {
  200. name: 'autoAssignment',
  201. type: 'choice',
  202. label: t('Prioritize Auto Assignment'),
  203. help: t(
  204. "When there's a conflict between suspect commit and ownership rules."
  205. ),
  206. choices: [
  207. [
  208. 'Auto Assign to Suspect Commits',
  209. t('Auto-assign to suspect commits'),
  210. ],
  211. ['Auto Assign to Issue Owner', t('Auto-assign to issue owner')],
  212. ['Turn off Auto-Assignment', t('Turn off auto-assignment')],
  213. ],
  214. disabled,
  215. },
  216. {
  217. name: 'codeownersAutoSync',
  218. type: 'boolean',
  219. label: t('Sync changes from CODEOWNERS'),
  220. help: t(
  221. 'We’ll update any changes you make to your CODEOWNERS files during a release.'
  222. ),
  223. disabled: disabled || !(this.state.codeowners || []).length,
  224. },
  225. ],
  226. },
  227. ]}
  228. />
  229. </Form>
  230. )}
  231. </Fragment>
  232. );
  233. }
  234. }
  235. export default ProjectOwnership;