suggestedOwners.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import {Fragment} from 'react';
  2. import {assignToActor, assignToUser} from 'sentry/actionCreators/group';
  3. import {promptsCheck, promptsUpdate} from 'sentry/actionCreators/prompts';
  4. import {Client} from 'sentry/api';
  5. import AsyncComponent from 'sentry/components/asyncComponent';
  6. import {Actor, CodeOwner, Committer, Group, Organization, Project} from 'sentry/types';
  7. import {Event} from 'sentry/types/event';
  8. import {trackIntegrationAnalytics} from 'sentry/utils/integrationUtil';
  9. import {promptIsDismissed} from 'sentry/utils/promptIsDismissed';
  10. import withApi from 'sentry/utils/withApi';
  11. import withCommitters from 'sentry/utils/withCommitters';
  12. import withOrganization from 'sentry/utils/withOrganization';
  13. import {findMatchedRules, Rules} from './findMatchedRules';
  14. import {OwnershipRules} from './ownershipRules';
  15. import {SuggestedAssignees} from './suggestedAssignees';
  16. type OwnerList = React.ComponentProps<typeof SuggestedAssignees>['owners'];
  17. type Props = {
  18. api: Client;
  19. event: Event;
  20. group: Group;
  21. organization: Organization;
  22. project: Project;
  23. committers?: Committer[];
  24. } & AsyncComponent['props'];
  25. type State = {
  26. codeowners: CodeOwner[];
  27. event_owners: {owners: Array<Actor>; rules: Rules};
  28. isDismissed: boolean;
  29. } & AsyncComponent['state'];
  30. class SuggestedOwners extends AsyncComponent<Props, State> {
  31. getDefaultState() {
  32. return {
  33. ...super.getDefaultState(),
  34. event: {rules: [], owners: []},
  35. codeowners: [],
  36. isDismissed: true,
  37. };
  38. }
  39. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  40. const {project, organization, event} = this.props;
  41. const endpoints = [
  42. [
  43. 'event_owners',
  44. `/projects/${organization.slug}/${project.slug}/events/${event.id}/owners/`,
  45. ],
  46. ];
  47. if (organization.features.includes('integrations-codeowners')) {
  48. endpoints.push([
  49. `codeowners`,
  50. `/projects/${organization.slug}/${project.slug}/codeowners/`,
  51. ]);
  52. }
  53. return endpoints as ReturnType<AsyncComponent['getEndpoints']>;
  54. }
  55. async onLoadAllEndpointsSuccess() {
  56. await this.checkCodeOwnersPrompt();
  57. }
  58. componentDidUpdate(prevProps: Props) {
  59. if (this.props.event && prevProps.event) {
  60. if (this.props.event.id !== prevProps.event.id) {
  61. // two events, with different IDs
  62. this.reloadData();
  63. }
  64. return;
  65. }
  66. if (this.props.event) {
  67. // going from having no event to having an event
  68. this.reloadData();
  69. }
  70. }
  71. async checkCodeOwnersPrompt() {
  72. const {api, organization, project} = this.props;
  73. this.setState({loading: true});
  74. // check our prompt backend
  75. const promptData = await promptsCheck(api, {
  76. organizationId: organization.id,
  77. projectId: project.id,
  78. feature: 'code_owners',
  79. });
  80. const isDismissed = promptIsDismissed(promptData, 30);
  81. this.setState({isDismissed, loading: false}, () => {
  82. if (!isDismissed) {
  83. // now record the results
  84. trackIntegrationAnalytics(
  85. 'integrations.show_code_owners_prompt',
  86. {
  87. view: 'stacktrace_issue_details',
  88. project_id: project.id,
  89. organization,
  90. },
  91. {startSession: true}
  92. );
  93. }
  94. });
  95. }
  96. handleCTAClose = () => {
  97. const {api, organization, project} = this.props;
  98. promptsUpdate(api, {
  99. organizationId: organization.id,
  100. projectId: project.id,
  101. feature: 'code_owners',
  102. status: 'dismissed',
  103. });
  104. this.setState({isDismissed: true}, () =>
  105. trackIntegrationAnalytics('integrations.dismissed_code_owners_prompt', {
  106. view: 'stacktrace_issue_details',
  107. project_id: project.id,
  108. organization,
  109. })
  110. );
  111. };
  112. /**
  113. * Combine the commiter and ownership data into a single array, merging
  114. * users who are both owners based on having commits, and owners matching
  115. * project ownership rules into one array.
  116. *
  117. * The return array will include objects of the format:
  118. *
  119. * {
  120. * actor: <
  121. * type, # Either user or team
  122. * SentryTypes.User, # API expanded user object
  123. * {email, id, name} # Sentry user which is *not* expanded
  124. * {email, name} # Unidentified user (from commits)
  125. * {id, name}, # Sentry team (check `type`)
  126. * >,
  127. *
  128. * # One or both of commits and rules will be present
  129. *
  130. * commits: [...] # List of commits made by this owner
  131. * rules: [...] # Project rules matched for this owner
  132. * }
  133. */
  134. getOwnerList() {
  135. const committers = this.props.committers ?? [];
  136. const owners = committers.map(commiter => ({
  137. actor: {...commiter.author, type: 'user' as Actor['type']},
  138. commits: commiter.commits,
  139. })) as OwnerList;
  140. this.state.event_owners.owners.forEach(owner => {
  141. const normalizedOwner = {
  142. actor: owner,
  143. rules: findMatchedRules(this.state.event_owners.rules || [], owner),
  144. };
  145. const existingIdx = owners.findIndex(o =>
  146. committers.length === 0 ? o.actor === owner : o.actor.email === owner.email
  147. );
  148. if (existingIdx > -1) {
  149. owners[existingIdx] = {...normalizedOwner, ...owners[existingIdx]};
  150. return;
  151. }
  152. owners.push(normalizedOwner);
  153. });
  154. return owners;
  155. }
  156. handleAssign = (actor: Actor) => () => {
  157. if (actor.id === undefined) {
  158. return;
  159. }
  160. const {event} = this.props;
  161. if (actor.type === 'user') {
  162. // TODO(ts): `event` here may not be 100% correct
  163. // in this case groupID should always exist on event
  164. // since this is only used in Issue Details
  165. assignToUser({
  166. id: event.groupID as string,
  167. user: actor,
  168. assignedBy: 'suggested_assignee',
  169. });
  170. }
  171. if (actor.type === 'team') {
  172. assignToActor({
  173. id: event.groupID as string,
  174. actor,
  175. assignedBy: 'suggested_assignee',
  176. });
  177. }
  178. };
  179. renderBody() {
  180. const {organization, project, group} = this.props;
  181. const {codeowners, isDismissed} = this.state;
  182. const owners = this.getOwnerList();
  183. return (
  184. <Fragment>
  185. {owners.length > 0 && (
  186. <SuggestedAssignees owners={owners} onAssign={this.handleAssign} />
  187. )}
  188. <OwnershipRules
  189. issueId={group.id}
  190. project={project}
  191. organization={organization}
  192. codeowners={codeowners}
  193. isDismissed={isDismissed}
  194. handleCTAClose={this.handleCTAClose}
  195. />
  196. </Fragment>
  197. );
  198. }
  199. }
  200. export default withApi(withOrganization(withCommitters(SuggestedOwners)));