withReleaseRepos.tsx 6.3 KB

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