withReleaseRepos.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import {Component} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  4. import type {Client} from 'sentry/api';
  5. import {LinkButton} from 'sentry/components/button';
  6. import EmptyMessage from 'sentry/components/emptyMessage';
  7. import {Body, Main} from 'sentry/components/layouts/thirds';
  8. import LoadingIndicator from 'sentry/components/loadingIndicator';
  9. import Panel from 'sentry/components/panels/panel';
  10. import {IconCommit} from 'sentry/icons';
  11. import {t} from 'sentry/locale';
  12. import type {Repository} from 'sentry/types/integrations';
  13. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  14. import type {Organization} from 'sentry/types/organization';
  15. import getDisplayName from 'sentry/utils/getDisplayName';
  16. import withApi from 'sentry/utils/withApi';
  17. import withOrganization from 'sentry/utils/withOrganization';
  18. import withRepositories from 'sentry/utils/withRepositories';
  19. import {ReleaseContext} from '..';
  20. // These props are required when using this HoC
  21. type DependentProps = RouteComponentProps<{release: string}, {}>;
  22. type HoCsProps = {
  23. api: Client;
  24. organization: Organization;
  25. repositories?: Repository[];
  26. repositoriesError?: Error;
  27. repositoriesLoading?: boolean;
  28. };
  29. type State = {
  30. isLoading: boolean;
  31. releaseRepos: Repository[];
  32. activeReleaseRepo?: Repository;
  33. };
  34. function withReleaseRepos<P extends DependentProps>(
  35. WrappedComponent: React.ComponentType<P>
  36. ) {
  37. class WithReleaseRepos extends Component<P & HoCsProps, State> {
  38. static displayName = `withReleaseRepos(${getDisplayName(WrappedComponent)})`;
  39. state: State = {
  40. releaseRepos: [],
  41. isLoading: true,
  42. };
  43. componentDidMount() {
  44. this.fetchReleaseRepos();
  45. }
  46. componentDidUpdate(prevProps: P & HoCsProps, prevState: State) {
  47. if (
  48. this.props.params.release !== prevProps.params.release ||
  49. (!!prevProps.repositoriesLoading && !this.props.repositoriesLoading)
  50. ) {
  51. this.fetchReleaseRepos();
  52. return;
  53. }
  54. if (
  55. prevState.releaseRepos.length !== this.state.releaseRepos.length ||
  56. prevProps.location.query?.activeRepo !== this.props.location.query?.activeRepo
  57. ) {
  58. this.setActiveReleaseRepo(this.props);
  59. }
  60. }
  61. declare context: React.ContextType<typeof ReleaseContext>;
  62. static contextType = ReleaseContext;
  63. setActiveReleaseRepo(props: P & HoCsProps) {
  64. const {releaseRepos, activeReleaseRepo} = this.state;
  65. if (!releaseRepos.length) {
  66. return;
  67. }
  68. const activeCommitRepo = props.location.query?.activeRepo;
  69. if (!activeCommitRepo) {
  70. this.setState({
  71. activeReleaseRepo: releaseRepos[0] ?? null,
  72. });
  73. return;
  74. }
  75. if (activeCommitRepo === activeReleaseRepo?.name) {
  76. return;
  77. }
  78. const matchedRepository = releaseRepos.find(
  79. commitRepo => commitRepo.name === activeCommitRepo
  80. );
  81. if (matchedRepository) {
  82. this.setState({
  83. activeReleaseRepo: matchedRepository,
  84. });
  85. return;
  86. }
  87. addErrorMessage(t('The repository you were looking for was not found.'));
  88. }
  89. async fetchReleaseRepos() {
  90. const {params, api, organization, repositories, repositoriesLoading} = this.props;
  91. if (repositoriesLoading === undefined || repositoriesLoading === true) {
  92. return;
  93. }
  94. if (!repositories?.length) {
  95. this.setState({isLoading: false});
  96. return;
  97. }
  98. const {release} = params;
  99. const {project} = this.context;
  100. this.setState({isLoading: true});
  101. try {
  102. const releasePath = encodeURIComponent(release);
  103. const releaseRepos = await api.requestPromise(
  104. `/projects/${organization.slug}/${project.slug}/releases/${releasePath}/repositories/`
  105. );
  106. this.setState({releaseRepos, isLoading: false});
  107. this.setActiveReleaseRepo(this.props);
  108. } catch (error) {
  109. Sentry.captureException(error);
  110. addErrorMessage(
  111. t(
  112. 'An error occurred while trying to fetch the repositories of the release: %s',
  113. release
  114. )
  115. );
  116. }
  117. }
  118. render() {
  119. const {isLoading, activeReleaseRepo, releaseRepos} = this.state;
  120. const {repositoriesLoading, repositories, params, router, location, organization} =
  121. this.props;
  122. if (isLoading || repositoriesLoading) {
  123. return <LoadingIndicator />;
  124. }
  125. const noRepositoryOrgRelatedFound = !repositories?.length;
  126. if (noRepositoryOrgRelatedFound) {
  127. return (
  128. <Body>
  129. <Main fullWidth>
  130. <Panel dashedBorder>
  131. <EmptyMessage
  132. icon={<IconCommit size="xl" />}
  133. title={t('Releases are better with commit data!')}
  134. description={t(
  135. 'Connect a repository to see commit info, files changed, and authors involved in future releases.'
  136. )}
  137. action={
  138. <LinkButton
  139. priority="primary"
  140. to={`/settings/${organization.slug}/repos/`}
  141. >
  142. {t('Connect a repository')}
  143. </LinkButton>
  144. }
  145. />
  146. </Panel>
  147. </Main>
  148. </Body>
  149. );
  150. }
  151. const noReleaseReposFound = !releaseRepos.length;
  152. if (noReleaseReposFound) {
  153. return (
  154. <Body>
  155. <Main fullWidth>
  156. <Panel dashedBorder>
  157. <EmptyMessage
  158. icon={<IconCommit size="xl" />}
  159. title={t('Releases are better with commit data!')}
  160. description={t(
  161. 'No commits associated with this release have been found.'
  162. )}
  163. />
  164. </Panel>
  165. </Main>
  166. </Body>
  167. );
  168. }
  169. if (activeReleaseRepo === undefined) {
  170. return <LoadingIndicator />;
  171. }
  172. const {release} = params;
  173. const orgSlug = organization.slug;
  174. return (
  175. <WrappedComponent
  176. {...this.props}
  177. orgSlug={orgSlug}
  178. projectSlug={this.context.project.slug}
  179. release={release}
  180. router={router}
  181. location={location}
  182. releaseRepos={releaseRepos}
  183. activeReleaseRepo={activeReleaseRepo}
  184. />
  185. );
  186. }
  187. }
  188. return withApi(withOrganization(withRepositories(WithReleaseRepos)));
  189. }
  190. export default withReleaseRepos;