Skip to content

Commit

Permalink
SONAR-9303 WS api/qualityprofiles/restore_built_in returns http 410 w…
Browse files Browse the repository at this point in the history
…hen called
  • Loading branch information
teryk authored and ehartmann committed Jun 14, 2017
1 parent 90c44d0 commit 702b639
Show file tree
Hide file tree
Showing 30 changed files with 242 additions and 734 deletions.
3 changes: 1 addition & 2 deletions it/it-tests/src/test/java/it/analysis/IssuesModeTest.java
Expand Up @@ -207,9 +207,8 @@ public void only_scan_changed_files_on_change() throws IOException {


// SONAR-8518 // SONAR-8518
@Test @Test
public void shoud_support_sonar_profile_prop() throws IOException { public void should_support_sonar_profile_prop() throws IOException {
restoreProfile("one-issue-per-line.xml"); restoreProfile("one-issue-per-line.xml");
restoreProfile("empty.xml");
orchestrator.getServer().provisionProject("sample", "xoo-sample"); orchestrator.getServer().provisionProject("sample", "xoo-sample");
orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "empty"); orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", "empty");


Expand Down
Expand Up @@ -152,9 +152,7 @@ public void testSettingDefault() throws Exception {
public void testRestoration() throws Exception { public void testRestoration() throws Exception {
deleteProfile("xoo", "empty"); deleteProfile("xoo", "empty");


runSelenese(orchestrator, runSelenese(orchestrator, "/organization/OrganizationQualityProfilesPageTest/should_restore.html");
"/organization/OrganizationQualityProfilesPageTest/should_restore.html",
"/organization/OrganizationQualityProfilesPageTest/should_restore_built_in.html");
} }


private static void createProfile(String language, String name) { private static void createProfile(String language, String name) {
Expand Down
Expand Up @@ -134,9 +134,7 @@ public void testSettingDefault() throws Exception {
public void testRestoration() throws Exception { public void testRestoration() throws Exception {
deleteProfile("xoo", "empty"); deleteProfile("xoo", "empty");


runSelenese(orchestrator, runSelenese(orchestrator, "/qualityProfile/QualityProfilesPageTest/should_restore.html");
"/qualityProfile/QualityProfilesPageTest/should_restore.html",
"/qualityProfile/QualityProfilesPageTest/should_restore_built_in.html");
} }


private static void createProfile(String language, String name) { private static void createProfile(String language, String name) {
Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -34,6 +34,7 @@
import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;


import static com.google.common.base.Preconditions.checkArgument;
import static org.sonar.server.ws.WsUtils.checkRequest; import static org.sonar.server.ws.WsUtils.checkRequest;


/** /**
Expand All @@ -55,12 +56,20 @@ public QProfileFactory(DbClient db, UuidFactory uuidFactory, System2 system2, Ac


// ------------- CREATION // ------------- CREATION


private static OrganizationDto requireNonNull(@Nullable OrganizationDto organization) {
Objects.requireNonNull(organization, "Organization is required, when creating a quality profile.");
return organization;
}

RulesProfileDto getOrCreateCustom(DbSession dbSession, OrganizationDto organization, QProfileName name) { RulesProfileDto getOrCreateCustom(DbSession dbSession, OrganizationDto organization, QProfileName name) {
requireNonNull(organization); requireNonNull(organization);
RulesProfileDto profile = db.qualityProfileDao().selectByNameAndLanguage(organization, name.getName(), name.getLanguage(), dbSession); RulesProfileDto profile = db.qualityProfileDao().selectByNameAndLanguage(organization, name.getName(), name.getLanguage(), dbSession);
if (profile == null) { if (profile == null) {
profile = doCreate(dbSession, organization, name, false, false); profile = doCreate(dbSession, organization, name, false, false);
} else {
checkArgument(!profile.isBuiltIn(), "Operation forbidden for built-in Quality Profile '%s' with language '%s'", profile.getName(), profile.getLanguage());
} }

return profile; return profile;
} }


Expand All @@ -85,11 +94,6 @@ public RulesProfileDto createBuiltIn(DbSession dbSession, OrganizationDto organi
return doCreate(dbSession, requireNonNull(organization), name, isDefault, true); return doCreate(dbSession, requireNonNull(organization), name, isDefault, true);
} }


private static OrganizationDto requireNonNull(@Nullable OrganizationDto organization) {
Objects.requireNonNull(organization, "Organization is required, when creating a quality profile.");
return organization;
}

private RulesProfileDto doCreate(DbSession dbSession, OrganizationDto organization, QProfileName name, boolean isDefault, boolean isBuiltIn) { private RulesProfileDto doCreate(DbSession dbSession, OrganizationDto organization, QProfileName name, boolean isDefault, boolean isBuiltIn) {
if (StringUtils.isEmpty(name.getName())) { if (StringUtils.isEmpty(name.getName())) {
throw BadRequestException.create("quality_profiles.profile_name_cant_be_blank"); throw BadRequestException.create("quality_profiles.profile_name_cant_be_blank");
Expand Down
Expand Up @@ -21,15 +21,9 @@


import java.util.Collection; import java.util.Collection;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualityprofile.RulesProfileDto; import org.sonar.db.qualityprofile.RulesProfileDto;


public interface QProfileReset { public interface QProfileReset {
/**
* Restore the built-in profiles provided by plugins for the specified language.
* Missing profiles are created and existing ones are updated.
*/
void resetLanguage(DbSession dbSession, OrganizationDto organization, String language);


/** /**
* Reset the rules of the specified profile. * Reset the rules of the specified profile.
Expand Down
Expand Up @@ -19,84 +19,41 @@
*/ */
package org.sonar.server.qualityprofile; package org.sonar.server.qualityprofile;


import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.ActiveRule;
import org.sonar.api.rules.ActiveRuleParam;
import org.sonar.api.server.ServerSide; import org.sonar.api.server.ServerSide;
import org.sonar.db.DbClient; import org.sonar.db.DbClient;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualityprofile.ActiveRuleDto; import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.ActiveRuleKey; import org.sonar.db.qualityprofile.ActiveRuleKey;
import org.sonar.db.qualityprofile.RulesProfileDto; import org.sonar.db.qualityprofile.RulesProfileDto;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;


import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;


@ServerSide @ServerSide
public class QProfileResetImpl implements QProfileReset { public class QProfileResetImpl implements QProfileReset {


private final DbClient db; private final DbClient db;
private final QProfileFactory factory;
private final RuleActivator activator; private final RuleActivator activator;
private final ActiveRuleIndexer activeRuleIndexer; private final ActiveRuleIndexer activeRuleIndexer;
private final BuiltInQProfileRepository builtInQProfileRepositories;


public QProfileResetImpl(DbClient db, RuleActivator activator, ActiveRuleIndexer activeRuleIndexer, QProfileFactory factory, public QProfileResetImpl(DbClient db, RuleActivator activator, ActiveRuleIndexer activeRuleIndexer) {
BuiltInQProfileRepository builtInQProfileRepository) {
this.db = db; this.db = db;
this.activator = activator; this.activator = activator;
this.activeRuleIndexer = activeRuleIndexer; this.activeRuleIndexer = activeRuleIndexer;
this.factory = factory;
this.builtInQProfileRepositories = builtInQProfileRepository;
}

@Override
public void resetLanguage(DbSession dbSession, OrganizationDto organization, String language) {
builtInQProfileRepositories.getQProfilesByLanguage()
.entrySet()
.stream()
.filter(entry -> entry.getKey().equals(language))
.map(Map.Entry::getValue)
.flatMap(List::stream)
.forEach(builtInQProfile -> resetProfile(dbSession, organization, builtInQProfile));
}

private void resetProfile(DbSession dbSession, OrganizationDto organization, BuiltInQProfile builtInQProfile) {
RulesProfileDto profile = factory.getOrCreateCustom(dbSession, organization, builtInQProfile.getQProfileName());

List<RuleActivation> activations = Lists.newArrayList();
builtInQProfile.getActiveRules().forEach(activeRule -> activations.add(getRuleActivation(dbSession, activeRule)));
reset(dbSession, profile, activations);
}

private RuleActivation getRuleActivation(DbSession dbSession, ActiveRule activeRule) {
RuleActivation activation = new RuleActivation(RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey()));
activation.setSeverity(activeRule.getSeverity().name());
if (!activeRule.getActiveRuleParams().isEmpty()) {
for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
activation.setParameter(param.getParamKey(), param.getValue());
}
} else {
for (RuleParamDto param : db.ruleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) {
activation.setParameter(param.getName(), param.getDefaultValue());
}
}
return activation;
} }


@Override @Override
public BulkChangeResult reset(DbSession dbSession, RulesProfileDto profile, Collection<RuleActivation> activations) { public BulkChangeResult reset(DbSession dbSession, RulesProfileDto profile, Collection<RuleActivation> activations) {
requireNonNull(profile.getId(), "Quality profile must be persisted"); requireNonNull(profile.getId(), "Quality profile must be persisted");
checkArgument(!profile.isBuiltIn(), "Operation forbidden for built-in Quality Profile '%s'", profile.getKee());
BulkChangeResult result = new BulkChangeResult(); BulkChangeResult result = new BulkChangeResult();
Set<RuleKey> ruleToBeDeactivated = Sets.newHashSet(); Set<RuleKey> ruleToBeDeactivated = Sets.newHashSet();
// Keep reference to all the activated rules before backup restore // Keep reference to all the activated rules before backup restore
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.sonar.core.util.Uuids; import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient; import org.sonar.db.DbClient;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.RulesProfileDto;
import org.sonar.server.qualityprofile.ActiveRuleChange; import org.sonar.server.qualityprofile.ActiveRuleChange;
import org.sonar.server.qualityprofile.RuleActivation; import org.sonar.server.qualityprofile.RuleActivation;
import org.sonar.server.qualityprofile.RuleActivator; import org.sonar.server.qualityprofile.RuleActivator;
Expand Down Expand Up @@ -106,7 +107,9 @@ public void handle(Request request, Response response) throws Exception {
String profileKey = request.mandatoryParam(PARAM_PROFILE_KEY); String profileKey = request.mandatoryParam(PARAM_PROFILE_KEY);
userSession.checkLoggedIn(); userSession.checkLoggedIn();
try (DbSession dbSession = dbClient.openSession(false)) { try (DbSession dbSession = dbClient.openSession(false)) {
wsSupport.checkPermission(dbSession, profileKey); RulesProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromKey(profileKey));
wsSupport.checkPermission(dbSession, profile);
wsSupport.checkNotBuiltInt(profile);
List<ActiveRuleChange> changes = ruleActivator.activate(dbSession, activation, profileKey); List<ActiveRuleChange> changes = ruleActivator.activate(dbSession, activation, profileKey);
dbSession.commit(); dbSession.commit();
activeRuleIndexer.index(changes); activeRuleIndexer.index(changes);
Expand Down

0 comments on commit 702b639

Please sign in to comment.