From ee41573b8b309a123bc23ac623663107cc410af3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9goire=20Aubert?= Date: Thu, 28 Sep 2017 17:15:43 +0200 Subject: [PATCH] SONAR-9792 Add background task error notification related to licensing --- server/sonar-web/src/main/js/api/ce.ts | 2 +- .../src/main/js/app/components/App.tsx | 16 ++- .../nav/component/ComponentNavBgTaskNotif.tsx | 116 +++++++++++------- .../ComponentNavBgTaskNotif-test.tsx | 46 +++---- .../ComponentNavBgTaskNotif-test.tsx.snap | 51 ++++---- .../resources/org/sonar/l10n/core.properties | 6 +- 6 files changed, 139 insertions(+), 98 deletions(-) diff --git a/server/sonar-web/src/main/js/api/ce.ts b/server/sonar-web/src/main/js/api/ce.ts index 01ed1a7a090..a61b0e2051b 100644 --- a/server/sonar-web/src/main/js/api/ce.ts +++ b/server/sonar-web/src/main/js/api/ce.ts @@ -21,7 +21,6 @@ import { getJSON, post, RequestData } from '../helpers/request'; import throwGlobalError from '../app/utils/throwGlobalError'; export interface PendingTask { - componentId: string; componentKey: string; componentName: string; componentQualifier: string; @@ -37,6 +36,7 @@ export interface PendingTask { export interface Task extends PendingTask { analysisId?: string; errorMessage?: string; + errorType?: string; executionTimeMs: number; executedAt: Date; hasErrorStacktrace: boolean; diff --git a/server/sonar-web/src/main/js/app/components/App.tsx b/server/sonar-web/src/main/js/app/components/App.tsx index a46f417a3ac..8ab6f6b8f40 100644 --- a/server/sonar-web/src/main/js/app/components/App.tsx +++ b/server/sonar-web/src/main/js/app/components/App.tsx @@ -33,21 +33,27 @@ interface Props { interface State { branchesEnabled: boolean; + canAdmin: boolean; loading: boolean; onSonarCloud: boolean; } class App extends React.PureComponent { mounted: boolean; - state: State = { branchesEnabled: false, loading: true, onSonarCloud: false }; + state: State = { branchesEnabled: false, canAdmin: false, loading: true, onSonarCloud: false }; static childContextTypes = { branchesEnabled: PropTypes.bool.isRequired, + canAdmin: PropTypes.bool.isRequired, onSonarCloud: PropTypes.bool }; getChildContext() { - return { branchesEnabled: this.state.branchesEnabled, onSonarCloud: this.state.onSonarCloud }; + return { + branchesEnabled: this.state.branchesEnabled, + canAdmin: this.state.canAdmin, + onSonarCloud: this.state.onSonarCloud + }; } componentDidMount() { @@ -69,7 +75,11 @@ class App extends React.PureComponent { const onSonarCloud = appState.settings != undefined && appState.settings['sonar.lf.sonarqube.com.enabled'] === 'true'; - this.setState({ branchesEnabled: appState.branchesEnabled, onSonarCloud }); + this.setState({ + branchesEnabled: appState.branchesEnabled, + canAdmin: appState.canAdmin, + onSonarCloud + }); } }); }; diff --git a/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavBgTaskNotif.tsx b/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavBgTaskNotif.tsx index 7869f9a6710..732b7ea3170 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavBgTaskNotif.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavBgTaskNotif.tsx @@ -18,12 +18,15 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import * as React from 'react'; +import { Link } from 'react-router'; +import { FormattedMessage } from 'react-intl'; +import * as PropTypes from 'prop-types'; import NavBarNotif from '../../../../components/nav/NavBarNotif'; import PendingIcon from '../../../../components/icons-components/PendingIcon'; import { Component } from '../../../types'; import { STATUSES } from '../../../../apps/background-tasks/constants'; import { getComponentBackgroundTaskUrl } from '../../../../helpers/urls'; -import { translate, translateWithParameters } from '../../../../helpers/l10n'; +import { hasMessage, translate } from '../../../../helpers/l10n'; import { Task } from '../../../../api/ce'; interface Props { @@ -33,54 +36,73 @@ interface Props { isPending?: boolean; } -export default function ComponentNavBgTaskNotif({ - component, - currentTask, - isInProgress, - isPending -}: Props) { - const canSeeBackgroundTasks = - component.configuration != undefined && component.configuration.showBackgroundTasks; - const url = getComponentBackgroundTaskUrl(component.key); +export default class ComponentNavBgTaskNotif extends React.PureComponent { + static contextTypes = { + canAdmin: PropTypes.bool.isRequired + }; - if (isInProgress) { - return ( - - - - - ); - } else if (isPending) { - return ( - - - - - ); - } else if (currentTask && currentTask.status === STATUSES.FAILED) { - return ( - - {translate('background_tasks.page')} }} /> - - ); + ); + } + + return {translate(messageKey)}; + } + + render() { + const { currentTask, isInProgress, isPending } = this.props; + + if (isInProgress) { + return ( + + + {this.renderMessage('component_navigation.status.in_progress')} + + ); + } else if (isPending) { + return ( + + + {this.renderMessage('component_navigation.status.pending')} + + ); + } else if (currentTask && currentTask.status === STATUSES.FAILED) { + if ( + currentTask.errorType && + currentTask.errorType.includes('LICENSING') && + hasMessage('license.component_navigation.button', currentTask.errorType) + ) { + return ( + + {currentTask.errorMessage} + {this.context.canAdmin && ( + + {translate('license.component_navigation.button', currentTask.errorType)}. + + )} + + ); + } + + return ( + + {this.renderMessage('component_navigation.status.failed')} + + ); + } + return null; } - return null; } diff --git a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavBgTaskNotif-test.tsx b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavBgTaskNotif-test.tsx index 3a06ad1fadb..72e517426cc 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavBgTaskNotif-test.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavBgTaskNotif-test.tsx @@ -17,6 +17,12 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +jest.mock('../../../../../helpers/l10n', () => { + const l10n = require.requireActual('../../../../../helpers/l10n'); + l10n.hasMessage = jest.fn(() => true); + return l10n; +}); + import * as React from 'react'; import { shallow } from 'enzyme'; import ComponentNavBgTaskNotif from '../ComponentNavBgTaskNotif'; @@ -33,34 +39,30 @@ const component = { }; it('renders background task error correctly', () => { - expect( - shallow( - - ) - ).toMatchSnapshot(); + expect(getWrapper()).toMatchSnapshot(); }); it('renders background task pending info correctly', () => { - expect( - shallow( - - ) - ).toMatchSnapshot(); + expect(getWrapper({ isPending: true })).toMatchSnapshot(); }); it('renders background task in progress info correctly', () => { + expect(getWrapper({ isInProgress: true, isPending: true })).toMatchSnapshot(); +}); + +it('renders background task license info correctly', () => { expect( - shallow( - - ) + getWrapper({ currentTask: { status: 'FAILED', errorType: 'LICENSING', errorMessage: 'Foo' } }) ).toMatchSnapshot(); }); + +function getWrapper(props = {}) { + return shallow( + , + { context: { canAdmin: true } } + ); +} diff --git a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavBgTaskNotif-test.tsx.snap b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavBgTaskNotif-test.tsx.snap index 423b6a5d618..679067cb000 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavBgTaskNotif-test.tsx.snap +++ b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavBgTaskNotif-test.tsx.snap @@ -4,13 +4,9 @@ exports[`renders background task error correctly 1`] = ` - + + component_navigation.status.failed + `; @@ -19,15 +15,30 @@ exports[`renders background task in progress info correctly 1`] = ` className="alert alert-info" > - + + component_navigation.status.in_progress + + +`; + +exports[`renders background task license info correctly 1`] = ` + + + Foo + + + license.component_navigation.button.LICENSING + . + `; @@ -38,12 +49,8 @@ exports[`renders background task pending info correctly 1`] = ` - + + component_navigation.status.pending + `; diff --git a/sonar-core/src/main/resources/org/sonar/l10n/core.properties b/sonar-core/src/main/resources/org/sonar/l10n/core.properties index e253fa046fe..e16a23b24fd 100644 --- a/sonar-core/src/main/resources/org/sonar/l10n/core.properties +++ b/sonar-core/src/main/resources/org/sonar/l10n/core.properties @@ -2776,11 +2776,11 @@ update_center.status.DEPS_REQUIRE_SYSTEM_UPGRADE=Some of dependencies requires s # #------------------------------------------------------------------------------ component_navigation.status.failed=The last analysis has failed. -component_navigation.status.failed.admin=The last analysis has failed. More details available on the Background Tasks page. +component_navigation.status.failed.admin=The last analysis has failed. More details available on the {url} page. component_navigation.status.pending=There is a pending analysis. -component_navigation.status.pending.admin=There is a pending analysis. More details available on the Background Tasks page. +component_navigation.status.pending.admin=There is a pending analysis. More details available on the {url} page. component_navigation.status.in_progress=The analysis is in progress. -component_navigation.status.in_progress.admin=The analysis is in progress. More details available on the Background Tasks page. +component_navigation.status.in_progress.admin=The analysis is in progress. More details available on the {url} page. background_task.status.ALL=All background_task.status.PENDING=Pending -- 2.39.5