Skip to content

Commit c1e4d3b

Browse files
committed
Add ability to retrieve status
1 parent aaf613d commit c1e4d3b

File tree

10 files changed

+143
-39
lines changed

10 files changed

+143
-39
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ public class Engagement {
3535
private String ocpClusterSize;
3636
private Launch launch;
3737
private List<EngagementUser> engagementUsers;
38+
39+
private Status status;
3840

3941
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.redhat.labs.omp.models;
2+
3+
import java.util.List;
4+
5+
import javax.json.bind.annotation.JsonbProperty;
6+
7+
import lombok.AllArgsConstructor;
8+
import lombok.Builder;
9+
import lombok.Data;
10+
import lombok.NoArgsConstructor;
11+
12+
@Data
13+
@Builder
14+
@NoArgsConstructor
15+
@AllArgsConstructor
16+
public class Status {
17+
18+
@JsonbProperty("overall_status")
19+
private String status;
20+
private List<String> messages;
21+
private String openshiftWebConsole;
22+
private String openshiftApi;
23+
}

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.slf4j.LoggerFactory;
2525

2626
import com.redhat.labs.omp.models.Engagement;
27+
import com.redhat.labs.omp.models.Status;
2728
import com.redhat.labs.omp.models.gitlab.Hook;
2829
import com.redhat.labs.omp.models.gitlab.Project;
2930
import com.redhat.labs.omp.service.EngagementService;
@@ -56,18 +57,18 @@ public Response createEngagement(Engagement engagement, @Context UriInfo uriInfo
5657
@Counted(name = "get-all-engagement", description = "Count of get all engagements")
5758
@Timed(name = "performedEngagementGetAll", description = "Time to get all engagements", unit = MetricUnits.MILLISECONDS)
5859
public Response findAllEngagements() {
60+
5961
List<Engagement> engagements = engagementService.getAllEngagements();
60-
6162
return Response.ok().entity(engagements).build();
6263
}
6364

6465
@GET
6566
@Path("/customer/{customer}/{engagement}")
6667
@Counted(name = "get-engagement", description = "Count of get engagement")
6768
@Timed(name = "performedEngagementGet", description = "Time to get an engagement", unit = MetricUnits.MILLISECONDS)
68-
public Response getEngagment(@PathParam("customer") String customer, @PathParam("engagement") String engagement) {
69-
Engagement response = engagementService.getEngagement(customer, engagement);
70-
69+
public Response getEngagement(@PathParam("customer") String customer, @PathParam("engagement") String engagement, @QueryParam("includeStatus") boolean includeStatus) {
70+
71+
Engagement response = engagementService.getEngagement(customer, engagement, includeStatus);
7172
return Response.ok().entity(response).build();
7273
}
7374

@@ -86,9 +87,19 @@ public Response createProjectHook(Hook hook, @PathParam("customer") String custo
8687
@Counted(name = "get-hook", description = "Count of get-hook requests")
8788
@Timed(name = "performedHookGetAll", description = "Time to get all hooks", unit = MetricUnits.MILLISECONDS)
8889
public Response findAllProjectHooks(@PathParam("customer") String customer, @PathParam("engagement") String engagement) {
90+
8991
List<Hook> engagements = engagementService.getHooks(customer, engagement);
90-
9192
return Response.ok().entity(engagements).build();
9293
}
94+
95+
@GET
96+
@Path("customer/{customer}/{engagement}/status")
97+
@Counted(name = "get-status", description = "Count of get-status requests")
98+
@Timed(name = "performedStatusGet", description = "Time to get status", unit = MetricUnits.MILLISECONDS)
99+
public Response getStatus(@PathParam("customer") String customer, @PathParam("engagement") String engagement) {
100+
101+
Status status = engagementService.getProjectStatus(customer, engagement);
102+
return Response.ok().entity(status).build();
103+
}
93104

94105
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public interface GitLabService {
138138
@Logged
139139
@Path("/projects/{id}/repository/files/{file_path}")
140140
@Produces("application/json")
141-
File getFile(@PathParam("id") @Encoded Integer projectId, @PathParam("file_path") @Encoded String filePath,
141+
File getFile(@PathParam("id") @Encoded String projectId, @PathParam("file_path") @Encoded String filePath,
142142
@QueryParam("ref") @Encoded String ref);
143143

144144
@GET

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void onStart(@Observes StartupEvent event) {
5050

5151
public File getConfigFile() {
5252

53-
Optional<File> optional = fileService.getFile(Integer.valueOf(configRepositoryId), configFile, gitRef);
53+
Optional<File> optional = fileService.getFile(configRepositoryId, configFile, gitRef);
5454

5555
if (!optional.isPresent()) {
5656
throw new FileNotFoundException("the configured file was not found in the gitlab repository.");
@@ -66,7 +66,7 @@ public List<HookConfig> getHookConfig() {
6666
}
6767

6868
String gitLabHookFile = webHooksFile.charAt(0) == '/' ? webHooksFile.substring(1) : webHooksFile;
69-
Optional<File> optional = fileService.getFile(Integer.valueOf(configRepositoryId), gitLabHookFile, gitRef);
69+
Optional<File> optional = fileService.getFile(configRepositoryId, gitLabHookFile, gitRef);
7070

7171
if (!optional.isPresent()) {
7272
LOGGER.error("No webhook file could be found. This is abnormal but not a deal breaker");

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

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import javax.enterprise.context.ApplicationScoped;
1010
import javax.inject.Inject;
1111
import javax.ws.rs.core.Response;
12-
import javax.ws.rs.core.Response.Status;
1312

1413
import org.eclipse.microprofile.config.inject.ConfigProperty;
1514
import org.slf4j.Logger;
@@ -18,6 +17,7 @@
1817
import com.redhat.labs.exception.UnexpectedGitLabResponseException;
1918
import com.redhat.labs.omp.config.JsonMarshaller;
2019
import com.redhat.labs.omp.models.Engagement;
20+
import com.redhat.labs.omp.models.Status;
2121
import com.redhat.labs.omp.models.gitlab.Action;
2222
import com.redhat.labs.omp.models.gitlab.CommitMultiple;
2323
import com.redhat.labs.omp.models.gitlab.File;
@@ -33,7 +33,9 @@ public class EngagementService {
3333
public static Logger LOGGER = LoggerFactory.getLogger(EngagementService.class);
3434

3535
private static final String ENGAGEMENT_PROJECT_NAME = "iac";
36-
private final String DEFAULT_BRANCH = "master";
36+
private static final String DEFAULT_BRANCH = "master";
37+
private static final String ENGAGEMENT_FILE = "engagement.json";
38+
private static final String STATUS_FILE = "status.json";
3739

3840
private String engagementPathPrefix;
3941

@@ -127,7 +129,7 @@ public List<Hook> getHooks(String customer, String engagment) {
127129
}
128130

129131
public Response createHook(String customerName, String engagementName, Hook hook) {
130-
Response created = Response.status(Status.BAD_REQUEST).entity("project doesn't exist").build();
132+
Response created = Response.status(javax.ws.rs.core.Response.Status.BAD_REQUEST).entity("project doesn't exist").build();
131133
Optional<Project> optional = getProject(customerName, engagementName);
132134

133135
if(optional.isPresent()) {
@@ -141,13 +143,18 @@ public Response createHook(String customerName, String engagementName, Hook hook
141143
return created;
142144
}
143145

146+
public Status getProjectStatus(String customerName, String engagementName) {
147+
Status status = null;
148+
Optional<File> file = fileService.getFile(this.getPath(customerName, engagementName), "status.json");
149+
if(file.isPresent()) {
150+
status = json.fromJson(file.get().getContent(), Status.class);
151+
}
152+
153+
return status;
154+
}
155+
144156
public Optional<Project> getProject(String customerName, String engagementName) {
145-
String fullPath = new StringBuilder(engagementPathPrefix)
146-
.append("/")
147-
.append(customerName)
148-
.append("/")
149-
.append(engagementName)
150-
.append("/iac").toString();
157+
String fullPath = this.getPath(customerName, engagementName);
151158

152159
LOGGER.debug("Full path {}", fullPath.toString());
153160
return projectService.getProjectByIdOrPath(fullPath);
@@ -170,7 +177,7 @@ public List<Engagement> getAllEngagements() {
170177

171178
for (Project project : projects) {
172179
LOGGER.debug("project id {}", project.getId());
173-
Optional<Engagement> engagement = getEngagement(project);
180+
Optional<Engagement> engagement = getEngagement(project, true);
174181
if(engagement.isPresent() ) {
175182
engagementList.add(engagement.get());
176183
}
@@ -179,33 +186,40 @@ public List<Engagement> getAllEngagements() {
179186
return engagementList;
180187
}
181188

182-
public Engagement getEngagement(String customerName, String engagementName) {
189+
public Engagement getEngagement(String customerName, String engagementName, boolean includeStatus) {
183190
Engagement engagement = null;
184191

185192
Optional<Project> project = getProject(customerName, engagementName);
186193

187194
if(project.isPresent()) {
188-
engagement = getEngagement(project.get()).orElse(null);
195+
engagement = getEngagement(project.get(), includeStatus).orElse(null);
189196
}
190197

191198
return engagement;
192199
}
193200

194-
private Optional<Engagement> getEngagement(Project project) {
201+
private Optional<Engagement> getEngagement(Project project, boolean includeStatus) {
195202
Engagement engagement = null;
196203

197-
Optional<File> engagementFile = fileService.getFileAllow404(project.getId(), "engagement.json");
204+
Optional<File> engagementFile = fileService.getFileAllow404(project.getId(), ENGAGEMENT_FILE);
198205
if (engagementFile.isPresent()) {
199206
engagement = json.fromJson(engagementFile.get().getContent(), Engagement.class);
200207
}
201208

209+
if(includeStatus && engagement != null) {
210+
Optional<File> statusFile = fileService.getFileAllow404(project.getId(), STATUS_FILE);
211+
if(statusFile.isPresent()) {
212+
engagement.setStatus(json.fromJson(statusFile.get().getContent(), Status.class));
213+
}
214+
}
215+
202216
return Optional.ofNullable(engagement);
203217
}
204218

205219
private File createEngagmentFile(Engagement engagement) {
206220

207221
String fileContent = json.toJson(engagement);
208-
File file = File.builder().content(fileContent).filePath("engagement.json").build();
222+
File file = File.builder().content(fileContent).filePath(ENGAGEMENT_FILE).build();
209223

210224
return file;
211225
}
@@ -317,5 +331,14 @@ private String getEmoji() {
317331

318332
return String.valueOf(mysteryEmoji);
319333
}
334+
335+
private String getPath(String customerName, String engagementName) {
336+
return new StringBuilder(engagementPathPrefix)
337+
.append("/")
338+
.append(customerName)
339+
.append("/")
340+
.append(engagementName)
341+
.append("/iac").toString();
342+
}
320343

321344
}

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public Optional<File> deleteFile(Integer projectId, String filePath) {
9696

9797
public Optional<File> deleteFile(Integer projectId, String filePath, String ref) {
9898

99-
Optional<File> optional = getFile(projectId, filePath, ref, false);
99+
Optional<File> optional = getFile(String.valueOf(projectId), filePath, ref, false);
100100

101101
if (optional.isPresent()) {
102102

@@ -112,30 +112,34 @@ public Optional<File> deleteFile(Integer projectId, String filePath, String ref)
112112

113113
return optional;
114114
}
115+
116+
public Optional<File> getFile(String projectPath, String filePath) {
117+
return getFile(projectPath, filePath, DEFAULT_REF, false);
118+
}
115119

116120
// get a file
117121
public Optional<File> getFile(Integer projectId, String filePath) {
118-
return getFile(projectId, filePath, DEFAULT_REF, false);
122+
return getFile(String.valueOf(projectId), filePath);
119123
}
120124

121125
// get a file
122126
public Optional<File> getFileAllow404(Integer projectId, String filePath) {
123-
return getFile(projectId, filePath, DEFAULT_REF, true);
127+
return getFile(String.valueOf(projectId), filePath, DEFAULT_REF, true);
124128
}
125129

126130
// get a file
127-
public Optional<File> getFile(Integer projectId, String filePath, String ref) {
128-
return getFile(projectId, filePath, ref, false);
131+
public Optional<File> getFile(String projectIdOrPAth, String filePath, String ref) {
132+
return getFile(projectIdOrPAth, filePath, ref, false);
129133
}
130134

131-
public Optional<File> getFile(Integer projectId, String filePath, String ref, boolean allow404) {
135+
public Optional<File> getFile(String projectIdOrPath, String filePath, String ref, boolean allow404) {
132136

133137
Optional<File> optional = Optional.empty();
134138

135139
try {
136140

137141
// get file
138-
File file = gitLabService.getFile(projectId, filePath, ref);
142+
File file = gitLabService.getFile(projectIdOrPath, filePath, ref);
139143

140144
if (null != file) {
141145
// decode file
@@ -144,10 +148,10 @@ public Optional<File> getFile(Integer projectId, String filePath, String ref, bo
144148
}
145149
} catch(WebApplicationException wae) {
146150
if(wae.getResponse().getStatus() != 404) {
147-
LOGGER.error("Get file {} for project {} failed with code {}", filePath, projectId, wae.getResponse().getStatus());
151+
LOGGER.error("Get file {} for project {} failed with code {}", filePath, projectIdOrPath, wae.getResponse().getStatus());
148152
throw wae;
149153
} else if(LOGGER.isDebugEnabled()) {
150-
LOGGER.debug("Get file {} for project {} failed with code {}", filePath, projectId, wae.getResponse().getStatus());
154+
LOGGER.debug("Get file {} for project {} failed with code {}", filePath, projectIdOrPath, wae.getResponse().getStatus());
151155
}
152156
}
153157

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void deleteProjectById(Integer projectId) {
147147
}
148148

149149
@Override
150-
public File getFile(Integer projectId, String filePath, String ref) {
150+
public File getFile(String projectId, String filePath, String ref) {
151151

152152
if ("schema/meta.dat".equalsIgnoreCase(filePath)) {
153153

@@ -190,6 +190,12 @@ public File getFile(Integer projectId, String filePath, String ref) {
190190
content = new String(EncodingUtils.base64Encode(content.getBytes()), StandardCharsets.UTF_8);
191191
return File.builder().filePath(filePath).content(content).build();
192192
}
193+
194+
if("status.json".equals(filePath)) {
195+
String content = ResourceLoader.load("status.json");
196+
content = new String(EncodingUtils.base64Encode(content.getBytes()), StandardCharsets.UTF_8);
197+
return File.builder().filePath(filePath).content(content).build();
198+
}
193199

194200
return null;
195201
}

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ public 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\",\"customer_name\":\"customer1\","
27-
+ "\"description\":\"Charleston\",\"end_date\":\"20201225\",\"engagement_lead_email\":\"[email protected]\",\"engagement_lead_name\":\"Doug Gilmour\","
28-
+ "\"location\":\"Raleigh, NC\",\"ocp_cloud_provider_name\":\"GCP\",\"ocp_cloud_provider_region\":\"West\",\"ocp_cluster_size\":\"medium\","
29-
+ "\"ocp_persistent_storage_size\":\"50GB\",\"ocp_sub_domain\":\"jello\",\"ocp_version\":\"v4.2\",\"project_id\":0,\"project_name\":\"project1\",\"start_date\":\"20200202\","
30-
+ "\"technical_lead_email\":\"[email protected]\",\"technical_lead_name\":\"Wendel Clark\"}]"));
26+
.body(is("[{\"archive_date\":\"20210125\",\"customer_contact_email\":\"[email protected]\",\"customer_contact_name\":\"Reg Dunlop\","
27+
+ "\"customer_name\":\"customer1\",\"description\":\"Charleston\",\"end_date\":\"20201225\",\"engagement_lead_email\":\"[email protected]\","
28+
+ "\"engagement_lead_name\":\"Doug Gilmour\",\"location\":\"Raleigh, NC\",\"ocp_cloud_provider_name\":\"GCP\",\"ocp_cloud_provider_region\":\"West\","
29+
+ "\"ocp_cluster_size\":\"medium\",\"ocp_persistent_storage_size\":\"50GB\",\"ocp_sub_domain\":\"jello\",\"ocp_version\":\"v4.2\",\"project_id\":0,\"project_name\":\"project1\","
30+
+ "\"start_date\":\"20200202\",\"status\":{\"messages\":[\"This is message 1\",\"This is message 2\",\"This is message 3\"],\"openshift_api\":\"https://console.s11.core.rht-labs.com/\","
31+
+ "\"openshift_web_console\":\"https://console.s11.core.rht-labs.com/\",\"overall_status\":\"green\"},\"technical_lead_email\":\"[email protected]\",\"technical_lead_name\":\"Wendel Clark\"}]"));
3132

3233
}
3334

@@ -79,5 +80,29 @@ public void testCreateProjectHookSuccess() {
7980
.then()
8081
.statusCode(201);
8182
}
82-
83+
84+
@Test
85+
public void testGetStatusSuccess() {
86+
given()
87+
.when()
88+
.contentType(ContentType.JSON)
89+
.get("/api/v1/engagements/customer/jello/lemon/status")
90+
.then()
91+
.statusCode(200)
92+
.body(is("{\"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/\",\"overall_status\":\"green\"}"));
93+
}
94+
95+
@Test
96+
public void testGetProjectSuccess() {
97+
given()
98+
.when()
99+
.contentType(ContentType.JSON)
100+
.queryParam("includeStatus", true)
101+
.get("/api/v1/engagements/customer/jello/lemon")
102+
.then()
103+
.statusCode(200)
104+
.body(is("\"archive_date\":\"20210125\",\"customer_contact_email\":\"[email protected]\",\"customer_contact_name\":\"Reg Dunlop\",\"customer_name\":\"customer1\",\"description\":\"Charleston\",\"end_date\":\"20201225\",\"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\",\"ocp_persistent_storage_size\":\"50GB\",\"ocp_sub_domain\":\"jello\",\"ocp_version\":\"v4.2\",\"project_id\":0,\"project_name\":\"project1\",\"start_date\":\"20200202\",\"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/\",\"overall_status\":\"green\"},\"technical_lead_email\":\"[email protected]\",\"technical_lead_name\":\"Wendel Clark\"}"));
105+
}
106+
107+
83108
}

src/test/resources/status.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"messages": [
3+
"This is message 1",
4+
"This is message 2",
5+
"This is message 3"
6+
],
7+
"openshift_api": "https://console.s11.core.rht-labs.com/",
8+
"openshift_web_console": "https://console.s11.core.rht-labs.com/",
9+
"overall_status": "green"
10+
}

0 commit comments

Comments
 (0)