Skip to content

Commit

Permalink
Fix rebase-conflicts regarding ChangesetApi updates
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Rögner <benjamin.roegner@here.com>
  • Loading branch information
roegi committed Oct 18, 2024
1 parent 9d32c2a commit 7d76c08
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,64 +49,53 @@
public class ChangesetApi extends SpaceBasedApi {

public ChangesetApi(RouterBuilder rb) {
rb.getRoute("getChangesets").setDoValidation(false).addHandler(this::getChangesets);
rb.getRoute("getChangeset").setDoValidation(false).addHandler(this::getChangeset);
rb.getRoute("deleteChangesets").setDoValidation(false).addHandler(this::deleteChangesets);
rb.getRoute("getChangesetStatistics").setDoValidation(false).addHandler(this::getChangesetStatistics);
rb.getRoute("getChangesets").setDoValidation(false).addHandler(handleErrors(this::getChangesets));
rb.getRoute("getChangeset").setDoValidation(false).addHandler(handleErrors(this::getChangeset));
rb.getRoute("deleteChangesets").setDoValidation(false).addHandler(handleErrors(this::deleteChangesets));
rb.getRoute("getChangesetStatistics").setDoValidation(false).addHandler(handleErrors(this::getChangesetStatistics));
}

/**
* Get changesets by version
*/
private void getChangesets(final RoutingContext context) {
try {
long startVersion = getLongQueryParam(context, START_VERSION);
long endVersion = getLongQueryParam(context, END_VERSION);
long startVersion = getLongQueryParam(context, START_VERSION, 0);
long endVersion = getLongQueryParam(context, END_VERSION, -1);

if (startVersion > endVersion)
throw new IllegalArgumentException("The parameter \"" + START_VERSION + "\" needs to be smaller than \"" + END_VERSION + "\".");
if (endVersion != -1 && startVersion > endVersion)
throw new IllegalArgumentException("The parameter \"" + START_VERSION + "\" needs to be smaller than or equal to \"" + END_VERSION + "\".");

IterateChangesetsEvent event = buildIterateChangesetsEvent(context, startVersion, endVersion);
//TODO: Add static caching to this endpoint, once the execution pipelines have been refactored.
SpaceConnectorBasedHandler.execute(getMarker(context),
space -> Authorization.authorizeManageSpacesRights(context, space.getId(), space.getOwner()).map(space), event)
.onSuccess(result -> sendResponse(context, result))
.onFailure(t -> sendErrorResponse(context, t));
}
catch (Exception e) {
sendErrorResponse(context, e);
}
IterateChangesetsEvent event = buildIterateChangesetsEvent(context, startVersion, endVersion);
//TODO: Add static caching to this endpoint, once the execution pipelines have been refactored.
SpaceConnectorBasedHandler.execute(getMarker(context),
space -> Authorization.authorizeManageSpacesRights(context, space.getId(), space.getOwner()).map(space), event)
.onSuccess(result -> sendResponse(context, result))
.onFailure(t -> sendErrorResponse(context, t));
}

/**
* Get changesets by version
*/
private void getChangeset(RoutingContext context) {
try {
long version = getVersionFromPathParam(context);
IterateChangesetsEvent event = buildIterateChangesetsEvent(context, version, version);
//TODO: Add static caching to this endpoint, once the execution pipelines have been refactored.
SpaceConnectorBasedHandler.execute(getMarker(context),
space -> Authorization.authorizeManageSpacesRights(context, space.getId(), space.getOwner()).map(space), event)
.onSuccess(result -> {
ChangesetCollection changesets = (ChangesetCollection) result;
if (changesets.getVersions().isEmpty())
sendErrorResponse(context, new HttpException(NOT_FOUND, "No changeset was found for version " + version));
else
sendResponse(context, changesets.getVersions().get(version).withNextPageToken(changesets.getNextPageToken()));
})
.onFailure(t -> sendErrorResponse(context, t));

}
catch (Exception e) {
sendErrorResponse(context, e);
}
long version = getVersionFromPathParam(context);
IterateChangesetsEvent event = buildIterateChangesetsEvent(context, version, version);
//TODO: Add static caching to this endpoint, once the execution pipelines have been refactored.
SpaceConnectorBasedHandler.execute(getMarker(context),
space -> Authorization.authorizeManageSpacesRights(context, space.getId(), space.getOwner()).map(space), event)
.onSuccess(result -> {
ChangesetCollection changesets = (ChangesetCollection) result;
if (changesets.getVersions().isEmpty())
sendErrorResponse(context, new HttpException(NOT_FOUND, "No changeset was found for version " + version));
else
sendResponse(context, changesets.getVersions().get(version).withNextPageToken(changesets.getNextPageToken()));
})
.onFailure(t -> sendErrorResponse(context, t));
}

/**
* Delete changesets by version number
*/
private void deleteChangesets(final RoutingContext context) {
private void deleteChangesets(final RoutingContext context) throws HttpException {
final String spaceId = getSpaceId(context);
final PropertyQuery version = Query.getPropertyQuery(context.request().query(), "version", false);

Expand Down Expand Up @@ -141,8 +130,7 @@ else if (version.getOperation() != LESS_THAN) {
.onFailure(t -> sendErrorResponse(context, t));
}
catch (NumberFormatException e) {
sendErrorResponse(context,
new HttpException(HttpResponseStatus.BAD_REQUEST, "Query parameter version must be a valid number larger than 0"));
throw new HttpException(HttpResponseStatus.BAD_REQUEST, "Query parameter version must be a valid number larger than 0");
}
}

Expand All @@ -155,8 +143,7 @@ private void getChangesetStatistics(final RoutingContext context) {
.onFailure(t -> sendErrorResponse(context, t));
}

private IterateChangesetsEvent buildIterateChangesetsEvent(final RoutingContext context, long startVersion, long endVersion)
throws HttpException {
private IterateChangesetsEvent buildIterateChangesetsEvent(final RoutingContext context, long startVersion, long endVersion) {
String pageToken = Query.getString(context, Query.PAGE_TOKEN, null);
long limit = Query.getLong(context, Query.LIMIT, IterateChangesets.DEFAULT_LIMIT);

Expand All @@ -168,15 +155,15 @@ private IterateChangesetsEvent buildIterateChangesetsEvent(final RoutingContext
.withLimit(limit);
}

private long getLongQueryParam(RoutingContext context, String paramName) {
private long getLongQueryParam(RoutingContext context, String paramName, long defaultValue) {
try {
long paramValue = Query.getLong(context, paramName);
if (paramValue < 0)
throw new IllegalArgumentException("The parameter \"" + paramName + "\" must be >= 0.");
return paramValue;
}
catch (NullPointerException e) {
throw new IllegalArgumentException("The parameter \"" + paramName + "\" is required.", e);
return defaultValue;
}
catch (NumberFormatException e) {
throw new IllegalArgumentException("The parameter \"" + paramName + "\" is not a number.", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class IterateChangesetsEvent extends SearchForFeaturesEvent<Iterate
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private long startVersion;
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private long endVersion;
private long endVersion = -1;

private int versionsToKeep;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ public SQLQuery buildIterateChangesets(IterateChangesetsEvent event){
.withNamedParameter("versionsToKeep", event.getVersionsToKeep())
.withNamedParameter("start", startVersion));

query.setQueryFragment("end_version", new SQLQuery("AND version <= #{end}")
.withNamedParameter("end", event.getEndVersion()));
query.setQueryFragment("end_version", event.getEndVersion() != -1 ? new SQLQuery("AND version <= #{end}")
.withNamedParameter("end", event.getEndVersion()) : new SQLQuery(""));

//Query one more feature as requested, to be able to determine if we need to include a nextPageToken
query.setQueryFragment("limit", new SQLQuery("LIMIT #{limit}").withNamedParameter("limit", limit + 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ protected Handler<RoutingContext> handleErrors(ThrowingHandler<RoutingContext> h
return context -> {
try {
handler.handle(context);
} catch (HttpException e) {
}
catch (HttpException | IllegalArgumentException | ValidationException | AccessDeniedException | RequestCancelledException e) {
sendErrorResponse(context, e);
} catch (Exception e) {
}
catch (Exception e) {
sendInternalServerError(context, e);
}
};
Expand Down

0 comments on commit 7d76c08

Please sign in to comment.