@@ -32,7 +32,7 @@ public final class SnapshotDto { | |||
*/ | |||
public static final String STATUS_UNPROCESSED = "U"; | |||
public static final String STATUS_PROCESSED = "P"; | |||
private static final int MAX_VERSION_LENGTH = 100; | |||
public static final int MAX_VERSION_LENGTH = 100; | |||
private Long id; | |||
private String uuid; | |||
@@ -122,7 +122,7 @@ public final class SnapshotDto { | |||
public SnapshotDto setCodePeriodVersion(@Nullable String codePeriodVersion) { | |||
if (codePeriodVersion != null) { | |||
checkArgument(codePeriodVersion.length() <= MAX_VERSION_LENGTH, | |||
"Event name length (%s) is longer than the maximum authorized (%s). '%s' was provided.", codePeriodVersion.length(), MAX_VERSION_LENGTH, codePeriodVersion); | |||
"codePeriodVersion length (%s) is longer than the maximum authorized (%s). '%s' was provided.", codePeriodVersion.length(), MAX_VERSION_LENGTH, codePeriodVersion); | |||
} | |||
this.codePeriodVersion = codePeriodVersion; | |||
return this; |
@@ -62,7 +62,8 @@ public class SnapshotDtoTest { | |||
snapshotDto.setCodePeriodVersion(repeat("a", 100)); | |||
expectedException.expect(IllegalArgumentException.class); | |||
expectedException.expectMessage("Event name length (101) is longer than the maximum authorized (100). " + | |||
expectedException.expectMessage("codePeriodVersion" + | |||
" length (101) is longer than the maximum authorized (100). " + | |||
"'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided."); | |||
snapshotDto.setCodePeriodVersion(repeat("a", 101)); |
@@ -52,6 +52,7 @@ import static org.sonar.db.event.EventValidator.MAX_NAME_LENGTH; | |||
import static org.sonar.server.projectanalysis.ws.EventCategory.OTHER; | |||
import static org.sonar.server.projectanalysis.ws.EventCategory.VERSION; | |||
import static org.sonar.server.projectanalysis.ws.EventCategory.fromLabel; | |||
import static org.sonar.server.projectanalysis.ws.EventValidator.checkVersionName; | |||
import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_ANALYSIS; | |||
import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_CATEGORY; | |||
import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_NAME; | |||
@@ -150,6 +151,7 @@ public class CreateEventAction implements ProjectAnalysesWsAction { | |||
private void checkRequest(CreateEventRequest request, ComponentDto component) { | |||
userSession.checkComponentPermission(UserRole.ADMIN, component); | |||
checkArgument(EventCategory.VERSION != request.getCategory() || Qualifiers.PROJECT.equals(component.qualifier()), "A version event must be created on a project"); | |||
checkVersionName(request.getCategory(), request.getName()); | |||
} | |||
private static CreateEventRequest toAddEventRequest(Request request) { |
@@ -23,6 +23,8 @@ import com.google.common.base.Joiner; | |||
import com.google.common.collect.ImmutableSet; | |||
import java.util.Set; | |||
import java.util.function.Consumer; | |||
import javax.annotation.Nullable; | |||
import org.sonar.db.component.SnapshotDto; | |||
import org.sonar.db.event.EventDto; | |||
import static com.google.common.base.Preconditions.checkArgument; | |||
@@ -43,4 +45,16 @@ class EventValidator { | |||
"Event of category '%s' cannot be modified. Authorized categories: %s", | |||
EventCategory.fromLabel(event.getCategory()), AUTHORIZED_CATEGORIES_INLINED); | |||
} | |||
static void checkVersionName(EventCategory category, @Nullable String name) { | |||
checkVersionName(category.getLabel(), name); | |||
} | |||
static void checkVersionName(@Nullable String category, @Nullable String name) { | |||
if (VERSION.getLabel().equals(category) && name != null) { | |||
// check against max version length defined on SnapshotDto to enforce consistency between version events and snapshot versions | |||
checkArgument(name.length() <= SnapshotDto.MAX_VERSION_LENGTH, | |||
"Event name length (%s) is longer than the maximum authorized (%s). '%s' was provided.", name.length(), SnapshotDto.MAX_VERSION_LENGTH, name); | |||
} | |||
} | |||
} |
@@ -46,6 +46,7 @@ import static java.util.Objects.requireNonNull; | |||
import static java.util.Optional.ofNullable; | |||
import static org.apache.commons.lang.StringUtils.isNotBlank; | |||
import static org.sonar.server.projectanalysis.ws.EventValidator.checkModifiable; | |||
import static org.sonar.server.projectanalysis.ws.EventValidator.checkVersionName; | |||
import static org.sonar.server.ws.WsUtils.writeProtobuf; | |||
import static org.sonar.server.projectanalysis.ws.EventCategory.VERSION; | |||
import static org.sonar.server.projectanalysis.ws.EventCategory.fromLabel; | |||
@@ -53,7 +54,6 @@ import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PA | |||
import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_NAME; | |||
public class UpdateEventAction implements ProjectAnalysesWsAction { | |||
private static final int MAX_NAME_LENGTH = 100; | |||
private final DbClient dbClient; | |||
private final UserSession userSession; | |||
@@ -152,13 +152,7 @@ public class UpdateEventAction implements ProjectAnalysesWsAction { | |||
} | |||
private static Consumer<EventDto> checkVersionNameLength(UpdateEventRequest request) { | |||
String name = request.getName(); | |||
return candidateEvent -> { | |||
if (name != null && VERSION.getLabel().equals(candidateEvent.getCategory())) { | |||
checkArgument(name.length() <= MAX_NAME_LENGTH, | |||
"Version length (%s) is longer than the maximum authorized (%s). '%s' was provided.", name.length(), MAX_NAME_LENGTH, name); | |||
} | |||
}; | |||
return candidateEvent -> checkVersionName(candidateEvent.getCategory(), request.getName()); | |||
} | |||
private SnapshotDto getAnalysis(DbSession dbSession, EventDto event) { |
@@ -236,7 +236,7 @@ public class UpdateEventActionTest { | |||
call("E2", repeat("a", 100)); | |||
expectedException.expect(IllegalArgumentException.class); | |||
expectedException.expectMessage("Version length (101) is longer than the maximum authorized (100). 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided"); | |||
expectedException.expectMessage("Event name length (101) is longer than the maximum authorized (100). 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided"); | |||
call("E2", repeat("a", 101)); | |||
} |