Skip to content

Commit

Permalink
SONAR-11154 Remove parameters deprecated before 6.7 and enforce manda…
Browse files Browse the repository at this point in the history
…tory parameters
  • Loading branch information
dbmeneses authored and SonarTech committed Oct 30, 2019
1 parent dcd9175 commit aa1c782
Show file tree
Hide file tree
Showing 40 changed files with 207 additions and 522 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,11 @@ public void define(WebService.NewController controller) {

activate.createParam(PARAM_KEY)
.setDescription("Quality Profile key. Can be obtained through <code>api/qualityprofiles/search</code>")
.setDeprecatedKey("profile_key", "6.5")
.setRequired(true)
.setExampleValue(UUID_EXAMPLE_01);

activate.createParam(PARAM_RULE)
.setDescription("Rule key")
.setDeprecatedKey("rule_key", "6.5")
.setRequired(true)
.setExampleValue("squid:AvoidCycles");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,11 @@ public void define(WebService.NewController controller) {

activate.createParam(PARAM_TARGET_KEY)
.setDescription("Quality Profile key on which the rule activation is done. To retrieve a quality profile key please see <code>api/qualityprofiles/search</code>")
.setDeprecatedKey("profile_key", "6.5")
.setRequired(true)
.setExampleValue(UUID_EXAMPLE_03);

activate.createParam(PARAM_TARGET_SEVERITY)
.setDescription("Severity to set on the activated rules")
.setDeprecatedKey("activation_severity", "6.5")
.setPossibleValues(Severity.ALL);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@
import org.sonar.server.component.ComponentFinder;
import org.sonar.server.user.UserSession;

import static org.sonar.core.util.Uuids.UUID_EXAMPLE_08;
import static org.sonar.server.component.ComponentFinder.ParamNames.PROJECT_UUID_AND_KEY;
import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_ADD_PROJECT;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT_UUID;

public class AddProjectAction implements QProfileWsAction {

Expand Down Expand Up @@ -77,13 +74,8 @@ public void define(WebService.NewController controller) {

action.createParam(PARAM_PROJECT)
.setDescription("Project key")
.setDeprecatedKey("projectKey", "6.5")
.setRequired(true)
.setExampleValue(KEY_PROJECT_EXAMPLE_001);

action.createParam(PARAM_PROJECT_UUID)
.setDescription("Project ID. Either this parameter or '%s' must be set.", PARAM_PROJECT)
.setDeprecatedSince("6.5")
.setExampleValue(UUID_EXAMPLE_08);
}

@Override
Expand All @@ -92,7 +84,7 @@ public void handle(Request request, Response response) throws Exception {

try (DbSession dbSession = dbClient.openSession(false)) {
ComponentDto project = loadProject(dbSession, request);
QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.from(request));
QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromName(request));
OrganizationDto organization = wsSupport.getOrganization(dbSession, profile);
checkPermissions(dbSession, organization, profile, project);

Expand All @@ -115,9 +107,8 @@ public void handle(Request request, Response response) throws Exception {
}

private ComponentDto loadProject(DbSession dbSession, Request request) {
String projectKey = request.param(PARAM_PROJECT);
String projectUuid = request.param(PARAM_PROJECT_UUID);
return componentFinder.getByUuidOrKey(dbSession, projectUuid, projectKey, PROJECT_UUID_AND_KEY);
String projectKey = request.mandatoryParam(PARAM_PROJECT);
return componentFinder.getByKey(dbSession, projectKey);
}

private void checkPermissions(DbSession dbSession, OrganizationDto organization, QProfileDto profile, ComponentDto project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void handle(Request request, Response response) throws Exception {
try (OutputStreamWriter writer = new OutputStreamWriter(stream.output(), UTF_8);
DbSession dbSession = dbClient.openSession(false)) {

QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.from(request));
QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromName(request));
response.setHeader("Content-Disposition", String.format("attachment; filename=%s.xml", profile.getKee()));
backuper.backup(dbSession, profile, writer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
import org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters;

import static org.apache.commons.lang.StringUtils.isEmpty;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PARENT_KEY;

public class ChangeParentAction implements QProfileWsAction {

Expand Down Expand Up @@ -71,36 +69,29 @@ public void define(NewController context) {
.setSince("6.4");
QProfileReference.defineParams(inheritance, languages);

inheritance.createParam(PARAM_PARENT_KEY)
.setDescription("New parent profile key.<br> " +
"If no profile is provided, the inheritance link with current parent profile (if any) is broken, which deactivates all rules " +
"which come from the parent and are not overridden.")
.setDeprecatedSince("6.6")
.setExampleValue(UUID_EXAMPLE_02);

inheritance.createParam(QualityProfileWsParameters.PARAM_PARENT_QUALITY_PROFILE)
.setDescription("Quality profile name. If this parameter is set, '%s' must not be set and '%s' must be set to disambiguate.", PARAM_PARENT_KEY, PARAM_LANGUAGE)
.setDescription("New parent profile name. <br> " +
"If no profile is provided, the inheritance link with current parent profile (if any) is broken, which deactivates all rules " +
"which come from the parent and are not overridden.")
.setExampleValue("Sonar way");
}

@Override
public void handle(Request request, Response response) throws Exception {
userSession.checkLoggedIn();
QProfileReference reference = QProfileReference.from(request);
QProfileReference reference = QProfileReference.fromName(request);

try (DbSession dbSession = dbClient.openSession(false)) {
QProfileDto profile = wsSupport.getProfile(dbSession, reference);
OrganizationDto organization = wsSupport.getOrganization(dbSession, profile);
wsSupport.checkCanEdit(dbSession, organization, profile);

String parentKey = request.param(PARAM_PARENT_KEY);
String parentName = request.param(QualityProfileWsParameters.PARAM_PARENT_QUALITY_PROFILE);
if (isEmpty(parentKey) && isEmpty(parentName)) {
if (isEmpty(parentName)) {
ruleActivator.removeParentAndCommit(dbSession, profile);
} else {
String parentOrganizationKey = parentKey == null ? organization.getKey() : null;
String parentLanguage = parentKey == null ? request.param(PARAM_LANGUAGE) : null;
QProfileReference parentRef = QProfileReference.from(parentKey, parentOrganizationKey, parentLanguage, parentName);
String parentLanguage = request.mandatoryParam(PARAM_LANGUAGE);
QProfileReference parentRef = QProfileReference.fromName(organization.getKey(), parentLanguage, parentName);
QProfileDto parent = wsSupport.getProfile(dbSession, parentRef);
ruleActivator.setParentAndCommit(dbSession, profile, parent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void define(NewController context) {

@Override
public void handle(Request request, Response response) throws Exception {
QProfileReference reference = QProfileReference.from(request);
QProfileReference reference = QProfileReference.fromName(request);
try (DbSession dbSession = dbClient.openSession(false)) {
QProfileDto profile = wsSupport.getProfile(dbSession, reference);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ public void define(WebService.NewController controller) {
.setRequired(true)
.setMaximumLength(NAME_MAXIMUM_LENGTH)
.setDescription("Quality profile name")
.setExampleValue("My Sonar way")
.setDeprecatedKey("profileName", "6.6");
.setExampleValue("My Sonar way");

create.createParam(PARAM_LANGUAGE)
.setRequired(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,11 @@ public void define(WebService.NewController controller) {

deactivate.createParam(PARAM_KEY)
.setDescription("Quality Profile key. Can be obtained through <code>api/qualityprofiles/search</code>")
.setDeprecatedKey("profile_key", "6.5")
.setRequired(true)
.setExampleValue(UUID_EXAMPLE_01);

deactivate.createParam(PARAM_RULE)
.setDescription("Rule key")
.setDeprecatedKey("rule_key", "6.5")
.setRequired(true)
.setExampleValue("squid:AvoidCycles");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public void define(WebService.NewController controller) {

deactivate.createParam(PARAM_TARGET_KEY)
.setDescription("Quality Profile key on which the rule deactivation is done. To retrieve a profile key please see <code>api/qualityprofiles/search</code>")
.setDeprecatedKey("profile_key", "6.5")
.setRequired(true)
.setExampleValue(UUID_EXAMPLE_04);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void handle(Request request, Response response) {
userSession.checkLoggedIn();

try (DbSession dbSession = dbClient.openSession(false)) {
QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.from(request));
QProfileDto profile = wsSupport.getProfile(dbSession, QProfileReference.fromName(request));
OrganizationDto organization = wsSupport.getOrganization(dbSession, profile);
wsSupport.checkCanEdit(dbSession, organization, profile);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@
import org.sonarqube.ws.MediaTypes;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
import static org.sonar.server.exceptions.BadRequestException.checkRequest;
import static org.sonar.server.exceptions.NotFoundException.checkFound;
import static org.sonar.server.qualityprofile.ws.QProfileWsSupport.createOrganizationParam;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_ORGANIZATION;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;

Expand Down Expand Up @@ -82,19 +79,13 @@ public void define(WebService.NewController controller) {
.setResponseExample(getClass().getResource("export-example.xml"))
.setHandler(this);

action.createParam(PARAM_KEY)
.setDescription("Quality profile key")
.setSince("6.5")
.setDeprecatedSince("6.6")
.setExampleValue(UUID_EXAMPLE_01);

action.createParam(PARAM_QUALITY_PROFILE)
.setDescription("Quality profile name to export. If left empty, the default profile for the language is exported.")
.setDeprecatedKey("name", "6.6")
.setExampleValue("My Sonar way");

action.createParam(PARAM_LANGUAGE)
.setDescription("Quality profile language")
.setRequired(true)
.setExampleValue(LanguageParamUtils.getExampleValue(languages))
.setPossibleValues(LanguageParamUtils.getOrderedLanguageKeys(languages));

Expand All @@ -118,14 +109,12 @@ public void define(WebService.NewController controller) {

@Override
public void handle(Request request, Response response) throws Exception {
String key = request.param(PARAM_KEY);
String name = request.param(PARAM_QUALITY_PROFILE);
String language = request.param(PARAM_LANGUAGE);
checkRequest(key != null ^ language != null, "Either '%s' or '%s' must be provided.", PARAM_KEY, PARAM_LANGUAGE);
String language = request.mandatoryParam(PARAM_LANGUAGE);

try (DbSession dbSession = dbClient.openSession(false)) {
OrganizationDto organization = wsSupport.getOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION));
QProfileDto profile = loadProfile(dbSession, organization, key, language, name);
QProfileDto profile = loadProfile(dbSession, organization, language, name);
String exporterKey = exporters.exportersForLanguage(profile.getLanguage()).isEmpty() ? null : request.param(PARAM_EXPORTER_KEY);
writeResponse(dbSession, profile, exporterKey, response);
}
Expand All @@ -148,14 +137,9 @@ private void writeResponse(DbSession dbSession, QProfileDto profile, @Nullable S
IOUtils.write(bufferStream.toByteArray(), output);
}

private QProfileDto loadProfile(DbSession dbSession, OrganizationDto organization, @Nullable String key, @Nullable String language, @Nullable String name) {
private QProfileDto loadProfile(DbSession dbSession, OrganizationDto organization, String language, @Nullable String name) {
QProfileDto profile;
if (key != null) {
profile = dbClient.qualityProfileDao().selectByUuid(dbSession, key);
return checkFound(profile, "Could not find profile with key '%s'", key);
}

checkRequest(language != null, "Parameter '%s' must be provided", PARAM_LANGUAGE);
if (name == null) {
// return the default profile
profile = dbClient.qualityProfileDao().selectDefaultProfile(dbSession, organization, language);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void define(NewController context) {

@Override
public void handle(Request request, Response response) throws Exception {
QProfileReference reference = QProfileReference.from(request);
QProfileReference reference = QProfileReference.fromName(request);
try (DbSession dbSession = dbClient.openSession(false)) {
QProfileDto profile = wsSupport.getProfile(dbSession, reference);
OrganizationDto organization = wsSupport.getOrganization(dbSession, profile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,9 @@ public void define(NewController controller) {
.setExampleValue(UUID_EXAMPLE_01);
action.addSelectionModeParam();

action.createSearchQuery("sonar", "projects")
.setDeprecatedKey("query", "6.5");
action.createSearchQuery("sonar", "projects");

action.createPageParam()
.setDeprecatedKey("page", "6.5");
action.createPageParam();

action.createPageSize(100, MAX_PAGE_SIZE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@
import org.sonar.api.server.ws.WebService;
import org.sonar.core.util.stream.MoreCollectors;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
import static org.apache.commons.lang.StringUtils.isEmpty;
import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_ORGANIZATION;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;

Expand Down Expand Up @@ -149,22 +145,10 @@ public int hashCode() {
return result;
}

public static QProfileReference from(Request request) {
String key = request.param(PARAM_KEY);
public static QProfileReference fromName(Request request) {
String organizationKey = request.param(PARAM_ORGANIZATION);
String lang = request.param(PARAM_LANGUAGE);
String name = request.param(PARAM_QUALITY_PROFILE);
return from(key, organizationKey, lang, name);
}

public static QProfileReference from(@Nullable String key, @Nullable String organizationKey, @Nullable String lang, @Nullable String name) {
if (key != null) {
checkArgument(isEmpty(organizationKey) && isEmpty(lang) && isEmpty(name),
"When a quality profile key is set, '%s' '%s' and '%s' can't be set", PARAM_ORGANIZATION, PARAM_LANGUAGE, PARAM_QUALITY_PROFILE);
return fromKey(key);
}
checkArgument(!isEmpty(lang) && !isEmpty(name),
"If '%s' is not specified, '%s' and '%s' must be set", PARAM_KEY, PARAM_QUALITY_PROFILE, PARAM_LANGUAGE);
String lang = request.mandatoryParam(PARAM_LANGUAGE);
String name = request.mandatoryParam(PARAM_QUALITY_PROFILE);
return fromName(organizationKey, lang, name);
}

Expand All @@ -177,19 +161,14 @@ public static QProfileReference fromName(@Nullable String organizationKey, Strin
}

public static void defineParams(WebService.NewAction action, Languages languages) {
action.createParam(PARAM_KEY)
.setDescription("Quality profile key. Mandatory unless 'qualityProfile' and 'language' are specified.")
.setDeprecatedKey("profileKey", "6.5")
.setDeprecatedSince("6.6")
.setExampleValue(UUID_EXAMPLE_01);

action.createParam(PARAM_QUALITY_PROFILE)
.setDescription("Quality profile name. Mandatory if 'key' is not set.")
.setDeprecatedKey("profileName", "6.6")
.setDescription("Quality profile name.")
.setRequired(true)
.setExampleValue("Sonar way");

action.createParam(PARAM_LANGUAGE)
.setDescription("Quality profile language. Mandatory if 'key' is not set.")
.setDescription("Quality profile language.")
.setRequired(true)
.setPossibleValues(Arrays.stream(languages.all()).map(Language::getKey).collect(MoreCollectors.toSet()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
import static org.sonar.db.organization.OrganizationDto.Subscription.PAID;
import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
import static org.sonar.server.exceptions.BadRequestException.checkRequest;
import static org.sonar.server.exceptions.NotFoundException.checkFound;
import static org.sonar.server.exceptions.NotFoundException.checkFoundWithOptional;
import static org.sonar.server.exceptions.BadRequestException.checkRequest;
import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_ORGANIZATION;

@ServerSide
Expand Down
Loading

0 comments on commit aa1c782

Please sign in to comment.