From 01a703dd8baa53f114195ba035aca1fabda90801 Mon Sep 17 00:00:00 2001 From: Jeremy Davis Date: Thu, 30 Apr 2020 11:40:31 +0200 Subject: SONAR-13342 Fix broken projects links in portfolios --- .../background-tasks/components/TaskComponent.tsx | 12 ++- .../components/__tests__/TaskComponent-test.tsx | 42 ++++---- .../__snapshots__/TaskComponent-test.tsx.snap | 65 ++++++++++--- .../src/main/js/apps/overview/components/App.tsx | 47 +++------ .../main/js/apps/projectsManagement/ProjectRow.tsx | 14 ++- .../__tests__/ProjectRow-test.tsx | 10 +- .../__snapshots__/ProjectRow-test.tsx.snap | 107 ++++++++++++++++++++- 7 files changed, 221 insertions(+), 76 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/background-tasks/components/TaskComponent.tsx b/server/sonar-web/src/main/js/apps/background-tasks/components/TaskComponent.tsx index bd9c27cb058..9a7b9662ebb 100644 --- a/server/sonar-web/src/main/js/apps/background-tasks/components/TaskComponent.tsx +++ b/server/sonar-web/src/main/js/apps/background-tasks/components/TaskComponent.tsx @@ -23,7 +23,13 @@ import BranchIcon from 'sonar-ui-common/components/icons/BranchIcon'; import PullRequestIcon from 'sonar-ui-common/components/icons/PullRequestIcon'; import QualifierIcon from 'sonar-ui-common/components/icons/QualifierIcon'; import Organization from '../../../components/shared/Organization'; -import { getBranchUrl, getProjectUrl, getPullRequestUrl } from '../../../helpers/urls'; +import { + getBranchUrl, + getPortfolioUrl, + getProjectUrl, + getPullRequestUrl +} from '../../../helpers/urls'; +import { ComponentQualifier } from '../../../types/component'; import TaskType from './TaskType'; interface Props { @@ -79,7 +85,9 @@ export default function TaskComponent({ task }: Props) { } function getTaskComponentUrl(componentKey: string, task: T.Task) { - if (task.branch) { + if (task.componentQualifier === ComponentQualifier.Portfolio) { + return getPortfolioUrl(componentKey); + } else if (task.branch) { return getBranchUrl(componentKey, task.branch); } else if (task.pullRequest) { return getPullRequestUrl(componentKey, task.pullRequest); diff --git a/server/sonar-web/src/main/js/apps/background-tasks/components/__tests__/TaskComponent-test.tsx b/server/sonar-web/src/main/js/apps/background-tasks/components/__tests__/TaskComponent-test.tsx index 3b74956ba00..13477c7d4dc 100644 --- a/server/sonar-web/src/main/js/apps/background-tasks/components/__tests__/TaskComponent-test.tsx +++ b/server/sonar-web/src/main/js/apps/background-tasks/components/__tests__/TaskComponent-test.tsx @@ -19,27 +19,31 @@ */ import { shallow } from 'enzyme'; import * as React from 'react'; +import { ComponentQualifier } from '../../../../types/component'; import TaskComponent from '../TaskComponent'; -const TASK = { - componentKey: 'foo', - componentName: 'foo', - componentQualifier: 'TRK', - id: 'bar', - organization: 'org', - status: 'PENDING', - submittedAt: '2017-01-01', - submitterLogin: 'yoda', - type: 'REPORT' -}; - it('renders correctly', () => { - expect(shallow()).toMatchSnapshot(); - expect(shallow()).toMatchSnapshot(); + expect(shallowRender()).toMatchSnapshot(); + expect(shallowRender({ componentKey: undefined })).toMatchSnapshot('undefined key'); + expect(shallowRender({ componentQualifier: ComponentQualifier.Portfolio })).toMatchSnapshot( + 'portfolio' + ); + expect(shallowRender({ branch: 'feature' })).toMatchSnapshot('branch'); + expect(shallowRender({ branch: 'branch-6.7' })).toMatchSnapshot('branch'); + expect(shallowRender({ pullRequest: 'pr-89' })).toMatchSnapshot('pull request'); }); -it('renders correctly for branches and pullrequest', () => { - expect(shallow()).toMatchSnapshot(); - expect(shallow()).toMatchSnapshot(); - expect(shallow()).toMatchSnapshot(); -}); +function shallowRender(taskOverrides: Partial = {}) { + const TASK = { + componentKey: 'foo', + componentName: 'foo', + componentQualifier: 'TRK', + id: 'bar', + organization: 'org', + status: 'PENDING', + submittedAt: '2017-01-01', + submitterLogin: 'yoda', + type: 'REPORT' + }; + return shallow(); +} diff --git a/server/sonar-web/src/main/js/apps/background-tasks/components/__tests__/__snapshots__/TaskComponent-test.tsx.snap b/server/sonar-web/src/main/js/apps/background-tasks/components/__tests__/__snapshots__/TaskComponent-test.tsx.snap index 653b69c156f..c6704006123 100644 --- a/server/sonar-web/src/main/js/apps/background-tasks/components/__tests__/__snapshots__/TaskComponent-test.tsx.snap +++ b/server/sonar-web/src/main/js/apps/background-tasks/components/__tests__/__snapshots__/TaskComponent-test.tsx.snap @@ -34,20 +34,7 @@ exports[`renders correctly 1`] = ` `; -exports[`renders correctly 2`] = ` - - - bar - - - -`; - -exports[`renders correctly for branches and pullrequest 1`] = ` +exports[`renders correctly: branch 1`] = ` `; -exports[`renders correctly for branches and pullrequest 2`] = ` +exports[`renders correctly: branch 2`] = ` `; -exports[`renders correctly for branches and pullrequest 3`] = ` +exports[`renders correctly: portfolio 1`] = ` + + + + + + + foo + + + +`; + +exports[`renders correctly: pull request 1`] = ` `; + +exports[`renders correctly: undefined key 1`] = ` + + + bar + + + +`; diff --git a/server/sonar-web/src/main/js/apps/overview/components/App.tsx b/server/sonar-web/src/main/js/apps/overview/components/App.tsx index fbd6ea0c950..2fb35de7b06 100644 --- a/server/sonar-web/src/main/js/apps/overview/components/App.tsx +++ b/server/sonar-web/src/main/js/apps/overview/components/App.tsx @@ -39,17 +39,6 @@ interface Props { } export class App extends React.PureComponent { - componentDidMount() { - const { component } = this.props; - - if (this.isPortfolio()) { - this.props.router.replace({ - pathname: '/portfolio', - query: { id: component.key } - }); - } - } - isPortfolio = () => { return ([ComponentQualifier.Portfolio, ComponentQualifier.SubPortfolio] as string[]).includes( this.props.component.qualifier @@ -63,28 +52,24 @@ export class App extends React.PureComponent { return null; } - return ( + return isPullRequest(branchLike) ? ( <> - {isPullRequest(branchLike) ? ( - <> - - - - ) : ( - <> - + + + + ) : ( + <> + - {!component.analysisDate ? ( - - ) : ( - - )} - + {!component.analysisDate ? ( + + ) : ( + )} ); diff --git a/server/sonar-web/src/main/js/apps/projectsManagement/ProjectRow.tsx b/server/sonar-web/src/main/js/apps/projectsManagement/ProjectRow.tsx index dc32383e3e1..a12364c1a7f 100644 --- a/server/sonar-web/src/main/js/apps/projectsManagement/ProjectRow.tsx +++ b/server/sonar-web/src/main/js/apps/projectsManagement/ProjectRow.tsx @@ -25,6 +25,8 @@ import QualifierIcon from 'sonar-ui-common/components/icons/QualifierIcon'; import DateTooltipFormatter from 'sonar-ui-common/components/intl/DateTooltipFormatter'; import { Project } from '../../api/components'; import PrivacyBadgeContainer from '../../components/common/PrivacyBadgeContainer'; +import { getPortfolioUrl, getProjectUrl } from '../../helpers/urls'; +import { ComponentQualifier } from '../../types/component'; import './ProjectRow.css'; import ProjectRowActions from './ProjectRowActions'; @@ -41,6 +43,12 @@ export default class ProjectRow extends React.PureComponent { this.props.onProjectCheck(this.props.project, checked); }; + getComponentUrl(project: Project) { + return project.qualifier === ComponentQualifier.Portfolio + ? getPortfolioUrl(project.key) + : getProjectUrl(project.key); + } + render() { const { organization, project, selected } = this.props; @@ -51,10 +59,8 @@ export default class ProjectRow extends React.PureComponent { - - + + {project.name} diff --git a/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/ProjectRow-test.tsx b/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/ProjectRow-test.tsx index 60deca5f98d..b1117bd40a6 100644 --- a/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/ProjectRow-test.tsx +++ b/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/ProjectRow-test.tsx @@ -19,20 +19,24 @@ */ import { shallow } from 'enzyme'; import * as React from 'react'; +import { ComponentQualifier, Visibility } from '../../../types/component'; import ProjectRow from '../ProjectRow'; const project = { key: 'project', name: 'Project', - qualifier: 'TRK', - visibility: 'private' + qualifier: ComponentQualifier.Project, + visibility: Visibility.Private }; it('renders', () => { expect(shallowRender()).toMatchSnapshot(); expect( shallowRender({ project: { ...project, lastAnalysisDate: '2017-04-08T00:00:00.000Z' } }) - ).toMatchSnapshot(); + ).toMatchSnapshot('with lastAnalysisDate'); + expect( + shallowRender({ project: { ...project, qualifier: ComponentQualifier.Portfolio } }) + ).toMatchSnapshot('portfolio'); }); it('checks project', () => { diff --git a/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/__snapshots__/ProjectRow-test.tsx.snap b/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/__snapshots__/ProjectRow-test.tsx.snap index bae87bce87c..15c4f5964f2 100644 --- a/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/__snapshots__/ProjectRow-test.tsx.snap +++ b/server/sonar-web/src/main/js/apps/projectsManagement/__tests__/__snapshots__/ProjectRow-test.tsx.snap @@ -24,12 +24,14 @@ exports[`renders 1`] = ` Object { "pathname": "/dashboard", "query": Object { + "branch": undefined, "id": "project", }, } } > `; -exports[`renders 2`] = ` +exports[`renders: portfolio 1`] = ` + + + + + + + + + + Project + + + + + + + + + + + project + + + + + + — + + + + + + +`; + +exports[`renders: with lastAnalysisDate 1`] = ` @@ -124,12 +227,14 @@ exports[`renders 2`] = ` Object { "pathname": "/dashboard", "query": Object { + "branch": undefined, "id": "project", }, } } >