Skip to content

Commit fac861d

Browse files
authored
Merge pull request #65 from mcanoy/commits
add commits to engagement and add separate call
2 parents c32af08 + 12393f2 commit fac861d

File tree

11 files changed

+177
-7
lines changed

11 files changed

+177
-7
lines changed

src/main/java/com/redhat/labs/omp/models/Engagement.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.List;
44

5+
import com.redhat.labs.omp.models.gitlab.Commit;
6+
57
import lombok.AllArgsConstructor;
68
import lombok.Builder;
79
import lombok.Data;
@@ -37,5 +39,6 @@ public class Engagement {
3739
private List<EngagementUser> engagementUsers;
3840

3941
private Status status;
42+
private List<Commit> commits;
4043

4144
}

src/main/java/com/redhat/labs/omp/models/gitlab/Commit.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,36 @@
22

33
import javax.json.bind.annotation.JsonbProperty;
44

5-
public class Commit extends File {
65

7-
@JsonbProperty("action")
8-
public FileAction action;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Builder;
8+
import lombok.Data;
9+
import lombok.NoArgsConstructor;
10+
11+
@Data
12+
@Builder
13+
@NoArgsConstructor
14+
@AllArgsConstructor
15+
public class Commit {
16+
17+
private String id;
18+
@JsonbProperty("short_id")
19+
private String shortId;
20+
private String title;
21+
@JsonbProperty("author_name")
22+
private String authorName;
23+
@JsonbProperty("author_email")
24+
private String authorEmail;
25+
@JsonbProperty("committer_name")
26+
private String commiterName;
27+
@JsonbProperty("committer_email")
28+
private String commiterEmail;
29+
@JsonbProperty("authored_date")
30+
private String authoredDate;
31+
@JsonbProperty("committed_date")
32+
private String commitDate;
33+
private String message;
34+
@JsonbProperty("web_url")
35+
private String url;
936

1037
}

src/main/java/com/redhat/labs/omp/resource/EngagementResource.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import com.redhat.labs.omp.models.Engagement;
2727
import com.redhat.labs.omp.models.Status;
28+
import com.redhat.labs.omp.models.gitlab.Commit;
2829
import com.redhat.labs.omp.models.gitlab.Hook;
2930
import com.redhat.labs.omp.models.gitlab.Project;
3031
import com.redhat.labs.omp.service.EngagementService;
@@ -82,6 +83,16 @@ public Response createProjectHook(Hook hook, @PathParam("customer") String custo
8283
return response;
8384
}
8485

86+
@GET
87+
@Path("/customer/{customer}/{engagement}/commits")
88+
@Counted(name = "get-engagement-commits", description = "Count of get engagement commits")
89+
@Timed(name = "performedEngagementCommitsGet", description = "Time to get engagement commits", unit = MetricUnits.MILLISECONDS)
90+
public Response getEngagementCommits(@PathParam("customer") String customer, @PathParam("engagement") String engagement) {
91+
92+
List<Commit> commitList = engagementService.getCommitLog(customer, engagement);
93+
return Response.ok().entity(commitList).build();
94+
}
95+
8596
@GET
8697
@Path("customer/{customer}/{engagement}/hooks")
8798
@Counted(name = "get-hook", description = "Count of get-hook requests")

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ File updateFile(@PathParam("id") @Encoded Integer projectId, @PathParam("file_pa
175175
@Path("/projects/{id}/repository/commits")
176176
@Produces("application/json")
177177
Response commitMultipleFiles(@PathParam("id") @Encoded Integer projectId, CommitMultiple commit);
178+
179+
@GET
180+
@Logged
181+
@Path("/projects/{id}/repository/commits")
182+
@Produces("application/json")
183+
Response getCommitLog(@PathParam("id") @Encoded String projectId, @QueryParam("per_page") int perPage, @QueryParam("page") int page);
178184

179185
// Deploy Keys
180186

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.redhat.labs.omp.models.Engagement;
2020
import com.redhat.labs.omp.models.Status;
2121
import com.redhat.labs.omp.models.gitlab.Action;
22+
import com.redhat.labs.omp.models.gitlab.Commit;
2223
import com.redhat.labs.omp.models.gitlab.CommitMultiple;
2324
import com.redhat.labs.omp.models.gitlab.File;
2425
import com.redhat.labs.omp.models.gitlab.FileAction;
@@ -117,6 +118,11 @@ public Project createEngagement(Engagement engagement, String author, String aut
117118

118119
}
119120

121+
public List<Commit> getCommitLog(String customerName, String engagementName) {
122+
String projectPath = getPath(customerName, engagementName);
123+
return projectService.getCommitLog(projectPath);
124+
}
125+
120126
public List<Hook> getHooks(String customer, String engagment) {
121127
Optional<Project> project = getProject(customer, engagment);
122128

@@ -203,6 +209,9 @@ private Optional<Engagement> getEngagement(Project project, boolean includeStatu
203209
Optional<File> engagementFile = fileService.getFileAllow404(project.getId(), ENGAGEMENT_FILE);
204210
if (engagementFile.isPresent()) {
205211
engagement = json.fromJson(engagementFile.get().getContent(), Engagement.class);
212+
213+
List<Commit> commits = projectService.getCommitLog(String.valueOf(engagement.getProjectId()));
214+
engagement.setCommits(commits);
206215
}
207216

208217
if(includeStatus && engagement != null) {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
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;
1114
import org.slf4j.Logger;
1215
import org.slf4j.LoggerFactory;
1316

17+
import com.redhat.labs.omp.models.gitlab.Commit;
1418
import com.redhat.labs.omp.models.gitlab.DeployKey;
1519
import com.redhat.labs.omp.models.gitlab.Project;
1620
import com.redhat.labs.omp.models.gitlab.ProjectSearchResults;
@@ -26,6 +30,9 @@ public class ProjectService {
2630

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

3037
// get a project
3138
public Optional<Project> getProjectByName(Integer namespaceId, String name) {
@@ -121,5 +128,29 @@ public void enableDeploymentKeyOnProject(Integer projectId, Integer deployKey) {
121128
gitLabService.enableDeployKey(projectId, deployKey);
122129
gitLabService.updateDeployKey(projectId, deployKey, DeployKey.builder().title("LodeStar DK").canPush(true).build());
123130
}
131+
132+
public List<Commit> getCommitLog(String 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;
154+
}
124155

125156
}

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: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import org.eclipse.microprofile.rest.client.inject.RestClient;
1313

14+
import com.redhat.labs.omp.config.JsonMarshaller;
15+
import com.redhat.labs.omp.models.gitlab.Commit;
1416
import com.redhat.labs.omp.models.gitlab.CommitMultiple;
1517
import com.redhat.labs.omp.models.gitlab.DeployKey;
1618
import com.redhat.labs.omp.models.gitlab.File;
@@ -32,7 +34,6 @@ public class MockGitLabService implements GitLabService {
3234

3335
@Override
3436
public Response getProjects() {
35-
// TODO Auto-generated method stub
3637
return null;
3738
}
3839

@@ -95,7 +96,7 @@ public List<ProjectSearchResults> getProjectByName(String name) {
9596

9697
@Override
9798
public Project getProjectById(String projectId) {
98-
System.out.println("p "+ projectId);
99+
99100
if(projectId == "66") {
100101
return Project.builder().id(66).build();
101102
}
@@ -296,4 +297,20 @@ public Response updateDeployKey(Integer projectId, Integer deployKeyId, DeployKe
296297
return null;
297298
}
298299

300+
@Override
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+
305+
if("top/dog/jello/lemon/iac".equals(projectId)) {
306+
return Response.ok(commitList).header("X-Total-Pages", 1).build();
307+
}
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();
314+
}
315+
299316
}

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

Lines changed: 21 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,12 +124,31 @@ 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/\","
131131
+ "\"overall_status\":\"green\"},\"technical_lead_email\":\"[email protected]\",\"technical_lead_name\":\"Wendel Clark\"}"));
132132
}
133+
134+
@Test
135+
void testGetCommitsSuccess() {
136+
given()
137+
.when()
138+
.contentType(ContentType.JSON)
139+
.get("/api/v1/engagements/customer/jello/lemon/commits")
140+
.then()
141+
.statusCode(200)
142+
.body(is("[{\"author_email\":\"[email protected]\",\"author_name\":\"bot\",\"authored_date\":\"2020-06-16T00:12:27.000+00:00\",\"committed_date\":\"2020-06-16T00:12:27.000+00:00\",\"id\":\"551eefc6e367aa2ad3c56bdd7229c7bc525d4f0c\","
143+
+ "\"message\":\"Auto-update generated files\",\"short_id\":\"551eefc6\",\"title\":\"Auto-update generated files\",\"web_url\":\"https://gitlab.example.com/store/jello/lemon/iac/-/commit/551eefc6e367aa2ad3c56bdd7229c7bc525d4f0c\"},"
144+
+ "{\"author_email\":\"[email protected]\",\"author_name\":\"Mitch Marner\",\"authored_date\":\"2020-06-16T00:12:18.000+00:00\",\"committed_date\":\"2020-06-16T00:12:18.000+00:00\","
145+
+ "\"id\":\"5178ffab3566ac591af95c3383d1c5916de4a3a9\",\"message\":\"Update engagement.json\",\"short_id\":\"5178ffab\",\"title\":\"Update engagement.json\","
146+
+ "\"web_url\":\"https://gitlab.example.com/store/jello/lemon/iac/-/commit/5178ffab3566ac591af95c3383d1c5916de4a3a9\"},{\"author_email\":\"[email protected]\",\"author_name\":\"John Tavares\","
147+
+ "\"authored_date\":\"2020-06-11T16:46:19.000+00:00\",\"committed_date\":\"2020-06-11T16:46:19.000+00:00\",\"id\":\"7865570dc63b1463d9fb4d02bd09ff46d244e694\",\"message\":\"Update status.json\","
148+
+ "\"short_id\":\"7865570d\",\"title\":\"Update status.json\",\"web_url\":\"https://gitlab.example.com/store/jello/lemon/iac/-/commit/7865570dc63b1463d9fb4d02bd09ff46d244e694\"},{\"author_email\":\"[email protected]\","
149+
+ "\"author_name\":\"Mitch Marner\",\"authored_date\":\"2020-06-04T22:34:10.000+00:00\",\"committed_date\":\"2020-06-04T22:34:10.000+00:00\",\"id\":\"dd0cc0fa7868210e2eb5a030f07cc0221dd6bc9f\","
150+
+ "\"message\":\"Bump OCP version (jacob test)\",\"short_id\":\"dd0cc0fa\",\"title\":\"Bump OCP version (test)\",\"web_url\":\"https://gitlab.example.com/store/jello/lemon/iac/-/commit/dd0cc0fa7868210e2eb5a030f07cc0221dd6bc9f\"}]"));
151+
}
133152

134153

135154
}

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)