externalIssuesList.tsx 7.8 KB

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