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 {t} from 'sentry/locale';
  11. import ExternalIssueStore from 'sentry/stores/externalIssueStore';
  12. import SentryAppComponentsStore from 'sentry/stores/sentryAppComponentsStore';
  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 SidebarSection from './sidebarSection';
  27. type Props = AsyncComponent['props'] & {
  28. event: Event;
  29. group: Group;
  30. organization: Organization;
  31. project: Project;
  32. };
  33. type State = AsyncComponent['state'] & {
  34. components: SentryAppComponent[];
  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. components: SentryAppComponentsStore.getInitialState(),
  49. sentryAppInstallations: SentryAppInstallationStore.getInitialState(),
  50. externalIssues: ExternalIssueStore.getInitialState(),
  51. });
  52. }
  53. UNSAFE_componentWillMount() {
  54. super.UNSAFE_componentWillMount();
  55. this.unsubscribables = [
  56. SentryAppInstallationStore.listen(this.onSentryAppInstallationChange, this),
  57. ExternalIssueStore.listen(this.onExternalIssueChange, this),
  58. SentryAppComponentsStore.listen(this.onSentryAppComponentsChange, this),
  59. ];
  60. this.fetchSentryAppData();
  61. }
  62. componentWillUnmount() {
  63. super.componentWillUnmount();
  64. this.unsubscribables.forEach(unsubscribe => unsubscribe());
  65. }
  66. onSentryAppInstallationChange = (sentryAppInstallations: SentryAppInstallation[]) => {
  67. this.setState({sentryAppInstallations});
  68. };
  69. onExternalIssueChange = (externalIssues: PlatformExternalIssue[]) => {
  70. this.setState({externalIssues});
  71. };
  72. onSentryAppComponentsChange = (sentryAppComponents: SentryAppComponent[]) => {
  73. const components = sentryAppComponents.filter(c => c.type === 'issue-link');
  74. this.setState({components});
  75. };
  76. // We want to do this explicitly so that we can handle errors gracefully,
  77. // instead of the entire component not rendering.
  78. //
  79. // Part of the API request here is fetching data from the Sentry App, so
  80. // we need to be more conservative about error cases since we don't have
  81. // control over those services.
  82. //
  83. fetchSentryAppData() {
  84. const {group, project, organization} = this.props;
  85. if (project && project.id && organization) {
  86. this.api
  87. .requestPromise(`/groups/${group.id}/external-issues/`)
  88. .then(data => {
  89. ExternalIssueStore.load(data);
  90. this.setState({externalIssues: data});
  91. })
  92. .catch(_error => {});
  93. }
  94. }
  95. async updateIntegrations(onSuccess = () => {}, onError = () => {}) {
  96. try {
  97. const {group} = this.props;
  98. const integrations = await this.api.requestPromise(
  99. `/groups/${group.id}/integrations/`
  100. );
  101. this.setState({integrations}, () => onSuccess());
  102. } catch (error) {
  103. onError();
  104. }
  105. }
  106. renderIntegrationIssues(integrations: GroupIntegration[] = []) {
  107. const {group} = this.props;
  108. const activeIntegrations = integrations.filter(
  109. integration => integration.status === 'active'
  110. );
  111. const activeIntegrationsByProvider: Map<string, GroupIntegration[]> =
  112. activeIntegrations.reduce((acc, curr) => {
  113. const items = acc.get(curr.provider.key);
  114. if (!!items) {
  115. acc.set(curr.provider.key, [...items, curr]);
  116. } else {
  117. acc.set(curr.provider.key, [curr]);
  118. }
  119. return acc;
  120. }, new Map());
  121. return activeIntegrations.length
  122. ? [...activeIntegrationsByProvider.entries()].map(([provider, configurations]) => (
  123. <ExternalIssueActions
  124. key={provider}
  125. configurations={configurations}
  126. group={group}
  127. onChange={this.updateIntegrations.bind(this)}
  128. />
  129. ))
  130. : null;
  131. }
  132. renderSentryAppIssues() {
  133. const {externalIssues, sentryAppInstallations, components} = this.state;
  134. const {group} = this.props;
  135. if (components.length === 0) {
  136. return null;
  137. }
  138. return components.map(component => {
  139. const {sentryApp, error: disabled} = component;
  140. const installation = sentryAppInstallations.find(
  141. i => i.app.uuid === sentryApp.uuid
  142. );
  143. // should always find a match but TS complains if we don't handle this case
  144. if (!installation) {
  145. return null;
  146. }
  147. const issue = (externalIssues || []).find(i => i.serviceType === sentryApp.slug);
  148. return (
  149. <ErrorBoundary key={sentryApp.slug} mini>
  150. <SentryAppExternalIssueActions
  151. key={sentryApp.slug}
  152. group={group}
  153. event={this.props.event}
  154. sentryAppComponent={component}
  155. sentryAppInstallation={installation}
  156. externalIssue={issue}
  157. disabled={disabled}
  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 secondary data-test-id="linked-issues" title={t('Issue Tracking')}>
  197. {showSetup && (
  198. <AlertLink
  199. priority="muted"
  200. size="small"
  201. to={`/settings/${this.props.organization.slug}/integrations/?category=issue%20tracking`}
  202. >
  203. {t('Track this issue in Jira, GitHub, etc.')}
  204. </AlertLink>
  205. )}
  206. {sentryAppIssues && <Wrapper>{sentryAppIssues}</Wrapper>}
  207. {integrationIssues && <Wrapper>{integrationIssues}</Wrapper>}
  208. {pluginIssues && <Wrapper>{pluginIssues}</Wrapper>}
  209. {pluginActions && <Wrapper>{pluginActions}</Wrapper>}
  210. </SidebarSection>
  211. );
  212. }
  213. }
  214. const Wrapper = styled('div')`
  215. margin-bottom: ${space(2)};
  216. `;
  217. export default withOrganization(ExternalIssueList);