withReleaseRepos.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/panel';
  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<{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, organization, 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} = 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/${organization.slug}/${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. return (
  126. <Body>
  127. <Main fullWidth>
  128. <Panel dashedBorder>
  129. <EmptyMessage
  130. icon={<IconCommit size="xl" />}
  131. title={t('Releases are better with commit data!')}
  132. description={t(
  133. 'Connect a repository to see commit info, files changed, and authors involved in future releases.'
  134. )}
  135. action={
  136. <Button
  137. priority="primary"
  138. to={`/settings/${organization.slug}/repos/`}
  139. >
  140. {t('Connect a repository')}
  141. </Button>
  142. }
  143. />
  144. </Panel>
  145. </Main>
  146. </Body>
  147. );
  148. }
  149. const noReleaseReposFound = !releaseRepos.length;
  150. if (noReleaseReposFound) {
  151. return (
  152. <Body>
  153. <Main fullWidth>
  154. <Panel dashedBorder>
  155. <EmptyMessage
  156. icon={<IconCommit size="xl" />}
  157. title={t('Releases are better with commit data!')}
  158. description={t(
  159. 'No commits associated with this release have been found.'
  160. )}
  161. />
  162. </Panel>
  163. </Main>
  164. </Body>
  165. );
  166. }
  167. if (activeReleaseRepo === undefined) {
  168. return <LoadingIndicator />;
  169. }
  170. const {release} = params;
  171. const orgSlug = organization.slug;
  172. return (
  173. <WrappedComponent
  174. {...this.props}
  175. orgSlug={orgSlug}
  176. projectSlug={this.context.project.slug}
  177. release={release}
  178. router={router}
  179. location={location}
  180. releaseRepos={releaseRepos}
  181. activeReleaseRepo={activeReleaseRepo}
  182. />
  183. );
  184. }
  185. }
  186. return withApi(withOrganization(withRepositories(WithReleaseRepos)));
  187. }
  188. export default withReleaseRepos;