Skip to content

Commit 1b352ba

Browse files
chrisrohrgmessner
authored andcommitted
Added encoding to the tag name where the tag name is used in the url path (#372)
* Added encoding to the tag name where the tag name is used in the URL path
1 parent 38fe1c8 commit 1b352ba

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/main/java/org/gitlab4j/api/TagsApi.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public Stream<Tag> getTagsStream(Object projectIdOrPath) throws GitLabApiExcepti
9494
* @throws GitLabApiException if any exception occurs
9595
*/
9696
public Tag getTag(Object projectIdOrPath, String tagName) throws GitLabApiException {
97-
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags", tagName);
97+
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags", urlEncode(tagName));
9898
return (response.readEntity(Tag.class));
9999
}
100100

@@ -198,7 +198,7 @@ public Tag createTag(Object projectIdOrPath, String tagName, String ref, String
198198
*/
199199
public void deleteTag(Object projectIdOrPath, String tagName) throws GitLabApiException {
200200
Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
201-
delete(expectedStatus, null, "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags", tagName);
201+
delete(expectedStatus, null, "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags", urlEncode(tagName));
202202
}
203203

204204
/**
@@ -215,7 +215,7 @@ public void deleteTag(Object projectIdOrPath, String tagName) throws GitLabApiEx
215215
public Release createRelease(Object projectIdOrPath, String tagName, String releaseNotes) throws GitLabApiException {
216216
Form formData = new GitLabApiForm().withParam("description", releaseNotes);
217217
Response response = post(Response.Status.CREATED, formData.asMap(),
218-
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags", tagName, "release");
218+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags", urlEncode(tagName), "release");
219219
return (response.readEntity(Release.class));
220220
}
221221

@@ -233,7 +233,8 @@ public Release createRelease(Object projectIdOrPath, String tagName, String rele
233233
public Release updateRelease(Object projectIdOrPath, String tagName, String releaseNotes) throws GitLabApiException {
234234
Form formData = new GitLabApiForm().withParam("description", releaseNotes);
235235
Response response = put(Response.Status.OK, formData.asMap(),
236-
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags", tagName, "release");
236+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags", urlEncode(tagName), "release");
237237
return (response.readEntity(Release.class));
238238
}
239+
239240
}

src/test/java/org/gitlab4j/api/TestTagsApi.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class TestTagsApi extends AbstractIntegrationTest {
2323

2424
private static final String TEST_TAG_NAME_1 = "test-tag-1";
2525
private static final String TEST_TAG_NAME_0 = "test-tag-0";
26+
private static final String TEST_TAG_WITH_SLASH = "env/test-tag";
2627

2728
private static GitLabApi gitLabApi;
2829
private static Project testProject;
@@ -46,6 +47,10 @@ public static void testSetup() {
4647
try {
4748
gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_NAME_1);
4849
} catch (Exception ignore) {}
50+
51+
try {
52+
gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_WITH_SLASH);
53+
} catch (Exception ignore) {}
4954
}
5055
}
5156

@@ -59,6 +64,10 @@ public static void tearDown() {
5964
try {
6065
gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_NAME_1);
6166
} catch (Exception ignore) {}
67+
68+
try {
69+
gitLabApi.getTagsApi().deleteTag(testProject, TEST_TAG_WITH_SLASH);
70+
} catch (Exception ignore) {}
6271
}
6372
}
6473

@@ -127,4 +136,15 @@ public void testGetTagsPager() throws GitLabApiException {
127136
assertTrue(tags.getTotalItems() > 0);
128137
assertTrue(tags.stream().map(Tag::getName).anyMatch(s -> TEST_TAG_NAME_0.equals(s)));
129138
}
139+
140+
@Test
141+
public void testGetTagWithSpecialCharacersInTagName() throws GitLabApiException {
142+
Tag testTag = gitLabApi.getTagsApi().createTag(testProject, TEST_TAG_WITH_SLASH, "master");
143+
assertNotNull(testTag);
144+
assertEquals(TEST_TAG_WITH_SLASH, testTag.getName());
145+
146+
testTag = gitLabApi.getTagsApi().getTag(testProject, TEST_TAG_WITH_SLASH);
147+
assertNotNull(testTag);
148+
assertEquals(TEST_TAG_WITH_SLASH, testTag.getName());
149+
}
130150
}

0 commit comments

Comments
 (0)