From 9903964b027a2b5bb2f29794f5621e4ae993e82f Mon Sep 17 00:00:00 2001 From: Wouter Admiraal Date: Tue, 22 Dec 2020 15:29:03 +0100 Subject: [PATCH] SONAR-12607 Update background task error message based on context --- .../nav/component/ComponentNavBgTaskNotif.tsx | 29 ++-- .../ComponentNavBgTaskNotif-test.tsx | 39 +++++- .../__snapshots__/ComponentNav-test.tsx.snap | 6 +- .../ComponentNavBgTaskNotif-test.tsx.snap | 126 +++++++++++++++++- .../resources/org/sonar/l10n/core.properties | 12 +- 5 files changed, 186 insertions(+), 26 deletions(-) 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 3454c4c7464..76aae8ee0b0 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 @@ -19,15 +19,16 @@ */ import * as React from 'react'; import { FormattedMessage } from 'react-intl'; -import { Link } from 'react-router'; +import { Link, WithRouterProps } from 'react-router'; import { Alert } from 'sonar-ui-common/components/ui/Alert'; import { hasMessage, translate } from 'sonar-ui-common/helpers/l10n'; import { STATUSES } from '../../../../apps/background-tasks/constants'; +import { withRouter } from '../../../../components/hoc/withRouter'; import { getComponentBackgroundTaskUrl } from '../../../../helpers/urls'; import { Task, TaskStatuses } from '../../../../types/tasks'; import ComponentNavLicenseNotif from './ComponentNavLicenseNotif'; -interface Props { +interface Props extends Pick { component: T.Component; currentTask?: Task; currentTaskOnSameBranch?: boolean; @@ -35,11 +36,13 @@ interface Props { isPending?: boolean; } -export default class ComponentNavBgTaskNotif extends React.PureComponent { +export class ComponentNavBgTaskNotif extends React.PureComponent { renderMessage(messageKey: string, status?: string, branch?: string) { - const { component, currentTask } = this.props; + const { component, currentTask, location } = this.props; + const backgroundTaskUrl = getComponentBackgroundTaskUrl(component.key, status); const canSeeBackgroundTasks = component.configuration && component.configuration.showBackgroundTasks; + const isOnBackgroundTaskPage = location.pathname === backgroundTaskUrl.pathname; let type; if (currentTask && hasMessage('background_task.type', currentTask.type)) { @@ -48,20 +51,24 @@ export default class ComponentNavBgTaskNotif extends React.PureComponent } let url; + let stacktrace; if (canSeeBackgroundTasks) { messageKey += '.admin'; - url = ( - - {translate('background_tasks.page')} - - ); + + if (isOnBackgroundTaskPage) { + messageKey += '.help'; + stacktrace = translate('background_tasks.show_stacktrace'); + } else { + messageKey += '.link'; + url = {translate('background_tasks.page')}; + } } return ( ); } @@ -112,3 +119,5 @@ export default class ComponentNavBgTaskNotif extends React.PureComponent return null; } } + +export default withRouter(ComponentNavBgTaskNotif); 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 39dfcd2d3b2..d86b2ea4c8a 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 @@ -20,9 +20,9 @@ import { shallow } from 'enzyme'; import * as React from 'react'; import { mockTask } from '../../../../../helpers/mocks/tasks'; -import { mockComponent } from '../../../../../helpers/testMocks'; +import { mockComponent, mockLocation } from '../../../../../helpers/testMocks'; import { TaskStatuses } from '../../../../../types/tasks'; -import ComponentNavBgTaskNotif from '../ComponentNavBgTaskNotif'; +import { ComponentNavBgTaskNotif } from '../ComponentNavBgTaskNotif'; jest.mock('sonar-ui-common/helpers/l10n', () => ({ ...jest.requireActual('sonar-ui-common/helpers/l10n'), @@ -56,6 +56,13 @@ it('renders correctly', () => { ).toMatchSnapshot('branch'); expect( shallowRender({ + currentTask: mockTask({ branch: 'my/branch', status: TaskStatuses.Failed }), + currentTaskOnSameBranch: false + }) + ).toMatchSnapshot('branch for admins'); + expect( + shallowRender({ + component: mockComponent({ configuration: { showBackgroundTasks: true } }), currentTask: mockTask({ pullRequest: '650', pullRequestTitle: 'feature/my_pr', @@ -63,7 +70,32 @@ it('renders correctly', () => { }), currentTaskOnSameBranch: false }) - ).toMatchSnapshot('pul request'); + ).toMatchSnapshot('pull request'); + expect( + shallowRender({ + component: mockComponent({ configuration: { showBackgroundTasks: true } }), + currentTask: mockTask({ + pullRequest: '650', + pullRequestTitle: 'feature/my_pr', + status: TaskStatuses.Failed + }), + currentTaskOnSameBranch: false + }) + ).toMatchSnapshot('pull request for admins'); + expect( + shallowRender({ + component: mockComponent({ configuration: { showBackgroundTasks: true } }), + location: mockLocation({ pathname: '/project/background_tasks' }) + }) + ).toMatchSnapshot('on background task page'); + expect( + shallowRender({ + component: mockComponent({ configuration: { showBackgroundTasks: true } }), + currentTask: mockTask({ branch: 'my/branch', status: TaskStatuses.Failed }), + currentTaskOnSameBranch: false, + location: mockLocation({ pathname: '/project/background_tasks' }) + }) + ).toMatchSnapshot('on background task page for branch'); expect(shallowRender({ currentTask: undefined })).toMatchSnapshot('no current task'); }); @@ -72,6 +104,7 @@ function shallowRender(props: Partial = {}) { ); diff --git a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNav-test.tsx.snap b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNav-test.tsx.snap index 5aee56e0c18..a5669ebe18d 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNav-test.tsx.snap +++ b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNav-test.tsx.snap @@ -151,7 +151,7 @@ exports[`renders correctly: has failed notification 1`] = ` height={102} id="context-navigation" notif={ - + +`; + +exports[`renders correctly: branch for admins 1`] = ` + + + + +`; + +exports[`renders correctly: on background task page for branch 1`] = ` + + + +`; + exports[`renders correctly: pending 1`] = ` `; -exports[`renders correctly: pul request 1`] = ` +exports[`renders correctly: pull request 1`] = ` + background_tasks.page + , + } + } + /> + +`; + +exports[`renders correctly: pull request for admins 1`] = ` + + + background_tasks.page + , } } /> 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 874ce2a0587..881cf90af17 100644 --- a/sonar-core/src/main/resources/org/sonar/l10n/core.properties +++ b/sonar-core/src/main/resources/org/sonar/l10n/core.properties @@ -2708,12 +2708,16 @@ marketplace.search=Search by features, tags, or categories... #------------------------------------------------------------------------------ component_navigation.status.failed=The last background task has failed. component_navigation.status.failed_X=The {type} has failed. -component_navigation.status.failed.admin=The last background task has failed. More details available on the {url} page. -component_navigation.status.failed_X.admin=The {type} has failed. More details available on the {url} page. +component_navigation.status.failed.admin.link=The last background task has failed. More details available on the {url} page. +component_navigation.status.failed_X.admin.link=The {type} has failed. More details available on the {url} page. +component_navigation.status.failed.admin.help=The last background task has failed. You can find more details below by clicking on the cog menu, and then "{stacktrace}". +component_navigation.status.failed_X.admin.help=The {type} has failed. You can find more details below by clicking on the cog menu, and then "{stacktrace}". component_navigation.status.failed_branch=The last analysis on this project ({branch}) failed. component_navigation.status.failed_branch_X=The last {type} on this project ({branch}) failed. -component_navigation.status.failed_branch.admin=The last analysis on this project ({branch}) failed. More details available on the {url} page. -component_navigation.status.failed_branch_X.admin=The last {type} on this project ({branch}) failed. More details available on the {url} page. +component_navigation.status.failed_branch.admin.link=The last analysis on this project ({branch}) failed. More details available on the {url} page. +component_navigation.status.failed_branch_X.admin.link=The last {type} on this project ({branch}) failed. More details available on the {url} page. +component_navigation.status.failed_branch.admin.help==The last analysis on this project ({branch}) failed. You can find more details below by clicking on the cog menu, and then "{stacktrace}". +component_navigation.status.failed_branch_X.admin.help=The last {type} on this project ({branch}) failed. You can find more details below by clicking on the cog menu, and then "{stacktrace}". component_navigation.status.pending=There is a pending background task. component_navigation.status.pending_X=There is a pending {type}. component_navigation.status.pending.admin=There is a pending background task. More details available on the {url} page. -- 2.39.5