Skip to content

Commit 596fadf

Browse files
committed
webhooks
1 parent 07947b1 commit 596fadf

File tree

19 files changed

+593
-66
lines changed

19 files changed

+593
-66
lines changed

src/main/java/com/redhat/labs/omp/config/JsonMarshaller.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
package com.redhat.labs.omp.config;
22

3+
import java.io.IOException;
4+
import java.lang.reflect.Type;
5+
import java.nio.file.Files;
6+
import java.nio.file.Path;
7+
import java.nio.file.Paths;
8+
import java.util.List;
9+
310
import javax.enterprise.context.ApplicationScoped;
411
import javax.enterprise.event.Observes;
512
import javax.json.bind.Jsonb;
613
import javax.json.bind.JsonbBuilder;
714
import javax.json.bind.JsonbConfig;
815
import javax.json.bind.config.PropertyNamingStrategy;
916

17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
19+
20+
import com.fasterxml.jackson.databind.JavaType;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
23+
1024
import io.quarkus.runtime.StartupEvent;
1125

1226
/**
@@ -16,16 +30,59 @@
1630
*/
1731
@ApplicationScoped
1832
public class JsonMarshaller {
33+
public static Logger LOGGER = LoggerFactory.getLogger(JsonMarshaller.class);
1934

2035
private Jsonb jsonb;
2136

37+
private ObjectMapper om = new ObjectMapper(new YAMLFactory());
38+
2239
void onStart(@Observes StartupEvent event) {
2340
JsonbConfig config = new JsonbConfig()
2441
.withFormatting(true)
2542
.withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES);
2643
jsonb = JsonbBuilder.create(config);
44+
45+
}
46+
47+
/**
48+
* Reads a file from the system and returns the contents transformed into a new object. Must be a list. If this fails
49+
* for any IOException or the file is not readable it will return null
50+
* @param <T>
51+
* @param yamlFile The path to the file on disk
52+
* @param clazz The class that will be return as a list
53+
* @return A list!
54+
*/
55+
public <T> List<T> fromYamlFile(String yamlFile, Class<T> clazz) {
56+
57+
Path path = Paths.get(yamlFile);
58+
59+
if(Files.isReadable(path)) {
60+
LOGGER.debug("Loading config file {}", yamlFile);
61+
String yamlFileContent;
62+
try {
63+
yamlFileContent = new String(Files.readAllBytes(path));
64+
return fromYaml(yamlFileContent, clazz);
65+
} catch (IOException e) {
66+
LOGGER.error(String.format("Found but unable to read file %s", yamlFile), e);
67+
}
68+
}
69+
70+
return null;
2771
}
2872

73+
public <T> List<T> fromYaml(String yamlContent, Class<T> clazz) {
74+
JavaType type = om.getTypeFactory().constructCollectionType(List.class, clazz);
75+
76+
try {
77+
return om.readValue(yamlContent, type);
78+
} catch (IOException e) {
79+
LOGGER.error(String.format("Found but unable to map file %s", yamlContent), e);
80+
}
81+
82+
return null;
83+
}
84+
85+
2986
public <T> String toJson(T object) {
3087
return jsonb.toJson(object);
3188
}
@@ -35,5 +92,8 @@ public <T> T fromJson(String json, Class<T> type) {
3592
return jsonb.fromJson(json, type);
3693
}
3794

38-
95+
public <T> T fromJson(String json, Type type) {
96+
return jsonb.fromJson(json, type);
97+
}
98+
3999
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import javax.json.bind.annotation.JsonbProperty;
44
import javax.validation.constraints.NotBlank;
55

6+
import com.fasterxml.jackson.annotation.JsonProperty;
7+
68
import lombok.AllArgsConstructor;
79
import lombok.Builder;
810
import lombok.Data;
@@ -22,6 +24,8 @@ public class Group {
2224
@NotBlank
2325
@JsonbProperty("path")
2426
private String path;
27+
@JsonProperty("full_path")
28+
private String fullPath;
2529
@JsonbProperty("description")
2630
private String description;
2731
@JsonbProperty("membership_lock")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.redhat.labs.omp.models.gitlab;
2+
3+
import java.util.Date;
4+
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
10+
@Data
11+
@Builder
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
public class Hook {
15+
16+
private Integer id;
17+
18+
private String url;
19+
20+
private Integer projectId;
21+
22+
private Boolean pushEvents;
23+
24+
private String pushEventsBranchFilter;
25+
26+
private Date createdAt;
27+
28+
//Token not provide in GET requests
29+
private String token;
30+
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.redhat.labs.omp.models.gitlab;
2+
3+
import javax.json.bind.annotation.JsonbProperty;
4+
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
10+
@Data
11+
@Builder
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
public class HookConfig {
15+
16+
private String name;
17+
18+
@JsonbProperty("baseUrl")
19+
private String baseUrl;
20+
21+
@JsonbProperty("pushDvent")
22+
private boolean pushEvent;
23+
24+
@JsonbProperty("pushEventsBranchFilter")
25+
private String pushEventsBranchFilter;
26+
27+
@JsonbProperty("token")
28+
private String token;
29+
30+
}

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

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

3+
import java.util.List;
4+
35
import javax.inject.Inject;
46
import javax.ws.rs.Consumes;
57
import javax.ws.rs.GET;
@@ -15,6 +17,7 @@
1517
import com.fasterxml.jackson.databind.ObjectMapper;
1618
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
1719
import com.redhat.labs.omp.models.gitlab.File;
20+
import com.redhat.labs.omp.models.gitlab.HookConfig;
1821
import com.redhat.labs.omp.service.ConfigService;
1922

2023
@Path("/api")
@@ -29,6 +32,7 @@ public class ConfigResource {
2932
@GET
3033
@Path("/v1/config")
3134
public File get() {
35+
LOGGER.info("V1 or undefined is deprecated");
3236
return configService.getConfigFile();
3337
}
3438

@@ -38,12 +42,20 @@ public Response getJson() {
3842
File configFile = configService.getConfigFile();
3943
ObjectMapper om = new ObjectMapper(new YAMLFactory());
4044
try {
41-
LOGGER.debug(configFile.getContent());
45+
LOGGER.trace(configFile.getContent());
4246
Object content = om.readValue(configFile.getContent(), Object.class);
4347
return Response.ok(content).build();
4448
} catch (JsonProcessingException e) {
4549
LOGGER.error(String.format("Error processing config file %s", configFile.getFilePath()), e);
4650
return Response.serverError().build();
4751
}
4852
}
53+
54+
@GET
55+
@Path("/v2/config/webhooks")
56+
public Response getWebhooks() {
57+
List<HookConfig> hooks = configService.getHookConfig();
58+
59+
return Response.ok(hooks).build();
60+
}
4961
}

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
import org.eclipse.microprofile.metrics.MetricUnits;
2020
import org.eclipse.microprofile.metrics.annotation.Counted;
2121
import org.eclipse.microprofile.metrics.annotation.Timed;
22+
import org.jboss.resteasy.annotations.jaxrs.PathParam;
2223
import org.slf4j.Logger;
2324
import org.slf4j.LoggerFactory;
2425

2526
import com.redhat.labs.omp.models.Engagement;
27+
import com.redhat.labs.omp.models.gitlab.Hook;
2628
import com.redhat.labs.omp.models.gitlab.Project;
2729
import com.redhat.labs.omp.service.EngagementService;
2830

@@ -51,12 +53,42 @@ public Response createEngagement(Engagement engagement, @Context UriInfo uriInfo
5153
}
5254

5355
@GET
54-
@Counted(name = "get-engagement", description = "How many enagement requests have been requested")
56+
@Counted(name = "get-all-engagement", description = "Count of get all engagements")
5557
@Timed(name = "performedEngagementGetAll", description = "Time to get all engagements", unit = MetricUnits.MILLISECONDS)
5658
public Response findAllEngagements() {
5759
List<Engagement> engagements = engagementService.getAllEngagements();
5860

5961
return Response.ok().entity(engagements).build();
6062
}
63+
64+
@GET
65+
@Path("/customer/{customer}/{engagement}")
66+
@Counted(name = "get-engagement", description = "Count of get engagement")
67+
@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+
71+
return Response.ok().entity(response).build();
72+
}
73+
74+
@POST
75+
@Path("customer/{customer}/{engagement}/hooks")
76+
@Counted(name = "create-engagement-hook", description = "Count of create-hook requestst")
77+
@Timed(name = "performedHookCreate", description = "Time to create hook", unit = MetricUnits.MILLISECONDS)
78+
public Response createProjectHook(Hook hook, @PathParam("customer") String customer, @PathParam("engagement") String engagement) {
79+
80+
Response response = engagementService.createHook(customer, engagement, hook);
81+
return response;
82+
}
83+
84+
@GET
85+
@Path("customer/{customer}/{engagement}/hooks")
86+
@Counted(name = "get-hook", description = "Count of get-hook requests")
87+
@Timed(name = "performedHookGetAll", description = "Time to get all hooks", unit = MetricUnits.MILLISECONDS)
88+
public Response findAllProjectHooks(@PathParam("customer") String customer, @PathParam("engagement") String engagement) {
89+
List<Hook> engagements = engagementService.getHooks(customer, engagement);
90+
91+
return Response.ok().entity(engagements).build();
92+
}
6193

6294
}

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.redhat.labs.omp.models.gitlab.CommitMultiple;
2121
import com.redhat.labs.omp.models.gitlab.File;
2222
import com.redhat.labs.omp.models.gitlab.Group;
23+
import com.redhat.labs.omp.models.gitlab.Hook;
2324
import com.redhat.labs.omp.models.gitlab.Project;
2425
import com.redhat.labs.omp.models.gitlab.ProjectSearchResults;
2526
import com.redhat.labs.omp.resources.filter.Logged;
@@ -61,6 +62,12 @@ public interface GitLabService {
6162
@Path("/groups")
6263
@Produces("application/json")
6364
List<Group> getGroupByName(@QueryParam("search") @Encoded String name);
65+
66+
@GET
67+
@Logged
68+
@Path("/groups/{idOrPath}")
69+
@Produces("application/json")
70+
Group getGroupByIdOrPath(@PathParam("idOrPath") @Encoded String idOrPath);
6471

6572
@DELETE
6673
@Logged
@@ -86,7 +93,7 @@ public interface GitLabService {
8693
@Logged
8794
@Path("/projects/{id}")
8895
@Produces("application/json")
89-
Project getProjectById(@PathParam("id") @Encoded Integer projectId);
96+
Project getProjectById(@PathParam("id") @Encoded String projectId);
9097

9198
@POST
9299
@Logged
@@ -105,6 +112,25 @@ public interface GitLabService {
105112
@Logged
106113
@Path("/projects/{id}")
107114
void deleteProjectById(@PathParam("id") @Encoded Integer projectId);
115+
116+
@POST
117+
@Logged
118+
@Path("/projects/{id}/hooks")
119+
@Produces("application/json")
120+
Response createProjectHook(@PathParam("id") @Encoded Integer projectId, Hook hook);
121+
122+
@PUT
123+
@Logged
124+
@Path("/projects/{id}/hooks/{hookId}")
125+
@Produces("application/json")
126+
Response updateProjectHook(@PathParam("id") @Encoded Integer projectId, @PathParam("hookId") @Encoded Integer hookId, Hook hook);
127+
128+
@GET
129+
@Logged
130+
@Path("/projects/{id}/hooks")
131+
@Produces("application/json")
132+
@Consumes("application/json")
133+
List<Hook> getProjectHooks(@PathParam("id") @Encoded Integer projectId);
108134

109135
// FILES
110136

0 commit comments

Comments
 (0)