Skip to content

Commit 5e2ee52

Browse files
authored
Merge pull request #4 from mcanoy/by-artifact
By artifact
2 parents 3f1d9ef + 1c24a05 commit 5e2ee52

File tree

6 files changed

+132
-30
lines changed

6 files changed

+132
-30
lines changed

.github/workflows/ci-cd.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77

88
jobs:
99
build:
10-
if: github.repository == 'rht-labs/lodestar-atifacts'
10+
if: github.repository == 'rht-labs/lodestar-artifacts'
1111
runs-on: ubuntu-latest
1212
steps:
1313
- uses: actions/checkout@v2

deployment/values-dev.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ git:
1414
ref: master
1515

1616
db:
17-
mongodbServiceName: lodestar-backend-mongodb
17+
mongodbServiceName: lodestar-artifacts-mongodb
1818
mongodbUser: mongouser
1919
mongodbPassword: mongopassword
20-
mongodbDatabase: engagements
20+
mongodbDatabase: artifacts
2121
mongodbAdminPassword: mongoadminpassword
2222

2323
api:

src/main/java/com/redhat/labs/lodestar/model/Artifact.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ public static ArtifactCount countAllArtifacts() {
7575
public static ArtifactCount countArtifactsByEngagementUuid(String engagementUuid) {
7676
return ArtifactCount.builder().count(count(ENGAGEMENT_UUID, engagementUuid)).build();
7777
}
78+
79+
/**
80+
* Returns an {@link ArtifactCount} containing the count for the number of
81+
* {@link Artifact} matching the typegit.
82+
*
83+
* @param engagementUuid
84+
* @return
85+
*/
86+
public static ArtifactCount countArtifactsByType(String type) {
87+
return ArtifactCount.builder().count(count("type", type)).build();
88+
}
7889

7990
/**
8091
* Returns {@link List} of {@link Artifact}s sorted descending on modified
@@ -87,6 +98,18 @@ public static ArtifactCount countArtifactsByEngagementUuid(String engagementUuid
8798
public static List<Artifact> pagedArtifacts(int page, int pageSize) {
8899
return findAll(Sort.descending(MODIFIED)).page(page, pageSize).list();
89100
}
101+
102+
/**
103+
* Returns {@link List} of {@link Artifact}s sorted descending on modified
104+
* timestamp using the page specified where type is specified.
105+
*
106+
* @param page
107+
* @param pageSize
108+
* @return
109+
*/
110+
public static List<Artifact> pagedArtifactsByType(String type, int page, int pageSize) {
111+
return find("type", Sort.descending(MODIFIED), type).page(page, pageSize).list();
112+
}
90113

91114
/**
92115
* Returns a {@link List} of {@link Artifact}s for the given engagement uuid,

src/main/java/com/redhat/labs/lodestar/model/GetOptions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@ public class GetOptions {
1818
@Parameter(name = "engagementUuid", required = false, description = "return only artifacts for the given engagement uuid")
1919
@QueryParam("engagementUuid")
2020
private String engagementUuid;
21+
22+
@Parameter(name = "type", required = false, description = "return only artifacts for the given type. Do not use with engagementUuid")
23+
@QueryParam("type")
24+
private String type;
2125

2226
public Optional<String> getEngagementUuid() {
2327
return Optional.ofNullable(engagementUuid);
2428
}
29+
30+
public Optional<String> getType() {
31+
return Optional.ofNullable(type);
32+
}
2533

2634
}

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

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import java.time.LocalDateTime;
44
import java.time.ZoneId;
5-
import java.util.ArrayList;
65
import java.util.Arrays;
7-
import java.util.Collection;
8-
import java.util.Collections;
96
import java.util.List;
107
import java.util.Map;
118
import java.util.Optional;
@@ -81,21 +78,7 @@ public void purge() {
8178
* and inserts into the database.
8279
*/
8380
public void refresh() {
84-
85-
engagementRestClient.getAllEngagementProjects().stream().map(Engagement::getProjectId)
86-
.map(this::getArtifactsFromGitlabByProjectId).flatMap(Collection::stream)
87-
.forEach(a -> {
88-
89-
// set uuid if missing
90-
if (null == a.getUuid()) {
91-
a.setUuid(UUID.randomUUID().toString());
92-
}
93-
94-
// persist the artifact
95-
createOrUpdateArtifact(a);
96-
97-
});
98-
81+
engagementRestClient.getAllEngagementProjects().parallelStream().forEach(this::reloadFromGitlabByEngagement);
9982
}
10083

10184
/**
@@ -106,22 +89,38 @@ public void refresh() {
10689
* @param projectId
10790
* @return
10891
*/
109-
List<Artifact> getArtifactsFromGitlabByProjectId(long projectId) {
92+
void reloadFromGitlabByEngagement(Engagement engagement) {
93+
if(engagement.getUuid() == null) {
94+
LOGGER.error("Engagement found with no uuid. Check description of project {}", engagement.getProjectId());
95+
return;
96+
}
97+
11098
try {
111-
File file = gitlabRestClient.getFile(projectId, artifactsFile, defaultBranch);
99+
File file = gitlabRestClient.getFile(engagement.getProjectId(), artifactsFile, defaultBranch);
112100
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();
101+
LOGGER.error("IMPOSSIBLE. NO FILE DATA FROM GITLAB FOR PROJECT {}. THIS SHALL NOT STAND", engagement.getProjectId());
102+
return;
115103
}
116104

117105
file.decodeFileAttributes();
118-
return Arrays.asList(jsonb.fromJson(file.getContent(), Artifact[].class));
106+
List<Artifact> artifacts = Arrays.asList(jsonb.fromJson(file.getContent(), Artifact[].class));
107+
108+
artifacts.forEach(a -> {
109+
a.setEngagementUuid(engagement.getUuid());
110+
// set uuid if missing
111+
if (null == a.getUuid()) {
112+
a.setUuid(UUID.randomUUID().toString());
113+
}
114+
115+
// persist the artifact
116+
createOrUpdateArtifact(a);
117+
});
119118

120119
} catch(WebApplicationException wae) {
121120
if(wae.getResponse().getStatus() != 404) {
122121
throw wae;
123122
}
124-
return Collections.emptyList();
123+
LOGGER.error("NO FILE DATA FROM GITLAB FOR PROJECT {}. THIS SHALL NOT STAND", engagement.getProjectId());
125124
}
126125
}
127126

@@ -161,6 +160,10 @@ public void process(List<Artifact> artifacts, Optional<String> authorEmail, Opti
161160
* @return
162161
*/
163162
public List<Artifact> getArtifacts(GetListOptions options) {
163+
164+
if(options.getType().isPresent()) {
165+
return getArtifactsByType(options);
166+
}
164167

165168
Optional<String> engagementUuid = options.getEngagementUuid();
166169

@@ -170,6 +173,16 @@ public List<Artifact> getArtifacts(GetListOptions options) {
170173
: Artifact.pagedArtifacts(options.getPage(), options.getPageSize());
171174

172175
}
176+
177+
public List<Artifact> getArtifactsByType(GetListOptions options) {
178+
179+
if(options.getEngagementUuid().isPresent()) {
180+
throw new WebApplicationException("Type and engagement together is not supported", 400);
181+
}
182+
183+
return Artifact.pagedArtifactsByType(options.getType().get(), options.getPage(), options.getPageSize());
184+
185+
}
173186

174187
/**
175188
* Returns a {@link ArtifactCount} with the count of {@link Artifact}s matching
@@ -182,8 +195,17 @@ public ArtifactCount countArtifacts(GetOptions options) {
182195

183196
Optional<String> engagementUuid = options.getEngagementUuid();
184197

185-
return engagementUuid.isPresent() ? Artifact.countArtifactsByEngagementUuid(engagementUuid.get())
186-
: Artifact.countAllArtifacts();
198+
ArtifactCount count;
199+
200+
if(options.getType().isPresent()) {
201+
count = Artifact.countArtifactsByType(options.getType().get());
202+
} else if(engagementUuid.isPresent()) {
203+
count = Artifact.countArtifactsByEngagementUuid(engagementUuid.get());
204+
} else {
205+
count = Artifact.countAllArtifacts();
206+
}
207+
208+
return count;
187209

188210
}
189211

src/test/java/com/redhat/labs/service/ArtifactServiceTest.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
56

67
import java.util.ArrayList;
78
import java.util.Arrays;
@@ -10,6 +11,7 @@
1011

1112
import javax.inject.Inject;
1213
import javax.json.bind.Jsonb;
14+
import javax.ws.rs.WebApplicationException;
1315

1416
import org.junit.jupiter.api.BeforeEach;
1517
import org.junit.jupiter.api.Test;
@@ -125,6 +127,38 @@ void testGetArtifactsByEngagement() {
125127
assertEquals(2, artifacts.size());
126128

127129
}
130+
131+
@Test
132+
void testGetArtifactsByType() {
133+
134+
// given
135+
GetListOptions options = new GetListOptions();
136+
options.setType("Demo");
137+
138+
// when
139+
List<Artifact> artifacts = artifactService.getArtifacts(options);
140+
141+
// then
142+
assertNotNull(artifacts);
143+
assertEquals(1, artifacts.size());
144+
145+
}
146+
147+
@Test
148+
void testGetArtifactsByTypeException() {
149+
150+
// given
151+
GetListOptions options = new GetListOptions();
152+
options.setType("Demo");
153+
options.setEngagementUuid("1111");
154+
155+
WebApplicationException ex = assertThrows(WebApplicationException.class, () -> {
156+
artifactService.getArtifacts(options);
157+
});
158+
159+
assertEquals(400, ex.getResponse().getStatus());
160+
161+
}
128162

129163
@Test
130164
void testGetArtifactsPaging() {
@@ -167,12 +201,27 @@ void testCountArtifactsNoOptions() {
167201
assertEquals(2, count.getCount());
168202

169203
}
204+
205+
@Test
206+
void testCountArtifactsByType() {
207+
208+
// given
209+
GetOptions options = new GetOptions(null, "Demo");
210+
211+
// when
212+
ArtifactCount count = artifactService.countArtifacts(options);
213+
214+
// then
215+
assertNotNull(count);
216+
assertEquals(1, count.getCount());
217+
218+
}
170219

171220
@Test
172221
void testCountArtifactsByEngagement() {
173222

174223
// given
175-
GetOptions options = new GetOptions("1111");
224+
GetOptions options = new GetOptions("1111", null);
176225

177226
// when
178227
ArtifactCount count = artifactService.countArtifacts(options);

0 commit comments

Comments
 (0)