Skip to content

Commit 0816a12

Browse files
committed
path translation fixes and tests
1 parent 97b507e commit 0816a12

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

src/main/java/com/redhat/labs/omp/utils/GitLabPathUtils.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.redhat.labs.omp.utils;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
36
public class GitLabPathUtils {
47

8+
private static Logger LOGGER = LoggerFactory.getLogger(GitLabPathUtils.class);
9+
510
public static String generateValidPath(String input) {
611

712
if (null == input || input.trim().length() == 0) {
@@ -11,9 +16,6 @@ public static String generateValidPath(String input) {
1116
// trim
1217
String path = input.trim();
1318

14-
// remove leading or trailing hyphens
15-
path = path.replaceFirst("^-*", "").replaceFirst("-*$", "");
16-
1719
// turn to lowercase
1820
path = path.toLowerCase();
1921

@@ -23,6 +25,14 @@ public static String generateValidPath(String input) {
2325
// remove any characters other than A-Z, a-z, 0-9, ., -
2426
path = path.replaceAll("[^A-Za-z0-9-\\.]", "");
2527

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+
2636
return path;
2737

2838
}

src/test/java/com/redhat/labs/utils/GitLabPathUtilsTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
public class GitLabPathUtilsTest {
88

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+
913
@Test
1014
public void testGenerateValidInputNull() {
1115

@@ -98,4 +102,43 @@ public void testGenerateValidPathFullReplace() {
98102

99103
}
100104

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+
101144
}

0 commit comments

Comments
 (0)