externalIssuesList.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import styled from '@emotion/styled';
  2. import AlertLink from 'sentry/components/alertLink';
  3. import AsyncComponent from 'sentry/components/asyncComponent';
  4. import ErrorBoundary from 'sentry/components/errorBoundary';
  5. import ExternalIssueActions from 'sentry/components/group/externalIssueActions';
  6. import PluginActions from 'sentry/components/group/pluginActions';
  7. import SentryAppExternalIssueActions from 'sentry/components/group/sentryAppExternalIssueActions';
  8. import IssueSyncListElement from 'sentry/components/issueSyncListElement';
  9. import Placeholder from 'sentry/components/placeholder';
  10. import * as SidebarSection from 'sentry/components/sidebarSection';
  11. import {t} from 'sentry/locale';
  12. import ExternalIssueStore from 'sentry/stores/externalIssueStore';
  13. import SentryAppInstallationStore from 'sentry/stores/sentryAppInstallationsStore';
  14. import space from 'sentry/styles/space';
  15. import {
  16. Group,
  17. GroupIntegration,
  18. Organization,
  19. PlatformExternalIssue,
  20. Project,
  21. SentryAppComponent,
  22. SentryAppInstallation,
  23. } from 'sentry/types';
  24. import {Event} from 'sentry/types/event';
  25. import withOrganization from 'sentry/utils/withOrganization';
  26. import withSentryAppComponents from 'sentry/utils/withSentryAppComponents';
  27. type Props = AsyncComponent['props'] & {
  28. components: SentryAppComponent[];
  29. event: Event;
  30. group: Group;
  31. organization: Organization;
  32. project: Project;
  33. };
  34. type State = AsyncComponent['state'] & {
  35. externalIssues: PlatformExternalIssue[];
  36. integrations: GroupIntegration[];
  37. sentryAppInstallations: SentryAppInstallation[];
  38. };
  39. class ExternalIssueList extends AsyncComponent<Props, State> {
  40. unsubscribables: any[] = [];
  41. getEndpoints(): ReturnType<AsyncComponent['getEndpoints']> {
  42. const {group} = this.props;
  43. return [['integrations', `/groups/${group.id}/integrations/`]];
  44. }
  45. constructor(props: Props) {
  46. super(props, {});
  47. this.state = Object.assign({}, this.state, {
  48. sentryAppInstallations: SentryAppInstallationStore.getInitialState(),
  49. externalIssues: ExternalIssueStore.getInitialState(),
  50. });
  51. }
  52. UNSAFE_componentWillMount() {
  53. super.UNSAFE_componentWillMount();
  54. this.unsubscribables = [
  55. SentryAppInstallationStore.listen(this.onSentryAppInstallationChange, this),
  56. ExternalIssueStore.listen(this.onExternalIssueChange, this),
  57. ];
  58. this.fetchSentryAppData();
  59. }
  60. componentWillUnmount() {
  61. super.componentWillUnmount();
  62. this.unsubscribables.forEach(unsubscribe => unsubscribe());
  63. }
  64. onSentryAppInstallationChange = (sentryAppInstallations: SentryAppInstallation[]) => {
  65. this.setState({sentryAppInstallations});
  66. };
  67. onExternalIssueChange = (externalIssues: PlatformExternalIssue[]) => {
  68. this.setState({externalIssues});
  69. };
  70. // We want to do this explicitly so that we can handle errors gracefully,
  71. // instead of the entire component not rendering.
  72. //
  73. // Part of the API request here is fetching data from the Sentry App, so
  74. // we need to be more conservative about error cases since we don't have
  75. // control over those services.
  76. //
  77. fetchSentryAppData() {
  78. const {group, project, organization} = this.props;
  79. if (project && project.id && organization) {
  80. this.api
  81. .requestPromise(`/groups/${group.id}/external-issues/`)
  82. .then(data => {
  83. ExternalIssueStore.load(data);
  84. this.setState({externalIssues: data});
  85. })
  86. .catch(_error => {});
  87. }
  88. }
  89. async updateIntegrations(onSuccess = () => {}, onError = () => {}) {
  90. try {
  91. const {group} = this.props;
  92. const integrations = await this.api.requestPromise(
  93. `/groups/${group.id}/integrations/`
  94. );
  95. this.setState({integrations}, () => onSuccess());
  96. } catch (error) {
  97. onError();
  98. }
  99. }
  100. renderIntegrationIssues(integrations: GroupIntegration[] = []) {
  101. const {group} = this.props;
  102. const activeIntegrations = integrations.filter(
  103. integration => integration.status === 'active'
  104. );
  105. const activeIntegrationsByProvider: Map<string, GroupIntegration[]> =
  106. activeIntegrations.reduce((acc, curr) => {
  107. const items = acc.get(curr.provider.key);
  108. if (items) {
  109. acc.set(curr.provider.key, [...items, curr]);
  110. } else {
  111. acc.set(curr.provider.key, [curr]);
  112. }
  113. return acc;
  114. }, new Map());
  115. return activeIntegrations.length
  116. ? [...activeIntegrationsByProvider.entries()].map(([provider, configurations]) => (
  117. <ExternalIssueActions
  118. key={provider}
  119. configurations={configurations}
  120. group={group}
  121. onChange={this.updateIntegrations.bind(this)}
  122. />
  123. ))
  124. : null;
  125. }
  126. renderSentryAppIssues() {
  127. const {externalIssues, sentryAppInstallations} = this.state;
  128. const {components, group} = this.props;
  129. if (components.length === 0) {
  130. return null;
  131. }
  132. return components.map(component => {
  133. const {sentryApp, error: disabled} = component;
  134. const installation = sentryAppInstallations.find(
  135. i => i.app.uuid === sentryApp.uuid
  136. );
  137. // should always find a match but TS complains if we don't handle this case
  138. if (!installation) {
  139. return null;
  140. }
  141. const issue = (externalIssues || []).find(i => i.serviceType === sentryApp.slug);
  142. return (
  143. <ErrorBoundary key={sentryApp.slug} mini>
  144. <SentryAppExternalIssueActions
  145. key={sentryApp.slug}
  146. group={group}
  147. event={this.props.event}
  148. sentryAppComponent={component}
  149. sentryAppInstallation={installation}
  150. externalIssue={issue}
  151. disabled={disabled}
  152. />
  153. </ErrorBoundary>
  154. );
  155. });
  156. }
  157. renderPluginIssues() {
  158. const {group, project} = this.props;
  159. return group.pluginIssues && group.pluginIssues.length
  160. ? group.pluginIssues.map((plugin, i) => (
  161. <PluginActions group={group} project={project} plugin={plugin} key={i} />
  162. ))
  163. : null;
  164. }
  165. renderPluginActions() {
  166. const {group} = this.props;
  167. return group.pluginActions && group.pluginActions.length
  168. ? group.pluginActions.map((plugin, i) => (
  169. <IssueSyncListElement externalIssueLink={plugin[1]} key={i}>
  170. {plugin[0]}
  171. </IssueSyncListElement>
  172. ))
  173. : null;
  174. }
  175. renderLoading() {
  176. return (
  177. <SidebarSection.Wrap data-test-id="linked-issues">
  178. <SidebarSection.Title>{t('Linked Issues')}</SidebarSection.Title>
  179. <SidebarSection.Content>
  180. <Placeholder height="120px" />
  181. </SidebarSection.Content>
  182. </SidebarSection.Wrap>
  183. );
  184. }
  185. renderBody() {
  186. const sentryAppIssues = this.renderSentryAppIssues();
  187. const integrationIssues = this.renderIntegrationIssues(this.state.integrations);
  188. const pluginIssues = this.renderPluginIssues();
  189. const pluginActions = this.renderPluginActions();
  190. const showSetup =
  191. !sentryAppIssues && !integrationIssues && !pluginIssues && !pluginActions;
  192. return (
  193. <SidebarSection.Wrap data-test-id="linked-issues">
  194. <SidebarSection.Title>{t('Issue Tracking')}</SidebarSection.Title>
  195. <SidebarSection.Content>
  196. {showSetup && (
  197. <AlertLink
  198. priority="muted"
  199. size="small"
  200. to={`/settings/${this.props.organization.slug}/integrations/?category=issue%20tracking`}
  201. >
  202. {t('Track this issue in Jira, GitHub, etc.')}
  203. </AlertLink>
  204. )}
  205. {sentryAppIssues && <Wrapper>{sentryAppIssues}</Wrapper>}
  206. {integrationIssues && <Wrapper>{integrationIssues}</Wrapper>}
  207. {pluginIssues && <Wrapper>{pluginIssues}</Wrapper>}
  208. {pluginActions && <Wrapper>{pluginActions}</Wrapper>}
  209. </SidebarSection.Content>
  210. </SidebarSection.Wrap>
  211. );
  212. }
  213. }
  214. const Wrapper = styled('div')`
  215. margin-bottom: ${space(2)};
  216. `;
  217. export default withSentryAppComponents(withOrganization(ExternalIssueList), {
  218. componentType: 'issue-link',
  219. });