integrationRepos.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import {Fragment} from 'react';
  2. import type {WithRouterProps} from 'react-router';
  3. import {Alert} from 'sentry/components/alert';
  4. import {LinkButton} from 'sentry/components/button';
  5. import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  6. import EmptyMessage from 'sentry/components/emptyMessage';
  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 RepositoryRow from 'sentry/components/repositoryRow';
  12. import {IconCommit} from 'sentry/icons';
  13. import {t} from 'sentry/locale';
  14. import RepositoryStore from 'sentry/stores/repositoryStore';
  15. import type {Integration, Organization, Repository} from 'sentry/types';
  16. import withOrganization from 'sentry/utils/withOrganization';
  17. import withSentryRouter from 'sentry/utils/withSentryRouter';
  18. import {IntegrationReposAddRepository} from './integrationReposAddRepository';
  19. type Props = DeprecatedAsyncComponent['props'] &
  20. WithRouterProps & {
  21. integration: Integration;
  22. organization: Organization;
  23. };
  24. type State = DeprecatedAsyncComponent['state'] & {
  25. integrationReposErrorStatus: number | null | undefined;
  26. itemList: Repository[];
  27. };
  28. class IntegrationRepos extends DeprecatedAsyncComponent<Props, State> {
  29. getDefaultState(): State {
  30. return {
  31. ...super.getDefaultState(),
  32. itemList: [],
  33. integrationReposErrorStatus: null,
  34. };
  35. }
  36. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  37. const {organization, integration} = this.props;
  38. return [
  39. [
  40. 'itemList',
  41. `/organizations/${organization.slug}/repos/`,
  42. {query: {status: 'active', integration_id: integration.id}},
  43. ],
  44. ];
  45. }
  46. // Called by row to signal repository change.
  47. onRepositoryChange = (data: Repository) => {
  48. const itemList = this.state.itemList;
  49. itemList.forEach(item => {
  50. if (item.id === data.id) {
  51. item.status = data.status;
  52. // allow for custom scm repositories to be updated, and
  53. // url is optional and therefore can be an empty string
  54. item.url = data.url === undefined ? item.url : data.url;
  55. item.name = data.name || item.name;
  56. }
  57. });
  58. this.setState({itemList});
  59. RepositoryStore.resetRepositories();
  60. };
  61. handleSearchError = (errorStatus: number | null | undefined) => {
  62. this.setState({integrationReposErrorStatus: errorStatus});
  63. };
  64. handleAddRepository = (repo: Repository) => {
  65. this.setState(state => ({
  66. itemList: [...state.itemList, repo],
  67. }));
  68. };
  69. renderBody() {
  70. const {integration} = this.props;
  71. const {itemListPageLinks, integrationReposErrorStatus, itemList} = this.state;
  72. return (
  73. <Fragment>
  74. {integrationReposErrorStatus === 400 && (
  75. <Alert type="error" showIcon>
  76. {t(
  77. 'We were unable to fetch repositories for this integration. Try again later. If this error continues, please reconnect this integration by uninstalling and then reinstalling.'
  78. )}
  79. </Alert>
  80. )}
  81. <Panel>
  82. <PanelHeader hasButtons>
  83. <div>{t('Repositories')}</div>
  84. <IntegrationReposAddRepository
  85. integration={integration}
  86. currentRepositories={itemList}
  87. onSearchError={this.handleSearchError}
  88. onAddRepository={this.handleAddRepository}
  89. />
  90. </PanelHeader>
  91. <PanelBody>
  92. {itemList.length === 0 && (
  93. <EmptyMessage
  94. icon={<IconCommit />}
  95. title={t('Sentry is better with commit data')}
  96. description={t(
  97. 'Add a repository to begin tracking its commit data. Then, set up release tracking to unlock features like suspect commits, suggested issue owners, and deploy emails.'
  98. )}
  99. action={
  100. <LinkButton href="https://docs.sentry.io/product/releases/" external>
  101. {t('Learn More')}
  102. </LinkButton>
  103. }
  104. />
  105. )}
  106. {itemList.map(repo => (
  107. <RepositoryRow
  108. api={this.api}
  109. key={repo.id}
  110. repository={repo}
  111. orgSlug={this.props.organization.slug}
  112. onRepositoryChange={this.onRepositoryChange}
  113. />
  114. ))}
  115. </PanelBody>
  116. </Panel>
  117. <Pagination pageLinks={itemListPageLinks} />
  118. </Fragment>
  119. );
  120. }
  121. }
  122. export default withOrganization(withSentryRouter(IntegrationRepos));