Skip to content

Commit

Permalink
fix session timeout issue uwolfer#178
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Stafichuk committed Mar 11, 2018
1 parent 6ecb76f commit 7465d80
Showing 1 changed file with 53 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public class GerritUtil {
@Inject
private NotificationService notificationService;
@Inject
private GerritApiProvider gerritApiProvider;
@Inject
private GerritRestApi gerritClient;
@Inject
private GerritRestApiFactory gerritRestApiFactory;
Expand Down Expand Up @@ -316,42 +318,61 @@ public void getChanges(final Changes.QueryRequest queryRequest, final Project pr
Supplier<List<ChangeInfo>> supplier = new Supplier<List<ChangeInfo>>() {
@Override
public List<ChangeInfo> get() {
try {
return queryRequest.get();
} catch (RestApiException e) {
// remove special handling (-> just notify error) once we drop Gerrit < 2.9 support
if (e instanceof HttpStatusException) {
HttpStatusException httpStatusException = (HttpStatusException) e;
if (httpStatusException.getStatusCode() == 400) {
boolean tryFallback = false;
String message = httpStatusException.getMessage();
if (message.matches(".*Content:.*\"-S\".*")) {
tryFallback = true;
queryRequest.withStart(0); // remove start, trust that sortkey is set
}
if (message.matches(".*Content:.*\"(CHANGE_ACTIONS|CURRENT_ACTIONS)\".*\"-o\".*")) {
tryFallback = true;
EnumSet<ListChangesOption> options = queryRequest.getOptions();
options.remove(ListChangesOption.CHANGE_ACTIONS);
options.remove(ListChangesOption.CURRENT_ACTIONS);
queryRequest.withOptions(options);
}
if (tryFallback) {
try {
return queryRequest.get();
} catch (RestApiException ex) {
notifyError(ex, "Failed to get Gerrit changes.", project);
return Collections.emptyList();
}
}
return GerritUtil.this.getChanges(queryRequest, project, true);
}
};
accessGerrit(supplier, consumer, project);
}

private List<ChangeInfo> getChanges(Changes.QueryRequest queryRequest, Project project, boolean retry) {
try {
return queryRequest.get();
} catch (RestApiException e) {
// remove special handling (-> just notify error) once we drop Gerrit < 2.9 support
if (e instanceof HttpStatusException) {
HttpStatusException httpStatusException = (HttpStatusException) e;
if (httpStatusException.getStatusCode() == 400) {
boolean tryFallback = false;
String message = httpStatusException.getMessage();
if (message.matches(".*Content:.*\"-S\".*")) {
tryFallback = true;
queryRequest.withStart(0); // remove start, trust that sortkey is set
}
if (message.matches(".*Content:.*\"(CHANGE_ACTIONS|CURRENT_ACTIONS)\".*\"-o\".*")) {
tryFallback = true;
EnumSet<ListChangesOption> options = queryRequest.getOptions();
options.remove(ListChangesOption.CHANGE_ACTIONS);
options.remove(ListChangesOption.CURRENT_ACTIONS);
queryRequest.withOptions(options);
}
if (tryFallback) {
try {
return queryRequest.get();
} catch (RestApiException ex) {
return handleException(ex, project, queryRequest, retry);
}
}
notifyError(e, "Failed to get Gerrit changes.", project);
return Collections.emptyList();
}
}
};
accessGerrit(supplier, consumer, project);
return handleException(e, project, queryRequest, retry);
}
}

private List<ChangeInfo> handleException(RestApiException ex, Project project, Changes.QueryRequest queryRequest,
boolean retry) {
if (retry) {
gerritClient = gerritApiProvider.get();
Changes.QueryRequest newQueryRequest = gerritClient.changes()
.query(queryRequest.getQuery())
.withLimit(queryRequest.getLimit())
.withStart(queryRequest.getStart())
.withSortkey(queryRequest.getSortkey())
.withOptions(queryRequest.getOptions());
return getChanges(newQueryRequest, project, false);
} else {
notifyError(ex, "Failed to get Gerrit changes.", project);
return Collections.emptyList();
}
}

private String appendQueryStringForProject(Project project, String query) {
Expand Down

0 comments on commit 7465d80

Please sign in to comment.