|
1 | 1 | package com.redhat.labs.omp.resources;
|
2 | 2 |
|
3 | 3 | import javax.inject.Inject;
|
4 |
| -import javax.json.bind.JsonbBuilder; |
5 |
| -import javax.json.bind.JsonbConfig; |
6 |
| -import javax.json.bind.config.PropertyNamingStrategy; |
7 | 4 | import javax.ws.rs.Consumes;
|
8 | 5 | import javax.ws.rs.POST;
|
9 | 6 | import javax.ws.rs.Path;
|
|
14 | 11 | import javax.ws.rs.core.UriBuilder;
|
15 | 12 | import javax.ws.rs.core.UriInfo;
|
16 | 13 |
|
17 |
| -import org.eclipse.microprofile.config.inject.ConfigProperty; |
18 | 14 | import org.eclipse.microprofile.metrics.MetricUnits;
|
19 | 15 | import org.eclipse.microprofile.metrics.annotation.Counted;
|
20 | 16 | import org.eclipse.microprofile.metrics.annotation.Timed;
|
21 |
| -import org.eclipse.microprofile.rest.client.inject.RestClient; |
22 | 17 | import org.slf4j.Logger;
|
23 | 18 | import org.slf4j.LoggerFactory;
|
24 | 19 |
|
25 |
| -import com.redhat.labs.omp.models.CreateResidencyGroupStructure; |
26 | 20 | 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; |
34 | 23 |
|
35 |
| -@Path("/api/residencies") |
| 24 | +@Path("/api/v1/engagements") |
36 | 25 | @Produces(MediaType.APPLICATION_JSON)
|
37 | 26 | @Consumes(MediaType.APPLICATION_JSON)
|
38 | 27 | public class EngagementResource {
|
39 |
| - public static Logger LOGGER = LoggerFactory.getLogger(EngagementResource.class); |
40 |
| - |
41 |
| - @ConfigProperty(name = "stripPathPrefix", defaultValue = "schema/") |
42 |
| - protected String stripPathPrefix; |
43 | 28 |
|
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); |
52 | 30 |
|
53 | 31 | @Inject
|
54 |
| - @RestClient |
55 |
| - protected GitLabService gitLabService; |
| 32 | + EngagementService engagementService; |
56 | 33 |
|
57 | 34 | @POST
|
58 | 35 | @Counted(name = "engagement", description = "How many engagements request have been requested")
|
59 | 36 | @Timed(name = "performedChecks", description = "How much time it takes to create an engagement", unit = MetricUnits.MILLISECONDS)
|
60 | 37 | public Response createEngagement(Engagement engagement, @Context UriInfo uriInfo) {
|
61 | 38 |
|
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(); |
87 | 44 |
|
88 | 45 | }
|
89 | 46 |
|
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 |
| - } |
116 | 47 | }
|
0 commit comments