Skip to content

Commit 7ace99d

Browse files
author
Hugo Dias
committed
Fix Authentication configuration.
Support branches with slashes. There's an error when you add authentication configuration in the configuration. This will fix it. When you have branches' names with slashes, i.e. feature/foo, when you use %b to add the branch name in the version number, it will replace the shash with an underscore. [patch]
1 parent 35a0983 commit 7ace99d

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

src/main/java/pt/com/hugodias/gradle/gitversioner/configuration/git/Authentication.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* (C) 2022 Hugo Dias */
22
package pt.com.hugodias.gradle.gitversioner.configuration.git;
33

4+
import org.gradle.api.Action;
45
import org.gradle.api.tasks.Nested;
56

67
public interface Authentication {
@@ -9,4 +10,12 @@ public interface Authentication {
910

1011
@Nested
1112
Https getHttps();
13+
14+
default void ssh(Action<? super Ssh> action) {
15+
action.execute(getSsh());
16+
}
17+
18+
default void https(Action<? super Https> action) {
19+
action.execute(getHttps());
20+
}
1221
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
/* (C) 2022 Hugo Dias */
22
package pt.com.hugodias.gradle.gitversioner.configuration.git;
33

4+
import org.gradle.api.Action;
45
import org.gradle.api.tasks.Nested;
56

67
public interface Git {
78
@Nested
89
Authentication getAuthentication();
10+
11+
default void authentication(Action<? super Authentication> action) {
12+
action.execute(getAuthentication());
13+
}
914
}

src/main/java/pt/com/hugodias/gradle/gitversioner/core/version/Version.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public String print(String pattern) {
2222
.replace("%m", String.valueOf(minor))
2323
.replace("%p", String.valueOf(patch))
2424
.replace("%c", String.valueOf(commit))
25-
.replace("%b", branch)
25+
.replace("%b", removeSlashes(branch))
2626
.replace("%H", hash)
2727
.replace("%h", hash.isEmpty() ? "" : hash.substring(0, 7));
2828
return commit != 0 ? removeParentheses(filledVersion) : removeCommitConditionals(filledVersion);
@@ -35,4 +35,8 @@ private String removeCommitConditionals(String version) {
3535
private String removeParentheses(String version) {
3636
return version.replace("(", "").replace(")", "");
3737
}
38+
39+
private String removeSlashes(String branch) {
40+
return branch.replaceAll("\\/", "_");
41+
}
3842
}

src/test/java/pt/com/hugodias/gradle/gitversioner/version/VersionTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ class VersionTest {
3232
.hash("myhash123")
3333
.build();
3434

35+
private Version versionWithCommitAndBranchWithSlashes =
36+
Version.builder()
37+
.major(1)
38+
.minor(2)
39+
.patch(3)
40+
.commit(4)
41+
.branch("feature/mybranch")
42+
.hash("myhash123")
43+
.build();
44+
3545
@ParameterizedTest(name = "{index} pattern {0} prints the version correctly")
3646
@CsvSource(
3747
value = {
@@ -53,4 +63,20 @@ void testWithCommit(String pattern, String expected) {
5363
void testWithoutCommit(String pattern, String expected) {
5464
assertThat(versionWithoutCommit.print(pattern)).isEqualTo(expected);
5565
}
66+
67+
@ParameterizedTest(name = "{index} pattern {0} prints the version correctly")
68+
@CsvSource(
69+
value = {
70+
"%M.%m.%p.%c,1.2.3.4",
71+
"%M.%m.%p-%c,1.2.3-4",
72+
"%M.%m.%p-%H,1.2.3-myhash123",
73+
"%M.%m.%p-%h,1.2.3-myhash1",
74+
"%M.%m.%p-%b,1.2.3-feature_mybranch",
75+
"%M.%m.%p(-%c),1.2.3-4",
76+
"%M.%m.%p(-SNAPSHOT),1.2.3-SNAPSHOT"
77+
})
78+
@DisplayName("prints version correctly according to pattern")
79+
void testWithCommitAndBranchWithSlashes(String pattern, String expected) {
80+
assertThat(versionWithCommitAndBranchWithSlashes.print(pattern)).isEqualTo(expected);
81+
}
5682
}

src/test/resources/functionalTest/configured-build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ versioner {
1919
pattern {
2020
pattern = "%M.%m.%p(-%c)"
2121
}
22+
git {
23+
authentication {
24+
https {
25+
username = "user"
26+
token = "token"
27+
}
28+
}
29+
}
30+
2231
}
2332

2433
task("printVersionEarly") {

0 commit comments

Comments
 (0)