Skip to content

Commit

Permalink
SONAR-17115 Migrate Multiple ALM feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-davis-sonarsource authored and sonartech committed Sep 27, 2022
1 parent a42eb5b commit 57a8a41
Show file tree
Hide file tree
Showing 27 changed files with 163 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,32 @@
*/
package org.sonar.server.almsettings;

import java.util.Optional;
import org.sonar.core.platform.EditionProvider;
import org.sonar.core.platform.PlatformEditionProvider;
import org.sonar.api.SonarRuntime;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.server.ServerSide;
import org.sonar.server.feature.SonarQubeFeature;

public class MultipleAlmFeatureProvider {
import static org.sonar.api.SonarEdition.DATACENTER;
import static org.sonar.api.SonarEdition.ENTERPRISE;

private PlatformEditionProvider editionProvider;
@ServerSide
@ComputeEngineSide
public class MultipleAlmFeature implements SonarQubeFeature {

public MultipleAlmFeatureProvider(PlatformEditionProvider editionProvider) {
this.editionProvider = editionProvider;
private final SonarRuntime sonarRuntime;

public MultipleAlmFeature(SonarRuntime sonarRuntime) {
this.sonarRuntime = sonarRuntime;
}

@Override
public String getName() {
return "multiple-alm";
}

public boolean enabled() {
Optional<EditionProvider.Edition> edition = editionProvider.get();
if (!edition.isPresent()) {
return false;
}
switch (edition.get()) {
case ENTERPRISE:
case DATACENTER:
return true;
default:
return false;
}
@Override
public boolean isEnabled() {
return sonarRuntime.getEdition() == ENTERPRISE || sonarRuntime.getEdition() == DATACENTER;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* SonarQube
* Copyright (C) 2009-2022 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.
*/
package org.sonar.server.almsettings;

import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sonar.api.SonarEdition;
import org.sonar.api.SonarRuntime;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(DataProviderRunner.class)
public class MultipleAlmFeatureTest {

private final SonarRuntime sonarRuntime = mock(SonarRuntime.class);
private final MultipleAlmFeature underTest = new MultipleAlmFeature(sonarRuntime);

@Test
public void getName_nameShouldBeCorrect() {
assertEquals("multiple-alm",underTest.getName());
}

@Test
@UseDataProvider("editionsAndMultipleAlmAvailability")
public void isEnabled_shouldOnlyBeEnabledInEnterpriseEditionPlus(SonarEdition edition, boolean shouldBeEnabled) {
when(sonarRuntime.getEdition()).thenReturn(edition);

boolean isEnabled = underTest.isEnabled();

assertThat(isEnabled).isEqualTo(shouldBeEnabled);
}

@DataProvider
public static Object[][] editionsAndMultipleAlmAvailability() {
return new Object[][] {
{SonarEdition.COMMUNITY, false},
{SonarEdition.DEVELOPER, false},
{SonarEdition.ENTERPRISE, true},
{SonarEdition.DATACENTER, true}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ exports[`should render additional categories component correctly 3`] = `
`;

exports[`should render additional categories component correctly 4`] = `
<withRouter(withAppStateContext(AlmIntegration))
<withRouter(withAppStateContext(withAvailableFeaturesContext(AlmIntegration)))
categories={Array []}
component={
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ exports[`should render almintegration correctly 1`] = `
className="big-padded"
key="almintegration"
>
<withRouter(withAppStateContext(AlmIntegration))
<withRouter(withAppStateContext(withAvailableFeaturesContext(AlmIntegration)))
categories={
Array [
"foo category",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
validateAlmSettings
} from '../../../../api/alm-settings';
import withAppStateContext from '../../../../app/components/app-state/withAppStateContext';
import withAvailableFeatures from '../../../../app/components/available-features/withAvailableFeatures';
import { Location, Router, withRouter } from '../../../../components/hoc/withRouter';
import {
AlmBindingDefinitionBase,
Expand All @@ -34,11 +35,13 @@ import {
AlmSettingsBindingStatusType
} from '../../../../types/alm-settings';
import { AppState } from '../../../../types/appstate';
import { Feature } from '../../../../types/features';
import { Dict } from '../../../../types/types';
import AlmIntegrationRenderer from './AlmIntegrationRenderer';

interface Props {
appState: AppState;
hasFeature: (feature: Feature) => boolean;
location: Location;
router: Router;
}
Expand Down Expand Up @@ -210,7 +213,8 @@ export class AlmIntegration extends React.PureComponent<Props, State> {

render() {
const {
appState: { branchesEnabled, multipleAlmEnabled }
appState: { branchesEnabled },
hasFeature
} = this.props;
const {
currentAlmTab,
Expand All @@ -225,7 +229,7 @@ export class AlmIntegration extends React.PureComponent<Props, State> {
return (
<AlmIntegrationRenderer
branchesEnabled={Boolean(branchesEnabled)}
multipleAlmEnabled={Boolean(multipleAlmEnabled)}
multipleAlmEnabled={hasFeature(Feature.MultipleAlm)}
onCancelDelete={this.handleCancelDelete}
onConfirmDelete={this.handleConfirmDelete}
onCheckConfiguration={this.handleCheck}
Expand All @@ -244,4 +248,4 @@ export class AlmIntegration extends React.PureComponent<Props, State> {
}
}

export default withRouter(withAppStateContext(AlmIntegration));
export default withRouter(withAppStateContext(withAvailableFeatures(AlmIntegration)));
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ function shallowRender(props: Partial<AlmIntegration['props']> = {}) {
<AlmIntegration
appState={mockAppState({ branchesEnabled: true })}
location={mockLocation()}
hasFeature={jest.fn()}
router={mockRouter()}
{...props}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ exports[`should render correctly 1`] = `
}
loadingAlmDefinitions={true}
loadingProjectCount={false}
multipleAlmEnabled={false}
onCancelDelete={[Function]}
onCheckConfiguration={[Function]}
onConfirmDelete={[Function]}
Expand Down
1 change: 0 additions & 1 deletion server/sonar-web/src/main/js/types/appstate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export interface AppState {
edition?: EditionKey;
globalPages?: Extension[];
instanceUsesDefaultAdminCredentials?: boolean;
multipleAlmEnabled?: boolean;
needIssueSync?: boolean;
productionDatabase: boolean;
qualifiers: string[];
Expand Down
3 changes: 2 additions & 1 deletion server/sonar-web/src/main/js/types/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
export enum Feature {
MonoRepositoryPullRequestDecoration = 'monorepo',
RegulatoryReport = 'regulatory-reports',
ProjectImport = 'project-import'
ProjectImport = 'project-import',
MultipleAlm = 'multiple-alm'
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.sonar.db.alm.setting.ALM;
import org.sonar.db.alm.setting.AlmSettingDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.server.almsettings.MultipleAlmFeatureProvider;
import org.sonar.server.almsettings.MultipleAlmFeature;
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException;
Expand All @@ -44,18 +44,18 @@ public class AlmSettingsSupport {
private final DbClient dbClient;
private final UserSession userSession;
private final ComponentFinder componentFinder;
private final MultipleAlmFeatureProvider multipleAlmFeatureProvider;
private final MultipleAlmFeature multipleAlmFeature;

public AlmSettingsSupport(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder,
MultipleAlmFeatureProvider multipleAlmFeatureProvider) {
MultipleAlmFeature multipleAlmFeature) {
this.dbClient = dbClient;
this.userSession = userSession;
this.componentFinder = componentFinder;
this.multipleAlmFeatureProvider = multipleAlmFeatureProvider;
this.multipleAlmFeature = multipleAlmFeature;
}

public MultipleAlmFeatureProvider getMultipleAlmFeatureProvider() {
return multipleAlmFeatureProvider;
public MultipleAlmFeature getMultipleAlmFeatureProvider() {
return multipleAlmFeature;
}

public void checkAlmSettingDoesNotAlreadyExist(DbSession dbSession, String almSetting) {
Expand All @@ -67,7 +67,7 @@ public void checkAlmSettingDoesNotAlreadyExist(DbSession dbSession, String almSe

public void checkAlmMultipleFeatureEnabled(ALM alm) {
try (DbSession dbSession = dbClient.openSession(false)) {
if (!multipleAlmFeatureProvider.enabled() && !dbClient.almSettingDao().selectByAlm(dbSession, alm).isEmpty()) {
if (!multipleAlmFeature.isEnabled() && !dbClient.almSettingDao().selectByAlm(dbSession, alm).isEmpty()) {
throw BadRequestException.create("A " + alm + " setting is already defined");
}
}
Expand Down
Loading

0 comments on commit 57a8a41

Please sign in to comment.