From: David Cho-Lerat Date: Tue, 5 Dec 2023 13:55:58 +0000 (+0100) Subject: SONAR-21056 Code page: don't show data from default branch on refresh X-Git-Tag: 10.4.0.87286~361 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=946d868e4945961f8bef34bc743915f2954822a3;p=sonarqube.git SONAR-21056 Code page: don't show data from default branch on refresh --- diff --git a/server/sonar-web/src/main/js/apps/code/__tests__/Code-it.ts b/server/sonar-web/src/main/js/apps/code/__tests__/Code-it.ts index 3584c582181..aaee0394a12 100644 --- a/server/sonar-web/src/main/js/apps/code/__tests__/Code-it.ts +++ b/server/sonar-web/src/main/js/apps/code/__tests__/Code-it.ts @@ -21,6 +21,7 @@ import { screen, waitFor, within } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { UserEvent } from '@testing-library/user-event/dist/types/setup/setup'; import { keyBy, times } from 'lodash'; +import BranchesServiceMock from '../../../api/mocks/BranchesServiceMock'; import ComponentsServiceMock from '../../../api/mocks/ComponentsServiceMock'; import IssuesServiceMock from '../../../api/mocks/IssuesServiceMock'; import { isDiffMetric } from '../../../helpers/measures'; @@ -43,16 +44,6 @@ jest.mock('../../../components/SourceViewer/helpers/lines', () => { }; }); -jest.mock('../../../api/branches', () => ({ - deleteBranch: jest.fn(), - deletePullRequest: jest.fn(), - excludeBranchFromPurge: jest.fn(), - getBranches: jest.fn(), - getPullRequests: jest.fn(), - renameBranch: jest.fn(), - setMainBranch: jest.fn(), -})); - jest.mock('../../../api/quality-gates', () => ({ getQualityGateProjectStatus: jest.fn(), })); @@ -60,8 +51,9 @@ jest.mock('../../../api/quality-gates', () => ({ const DEFAULT_LINES_LOADED = 19; const originalScrollTo = window.scrollTo; -const issuesHandler = new IssuesServiceMock(); +const branchesHandler = new BranchesServiceMock(); const componentsHandler = new ComponentsServiceMock(); +const issuesHandler = new IssuesServiceMock(); beforeAll(() => { Object.defineProperty(window, 'scrollTo', { @@ -80,8 +72,9 @@ afterAll(() => { }); beforeEach(() => { - issuesHandler.reset(); + branchesHandler.reset(); componentsHandler.reset(); + issuesHandler.reset(); }); it('should allow navigating through the tree', async () => { diff --git a/server/sonar-web/src/main/js/apps/code/components/CodeApp.tsx b/server/sonar-web/src/main/js/apps/code/components/CodeApp.tsx index f86495158a5..9343d730df2 100644 --- a/server/sonar-web/src/main/js/apps/code/components/CodeApp.tsx +++ b/server/sonar-web/src/main/js/apps/code/components/CodeApp.tsx @@ -22,22 +22,20 @@ import withComponentContext from '../../../app/components/componentContext/withC import withMetricsContext from '../../../app/components/metrics/withMetricsContext'; import { Location, Router, withRouter } from '../../../components/hoc/withRouter'; import { CodeScope, getCodeUrl, getProjectUrl } from '../../../helpers/urls'; -import { withBranchLikes } from '../../../queries/branch'; +import { useBranchesQuery } from '../../../queries/branch'; import { BranchLike } from '../../../types/branch-like'; -import { ComponentQualifier } from '../../../types/component'; +import { ComponentQualifier, isPortfolioLike } from '../../../types/component'; import { Breadcrumb, Component, ComponentMeasure, Dict, Metric } from '../../../types/types'; import { addComponent, addComponentBreadcrumbs, clearBucket } from '../bucket'; import '../code.css'; import { loadMoreChildren, retrieveComponent, retrieveComponentChildren } from '../utils'; import CodeAppRenderer from './CodeAppRenderer'; -interface Props { - branchLike?: BranchLike; - branchLikes: BranchLike[]; +interface Props extends WithBranchLikesProps { component: Component; location: Location; - router: Router; metrics: Dict; + router: Router; } interface State { @@ -46,11 +44,11 @@ interface State { components?: ComponentMeasure[]; highlighted?: ComponentMeasure; loading: boolean; + newCodeSelected: boolean; page: number; searchResults?: ComponentMeasure[]; sourceViewer?: ComponentMeasure; total: number; - newCodeSelected: boolean; } class CodeApp extends React.Component { @@ -62,9 +60,9 @@ class CodeApp extends React.Component { this.state = { breadcrumbs: [], loading: true, + newCodeSelected: true, page: 0, total: 0, - newCodeSelected: true, }; } @@ -92,41 +90,37 @@ class CodeApp extends React.Component { this, this.props.branchLike, ).then((r) => { - if (this.mounted) { - if ( - [ComponentQualifier.File, ComponentQualifier.TestFile].includes( - r.component.qualifier as ComponentQualifier, - ) - ) { - this.setState({ - breadcrumbs: r.breadcrumbs, - components: r.components, - loading: false, - page: 0, - searchResults: undefined, - sourceViewer: r.component, - total: 0, - }); - } else { - this.setState({ - baseComponent: r.component, - breadcrumbs: r.breadcrumbs, - components: r.components, - loading: false, - page: r.page, - searchResults: undefined, - sourceViewer: undefined, - total: r.total, - }); - } + if ( + [ComponentQualifier.File, ComponentQualifier.TestFile].includes( + r.component.qualifier as ComponentQualifier, + ) + ) { + this.setState({ + breadcrumbs: r.breadcrumbs, + components: r.components, + loading: false, + page: 0, + searchResults: undefined, + sourceViewer: r.component, + total: 0, + }); + } else { + this.setState({ + baseComponent: r.component, + breadcrumbs: r.breadcrumbs, + components: r.components, + loading: false, + page: r.page, + searchResults: undefined, + sourceViewer: undefined, + total: r.total, + }); } }, this.stopLoading); }; stopLoading = () => { - if (this.mounted) { - this.setState({ loading: false }); - } + this.setState({ loading: false }); }; handleComponentChange = () => { @@ -138,9 +132,7 @@ class CodeApp extends React.Component { this.setState({ loading: true }); retrieveComponentChildren(component.key, component.qualifier, this, branchLike).then(() => { addComponent(component); - if (this.mounted) { - this.handleUpdate(); - } + this.handleUpdate(); }, this.stopLoading); }; @@ -156,7 +148,7 @@ class CodeApp extends React.Component { this, this.props.branchLike, ).then((r) => { - if (this.mounted && r.components.length) { + if (r.components.length) { this.setState({ components: [...components, ...r.components], page: r.page, @@ -233,4 +225,27 @@ class CodeApp extends React.Component { } } +interface WithBranchLikesProps { + branchLikes?: BranchLike[]; + branchLike?: BranchLike; +} + +function withBranchLikes

( + WrappedComponent: React.ComponentType>, +): React.ComponentType>> { + return function WithBranchLike(p: P) { + const { data, isFetching } = useBranchesQuery(p.component); + + return ( + (isPortfolioLike(p.component?.qualifier) || !isFetching) && ( + + ) + ); + }; +} + export default withRouter(withComponentContext(withMetricsContext(withBranchLikes(CodeApp))));