index.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import {Fragment} from 'react';
  2. import {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 {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 CodeOwnersPanel from 'sentry/views/settings/project/projectOwnership/codeowners';
  23. import {OwnershipRulesTable} from 'sentry/views/settings/project/projectOwnership/ownershipRulesTable';
  24. import RulesPanel from 'sentry/views/settings/project/projectOwnership/rulesPanel';
  25. type Props = {
  26. organization: Organization;
  27. project: Project;
  28. } & RouteComponentProps<{projectId: string}, {}>;
  29. type State = {
  30. codeowners?: CodeOwner[];
  31. ownership?: null | IssueOwnership;
  32. } & DeprecatedAsyncView['state'];
  33. class ProjectOwnership extends DeprecatedAsyncView<Props, State> {
  34. // TODO: Remove with `streamline-targeting-context`
  35. getOwnershipTitle() {
  36. const {organization} = this.props;
  37. return organization.features?.includes('streamline-targeting-context')
  38. ? t('Ownership Rules')
  39. : t('Issue Owners');
  40. }
  41. getTitle() {
  42. const {project} = this.props;
  43. return routeTitleGen(this.getOwnershipTitle(), project.slug, false);
  44. }
  45. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  46. const {organization, project} = this.props;
  47. const endpoints: ReturnType<DeprecatedAsyncView['getEndpoints']> = [
  48. ['ownership', `/projects/${organization.slug}/${project.slug}/ownership/`],
  49. ];
  50. if (organization.features.includes('integrations-codeowners')) {
  51. endpoints.push([
  52. 'codeowners',
  53. `/projects/${organization.slug}/${project.slug}/codeowners/`,
  54. {query: {expand: ['codeMapping', 'ownershipSyntax']}},
  55. ]);
  56. }
  57. return endpoints;
  58. }
  59. handleAddCodeOwner = () => {
  60. openModal(modalProps => (
  61. <AddCodeOwnerModal
  62. {...modalProps}
  63. organization={this.props.organization}
  64. project={this.props.project}
  65. onSave={this.handleCodeOwnerAdded}
  66. />
  67. ));
  68. };
  69. getPlaceholder() {
  70. return `#example usage
  71. path:src/example/pipeline/* person@sentry.io #infra
  72. module:com.module.name.example #sdks
  73. url:http://example.com/settings/* #product
  74. tags.sku_class:enterprise #enterprise`;
  75. }
  76. handleOwnershipSave = (text: string | null) => {
  77. this.setState(prevState => ({
  78. ...(prevState.ownership
  79. ? {
  80. ownership: {
  81. ...prevState.ownership,
  82. raw: text || '',
  83. },
  84. }
  85. : {}),
  86. }));
  87. };
  88. handleCodeOwnerAdded = (data: CodeOwner) => {
  89. const {codeowners} = this.state;
  90. const newCodeowners = [data, ...(codeowners || [])];
  91. this.setState({codeowners: newCodeowners});
  92. };
  93. handleCodeOwnerDeleted = (data: CodeOwner) => {
  94. const {codeowners} = this.state;
  95. const newCodeowners = (codeowners || []).filter(
  96. codeowner => codeowner.id !== data.id
  97. );
  98. this.setState({codeowners: newCodeowners});
  99. };
  100. handleCodeOwnerUpdated = (data: CodeOwner) => {
  101. const codeowners = this.state.codeowners || [];
  102. const index = codeowners.findIndex(item => item.id === data.id);
  103. this.setState({
  104. codeowners: [...codeowners.slice(0, index), data, ...codeowners.slice(index + 1)],
  105. });
  106. };
  107. renderBody() {
  108. const {project, organization} = this.props;
  109. const {ownership, codeowners} = this.state;
  110. const disabled = !hasEveryAccess(['project:write'], {organization, project});
  111. const editOwnershipRulesDisabled = !hasEveryAccess(['project:read'], {
  112. organization,
  113. project,
  114. });
  115. const hasStreamlineTargetingContext = organization.features?.includes(
  116. 'streamline-targeting-context'
  117. );
  118. const hasCodeowners = organization.features?.includes('integrations-codeowners');
  119. return (
  120. <Fragment>
  121. <SettingsPageHeader
  122. title={this.getOwnershipTitle()}
  123. action={
  124. <ButtonBar gap={1}>
  125. {hasCodeowners && (
  126. <Access access={['org:integrations']} project={project}>
  127. {({hasAccess}) => (
  128. <Button
  129. onClick={this.handleAddCodeOwner}
  130. size="sm"
  131. data-test-id="add-codeowner-button"
  132. disabled={!hasAccess}
  133. >
  134. {t('Import CODEOWNERS')}
  135. </Button>
  136. )}
  137. </Access>
  138. )}
  139. {hasStreamlineTargetingContext && (
  140. <Button
  141. type="button"
  142. size="sm"
  143. icon={<IconEdit size="xs" />}
  144. priority="primary"
  145. onClick={() =>
  146. openEditOwnershipRules({
  147. organization,
  148. project,
  149. ownership: ownership!,
  150. onSave: this.handleOwnershipSave,
  151. })
  152. }
  153. disabled={!!ownership && editOwnershipRulesDisabled}
  154. >
  155. {t('Edit Rules')}
  156. </Button>
  157. )}
  158. </ButtonBar>
  159. }
  160. />
  161. <TextBlock>
  162. {tct(
  163. `Auto-assign issues to users and teams. To learn more, [link:read the docs].`,
  164. {
  165. link: (
  166. <ExternalLink href="https://docs.sentry.io/product/error-monitoring/issue-owners/" />
  167. ),
  168. }
  169. )}
  170. </TextBlock>
  171. <PermissionAlert
  172. access={!editOwnershipRulesDisabled ? ['project:read'] : ['project:write']}
  173. project={project}
  174. />
  175. <CodeOwnerErrors
  176. orgSlug={organization.slug}
  177. projectSlug={project.slug}
  178. codeowners={codeowners ?? []}
  179. />
  180. {hasStreamlineTargetingContext && ownership && (
  181. <ErrorBoundary mini>
  182. <OwnershipRulesTable
  183. projectRules={ownership.schema?.rules ?? []}
  184. codeowners={codeowners ?? []}
  185. />
  186. </ErrorBoundary>
  187. )}
  188. {!hasStreamlineTargetingContext && ownership && (
  189. <RulesPanel
  190. data-test-id="issueowners-panel"
  191. type="issueowners"
  192. raw={ownership.raw || ''}
  193. dateUpdated={ownership.lastUpdated}
  194. placeholder={this.getPlaceholder()}
  195. controls={[
  196. <Button
  197. key="edit"
  198. size="xs"
  199. onClick={() =>
  200. openEditOwnershipRules({
  201. organization,
  202. project,
  203. ownership,
  204. onSave: this.handleOwnershipSave,
  205. })
  206. }
  207. disabled={editOwnershipRulesDisabled}
  208. >
  209. {t('Edit')}
  210. </Button>,
  211. ]}
  212. />
  213. )}
  214. <PermissionAlert project={project} />
  215. {hasCodeowners &&
  216. (hasStreamlineTargetingContext ? (
  217. <CodeOwnerFileTable
  218. project={project}
  219. codeowners={codeowners ?? []}
  220. onDelete={this.handleCodeOwnerDeleted}
  221. onUpdate={this.handleCodeOwnerUpdated}
  222. disabled={disabled}
  223. />
  224. ) : (
  225. <CodeOwnersPanel
  226. codeowners={codeowners || []}
  227. onDelete={this.handleCodeOwnerDeleted}
  228. onUpdate={this.handleCodeOwnerUpdated}
  229. disabled={disabled}
  230. {...this.props}
  231. />
  232. ))}
  233. {ownership && (
  234. <Form
  235. apiEndpoint={`/projects/${organization.slug}/${project.slug}/ownership/`}
  236. apiMethod="PUT"
  237. saveOnBlur
  238. initialData={{
  239. fallthrough: ownership.fallthrough,
  240. autoAssignment: ownership.autoAssignment,
  241. codeownersAutoSync: ownership.codeownersAutoSync,
  242. }}
  243. hideFooter
  244. >
  245. <JsonForm
  246. forms={[
  247. {
  248. title: t('Issue Owners'),
  249. fields: [
  250. {
  251. name: 'autoAssignment',
  252. type: 'choice',
  253. label: t('Prioritize Auto Assignment'),
  254. help: t(
  255. "When there's a conflict between suspect commit and ownership rules."
  256. ),
  257. choices: [
  258. [
  259. 'Auto Assign to Suspect Commits',
  260. t('Auto-assign to suspect commits'),
  261. ],
  262. ['Auto Assign to Issue Owner', t('Auto-assign to issue owner')],
  263. ['Turn off Auto-Assignment', t('Turn off auto-assignment')],
  264. ],
  265. disabled,
  266. },
  267. ...(organization.features.includes('issue-alert-fallback-targeting')
  268. ? []
  269. : [
  270. {
  271. name: 'fallthrough',
  272. type: 'boolean' as const,
  273. label: t(
  274. 'Send alert to project members if there’s no assigned owner'
  275. ),
  276. help: t(
  277. 'Alerts will be sent to all users who have access to this project.'
  278. ),
  279. disabled,
  280. },
  281. ]),
  282. {
  283. name: 'codeownersAutoSync',
  284. type: 'boolean',
  285. label: t('Sync changes from CODEOWNERS'),
  286. help: t(
  287. 'We’ll update any changes you make to your CODEOWNERS files during a release.'
  288. ),
  289. disabled: disabled || !(this.state.codeowners || []).length,
  290. },
  291. ],
  292. },
  293. ]}
  294. />
  295. </Form>
  296. )}
  297. </Fragment>
  298. );
  299. }
  300. }
  301. export default ProjectOwnership;