withReleaseRepos.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import {Component} from 'react';
  2. import type {RouteComponentProps} from 'react-router';
  3. import * as Sentry from '@sentry/react';
  4. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  5. import type {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 type {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. declare context: React.ContextType<typeof ReleaseContext>;
  61. static contextType = ReleaseContext;
  62. setActiveReleaseRepo(props: P & HoCsProps) {
  63. const {releaseRepos, activeReleaseRepo} = this.state;
  64. if (!releaseRepos.length) {
  65. return;
  66. }
  67. const activeCommitRepo = props.location.query?.activeRepo;
  68. if (!activeCommitRepo) {
  69. this.setState({
  70. activeReleaseRepo: releaseRepos[0] ?? null,
  71. });
  72. return;
  73. }
  74. if (activeCommitRepo === activeReleaseRepo?.name) {
  75. return;
  76. }
  77. const matchedRepository = releaseRepos.find(
  78. commitRepo => commitRepo.name === activeCommitRepo
  79. );
  80. if (matchedRepository) {
  81. this.setState({
  82. activeReleaseRepo: matchedRepository,
  83. });
  84. return;
  85. }
  86. addErrorMessage(t('The repository you were looking for was not found.'));
  87. }
  88. async fetchReleaseRepos() {
  89. const {params, api, organization, repositories, repositoriesLoading} = this.props;
  90. if (repositoriesLoading === undefined || repositoriesLoading === true) {
  91. return;
  92. }
  93. if (!repositories?.length) {
  94. this.setState({isLoading: false});
  95. return;
  96. }
  97. const {release} = params;
  98. const {project} = this.context;
  99. this.setState({isLoading: true});
  100. try {
  101. const releasePath = encodeURIComponent(release);
  102. const releaseRepos = await api.requestPromise(
  103. `/projects/${organization.slug}/${project.slug}/releases/${releasePath}/repositories/`
  104. );
  105. this.setState({releaseRepos, isLoading: false});
  106. this.setActiveReleaseRepo(this.props);
  107. } catch (error) {
  108. Sentry.captureException(error);
  109. addErrorMessage(
  110. t(
  111. 'An error occurred while trying to fetch the repositories of the release: %s',
  112. release
  113. )
  114. );
  115. }
  116. }
  117. render() {
  118. const {isLoading, activeReleaseRepo, releaseRepos} = this.state;
  119. const {repositoriesLoading, repositories, params, router, location, organization} =
  120. this.props;
  121. if (isLoading || repositoriesLoading) {
  122. return <LoadingIndicator />;
  123. }
  124. const noRepositoryOrgRelatedFound = !repositories?.length;
  125. if (noRepositoryOrgRelatedFound) {
  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
  138. priority="primary"
  139. to={`/settings/${organization.slug}/repos/`}
  140. >
  141. {t('Connect a repository')}
  142. </Button>
  143. }
  144. />
  145. </Panel>
  146. </Main>
  147. </Body>
  148. );
  149. }
  150. const noReleaseReposFound = !releaseRepos.length;
  151. if (noReleaseReposFound) {
  152. return (
  153. <Body>
  154. <Main fullWidth>
  155. <Panel dashedBorder>
  156. <EmptyMessage
  157. icon={<IconCommit size="xl" />}
  158. title={t('Releases are better with commit data!')}
  159. description={t(
  160. 'No commits associated with this release have been found.'
  161. )}
  162. />
  163. </Panel>
  164. </Main>
  165. </Body>
  166. );
  167. }
  168. if (activeReleaseRepo === undefined) {
  169. return <LoadingIndicator />;
  170. }
  171. const {release} = params;
  172. const orgSlug = organization.slug;
  173. return (
  174. <WrappedComponent
  175. {...this.props}
  176. orgSlug={orgSlug}
  177. projectSlug={this.context.project.slug}
  178. release={release}
  179. router={router}
  180. location={location}
  181. releaseRepos={releaseRepos}
  182. activeReleaseRepo={activeReleaseRepo}
  183. />
  184. );
  185. }
  186. }
  187. return withApi(withOrganization(withRepositories(WithReleaseRepos)));
  188. }
  189. export default withReleaseRepos;