Skip to content

Commit aaf613d

Browse files
authored
Merge pull request #59 from mcanoy/hooks
add hooks
2 parents 55291b3 + 9dbeb59 commit aaf613d

26 files changed

+680
-71
lines changed

.github/workflows/pull-request-checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ jobs:
1919
with:
2020
java-version: 13.0.1
2121
- name: Build with Maven & Quarkus
22-
run: mvn test
22+
run: mvn test -q

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
![Container Build](https://github.com/rht-labs/open-management-portal-git-api/workflows/Container%20Build/badge.svg)
22

3-
# Open Management Portal - Git API
3+
# LodeStar - Git API
44

5-
The Git API for the Open Management Portal.
5+
The Git API for LodeStar.
66

7-
This API uses GitLab as a repository to store OMP resources.
7+
This API uses GitLab as a repository to store LodeStar resources.
88

99
## JSON REST APIs
1010

@@ -67,12 +67,15 @@ Deployment template will read from the above secret and inject following env var
6767
|------|---------------|----------|
6868
| CONFIG_REPOSITORY_ID | 1 | True |
6969
| CONFIG_FILE | my-config.yml | True |
70+
| CONFIG_GITLAB_REF | master | False |
71+
| WEBHOOK_FILE | webhooks.yml | False |
7072

7173
### Engagements Resource
7274

7375
| Name | Example Value | Required |
7476
|------|---------------|----------|
7577
| ENGAGEMENTS_REPOSITORY_ID | 2 | True |
78+
| WEBHOOK_DEFAULT_TOKEN | tolkien | False |
7679

7780
### Version Resource
7881

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: v1
2+
data:
3+
webhooks.yaml: |
4+
{{- range $hook := .Values.config.hooks }}
5+
- name: {{ $hook.name }}
6+
baseUrl: {{ $hook.baseUrl }}
7+
token: {{ $hook.token }}
8+
pushEvent: {{ $hook.pushEvent | default true }}
9+
pushEventsBranchFilter: {{ $hook.pushEventsBranchFilter | default "master" }}
10+
{{- end }}
11+
kind: ConfigMap
12+
metadata:
13+
name: hook-cm

deployment/templates/deploymentconfig.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,18 @@ spec:
4242
resources: {}
4343
terminationMessagePath: /dev/termination-log
4444
terminationMessagePolicy: File
45+
volumeMounts:
46+
- name: hook-cm-volume
47+
mountPath: {{ .Values.config.hookFile.path }}
4548
dnsPolicy: ClusterFirst
4649
restartPolicy: Always
4750
schedulerName: default-scheduler
4851
securityContext: {}
4952
terminationGracePeriodSeconds: 30
53+
volumes:
54+
- name: hook-cm-volume
55+
configMap:
56+
name: hook-cm
5057
test: false
5158
triggers:
5259
- type: ConfigChange

deployment/templates/gitlab-secret.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ stringData:
1111
GITLAB_API_URL: "{{ .Values.gitLabApiUrl }}"
1212
GITLAB_PERSONAL_ACCESS_TOKEN: "{{ .Values.gitLabPersonalAccessToken }}"
1313
ENGAGEMENTS_REPOSITORY_ID: "{{ .Values.engagementsRepositoryId }}"
14+
WEBHOOK_FILE: "{{ .Values.config.hookFile.path }}/{{ .Values.config.hookFile.name }}"
1415
{{- end }}

deployment/values-dev.yaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,17 @@ configRepositoryId: false
1818
deployKey: false
1919
gitLabApiUrl: false
2020
gitLabPersonalAccessToken: false
21-
engagementsRepositoryId: false
21+
engagementsRepositoryId: false
22+
23+
config:
24+
hookFile:
25+
path: /schema
26+
name: webhooks.yaml
27+
hooks:
28+
- name: hook1
29+
baseUrl: http://localhost/hook1
30+
token: ibaka
31+
- name: hook2
32+
baseUrl: http://localhost/hook2
33+
token: anunoby
34+

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("pushEvent")
22+
private boolean pushEvent;
23+
24+
@JsonbProperty("pushEventsBranchFilter")
25+
private String pushEventsBranchFilter;
26+
27+
@JsonbProperty("token")
28+
private String token;
29+
30+
}

0 commit comments

Comments
 (0)