import com.google.common.io.Resources;
import java.util.Collection;
+import java.util.Optional;
import javax.annotation.Nullable;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualitygate.QualityGateDto;
+import org.sonar.server.qualitygate.QualityGateFinder;
import org.sonarqube.ws.Qualitygates.ListWsResponse;
import org.sonarqube.ws.Qualitygates.ListWsResponse.QualityGate;
private final DbClient dbClient;
private final QualityGatesWsSupport wsSupport;
+ private final QualityGateFinder finder;
- public ListAction(DbClient dbClient, QualityGatesWsSupport wsSupport) {
+ public ListAction(DbClient dbClient, QualityGatesWsSupport wsSupport, QualityGateFinder finder) {
this.dbClient = dbClient;
this.wsSupport = wsSupport;
+ this.finder = finder;
}
@Override
@Override
public void handle(Request request, Response response) {
try (DbSession dbSession = dbClient.openSession(false)) {
- QualityGateDto defaultQualityGate = wsSupport.getDefault(dbSession);
+ Optional<QualityGateDto> defaultQualityGate = finder.getDefault(dbSession);
Collection<QualityGateDto> qualityGates = dbClient.qualityGateDao().selectAll(dbSession);
- writeProtobuf(buildResponse(qualityGates, defaultQualityGate), request, response);
+ writeProtobuf(buildResponse(qualityGates, defaultQualityGate.orElse(null)), request, response);
}
}
package org.sonar.server.qualitygate.ws;
-import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
-import org.apache.commons.lang.StringUtils;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.organization.OrganizationDto;
-import org.sonar.db.property.PropertyDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
-import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Qualitygates;
import static com.google.common.base.Preconditions.checkArgument;
-import static java.lang.String.format;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
-import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY;
import static org.sonar.server.ws.WsUtils.checkFound;
public class QualityGatesWsSupport {
return checkFound(dbClient.gateConditionDao().selectById(id, dbSession), "No quality gate condition with id '%d'", id);
}
- OrganizationDto getOrganization(DbSession dbSession) {
- String organizationKey = defaultOrganizationProvider.get().getKey();
- return dbClient.organizationDao().selectByKey(dbSession, organizationKey)
- .orElseThrow(() -> new NotFoundException(format("No organization with key '%s'", organizationKey)));
- }
-
boolean isQualityGateAdmin() {
return userSession.hasPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
}
.build();
}
- @CheckForNull
- QualityGateDto getDefault(DbSession dbSession) {
- Long defaultId = getDefaultId(dbSession);
- if (defaultId == null) {
- return null;
- }
- return dbClient.qualityGateDao().selectById(dbSession, defaultId);
- }
-
- @CheckForNull
- private Long getDefaultId(DbSession dbSession) {
- PropertyDto defaultQgate = dbClient.propertiesDao().selectGlobalProperty(dbSession, SONAR_QUALITYGATE_PROPERTY);
- if (defaultQgate == null || StringUtils.isBlank(defaultQgate.getValue())) {
- return null;
- }
- return Long.valueOf(defaultQgate.getValue());
- }
-
void checkCanEdit(QualityGateDto qualityGate) {
checkNotBuiltInt(qualityGate);
userSession.checkPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
import com.google.common.io.Resources;
import java.util.Collection;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nullable;
QualityGateDto qualityGate = qualityGateFinder.getByNameOrId(dbSession, name, id);
Collection<QualityGateConditionDto> conditions = getConditions(dbSession, qualityGate);
Map<Integer, MetricDto> metricsById = getMetricsById(dbSession, conditions);
- QualityGateDto defaultQualityGate = wsSupport.getDefault(dbSession);
- writeProtobuf(buildResponse(qualityGate, defaultQualityGate, conditions, metricsById), request, response);
+ Optional<QualityGateDto> defaultQualityGate = qualityGateFinder.getDefault(dbSession);
+ writeProtobuf(buildResponse(qualityGate, defaultQualityGate.orElse(null), conditions, metricsById), request, response);
}
}
import org.junit.rules.ExpectedException;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
+import org.sonar.server.qualitygate.QualityGateFinder;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
import org.sonarqube.ws.Qualitygates.ListWsResponse.QualityGate;
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public DbTester db = DbTester.create();
- private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
- private WsActionTester ws = new WsActionTester(new ListAction(db.getDbClient(), new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider)));
+ private DbClient dbClient = db.getDbClient();
+ private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
+ private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient);
+ private WsActionTester ws = new WsActionTester(new ListAction(db.getDbClient(),
+ new QualityGatesWsSupport(dbClient, userSession, defaultOrganizationProvider), qualityGateFinder));
@Test
public void verify_definition() {
import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.WebService.Param;
import org.sonar.api.utils.System2;
+import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.metric.MetricDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
+ private DbClient dbClient = db.getDbClient();
private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
-
private WsActionTester ws = new WsActionTester(
- new ShowAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()), new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider)));
+ new ShowAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()),
+ new QualityGatesWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider)));
@Test
public void verify_definition() {