Skip to content

Commit 6c279f6

Browse files
author
Derek Wasinger
committed
added engagement and legacy file endpoint
1 parent 76c0aaa commit 6c279f6

16 files changed

+390
-521
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.redhat.labs.exception;
2+
3+
public class FileNotFoundException extends RuntimeException {
4+
5+
private static final long serialVersionUID = 5596509664831558317L;
6+
7+
public FileNotFoundException() {
8+
super();
9+
}
10+
11+
public FileNotFoundException(String message, Throwable cause, boolean enableSuppression,
12+
boolean writableStackTrace) {
13+
super(message, cause, enableSuppression, writableStackTrace);
14+
}
15+
16+
public FileNotFoundException(String message, Throwable cause) {
17+
super(message, cause);
18+
}
19+
20+
public FileNotFoundException(String message) {
21+
super(message);
22+
}
23+
24+
public FileNotFoundException(Throwable cause) {
25+
super(cause);
26+
}
27+
28+
}

src/main/java/com/redhat/labs/exception/UnexpectedGitLabResponseException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.redhat.labs.exception;
22

3-
public class UnexpectedGitLabResponseException extends Exception {
3+
public class UnexpectedGitLabResponseException extends RuntimeException {
44

55
private static final long serialVersionUID = -2450016825084220551L;
66

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.redhat.labs.exception.mapper;
2+
3+
import javax.json.Json;
4+
import javax.json.JsonObject;
5+
import javax.ws.rs.core.Response;
6+
import javax.ws.rs.ext.ExceptionMapper;
7+
import javax.ws.rs.ext.Provider;
8+
9+
import org.apache.http.HttpStatus;
10+
11+
@Provider
12+
public class ApiExceptionMapper implements ExceptionMapper<RuntimeException> {
13+
14+
@Override
15+
public Response toResponse(RuntimeException exception) {
16+
17+
int status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
18+
19+
JsonObject model = Json.createObjectBuilder().add("error", exception.getMessage()).add("code", status).build();
20+
21+
// TODO: may need to be different response for each type of runtime exception
22+
return Response.status(status).entity(model.toString()).build();
23+
24+
}
25+
26+
}
Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
11
package com.redhat.labs.omp.models;
22

3-
public class Engagement {
4-
5-
public int id;
6-
public String customerName;
7-
public String projectName;
8-
public String description;
9-
public String location;
10-
public String startDate;
11-
public String endDate;
12-
public String archiveDate;
13-
public String engagementLeadName;
14-
public String engagementLeadEmail;
15-
public String technicalLeadName;
16-
public String technicalLeadEmail;
17-
public String customerContactName;
18-
public String customerContactEmail;
19-
public String ocpCloudProviderName;
20-
public String ocpCloudProviderRegion;
21-
public String ocpVersion;
22-
public String ocpSubDomain;
23-
public String ocpPersistentStorageSize;
24-
public String ocpClusterSize;
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
257

26-
public Engagement () {}
8+
@Data
9+
@Builder
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class Engagement {
2713

28-
public String toString() {
29-
String engagement = "Engagement (%d) Customer: %s Project: %s Description: %s Location: %s Start Date: %s"
30-
+ " End Date: %s Archive Date: %s + Engagement Lead %s (%s) Tech Lead %s (%s) Cust Contact %s (%s)"
31-
+ "OpenShift: Cloud Provider: %s Region: %s Version: %s Sub Domain: %s Storage: %s + Cluster size: %s";
14+
private int id;
15+
private String customerName;
16+
private String projectName;
17+
private String description;
18+
private String location;
19+
private String startDate;
20+
private String endDate;
21+
private String archiveDate;
22+
private String engagementLeadName;
23+
private String engagementLeadEmail;
24+
private String technicalLeadName;
25+
private String technicalLeadEmail;
26+
private String customerContactName;
27+
private String customerContactEmail;
28+
private String ocpCloudProviderName;
29+
private String ocpCloudProviderRegion;
30+
private String ocpVersion;
31+
private String ocpSubDomain;
32+
private String ocpPersistentStorageSize;
33+
private String ocpClusterSize;
3234

33-
return String.format(engagement, id, customerName, projectName, description, location, startDate,
34-
endDate, archiveDate, engagementLeadName, engagementLeadEmail, technicalLeadName, technicalLeadEmail,
35-
customerContactName, customerContactEmail, ocpCloudProviderName, ocpCloudProviderRegion,
36-
ocpVersion, ocpSubDomain, ocpPersistentStorageSize, ocpClusterSize);
37-
}
3835
}

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import javax.json.bind.annotation.JsonbProperty;
77
import javax.validation.constraints.NotBlank;
88

9+
import com.redhat.labs.exception.EncodingException;
910
import com.redhat.labs.omp.utils.EncodingUtils;
1011

1112
import lombok.AllArgsConstructor;
@@ -42,11 +43,16 @@ public class File {
4243
@JsonbProperty("author_name")
4344
private String authorName;
4445

45-
public void encodeFileAttributes() throws UnsupportedEncodingException {
46+
public void encodeFileAttributes() {
4647

4748
// encode file path
4849
if (null != filePath) {
49-
String encodedFilePath = EncodingUtils.urlEncode(this.filePath);
50+
String encodedFilePath;
51+
try {
52+
encodedFilePath = EncodingUtils.urlEncode(this.filePath);
53+
} catch (UnsupportedEncodingException e) {
54+
throw new EncodingException("failed to encode url." + filePath);
55+
}
5056
this.filePath = encodedFilePath;
5157
}
5258

@@ -58,11 +64,16 @@ public void encodeFileAttributes() throws UnsupportedEncodingException {
5864

5965
}
6066

61-
public void decodeFileAttributes() throws UnsupportedEncodingException {
67+
public void decodeFileAttributes() {
6268

6369
// decode file path
6470
if (null != filePath) {
65-
String decodedFilePath = EncodingUtils.urlDecode(this.filePath);
71+
String decodedFilePath;
72+
try {
73+
decodedFilePath = EncodingUtils.urlDecode(this.filePath);
74+
} catch (UnsupportedEncodingException e) {
75+
throw new EncodingException("failed to decodej url " + filePath);
76+
}
6677
this.filePath = decodedFilePath;
6778
}
6879

src/main/java/com/redhat/labs/omp/resources/CacheResource.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.redhat.labs.omp.resources;
22

3-
import java.nio.charset.StandardCharsets;
4-
import java.util.Base64;
5-
63
import javax.inject.Inject;
74
import javax.ws.rs.Consumes;
85
import javax.ws.rs.GET;
@@ -19,7 +16,6 @@
1916

2017
import com.redhat.labs.cache.ResidencyInformation;
2118
import com.redhat.labs.cache.cacheStore.ResidencyDataCache;
22-
import com.redhat.labs.omp.models.gitlab.response.GetFileResponse;
2319
import com.redhat.labs.omp.models.gitlab.response.RepositoryFile;
2420
import com.redhat.labs.omp.resources.filters.Logged;
2521
import com.redhat.labs.omp.rest.client.GitLabService;
Lines changed: 10 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.redhat.labs.omp.resources;
22

33
import javax.inject.Inject;
4-
import javax.json.bind.JsonbBuilder;
5-
import javax.json.bind.JsonbConfig;
6-
import javax.json.bind.config.PropertyNamingStrategy;
74
import javax.ws.rs.Consumes;
85
import javax.ws.rs.POST;
96
import javax.ws.rs.Path;
@@ -14,103 +11,37 @@
1411
import javax.ws.rs.core.UriBuilder;
1512
import javax.ws.rs.core.UriInfo;
1613

17-
import org.eclipse.microprofile.config.inject.ConfigProperty;
1814
import org.eclipse.microprofile.metrics.MetricUnits;
1915
import org.eclipse.microprofile.metrics.annotation.Counted;
2016
import org.eclipse.microprofile.metrics.annotation.Timed;
21-
import org.eclipse.microprofile.rest.client.inject.RestClient;
2217
import org.slf4j.Logger;
2318
import org.slf4j.LoggerFactory;
2419

25-
import com.redhat.labs.omp.models.CreateResidencyGroupStructure;
2620
import com.redhat.labs.omp.models.Engagement;
27-
import com.redhat.labs.omp.models.gitlab.FileAction;
28-
import com.redhat.labs.omp.models.gitlab.request.CommitMultipleFilesInRepsitoryRequest;
29-
import com.redhat.labs.omp.models.gitlab.request.CreateCommitFileRequest;
30-
import com.redhat.labs.omp.models.gitlab.response.GetMultipleFilesResponse;
31-
import com.redhat.labs.omp.models.gitlab.response.GitLabCreateProjectResponse;
32-
import com.redhat.labs.omp.rest.client.GitLabService;
33-
import com.redhat.labs.omp.utils.TemplateCombobulator;
21+
import com.redhat.labs.omp.models.gitlab.Project;
22+
import com.redhat.labs.omp.service.EngagementService;
3423

35-
@Path("/api/residencies")
24+
@Path("/api/v1/engagements")
3625
@Produces(MediaType.APPLICATION_JSON)
3726
@Consumes(MediaType.APPLICATION_JSON)
3827
public class EngagementResource {
39-
public static Logger LOGGER = LoggerFactory.getLogger(EngagementResource.class);
40-
41-
@ConfigProperty(name = "stripPathPrefix", defaultValue = "schema/")
42-
protected String stripPathPrefix;
4328

44-
@Inject
45-
protected TemplateCombobulator combobulator;
46-
47-
@Inject
48-
protected ProjectsResource projects;
49-
50-
@Inject
51-
protected GroupsResource groups;
29+
public static Logger LOGGER = LoggerFactory.getLogger(EngagementResource.class);
5230

5331
@Inject
54-
@RestClient
55-
protected GitLabService gitLabService;
32+
EngagementService engagementService;
5633

5734
@POST
5835
@Counted(name = "engagement", description = "How many engagements request have been requested")
5936
@Timed(name = "performedChecks", description = "How much time it takes to create an engagement", unit = MetricUnits.MILLISECONDS)
6037
public Response createEngagement(Engagement engagement, @Context UriInfo uriInfo) {
6138

62-
if (LOGGER.isDebugEnabled()) {
63-
LOGGER.debug("{}",
64-
JsonbBuilder
65-
.create(new JsonbConfig()
66-
.withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES))
67-
.toJson(engagement));
68-
}
69-
GitLabCreateProjectResponse gitLabCreateProjectResponse = createGitLabProject(engagement);
70-
engagement.id = gitLabCreateProjectResponse.id;
71-
72-
GetMultipleFilesResponse getMultipleFilesResponse = combobulator.process(engagement);
73-
74-
CommitMultipleFilesInRepsitoryRequest commitMultipleFilesInRepsitoryRequest = getCommitMultipleFilesInRepositoryRequest(
75-
engagement, getMultipleFilesResponse);
76-
77-
Response gitResponse = gitLabService.createFilesInRepository(gitLabCreateProjectResponse.id,
78-
commitMultipleFilesInRepsitoryRequest);
79-
80-
if (gitResponse.getStatus() == 201) {
81-
UriBuilder builder = uriInfo.getAbsolutePathBuilder();
82-
builder.path(Integer.toString(gitLabCreateProjectResponse.id));
83-
return Response.created(builder.build()).build();
84-
}
85-
86-
return gitResponse;
39+
Project project = engagementService.createEngagement(engagement);
40+
41+
UriBuilder builder = uriInfo.getAbsolutePathBuilder();
42+
builder.path(Integer.toString(project.getId()));
43+
return Response.created(builder.build()).build();
8744

8845
}
8946

90-
private CommitMultipleFilesInRepsitoryRequest getCommitMultipleFilesInRepositoryRequest(Engagement engagement,
91-
GetMultipleFilesResponse getMultipleFilesResponse) {
92-
CommitMultipleFilesInRepsitoryRequest commitMultipleFilesInRepsitoryRequest = new CommitMultipleFilesInRepsitoryRequest();
93-
getMultipleFilesResponse.files.stream().forEach(f -> {
94-
commitMultipleFilesInRepsitoryRequest.addFileRequest(
95-
new CreateCommitFileRequest(FileAction.create, stripPrefix(f.getFileName()), f.getFileContent()));
96-
});
97-
commitMultipleFilesInRepsitoryRequest.authorEmail = engagement.engagementLeadEmail;
98-
commitMultipleFilesInRepsitoryRequest.authorName = engagement.engagementLeadName;
99-
commitMultipleFilesInRepsitoryRequest.commitMessage = "\uD83E\uDD84 Created by OMP Git API \uD83D\uDE80 \uD83C\uDFC1";
100-
return commitMultipleFilesInRepsitoryRequest;
101-
}
102-
103-
public String stripPrefix(String in) {
104-
if (in != null && in.startsWith(stripPathPrefix)) {
105-
return in.split(stripPathPrefix)[1];
106-
}
107-
return in;
108-
}
109-
110-
private GitLabCreateProjectResponse createGitLabProject(Engagement residency) {
111-
CreateResidencyGroupStructure createResidencyGroupStructure = new CreateResidencyGroupStructure();
112-
createResidencyGroupStructure.projectName = residency.projectName;
113-
createResidencyGroupStructure.customerName = residency.customerName;
114-
return groups.createResidencyStructure(createResidencyGroupStructure);
115-
}
11647
}

0 commit comments

Comments
 (0)