Skip to content

Commit 12393f2

Browse files
committed
paginate commits
1 parent 164b6a8 commit 12393f2

File tree

6 files changed

+52
-11
lines changed

6 files changed

+52
-11
lines changed

src/main/java/com/redhat/labs/omp/rest/client/GitLabService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
1818
import org.jboss.resteasy.annotations.jaxrs.PathParam;
1919

20-
import com.redhat.labs.omp.models.gitlab.Commit;
2120
import com.redhat.labs.omp.models.gitlab.CommitMultiple;
2221
import com.redhat.labs.omp.models.gitlab.DeployKey;
2322
import com.redhat.labs.omp.models.gitlab.File;
@@ -181,7 +180,7 @@ File updateFile(@PathParam("id") @Encoded Integer projectId, @PathParam("file_pa
181180
@Logged
182181
@Path("/projects/{id}/repository/commits")
183182
@Produces("application/json")
184-
List<Commit> getCommitLog(@PathParam("id") @Encoded String projectId);
183+
Response getCommitLog(@PathParam("id") @Encoded String projectId, @QueryParam("per_page") int perPage, @QueryParam("page") int page);
185184

186185
// Deploy Keys
187186

src/main/java/com/redhat/labs/omp/service/ProjectService.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.redhat.labs.omp.service;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45
import java.util.Optional;
56

67
import javax.enterprise.context.ApplicationScoped;
78
import javax.inject.Inject;
9+
import javax.ws.rs.core.GenericType;
10+
import javax.ws.rs.core.Response;
811

912
import org.eclipse.microprofile.config.inject.ConfigProperty;
1013
import org.eclipse.microprofile.rest.client.inject.RestClient;
@@ -27,6 +30,9 @@ public class ProjectService {
2730

2831
@ConfigProperty(name = "engagements.do.not.delete")
2932
boolean doNotDelete;
33+
34+
@ConfigProperty(name = "commit.page.size")
35+
int commitPageSize;
3036

3137
// get a project
3238
public Optional<Project> getProjectByName(Integer namespaceId, String name) {
@@ -124,7 +130,27 @@ public void enableDeploymentKeyOnProject(Integer projectId, Integer deployKey) {
124130
}
125131

126132
public List<Commit> getCommitLog(String projectId) {
127-
return gitLabService.getCommitLog(projectId);
133+
List<Commit> totalCommits = new ArrayList<>();
134+
int totalPages = 1;
135+
int page = 0;
136+
137+
while(totalPages > page++) {
138+
LOGGER.trace("page {}", page);
139+
Response response = gitLabService.getCommitLog(projectId, commitPageSize, page);
140+
141+
totalCommits.addAll(response.readEntity(new GenericType<List<Commit>>() {}));
142+
143+
if(page == 1) {
144+
String totalPageString = response.getHeaderString("X-Total-Pages");
145+
totalPages = Integer.valueOf(totalPageString);
146+
LOGGER.trace("TOTAL PAGES {}", totalPages);
147+
}
148+
149+
}
150+
151+
LOGGER.debug("total commits for project {} {}", projectId, totalCommits.size());
152+
153+
return totalCommits;
128154
}
129155

130156
}

src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ config.file=${CONFIG_FILE:schema/config.yml}
3030
webhook.file=${WEBHOOK_FILE:/schema/webhooks.yaml}
3131
webhook.default.token=${WEBHOOK_DEFAULT_TOKEN:tolkien}
3232
config.gitlab.ref=${CONFIG_GITLAB_REF:master}
33+
commit.page.size=100
3334

3435
# engagements
3536
engagements.repository.id=${ENGAGEMENTS_REPOSITORY_ID:2}

src/test/java/com/redhat/labs/omp/mocks/MockGitLabService.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public List<ProjectSearchResults> getProjectByName(String name) {
9696

9797
@Override
9898
public Project getProjectById(String projectId) {
99-
System.out.println("p "+ projectId);
99+
100100
if(projectId == "66") {
101101
return Project.builder().id(66).build();
102102
}
@@ -298,13 +298,19 @@ public Response updateDeployKey(Integer projectId, Integer deployKeyId, DeployKe
298298
}
299299

300300
@Override
301-
public List<Commit> getCommitLog(String projectId) {
301+
public Response getCommitLog(String projectId, int perPage, int pageNumber) {
302+
String content = ResourceLoader.load("commits.yaml");
303+
List<Commit> commitList = new JsonMarshaller().fromYaml(content, Commit.class);
304+
302305
if("top/dog/jello/lemon/iac".equals(projectId)) {
303-
String content = ResourceLoader.load("commits.yaml");
304-
List<Commit> commitList = new JsonMarshaller().fromYaml(content, Commit.class);
305-
return commitList;
306+
return Response.ok(commitList).header("X-Total-Pages", 1).build();
306307
}
307-
return null;
308+
309+
if("multi/page/iac".equals(projectId)) {
310+
return Response.ok(commitList).header("X-Total-Pages", 2).build();
311+
}
312+
313+
return Response.ok(new ArrayList<Commit>()).header("X-Total-Pages", 0).build();
308314
}
309315

310316
}

src/test/java/com/redhat/labs/omp/resource/EngagementResourceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void testGetAllEngagementsSuccess() {
2323
.get("/api/v1/engagements")
2424
.then()
2525
.statusCode(200)
26-
.body(is("[{\"archive_date\":\"20210125\",\"customer_contact_email\":\"[email protected]\",\"customer_contact_name\":\"Reg Dunlop\","
26+
.body(is("[{\"archive_date\":\"20210125\",\"commits\":[],\"customer_contact_email\":\"[email protected]\",\"customer_contact_name\":\"Reg Dunlop\","
2727
+ "\"customer_name\":\"customer1\",\"description\":\"Charleston\",\"end_date\":\"20201225\",\"engagement_lead_email\":\"[email protected]\","
2828
+ "\"engagement_lead_name\":\"Doug Gilmour\",\"location\":\"Raleigh, NC\",\"ocp_cloud_provider_name\":\"GCP\",\"ocp_cloud_provider_region\":\"West\","
2929
+ "\"ocp_cluster_size\":\"medium\",\"ocp_persistent_storage_size\":\"50GB\",\"ocp_sub_domain\":\"jello\",\"ocp_version\":\"v4.2\",\"project_id\":0,\"project_name\":\"project1\","
@@ -124,7 +124,7 @@ void testGetProjectSuccess() {
124124
.get("/api/v1/engagements/customer/jello/lemon")
125125
.then()
126126
.statusCode(200)
127-
.body(is("{\"archive_date\":\"20210125\",\"customer_contact_email\":\"[email protected]\",\"customer_contact_name\":\"Reg Dunlop\",\"customer_name\":\"customer1\",\"description\":\"Charleston\",\"end_date\":\"20201225\","
127+
.body(is("{\"archive_date\":\"20210125\",\"commits\":[],\"customer_contact_email\":\"[email protected]\",\"customer_contact_name\":\"Reg Dunlop\",\"customer_name\":\"customer1\",\"description\":\"Charleston\",\"end_date\":\"20201225\","
128128
+ "\"engagement_lead_email\":\"[email protected]\",\"engagement_lead_name\":\"Doug Gilmour\",\"location\":\"Raleigh, NC\",\"ocp_cloud_provider_name\":\"GCP\",\"ocp_cloud_provider_region\":\"West\",\"ocp_cluster_size\":\"medium\","
129129
+ "\"ocp_persistent_storage_size\":\"50GB\",\"ocp_sub_domain\":\"jello\",\"ocp_version\":\"v4.2\",\"project_id\":0,\"project_name\":\"project1\",\"start_date\":\"20200202\","
130130
+ "\"status\":{\"messages\":[\"This is message 1\",\"This is message 2\",\"This is message 3\"],\"openshift_api\":\"https://console.s11.core.rht-labs.com/\",\"openshift_web_console\":\"https://console.s11.core.rht-labs.com/\","

src/test/java/com/redhat/labs/omp/service/ProjectServiceTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.junit.jupiter.api.Test;
1313

1414
import com.redhat.labs.omp.mocks.MockGitLabService;
15+
import com.redhat.labs.omp.models.gitlab.Commit;
1516
import com.redhat.labs.omp.models.gitlab.Project;
1617
import com.redhat.labs.omp.models.gitlab.ProjectSearchResults;
1718

@@ -101,5 +102,13 @@ class ProjectServiceTest {
101102

102103
@Test void deleteProjectWorks() {
103104
projectService.deleteProject(45);
105+
assertNotNull(projectService);
106+
}
107+
108+
@Test void getCommitsMultiPage() {
109+
List<Commit> commits = projectService.getCommitLog("multi/page/iac");
110+
assertNotNull(commits);
111+
assertEquals(8, commits.size());
112+
104113
}
105114
}

0 commit comments

Comments
 (0)