commits.tsx 4.1 KB

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