From 01a7e1a17fee12093288f44fd797058246a4f60b Mon Sep 17 00:00:00 2001 From: Roman Pedchenko Date: Wed, 3 Apr 2019 23:07:41 +0300 Subject: [PATCH] #9: - added offset/limit processing --- .../idea/issues/gitea/GiteaRepository.java | 44 +++++++++++++++---- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/biz/elfuego/idea/issues/gitea/GiteaRepository.java b/src/biz/elfuego/idea/issues/gitea/GiteaRepository.java index dedbbdb..4771048 100644 --- a/src/biz/elfuego/idea/issues/gitea/GiteaRepository.java +++ b/src/biz/elfuego/idea/issues/gitea/GiteaRepository.java @@ -97,7 +97,7 @@ class GiteaRepository extends BaseRepositoryImpl { @Override public Task[] getIssues(@Nullable String query, int offset, int limit, boolean withClosed, @NotNull ProgressIndicator cancelled) throws Exception { - return getIssues(); + return findIssues(query, offset, limit, withClosed, cancelled); } @NotNull @@ -174,7 +174,7 @@ class GiteaRepository extends BaseRepositoryImpl { @Override protected int getFeatures() { - return NATIVE_SEARCH | STATE_UPDATING; + return STATE_UPDATING; } private void doTest() throws Exception { @@ -245,15 +245,43 @@ class GiteaRepository extends BaseRepositoryImpl { } private Task[] getIssues() throws Exception { + return findIssues(null, -1, -1, false, null); + } + + private Task[] findIssues(String query, int offset, int limit, boolean withClosed, ProgressIndicator cancelled) throws Exception { if (!ensureUserId()) return new Task[]{}; - List result = new ArrayList<>(); - final String url = getApiUrl() + Consts.EndPoint.REPOS + getProject() + Consts.EndPoint.ISSUES; - final JsonElement response = executeMethod(new GetMethod(url)); + StringBuilder qu = new StringBuilder(); + if (query != null) + qu.append("?q=").append(query); + if (withClosed) + qu.append("&state=closed"); + qu.append("&page="); + if (qu.length() > 0) + qu.setCharAt(0, '?'); + List result = new ArrayList<>(); + final String url = getApiUrl() + Consts.EndPoint.REPOS + getProject() + Consts.EndPoint.ISSUES + qu.toString(); + + int firstPage = offset / 10; + int lastPage = (offset + limit) / 10; + + for (int p = firstPage + 1; p <= lastPage; p++) { + if (!loadPage(url, result, p)) + break; + } + + Collections.sort(result); + return result.toArray(new Task[0]); + } + + private boolean loadPage(String url, List result, int page) throws Exception { + final JsonElement response = executeMethod(new GetMethod(url + page)); if (response == null) - return new Task[]{}; + return false; JsonArray tasks = getArray(response); + if (tasks.size() < 1) + return false; for (int i = 0; i < tasks.size(); i++) { JsonObject current = tasks.get(i).getAsJsonObject(); GiteaTask raw = new GiteaTask(getProject(), current); @@ -263,9 +291,7 @@ class GiteaRepository extends BaseRepositoryImpl { GiteaTaskImpl mapped = new GiteaTaskImpl(this, raw); result.add(mapped); } - Collections.sort(result); - Task[] primArray = new Task[result.size()]; - return result.toArray(primArray); + return true; } private String getProject() {