Skip to content

Commit 8c7b8d4

Browse files
authored
Merge pull request #52 from dwasinge/bugfix-groups
Bugfix: Group Creation Not Saving to Correct Project
2 parents 9068370 + 1b6330e commit 8c7b8d4

File tree

5 files changed

+61
-23
lines changed

5 files changed

+61
-23
lines changed

src/main/java/com/redhat/labs/omp/service/EngagementService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ private Project createProjectStucture(Engagement engagement) {
140140

141141
private Group getOrCreateGroup(String groupName, Group groupToCreate) {
142142

143-
Optional<Group> optional = groupService.getGitLabGroupByName(groupName);
143+
Optional<Group> optional = groupService.getGitLabGroupByName(groupName, groupToCreate.getParentId());
144144

145145
if (!optional.isPresent()) {
146146

src/main/java/com/redhat/labs/omp/service/GroupService.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public class GroupService {
2424
GitLabService gitLabService;
2525

2626
// get a group
27-
public Optional<Group> getGitLabGroupByName(String name) throws UnexpectedGitLabResponseException {
27+
public Optional<Group> getGitLabGroupByName(String name, Integer parentId)
28+
throws UnexpectedGitLabResponseException {
2829

2930
Optional<Group> optional = Optional.empty();
3031

@@ -34,32 +35,27 @@ public Optional<Group> getGitLabGroupByName(String name) throws UnexpectedGitLab
3435
return optional;
3536
}
3637

37-
if (1 == groupList.size()) {
38-
return Optional.of(groupList.get(0));
39-
}
40-
41-
// found more than one group with name in either 'name' or 'path' attribute
42-
// should match path
43-
for(Group group : groupList) {
44-
if(name.equalsIgnoreCase(group.getPath())) {
38+
// look for a match between returned name and provided path
39+
for (Group group : groupList) {
40+
if (name.equals(group.getPath()) && parentId.equals(group.getParentId())) {
4541
return Optional.of(group);
4642
}
4743
}
4844

49-
throw new UnexpectedGitLabResponseException(
50-
"No resource found with name equal to path attribute.");
45+
return optional;
5146

5247
}
5348

54-
public List<Group> getAllGroups(Integer engagementRepositoryId) {
49+
public List<Group> getAllGroups(Integer engagementRepositoryId) {
5550

56-
//FIRST LEVEL
51+
// FIRST LEVEL
5752
List<Group> customerGroups = gitLabService.getSubGroups(engagementRepositoryId);
5853

5954
List<Group> customerEngagementGroups = new ArrayList<>();
60-
customerGroups.stream().forEach(group -> customerEngagementGroups.addAll(gitLabService.getSubGroups(group.getId())));
55+
customerGroups.stream()
56+
.forEach(group -> customerEngagementGroups.addAll(gitLabService.getSubGroups(group.getId())));
6157

62-
if(LOGGER.isDebugEnabled()) {
58+
if (LOGGER.isDebugEnabled()) {
6359
customerEngagementGroups.stream().forEach(group -> LOGGER.debug("Group -> {}", group.getName()));
6460
}
6561

src/main/java/com/redhat/labs/omp/service/ProjectService.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,9 @@ public Optional<Project> getProjectByName(Integer namespaceId, String name)
3535
return optional;
3636
}
3737

38-
if (1 == resultList.size()) {
39-
return Optional.of(Project.from(resultList.get(0)));
40-
}
41-
42-
// found more than one project with name in either 'name' or 'path' attribute
43-
// should match path
38+
// look for a project with name that matches the namespace id and the path
4439
for (ProjectSearchResults result : resultList) {
45-
if (namespaceId.equals(result.getNamespace().getId()) && name.equalsIgnoreCase(result.getPath())) {
40+
if (namespaceId.equals(result.getNamespace().getId()) && name.equals(result.getPath())) {
4641
return Optional.of(Project.from(result));
4742
}
4843
}

src/test/java/com/redhat/labs/omp/mocks/MockGitLabService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public List<Group> getGroupByName(String name) {
6161
groupList.add(Group.builder().id(3).name("customer1").path("customer1").build());
6262
} else if ("project4".equalsIgnoreCase(name)) {
6363
groupList.add(Group.builder().id(4).name("project1").path("project1").build());
64+
} else if ("customer".equalsIgnoreCase(name)) {
65+
groupList.add(Group.builder().id(11).name("customerA").path("customerA").parentId(10).build());
66+
groupList.add(Group.builder().id(12).name("customer").path("customer").parentId(10).build());
6467
}
6568

6669
return groupList;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.redhat.labs.omp.service;
2+
3+
import java.util.Optional;
4+
5+
import javax.inject.Inject;
6+
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
10+
import com.redhat.labs.omp.models.gitlab.Group;
11+
12+
import io.quarkus.test.junit.QuarkusTest;
13+
14+
@QuarkusTest
15+
public class GroupServiceTest {
16+
17+
@Inject
18+
GroupService groupService;
19+
20+
@Test
21+
public void testGetGitLabGroupByNameNoGroupsExist() {
22+
23+
Optional<Group> optional = groupService.getGitLabGroupByName("customerA", 1);
24+
Assertions.assertFalse(optional.isPresent());
25+
26+
}
27+
28+
@Test
29+
public void testGetGitLabGroupByNameMultipleGroupsExistNoMatch() {
30+
31+
Optional<Group> optional = groupService.getGitLabGroupByName("customer", 1);
32+
Assertions.assertFalse(optional.isPresent());
33+
34+
}
35+
36+
@Test
37+
public void testGetGitLabGroupByNameMultipleGroupsExistMatch() {
38+
39+
Optional<Group> optional = groupService.getGitLabGroupByName("customer", 10);
40+
Assertions.assertTrue(optional.isPresent());
41+
42+
}
43+
44+
}

0 commit comments

Comments
 (0)