From 4a478729ea3ba1c7903760253253d38a5b8cb6de Mon Sep 17 00:00:00 2001 From: Mathieu Suen Date: Wed, 10 Nov 2021 15:58:41 +0100 Subject: [PATCH] SONAR-13426 Enable project badge for private project --- .../src/main/js/api/project-badges.ts | 27 ++++++++ .../projectInformation/ProjectInformation.tsx | 1 - .../__tests__/ProjectInformation-test.tsx | 13 ++++ .../ProjectInformation-test.tsx.snap | 21 ++++++- .../projectInformation/badges/BadgeParams.tsx | 25 +------- .../badges/ProjectBadges.tsx | 36 +++++++++-- .../badges/__tests__/BadgeButton-test.tsx | 4 +- .../badges/__tests__/BadgeParams-test.tsx | 12 +--- .../badges/__tests__/ProjectBadges-test.tsx | 11 +++- .../__snapshots__/BadgeButton-test.tsx.snap | 4 +- .../__snapshots__/BadgeParams-test.tsx.snap | 63 ------------------- .../__snapshots__/ProjectBadges-test.tsx.snap | 12 ++-- .../badges/__tests__/utils-test.ts | 32 +++++----- .../projectInformation/badges/utils.ts | 20 +++--- .../main/js/components/common/CodeSnippet.tsx | 3 +- .../__snapshots__/CodeSnippet-test.tsx.snap | 4 ++ .../resources/org/sonar/l10n/core.properties | 2 +- 17 files changed, 144 insertions(+), 146 deletions(-) create mode 100644 server/sonar-web/src/main/js/api/project-badges.ts diff --git a/server/sonar-web/src/main/js/api/project-badges.ts b/server/sonar-web/src/main/js/api/project-badges.ts new file mode 100644 index 00000000000..692b8d793c0 --- /dev/null +++ b/server/sonar-web/src/main/js/api/project-badges.ts @@ -0,0 +1,27 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import throwGlobalError from '../app/utils/throwGlobalError'; +import { getJSON } from '../helpers/request'; + +export function getProjectBadgesToken(project: string) { + return getJSON('/api/project_badges/token', { project }) + .then(({ token }) => token) + .catch(throwGlobalError); +} diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/ProjectInformation.tsx b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/ProjectInformation.tsx index 578bd2225cd..0b2072f2d11 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/ProjectInformation.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/ProjectInformation.tsx @@ -90,7 +90,6 @@ export class ProjectInformation extends React.PureComponent { isLoggedIn(currentUser) && component.qualifier === ComponentQualifier.Project; const canUseBadges = metrics !== undefined && - component.visibility !== 'private' && (component.qualifier === ComponentQualifier.Application || component.qualifier === ComponentQualifier.Project); diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/__tests__/ProjectInformation-test.tsx b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/__tests__/ProjectInformation-test.tsx index 856efeeb83c..059fbcba520 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/__tests__/ProjectInformation-test.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/__tests__/ProjectInformation-test.tsx @@ -22,6 +22,8 @@ import * as React from 'react'; import { mockComponent } from '../../../../../../helpers/mocks/component'; import { mockCurrentUser, mockLoggedInUser, mockMetric } from '../../../../../../helpers/testMocks'; import { waitAndUpdate } from '../../../../../../helpers/testUtils'; +import { ComponentQualifier } from '../../../../../../types/component'; +import ProjectBadges from '../badges/ProjectBadges'; import { ProjectInformation } from '../ProjectInformation'; import { ProjectInformationPages } from '../ProjectInformationPages'; @@ -53,6 +55,17 @@ it('should handle page change', async () => { expect(wrapper.state().page).toBe(ProjectInformationPages.badges); }); +it('should display badge', () => { + const wrapper = shallowRender({ + component: mockComponent({ qualifier: ComponentQualifier.Project }) + }); + + expect(wrapper.find(ProjectBadges).type).toBeDefined(); + + wrapper.setProps({ component: mockComponent({ qualifier: ComponentQualifier.Application }) }); + expect(wrapper.find(ProjectBadges).type).toBeDefined(); +}); + function shallowRender(props: Partial = {}) { return shallow( + + + `; diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/BadgeParams.tsx b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/BadgeParams.tsx index 4aa6edc257e..92a8e74dbb2 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/BadgeParams.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/BadgeParams.tsx @@ -22,7 +22,7 @@ import * as React from 'react'; import { fetchWebApi } from '../../../../../../api/web-api'; import Select from '../../../../../../components/controls/Select'; import { getLocalizedMetricName, translate } from '../../../../../../helpers/l10n'; -import { BadgeColors, BadgeFormats, BadgeOptions, BadgeType } from './utils'; +import { BadgeFormats, BadgeOptions, BadgeType } from './utils'; interface Props { className?: string; @@ -90,10 +90,6 @@ export default class BadgeParams extends React.PureComponent { }); }; - handleColorChange = ({ value }: { value: BadgeColors }) => { - this.props.updateOptions({ color: value }); - }; - handleFormatChange = ({ value }: { value: BadgeFormats }) => { this.props.updateOptions({ format: value }); }; @@ -103,24 +99,7 @@ export default class BadgeParams extends React.PureComponent { }; renderBadgeType = (type: BadgeType, options: BadgeOptions) => { - if (type === BadgeType.marketing) { - return ( - <> - - - -