index.tsx 8.7 KB

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