index.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import {Fragment} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import {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 = (text: string | null) => {
  71. this.setState(prevState => ({
  72. ...(prevState.ownership
  73. ? {
  74. ownership: {
  75. ...prevState.ownership,
  76. raw: text || '',
  77. },
  78. }
  79. : {}),
  80. }));
  81. };
  82. handleCodeOwnerAdded = (data: CodeOwner) => {
  83. const {codeowners} = this.state;
  84. const newCodeowners = [data, ...(codeowners || [])];
  85. this.setState({codeowners: newCodeowners});
  86. };
  87. handleCodeOwnerDeleted = (data: CodeOwner) => {
  88. const {codeowners} = this.state;
  89. const newCodeowners = (codeowners || []).filter(
  90. codeowner => codeowner.id !== data.id
  91. );
  92. this.setState({codeowners: newCodeowners});
  93. };
  94. handleCodeOwnerUpdated = (data: CodeOwner) => {
  95. const codeowners = this.state.codeowners || [];
  96. const index = codeowners.findIndex(item => item.id === data.id);
  97. this.setState({
  98. codeowners: [...codeowners.slice(0, index), data, ...codeowners.slice(index + 1)],
  99. });
  100. };
  101. renderBody() {
  102. const {project, organization} = this.props;
  103. const {ownership, codeowners} = this.state;
  104. const disabled = !hasEveryAccess(['project:write'], {organization, project});
  105. const editOwnershipRulesDisabled = !hasEveryAccess(['project:read'], {
  106. organization,
  107. project,
  108. });
  109. const hasCodeowners = organization.features?.includes('integrations-codeowners');
  110. return (
  111. <Fragment>
  112. <SettingsPageHeader
  113. title={this.getOwnershipTitle()}
  114. action={
  115. <ButtonBar gap={1}>
  116. {hasCodeowners && (
  117. <Access access={['org:integrations']} project={project}>
  118. {({hasAccess}) => (
  119. <Button
  120. onClick={this.handleAddCodeOwner}
  121. size="sm"
  122. data-test-id="add-codeowner-button"
  123. disabled={!hasAccess}
  124. >
  125. {t('Import CODEOWNERS')}
  126. </Button>
  127. )}
  128. </Access>
  129. )}
  130. <Button
  131. type="button"
  132. size="sm"
  133. icon={<IconEdit />}
  134. priority="primary"
  135. onClick={() =>
  136. openEditOwnershipRules({
  137. organization,
  138. project,
  139. ownership: ownership!,
  140. onSave: this.handleOwnershipSave,
  141. })
  142. }
  143. disabled={!!ownership && editOwnershipRulesDisabled}
  144. >
  145. {t('Edit Rules')}
  146. </Button>
  147. </ButtonBar>
  148. }
  149. />
  150. <TextBlock>
  151. {tct(
  152. `Auto-assign issues to users and teams. To learn more, [link:read the docs].`,
  153. {
  154. link: (
  155. <ExternalLink href="https://docs.sentry.io/product/error-monitoring/issue-owners/" />
  156. ),
  157. }
  158. )}
  159. </TextBlock>
  160. <PermissionAlert
  161. access={!editOwnershipRulesDisabled ? ['project:read'] : ['project:write']}
  162. project={project}
  163. />
  164. <CodeOwnerErrors
  165. orgSlug={organization.slug}
  166. projectSlug={project.slug}
  167. codeowners={codeowners ?? []}
  168. />
  169. {ownership && (
  170. <ErrorBoundary mini>
  171. <OwnershipRulesTable
  172. projectRules={ownership.schema?.rules ?? []}
  173. codeowners={codeowners ?? []}
  174. />
  175. </ErrorBoundary>
  176. )}
  177. <PermissionAlert project={project} />
  178. {hasCodeowners && (
  179. <CodeOwnerFileTable
  180. project={project}
  181. codeowners={codeowners ?? []}
  182. onDelete={this.handleCodeOwnerDeleted}
  183. onUpdate={this.handleCodeOwnerUpdated}
  184. disabled={disabled}
  185. />
  186. )}
  187. {ownership && (
  188. <Form
  189. apiEndpoint={`/projects/${organization.slug}/${project.slug}/ownership/`}
  190. apiMethod="PUT"
  191. saveOnBlur
  192. initialData={{
  193. fallthrough: ownership.fallthrough,
  194. autoAssignment: ownership.autoAssignment,
  195. codeownersAutoSync: ownership.codeownersAutoSync,
  196. }}
  197. hideFooter
  198. >
  199. <JsonForm
  200. forms={[
  201. {
  202. title: t('Issue Owners'),
  203. fields: [
  204. {
  205. name: 'autoAssignment',
  206. type: 'choice',
  207. label: t('Prioritize Auto Assignment'),
  208. help: t(
  209. "When there's a conflict between suspect commit and ownership rules."
  210. ),
  211. choices: [
  212. [
  213. 'Auto Assign to Suspect Commits',
  214. t('Auto-assign to suspect commits'),
  215. ],
  216. ['Auto Assign to Issue Owner', t('Auto-assign to issue owner')],
  217. ['Turn off Auto-Assignment', t('Turn off auto-assignment')],
  218. ],
  219. disabled,
  220. },
  221. {
  222. name: 'codeownersAutoSync',
  223. type: 'boolean',
  224. label: t('Sync changes from CODEOWNERS'),
  225. help: t(
  226. 'We’ll update any changes you make to your CODEOWNERS files during a release.'
  227. ),
  228. disabled: disabled || !(this.state.codeowners || []).length,
  229. },
  230. ],
  231. },
  232. ]}
  233. />
  234. </Form>
  235. )}
  236. </Fragment>
  237. );
  238. }
  239. }
  240. export default ProjectOwnership;