integrationRepos.tsx 4.7 KB

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