Skip to content

Commit 5b3e3d6

Browse files
authored
Merge pull request #55 from dwasinge/grouppath
Group Path Must Conform to GitLab Requirements
2 parents aa669ef + 1b38e13 commit 5b3e3d6

File tree

4 files changed

+192
-3
lines changed

4 files changed

+192
-3
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public void encodeActionAttributes() throws UnsupportedEncodingException {
4040

4141
// encode contents
4242
if (null != content) {
43-
byte[] encodedContents = EncodingUtils.base64Encode(this.content.getBytes());
43+
byte[] encodedContents = EncodingUtils
44+
.base64Encode(this.content.getBytes(StandardCharsets.UTF_8.toString()));
4445
this.content = new String(encodedContents, StandardCharsets.UTF_8);
4546
}
4647

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.redhat.labs.omp.models.gitlab.Group;
2323
import com.redhat.labs.omp.models.gitlab.Project;
2424
import com.redhat.labs.omp.models.gitlab.ProjectSearchResults;
25+
import com.redhat.labs.omp.utils.GitLabPathUtils;
2526

2627
@ApplicationScoped
2728
public class EngagementService {
@@ -109,6 +110,7 @@ public List<Engagement> getAllEngagements() {
109110
}
110111

111112
private File createEngagmentFile(Engagement engagement) {
113+
112114
String fileContent = json.toJson(engagement);
113115
File file = File.builder().content(fileContent).filePath("engagement.json").build();
114116

@@ -119,12 +121,14 @@ private Project createProjectStucture(Engagement engagement) {
119121

120122
// create group for customer name
121123
Group customerGroup = getOrCreateGroup(engagement.getCustomerName(),
122-
Group.builder().name(engagement.getCustomerName()).path(engagement.getCustomerName())
124+
Group.builder().name(engagement.getCustomerName())
125+
.path(GitLabPathUtils.generateValidPath(engagement.getCustomerName()))
123126
.parentId(engagementRepositoryId).build());
124127

125128
// create group for project name
126129
Group projectGroup = getOrCreateGroup(engagement.getProjectName(),
127-
Group.builder().name(engagement.getProjectName()).path(engagement.getProjectName())
130+
Group.builder().name(engagement.getProjectName())
131+
.path(GitLabPathUtils.generateValidPath(engagement.getProjectName()))
128132
.parentId(customerGroup.getId()).build());
129133

130134
// create project under project name group
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.redhat.labs.omp.utils;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class GitLabPathUtils {
7+
8+
private static Logger LOGGER = LoggerFactory.getLogger(GitLabPathUtils.class);
9+
10+
public static String generateValidPath(String input) {
11+
12+
if (null == input || input.trim().length() == 0) {
13+
throw new IllegalArgumentException("input string cannot be blank.");
14+
}
15+
16+
// trim
17+
String path = input.trim();
18+
19+
// turn to lowercase
20+
path = path.toLowerCase();
21+
22+
// replace whitespace with a '-'
23+
path = path.replaceAll("\\s", "-");
24+
25+
// remove any characters other than A-Z, a-z, 0-9, ., -
26+
path = path.replaceAll("[^A-Za-z0-9-\\.]", "");
27+
28+
// remove leading or trailing hyphens
29+
path = path.replaceFirst("^-*", "").replaceFirst("-*$", "");
30+
31+
// remove ending '.', '.git', or '.atom'
32+
path = path.replaceAll("(\\.|\\.git|\\.atom)$", "");
33+
34+
LOGGER.debug("input name {}, converted to path {}", input, path);
35+
36+
return path;
37+
38+
}
39+
40+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package com.redhat.labs.utils;
2+
import org.junit.jupiter.api.Assertions;
3+
import org.junit.jupiter.api.Test;
4+
5+
import com.redhat.labs.omp.utils.GitLabPathUtils;
6+
7+
public class GitLabPathUtilsTest {
8+
9+
// Groups Requirements:
10+
// Name can contain only letters, digits, emojis, '_', '.', dash, space, parenthesis. It must start with letter, digit, emoji or '_'.
11+
// Path can contain only letters, digits, '_', '-' and '.'. Cannot start with '-' or end in '.', '.git' or '.atom'
12+
13+
@Test
14+
public void testGenerateValidInputNull() {
15+
16+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
17+
GitLabPathUtils.generateValidPath(null);
18+
});
19+
20+
}
21+
22+
@Test
23+
public void testGenerateValidInputEmptyString() {
24+
25+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
26+
GitLabPathUtils.generateValidPath("");
27+
});
28+
29+
}
30+
31+
@Test
32+
public void testGenerateValidInputEmptyBlank() {
33+
34+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
35+
GitLabPathUtils.generateValidPath(" ");
36+
});
37+
38+
}
39+
40+
@Test
41+
public void testGenerateValidPathDoNothing() {
42+
43+
// given
44+
String input = "something";
45+
46+
// when
47+
String output = GitLabPathUtils.generateValidPath(input);
48+
49+
Assertions.assertEquals("something", output);
50+
51+
}
52+
53+
@Test
54+
public void testGenerateValidPathReplaceSpaces() {
55+
56+
// given
57+
String input = "Some Name Here";
58+
59+
// when
60+
String output = GitLabPathUtils.generateValidPath(input);
61+
62+
Assertions.assertEquals("some-name-here", output);
63+
64+
}
65+
66+
@Test
67+
public void testGenerateValidPathRemoveLeadingOrTrailingHyphens() {
68+
69+
// given
70+
String input = "----Some Name Here--";
71+
72+
// when
73+
String output = GitLabPathUtils.generateValidPath(input);
74+
75+
Assertions.assertEquals("some-name-here", output);
76+
77+
}
78+
79+
@Test
80+
public void testGenerateValidPathReplaceSpacesAndRemoveSpecialCharacters() {
81+
82+
// given
83+
String input = "Some$1 Nameø 9-.Here!";
84+
85+
// when
86+
String output = GitLabPathUtils.generateValidPath(input);
87+
88+
Assertions.assertEquals("some1-name-9-.here", output);
89+
90+
}
91+
92+
@Test
93+
public void testGenerateValidPathFullReplace() {
94+
95+
// given
96+
String input = "-Some$1 Nameø 9-Here!---";
97+
98+
// when
99+
String output = GitLabPathUtils.generateValidPath(input);
100+
101+
Assertions.assertEquals("some1-name-9-here", output);
102+
103+
}
104+
105+
@Test
106+
public void testGenerateValidWithEndingPeriod() {
107+
108+
// given
109+
String input = "_My Favørite Proj.";
110+
111+
// when
112+
String output = GitLabPathUtils.generateValidPath(input);
113+
114+
Assertions.assertEquals("my-favrite-proj", output);
115+
116+
}
117+
118+
@Test
119+
public void testGenerateValidWithEndingPeriodGit() {
120+
121+
// given
122+
String input = "_My Favørite Proj.git";
123+
124+
// when
125+
String output = GitLabPathUtils.generateValidPath(input);
126+
127+
Assertions.assertEquals("my-favrite-proj", output);
128+
129+
}
130+
131+
@Test
132+
public void testGenerateValidWithEndingPeriodAtom() {
133+
134+
// given
135+
String input = "_My Favørite Proj.atom";
136+
137+
// when
138+
String output = GitLabPathUtils.generateValidPath(input);
139+
140+
Assertions.assertEquals("my-favrite-proj", output);
141+
142+
}
143+
144+
}

0 commit comments

Comments
 (0)