Skip to content

Commit c13236a

Browse files
committed
Now allows for empty content when creating or updating a file (#197).
1 parent 69f26d0 commit c13236a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,14 @@ private Form createForm(RepositoryFile file, String branchName, String commitMes
226226
}
227227

228228
addFormParam(form, "encoding", file.getEncoding(), false);
229-
addFormParam(form, "content", file.getContent(), true);
229+
230+
// Cannot use addFormParam() as it does not accept an empty or whitespace only string
231+
String content = file.getContent();
232+
if (content == null) {
233+
throw new IllegalArgumentException("content cannot be null");
234+
}
235+
form.param("content", content);
236+
230237
addFormParam(form, "commit_message", commitMessage, true);
231238
return (form);
232239
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,21 @@ public void testCreateFileAndDeleteFile() throws GitLabApiException {
236236
gitLabApi.getRepositoryFileApi().deleteFile(TEST_FILEPATH, project.getId(), TEST_BRANCH_NAME, "Testing deleteFile().");
237237
}
238238

239+
@Test
240+
public void testCreateFileWithEmptyContent() throws GitLabApiException {
241+
242+
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
243+
assertNotNull(project);
244+
245+
RepositoryFile file = new RepositoryFile();
246+
file.setFilePath(TEST_FILEPATH);
247+
file.setContent("");
248+
RepositoryFile createdFile = gitLabApi.getRepositoryFileApi().createFile(file, project.getId(), TEST_BRANCH_NAME, "Testing createFile().");
249+
assertNotNull(createdFile);
250+
251+
gitLabApi.getRepositoryFileApi().deleteFile(TEST_FILEPATH, project.getId(), TEST_BRANCH_NAME, "Testing deleteFile().");
252+
}
253+
239254
@Test
240255
public void testProtectBranch() throws GitLabApiException {
241256

0 commit comments

Comments
 (0)