From 9b598e4d1b7e89f5f162cdbfb6992510154cef37 Mon Sep 17 00:00:00 2001 From: Philippe Perrin Date: Fri, 19 Jun 2020 16:13:22 +0200 Subject: [PATCH] SONAR-13413 Issues page remains unavailable if indexation is completed with failures SONAR-13400 Portfolios page remains unavailable if indexation is completed with failures --- .../indexation/IndexationContextProvider.tsx | 4 --- .../IndexationNotificationHelper.ts | 12 +++++++-- .../PageUnavailableDueToIndexation.tsx | 5 +++- .../IndexationContextProvider-test.tsx | 3 +-- .../IndexationNotificationHelper-test.tsx | 2 +- .../PageUnavailableDueToIndexation-test.tsx | 25 ++++++++++++++++--- .../__tests__/withIndexationGuard-test.tsx | 15 ++++++++--- .../js/components/hoc/withIndexationGuard.tsx | 2 +- 8 files changed, 51 insertions(+), 17 deletions(-) diff --git a/server/sonar-web/src/main/js/app/components/indexation/IndexationContextProvider.tsx b/server/sonar-web/src/main/js/app/components/indexation/IndexationContextProvider.tsx index 4483ae34af0..164d1a75572 100644 --- a/server/sonar-web/src/main/js/app/components/indexation/IndexationContextProvider.tsx +++ b/server/sonar-web/src/main/js/app/components/indexation/IndexationContextProvider.tsx @@ -52,10 +52,6 @@ export class IndexationContextProvider extends React.PureComponent< } handleNewStatus = (newIndexationStatus: IndexationStatus) => { - if (newIndexationStatus.isCompleted) { - IndexationNotificationHelper.stopPolling(); - } - if (this.mounted) { this.setState({ status: newIndexationStatus }); } diff --git a/server/sonar-web/src/main/js/app/components/indexation/IndexationNotificationHelper.ts b/server/sonar-web/src/main/js/app/components/indexation/IndexationNotificationHelper.ts index 514e766ce38..792e7c83e6b 100644 --- a/server/sonar-web/src/main/js/app/components/indexation/IndexationNotificationHelper.ts +++ b/server/sonar-web/src/main/js/app/components/indexation/IndexationNotificationHelper.ts @@ -32,8 +32,10 @@ export default class IndexationNotificationHelper { this.stopPolling(); // eslint-disable-next-line promise/catch-or-return - this.poll(onNewStatus).finally(() => { - this.interval = setInterval(() => this.poll(onNewStatus), POLLING_INTERVAL_MS); + this.poll(onNewStatus).then(status => { + if (!status.isCompleted) { + this.interval = setInterval(() => this.poll(onNewStatus), POLLING_INTERVAL_MS); + } }); } @@ -47,6 +49,12 @@ export default class IndexationNotificationHelper { const status = await getIndexationStatus(); onNewStatus(status); + + if (status.isCompleted) { + this.stopPolling(); + } + + return status; } static markInProgressNotificationAsDisplayed() { diff --git a/server/sonar-web/src/main/js/app/components/indexation/PageUnavailableDueToIndexation.tsx b/server/sonar-web/src/main/js/app/components/indexation/PageUnavailableDueToIndexation.tsx index ef88aa8cef6..7e50b1aaacd 100644 --- a/server/sonar-web/src/main/js/app/components/indexation/PageUnavailableDueToIndexation.tsx +++ b/server/sonar-web/src/main/js/app/components/indexation/PageUnavailableDueToIndexation.tsx @@ -38,7 +38,10 @@ export enum PageContext { export class PageUnavailableDueToIndexation extends React.PureComponent { componentDidUpdate() { - if (this.props.indexationContext?.status.isCompleted) { + if ( + this.props.indexationContext.status.isCompleted && + !this.props.indexationContext.status.hasFailures + ) { window.location.reload(); } } diff --git a/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationContextProvider-test.tsx b/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationContextProvider-test.tsx index d381912d60f..9b92b363949 100644 --- a/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationContextProvider-test.tsx +++ b/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationContextProvider-test.tsx @@ -49,7 +49,7 @@ it('should not start polling if no issue sync is needed', () => { expect(wrapper.state().status).toEqual(expectedStatus); }); -it('should update the state on new status & stop polling if indexation is complete', () => { +it('should update the state on new status', () => { const wrapper = mountRender(); const triggerNewStatus = (IndexationNotificationHelper.startPolling as jest.Mock).mock @@ -63,7 +63,6 @@ it('should update the state on new status & stop polling if indexation is comple triggerNewStatus(newStatus); expect(wrapper.state().status).toEqual(newStatus); - expect(IndexationNotificationHelper.stopPolling).toHaveBeenCalled(); }); it('should stop polling when component is destroyed', () => { diff --git a/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationNotificationHelper-test.tsx b/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationNotificationHelper-test.tsx index 095d4b31948..ea3d923dd85 100644 --- a/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationNotificationHelper-test.tsx +++ b/server/sonar-web/src/main/js/app/components/indexation/__tests__/IndexationNotificationHelper-test.tsx @@ -41,7 +41,7 @@ jest.mock('sonar-ui-common/helpers/storage', () => ({ it('should properly start & stop polling for indexation status', async () => { const onNewStatus = jest.fn(); const newStatus: IndexationStatus = { - isCompleted: true, + isCompleted: false, percentCompleted: 100, hasFailures: false }; diff --git a/server/sonar-web/src/main/js/app/components/indexation/__tests__/PageUnavailableDueToIndexation-test.tsx b/server/sonar-web/src/main/js/app/components/indexation/__tests__/PageUnavailableDueToIndexation-test.tsx index 9d7f1945d4d..02892c8f915 100644 --- a/server/sonar-web/src/main/js/app/components/indexation/__tests__/PageUnavailableDueToIndexation-test.tsx +++ b/server/sonar-web/src/main/js/app/components/indexation/__tests__/PageUnavailableDueToIndexation-test.tsx @@ -28,7 +28,7 @@ it('should render correctly', () => { expect(wrapper).toMatchSnapshot(); }); -it('should refresh the page once the indexation is complete', () => { +it('should not refresh the page once the indexation is complete if there were failures', () => { const reload = jest.fn(); delete window.location; (window as any).location = { reload }; @@ -37,14 +37,33 @@ it('should refresh the page once the indexation is complete', () => { expect(reload).not.toHaveBeenCalled(); - wrapper.setProps({ indexationContext: { status: { isCompleted: true } } }); + wrapper.setProps({ + indexationContext: { status: { isCompleted: true, percentCompleted: 100, hasFailures: true } } + }); + wrapper.update(); + + expect(reload).not.toHaveBeenCalled(); +}); + +it('should refresh the page once the indexation is complete if there were NO failures', () => { + const reload = jest.fn(); + delete window.location; + (window as any).location = { reload }; + + const wrapper = shallowRender(); + + expect(reload).not.toHaveBeenCalled(); + + wrapper.setProps({ + indexationContext: { status: { isCompleted: true, percentCompleted: 100, hasFailures: false } } + }); wrapper.update(); expect(reload).toHaveBeenCalled(); }); function shallowRender(props?: PageUnavailableDueToIndexation['props']) { - return shallow( + return shallow( { - let wrapper = mountRender(); +it('should not render children because indexation is in progress', () => { + const wrapper = mountRender(); expect(wrapper.find(TestComponent).exists()).toBe(false); +}); + +it('should not render children because indexation has failures', () => { + const wrapper = mountRender({ + status: { isCompleted: true, percentCompleted: 100, hasFailures: true } + }); + expect(wrapper.find(TestComponent).exists()).toBe(false); +}); - wrapper = mountRender({ +it('should render children because indexation is completed without failures', () => { + const wrapper = mountRender({ status: { isCompleted: true, percentCompleted: 100, hasFailures: false } }); expect(wrapper.find(TestComponent).exists()).toBe(true); diff --git a/server/sonar-web/src/main/js/components/hoc/withIndexationGuard.tsx b/server/sonar-web/src/main/js/components/hoc/withIndexationGuard.tsx index 0036b19fdec..b1d316f7e47 100644 --- a/server/sonar-web/src/main/js/components/hoc/withIndexationGuard.tsx +++ b/server/sonar-web/src/main/js/components/hoc/withIndexationGuard.tsx @@ -33,7 +33,7 @@ export default function withIndexationGuard

( return ( {context => - context?.status.isCompleted ? ( + context?.status.isCompleted && !context?.status.hasFailures ? ( ) : ( -- 2.39.5