aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorDavid Cho-Lerat <david.cho-lerat@sonarsource.com>2023-12-05 14:55:58 +0100
committersonartech <sonartech@sonarsource.com>2023-12-05 20:02:45 +0000
commit946d868e4945961f8bef34bc743915f2954822a3 (patch)
tree4975c7a41d2114197c00a03cbcfb16e6fabfa02f /server
parentfd7c6e53b5b1eee5a6ab85c64a1731013194c4bc (diff)
downloadsonarqube-946d868e4945961f8bef34bc743915f2954822a3.tar.gz
sonarqube-946d868e4945961f8bef34bc743915f2954822a3.zip
SONAR-21056 Code page: don't show data from default branch on refresh
Diffstat (limited to 'server')
-rw-r--r--server/sonar-web/src/main/js/apps/code/__tests__/Code-it.ts17
-rw-r--r--server/sonar-web/src/main/js/apps/code/components/CodeApp.tsx99
2 files changed, 62 insertions, 54 deletions
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<Metric>;
+ 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<Props, State> {
@@ -62,9 +60,9 @@ class CodeApp extends React.Component<Props, State> {
this.state = {
breadcrumbs: [],
loading: true,
+ newCodeSelected: true,
page: 0,
total: 0,
- newCodeSelected: true,
};
}
@@ -92,41 +90,37 @@ class CodeApp extends React.Component<Props, State> {
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<Props, State> {
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<Props, State> {
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<Props, State> {
}
}
+interface WithBranchLikesProps {
+ branchLikes?: BranchLike[];
+ branchLike?: BranchLike;
+}
+
+function withBranchLikes<P extends { component?: Component }>(
+ WrappedComponent: React.ComponentType<React.PropsWithChildren<P & WithBranchLikesProps>>,
+): React.ComponentType<React.PropsWithChildren<Omit<P, 'branchLike' | 'branchLikes'>>> {
+ return function WithBranchLike(p: P) {
+ const { data, isFetching } = useBranchesQuery(p.component);
+
+ return (
+ (isPortfolioLike(p.component?.qualifier) || !isFetching) && (
+ <WrappedComponent
+ branchLikes={data?.branchLikes ?? []}
+ branchLike={data?.branchLike}
+ {...p}
+ />
+ )
+ );
+ };
+}
+
export default withRouter(withComponentContext(withMetricsContext(withBranchLikes(CodeApp))));