Skip to content

Commit 4b8309f

Browse files
committed
Add fault tolerance. Catch 404 errors and return empty list. Jacoco update. dev mode mongo connection. remove paagedresults.
1 parent b0bee3d commit 4b8309f

File tree

10 files changed

+46
-128
lines changed

10 files changed

+46
-128
lines changed

.github/workflows/ci-cd.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
env:
2828
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2929
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
30-
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
30+
run: mvn -B -q verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
3131
- name: Find and Replace Commit
3232
uses: jacobtomlinson/[email protected]
3333
with:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@ nb-configuration.xml
3939
.env
4040

4141
deployment/mongo-volume
42+
.adhoc

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ The following environment variables are available:
2121
| MONGODB_DATABASE | artifacts | The database name |
2222

2323
| GITLAB_API_URL | https://acmegit.com | The url to Gitlab |
24+
| ENGAGEMENT_API_URL | https://acmegit.com | The url to Gitlab |
2425
| GITLAB_TOKEN | t | The Access Token for Gitlab |
2526

26-
| GROUP_PARENT_ID | 1234 | Gitlab group ID containing engagements |
2727
| DEFAULT_BRANCH | master | Default branch to use if default not found for project |
2828
| DEFAULT_COMMIT_MESSAGE | updated artifacts list | Default commit message used if diff fails |
2929
| DEFAULT_PAGE_SIZE | 20 | Default number of artifacts that will be returned if pageSize not specified |
@@ -49,9 +49,9 @@ You can run your application in dev mode that enables live coding using:
4949

5050
```
5151
export GITLAB_API_URL=https://gitlab.com/
52+
export ENGAGEMENT_API_URL=http://git-api:8080
5253
export GITLAB_TOKEN=token
53-
export GROUP_PARENT_ID=<your-group-id>
5454
mvn quarkus:dev
5555
```
5656

57-
> **_NOTE:_** Quarkus now ships with a Dev UI, which is available in dev mode only at http://localhost:8080/q/dev/.
57+
> **_NOTE:_** Quarkus now ships with a Dev UI, which is available in dev mode only at http://localhost:8080/q/dev/.

deployment/values-dev.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ db:
2222

2323
api:
2424
gitlab: http://gitlab.com
25-
engagement: http://git-api:8080
25+
engagement: http://lodestar-git-api:8080
2626

2727
tokens:
2828
gitlab: nope
2929

3030
config:
31-
gitlabGroupId: 0
31+
gitlabGroupId: 0

pom.xml

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
<groupId>io.quarkus</groupId>
6363
<artifactId>quarkus-smallrye-openapi</artifactId>
6464
</dependency>
65+
<!-- Fault tolerance -->
66+
<dependency>
67+
<groupId>io.quarkus</groupId>
68+
<artifactId>quarkus-smallrye-fault-tolerance</artifactId>
69+
</dependency>
6570
<!-- REST Client -->
6671
<dependency>
6772
<groupId>io.quarkus</groupId>
@@ -73,6 +78,10 @@
7378
<artifactId>quarkus-smallrye-health</artifactId>
7479
</dependency>
7580
<!-- Other -->
81+
<dependency>
82+
<groupId>io.quarkus</groupId>
83+
<artifactId>quarkus-jacoco</artifactId>
84+
</dependency>
7685
<dependency>
7786
<groupId>io.quarkus</groupId>
7887
<artifactId>quarkus-arc</artifactId>
@@ -145,27 +154,6 @@
145154
</systemPropertyVariables>
146155
</configuration>
147156
</plugin>
148-
<plugin>
149-
<groupId>org.jacoco</groupId>
150-
<artifactId>jacoco-maven-plugin</artifactId>
151-
<version>0.8.5</version>
152-
<executions>
153-
<execution>
154-
<id>jacoco-initialize</id>
155-
<goals>
156-
<goal>prepare-agent</goal>
157-
</goals>
158-
<phase>test-compile</phase>
159-
</execution>
160-
<execution>
161-
<id>jacoco-site</id>
162-
<phase>verify</phase>
163-
<goals>
164-
<goal>report</goal>
165-
</goals>
166-
</execution>
167-
</executions>
168-
</plugin>
169157
</plugins>
170158
</build>
171159
<profiles>

src/main/java/com/redhat/labs/lodestar/model/pagination/PagedResults.java

Lines changed: 0 additions & 78 deletions
This file was deleted.

src/main/java/com/redhat/labs/lodestar/resource/ArtifactResource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public Response getArtifacts(@BeanParam GetListOptions options) {
5959
ArtifactCount count = service.countArtifacts(options);
6060

6161
return Response.ok(artifacts).header("x-page", options.getPage()).header("x-per-page", options.getPageSize())
62-
.header("x-total-activity", count.getCount())
62+
.header("x-total-artifacts", count.getCount())
6363
.header("x-total-pages", (count.getCount() / options.getPageSize()) + 1).build();
6464

6565
}

src/main/java/com/redhat/labs/lodestar/rest/client/GitlabRestClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
import javax.ws.rs.PathParam;
88
import javax.ws.rs.Produces;
99
import javax.ws.rs.QueryParam;
10+
import javax.ws.rs.WebApplicationException;
1011

12+
import org.apache.http.NoHttpResponseException;
13+
import org.eclipse.microprofile.faulttolerance.Retry;
1114
import org.eclipse.microprofile.rest.client.annotation.RegisterClientHeaders;
1215
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
1316
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
1417

1518
import com.redhat.labs.lodestar.model.gitlab.File;
1619

20+
@Retry(maxRetries = 5, delay = 1200, retryOn = NoHttpResponseException.class, abortOn = WebApplicationException.class)
1721
@Path("/api/v4")
1822
@RegisterRestClient(configKey = "gitlab.api")
1923
@RegisterClientHeaders(GitlabTokenFactory.class)

src/main/java/com/redhat/labs/lodestar/service/ArtifactService.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.ArrayList;
66
import java.util.Arrays;
77
import java.util.Collection;
8+
import java.util.Collections;
89
import java.util.List;
910
import java.util.Map;
1011
import java.util.Optional;
@@ -14,6 +15,7 @@
1415
import javax.enterprise.context.ApplicationScoped;
1516
import javax.inject.Inject;
1617
import javax.json.bind.Jsonb;
18+
import javax.ws.rs.WebApplicationException;
1719

1820
import org.eclipse.microprofile.config.inject.ConfigProperty;
1921
import org.eclipse.microprofile.rest.client.inject.RestClient;
@@ -22,6 +24,8 @@
2224
import org.javers.core.JaversBuilder;
2325
import org.javers.core.diff.Diff;
2426
import org.javers.core.diff.ListCompareAlgorithm;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
2529

2630
import com.redhat.labs.lodestar.model.Artifact;
2731
import com.redhat.labs.lodestar.model.ArtifactCount;
@@ -34,6 +38,7 @@
3438

3539
@ApplicationScoped
3640
public class ArtifactService {
41+
private static final Logger LOGGER = LoggerFactory.getLogger(ArtifactService.class);
3742

3843
@ConfigProperty(name = "artifacts.file", defaultValue = "artifacts.json")
3944
private String artifactsFile;
@@ -78,7 +83,7 @@ public void purge() {
7883
public void refresh() {
7984

8085
engagementRestClient.getAllEngagementProjects().stream().map(Engagement::getProjectId)
81-
.map(this::getArtifactsFileByProjectId).map(this::parseArtifactsFile).flatMap(Collection::stream)
86+
.map(this::getArtifactsFromGitlabByProjectId).flatMap(Collection::stream)
8287
.forEach(a -> {
8388

8489
// set uuid if missing
@@ -93,34 +98,31 @@ public void refresh() {
9398

9499
}
95100

96-
/**
97-
* Returns a {@link File} containing the artifacts content. Otherwise, null is
98-
* returned.
99-
*
100-
* @param projectId
101-
* @return
102-
*/
103-
File getArtifactsFileByProjectId(long projectId) {
104-
return gitlabRestClient.getFile(projectId, artifactsFile, defaultBranch);
105-
}
106-
107101
/**
108102
* Returns a {@link List} of {@link Artifact}s for the given {@link File}. An
109103
* empty {@link List} is returned if {@link File} or its contents are null or
110104
* blank.
111105
*
112-
* @param file
106+
* @param projectId
113107
* @return
114108
*/
115-
List<Artifact> parseArtifactsFile(File file) {
116-
117-
if (null == file || null == file.getContent() || file.getContent().isBlank()) {
118-
return new ArrayList<>();
109+
List<Artifact> getArtifactsFromGitlabByProjectId(long projectId) {
110+
try {
111+
File file = gitlabRestClient.getFile(projectId, artifactsFile, defaultBranch);
112+
if(null == file.getContent() || file.getContent().isBlank()) {
113+
LOGGER.error("NO FILE DATA FROM GITLAB FOR PROJECT {}. THIS SHALL NOT STAND", projectId);
114+
return Collections.emptyList();
115+
}
116+
117+
file.decodeFileAttributes();
118+
return Arrays.asList(jsonb.fromJson(file.getContent(), Artifact[].class));
119+
120+
} catch(WebApplicationException wae) {
121+
if(wae.getResponse().getStatus() != 404) {
122+
throw wae;
123+
}
124+
return Collections.emptyList();
119125
}
120-
121-
file.decodeFileAttributes();
122-
return Arrays.asList(jsonb.fromJson(file.getContent(), Artifact[].class));
123-
124126
}
125127

126128
/**
@@ -353,7 +355,7 @@ void updateArtifactsFile(String engagementUuid, List<Artifact> artifacts, Option
353355
// create
354356
File file = createArtifactsFile(content, defaultBranch, authorName, authorEmail, commitMessage);
355357

356-
// create or udpate in git
358+
// update in git
357359
gitlabRestClient.updateFile(project.getProjectId(), artifactsFile, file);
358360

359361
}

src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mongo.service.name=${DATABASE_SERVICE_NAME:localhost:27017}
1919
quarkus.mongodb.database=${MONGODB_DATABASE:artifacts}
2020

2121
quarkus.mongodb.connection-string=mongodb://${mongo.user}:${mongo.password}@${mongo.service.name}/${quarkus.mongodb.database}?uuidRepresentation=javaLegacy
22+
%dev.quarkus.mongodb.connection-string=mongodb://${mongo.user}:${mongo.password}@${mongo.service.name}/${quarkus.mongodb.database}?uuidRepresentation=javaLegacy&authSource=admin
2223

2324
%test.quarkus.mongodb.connection-string=mongodb://${mongo.service.name}/${quarkus.mongodb.database}?uuidRepresentation=javaLegacy
2425
%test.quarkus.mongodb.write-concern.journal=false

0 commit comments

Comments
 (0)