commits.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import {Fragment} from 'react';
  2. import {RouteComponentProps} from 'react-router';
  3. import {Location} from 'history';
  4. import {CommitRow} from 'sentry/components/commitRow';
  5. import * as Layout from 'sentry/components/layouts/thirds';
  6. import LoadingIndicator from 'sentry/components/loadingIndicator';
  7. import Pagination from 'sentry/components/pagination';
  8. import Panel from 'sentry/components/panels/panel';
  9. import PanelBody from 'sentry/components/panels/panelBody';
  10. import PanelHeader from 'sentry/components/panels/panelHeader';
  11. import {t} from 'sentry/locale';
  12. import {Commit, Organization, Project, Repository} from 'sentry/types';
  13. import {formatVersion} from 'sentry/utils/formatters';
  14. import routeTitleGen from 'sentry/utils/routeTitle';
  15. import DeprecatedAsyncView from 'sentry/views/deprecatedAsyncView';
  16. import {getCommitsByRepository, getQuery, getReposToRender} from '../utils';
  17. import EmptyState from './emptyState';
  18. import RepositorySwitcher from './repositorySwitcher';
  19. import withReleaseRepos from './withReleaseRepos';
  20. type Props = RouteComponentProps<{release: string}, {}> & {
  21. location: Location;
  22. orgSlug: Organization['slug'];
  23. projectSlug: Project['slug'];
  24. release: string;
  25. releaseRepos: Repository[];
  26. activeReleaseRepo?: Repository;
  27. } & DeprecatedAsyncView['props'];
  28. type State = {
  29. commits: Commit[];
  30. } & DeprecatedAsyncView['state'];
  31. class Commits extends DeprecatedAsyncView<Props, State> {
  32. getTitle() {
  33. const {params, orgSlug, projectSlug} = this.props;
  34. return routeTitleGen(
  35. t('Commits - Release %s', formatVersion(params.release)),
  36. orgSlug,
  37. false,
  38. projectSlug
  39. );
  40. }
  41. getDefaultState(): State {
  42. return {
  43. ...super.getDefaultState(),
  44. commits: [],
  45. };
  46. }
  47. componentDidUpdate(prevProps: Props, prevState: State) {
  48. if (prevProps.activeReleaseRepo?.name !== this.props.activeReleaseRepo?.name) {
  49. this.remountComponent();
  50. return;
  51. }
  52. super.componentDidUpdate(prevProps, prevState);
  53. }
  54. getEndpoints(): ReturnType<DeprecatedAsyncView['getEndpoints']> {
  55. const {
  56. projectSlug,
  57. activeReleaseRepo: activeRepository,
  58. location,
  59. orgSlug,
  60. release,
  61. } = this.props;
  62. const query = getQuery({location, activeRepository});
  63. return [
  64. [
  65. 'commits',
  66. `/projects/${orgSlug}/${projectSlug}/releases/${encodeURIComponent(
  67. release
  68. )}/commits/`,
  69. {query},
  70. ],
  71. ];
  72. }
  73. renderLoading() {
  74. return this.renderBody();
  75. }
  76. renderContent() {
  77. const {commits, commitsPageLinks, loading} = this.state;
  78. const {activeReleaseRepo} = this.props;
  79. if (loading) {
  80. return <LoadingIndicator />;
  81. }
  82. if (!commits.length) {
  83. return (
  84. <EmptyState>
  85. {!activeReleaseRepo
  86. ? t('There are no commits associated with this release.')
  87. : t(
  88. 'There are no commits associated with this release in the %s repository.',
  89. activeReleaseRepo.name
  90. )}
  91. </EmptyState>
  92. );
  93. }
  94. const commitsByRepository = getCommitsByRepository(commits);
  95. const reposToRender = getReposToRender(Object.keys(commitsByRepository));
  96. return (
  97. <Fragment>
  98. {reposToRender.map(repoName => (
  99. <Panel key={repoName}>
  100. <PanelHeader>{repoName}</PanelHeader>
  101. <PanelBody>
  102. {commitsByRepository[repoName]?.map(commit => (
  103. <CommitRow key={commit.id} commit={commit} />
  104. ))}
  105. </PanelBody>
  106. </Panel>
  107. ))}
  108. <Pagination pageLinks={commitsPageLinks} />
  109. </Fragment>
  110. );
  111. }
  112. renderBody() {
  113. const {location, router, activeReleaseRepo, releaseRepos} = this.props;
  114. return (
  115. <Fragment>
  116. {releaseRepos.length > 1 && (
  117. <RepositorySwitcher
  118. repositories={releaseRepos}
  119. activeRepository={activeReleaseRepo}
  120. location={location}
  121. router={router}
  122. />
  123. )}
  124. {this.renderContent()}
  125. </Fragment>
  126. );
  127. }
  128. renderComponent() {
  129. return (
  130. <Layout.Body>
  131. <Layout.Main fullWidth>{super.renderComponent()}</Layout.Main>
  132. </Layout.Body>
  133. );
  134. }
  135. }
  136. export default withReleaseRepos(Commits);