Skip to content

Commit c3a99e9

Browse files
authored
Change the response type of "createFile" and "updateFile" (#953)
Fixes #893
1 parent df054ee commit c3a99e9

File tree

6 files changed

+65
-12
lines changed

6 files changed

+65
-12
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.gitlab4j.api.GitLabApi.ApiVersion;
1717
import org.gitlab4j.api.models.Blame;
1818
import org.gitlab4j.api.models.RepositoryFile;
19+
import org.gitlab4j.api.models.RepositoryFileResponse;
1920

2021
/**
2122
* This class provides an entry point to all the GitLab API repository files calls.
@@ -211,7 +212,7 @@ protected RepositoryFile getFileV3(String filePath, Long projectId, String ref)
211212
* @return a RepositoryFile instance with the created file info
212213
* @throws GitLabApiException if any exception occurs
213214
*/
214-
public RepositoryFile createFile(Object projectIdOrPath, RepositoryFile file, String branchName, String commitMessage) throws GitLabApiException {
215+
public RepositoryFileResponse createFile(Object projectIdOrPath, RepositoryFile file, String branchName, String commitMessage) throws GitLabApiException {
215216

216217
Form formData = createForm(file, branchName, commitMessage);
217218
Response response;
@@ -223,7 +224,7 @@ public RepositoryFile createFile(Object projectIdOrPath, RepositoryFile file, St
223224
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(file.getFilePath()));
224225
}
225226

226-
return (response.readEntity(RepositoryFile.class));
227+
return (response.readEntity(RepositoryFileResponse.class));
227228
}
228229

229230
/**
@@ -246,7 +247,7 @@ public RepositoryFile createFile(Object projectIdOrPath, RepositoryFile file, St
246247
* @deprecated Will be removed in version 6.0, replaced by {@link #createFile(Object, RepositoryFile, String, String)}
247248
*/
248249
@Deprecated
249-
public RepositoryFile createFile(RepositoryFile file, Long projectId, String branchName, String commitMessage) throws GitLabApiException {
250+
public RepositoryFileResponse createFile(RepositoryFile file, Long projectId, String branchName, String commitMessage) throws GitLabApiException {
250251
return (createFile(projectId, file, branchName, commitMessage));
251252
}
252253

@@ -268,7 +269,7 @@ public RepositoryFile createFile(RepositoryFile file, Long projectId, String bra
268269
* @return a RepositoryFile instance with the updated file info
269270
* @throws GitLabApiException if any exception occurs
270271
*/
271-
public RepositoryFile updateFile(Object projectIdOrPath, RepositoryFile file, String branchName, String commitMessage) throws GitLabApiException {
272+
public RepositoryFileResponse updateFile(Object projectIdOrPath, RepositoryFile file, String branchName, String commitMessage) throws GitLabApiException {
272273

273274
Form formData = createForm(file, branchName, commitMessage);
274275
Response response;
@@ -280,7 +281,7 @@ public RepositoryFile updateFile(Object projectIdOrPath, RepositoryFile file, St
280281
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(file.getFilePath()));
281282
}
282283

283-
return (response.readEntity(RepositoryFile.class));
284+
return (response.readEntity(RepositoryFileResponse.class));
284285
}
285286

286287
/**
@@ -303,7 +304,7 @@ public RepositoryFile updateFile(Object projectIdOrPath, RepositoryFile file, St
303304
* @deprecated Will be removed in version 6.0, replaced by {@link #updateFile(Object, RepositoryFile, String, String)}
304305
*/
305306
@Deprecated
306-
public RepositoryFile updateFile(RepositoryFile file, Long projectId, String branchName, String commitMessage) throws GitLabApiException {
307+
public RepositoryFileResponse updateFile(RepositoryFile file, Long projectId, String branchName, String commitMessage) throws GitLabApiException {
307308
return (updateFile(projectId, file, branchName, commitMessage));
308309
}
309310

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
package org.gitlab4j.api.models;
3+
4+
import java.util.Base64;
5+
6+
import org.gitlab4j.api.Constants.Encoding;
7+
import org.gitlab4j.api.utils.JacksonJson;
8+
9+
import com.fasterxml.jackson.annotation.JsonIgnore;
10+
11+
public class RepositoryFileResponse {
12+
13+
private String filePath; // full path to file. Ex. lib/class.rb
14+
private String branch;
15+
16+
public String getFilePath() {
17+
return filePath;
18+
}
19+
20+
public void setFilePath(String filePath) {
21+
this.filePath = filePath;
22+
}
23+
24+
public String getBranch() {
25+
return branch;
26+
}
27+
28+
public void setBranch(String branch) {
29+
this.branch = branch;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return (JacksonJson.toJsonString(this));
35+
}
36+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
import org.gitlab4j.api.models.Release;
106106
import org.gitlab4j.api.models.RemoteMirror;
107107
import org.gitlab4j.api.models.RepositoryFile;
108+
import org.gitlab4j.api.models.RepositoryFileResponse;
108109
import org.gitlab4j.api.models.Runner;
109110
import org.gitlab4j.api.models.RunnerDetail;
110111
import org.gitlab4j.api.models.SearchBlob;
@@ -555,6 +556,12 @@ public void testRepositoryFile() throws Exception {
555556
RepositoryFile file = unmarshalResource(RepositoryFile.class, "repository-file.json");
556557
assertTrue(compareJson(file, "repository-file.json"));
557558
}
559+
560+
@Test
561+
public void testRepositoryFileResponse() throws Exception {
562+
RepositoryFileResponse file = unmarshalResource(RepositoryFileResponse.class, "repository-file-response.json");
563+
assertTrue(compareJson(file, "repository-file-response.json"));
564+
}
558565

559566
@Test
560567
public void testRunnerDetail() throws Exception {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.gitlab4j.api.models.PipelineSchedule;
1919
import org.gitlab4j.api.models.Project;
2020
import org.gitlab4j.api.models.RepositoryFile;
21+
import org.gitlab4j.api.models.RepositoryFileResponse;
2122
import org.gitlab4j.api.models.Trigger;
2223
import org.gitlab4j.api.models.Variable;
2324
import org.junit.jupiter.api.AfterAll;
@@ -39,8 +40,8 @@ public class TestPipelineApi extends AbstractIntegrationTest {
3940

4041
private static GitLabApi gitLabApi;
4142
private static Project testProject;
42-
private static RepositoryFile createdGitlabCiYml;
43-
private static RepositoryFile gitlabCiYml;
43+
private static RepositoryFileResponse createdGitlabCiYml;
44+
private static RepositoryFileResponse gitlabCiYml;
4445

4546
public TestPipelineApi() {
4647
super();
@@ -91,7 +92,10 @@ public static void setup() {
9192
Optional<RepositoryFile> fileInfo =
9293
gitLabApi.getRepositoryFileApi().getOptionalFileInfo(testProject, ".gitlab-ci.yml", "master");
9394
if (fileInfo.isPresent()) {
94-
gitlabCiYml = fileInfo.get();
95+
RepositoryFileResponse file = new RepositoryFileResponse();
96+
file.setBranch(fileInfo.get().getRef());
97+
file.setFilePath(fileInfo.get().getFilePath());
98+
gitlabCiYml = file;
9599
} else {
96100

97101
try {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.gitlab4j.api.models.Branch;
1818
import org.gitlab4j.api.models.Project;
1919
import org.gitlab4j.api.models.RepositoryFile;
20+
import org.gitlab4j.api.models.RepositoryFileResponse;
2021
import org.junit.jupiter.api.AfterAll;
2122
import org.junit.jupiter.api.BeforeAll;
2223
import org.junit.jupiter.api.BeforeEach;
@@ -156,7 +157,7 @@ public void testCreateFileAndDeleteFile() throws GitLabApiException {
156157
RepositoryFile file = new RepositoryFile();
157158
file.setFilePath(TEST_FILEPATH);
158159
file.setContent(TEST_CONTENT);
159-
RepositoryFile createdFile = gitLabApi.getRepositoryFileApi().createFile(project.getId(), file, TEST_BRANCH_NAME, "Testing createFile().");
160+
RepositoryFileResponse createdFile = gitLabApi.getRepositoryFileApi().createFile(project.getId(), file, TEST_BRANCH_NAME, "Testing createFile().");
160161
assertNotNull(createdFile);
161162

162163
gitLabApi.getRepositoryFileApi().deleteFile(project.getId(), TEST_FILEPATH, TEST_BRANCH_NAME, "Testing deleteFile().");
@@ -175,7 +176,7 @@ public void testCreateFileWithEmptyContent() throws GitLabApiException {
175176
RepositoryFile file = new RepositoryFile();
176177
file.setFilePath(TEST_FILEPATH);
177178
file.setContent("");
178-
RepositoryFile createdFile = gitLabApi.getRepositoryFileApi().createFile(project.getId(), file, TEST_BRANCH_NAME, "Testing createFile().");
179+
RepositoryFileResponse createdFile = gitLabApi.getRepositoryFileApi().createFile(project.getId(), file, TEST_BRANCH_NAME, "Testing createFile().");
179180
assertNotNull(createdFile);
180181

181182
gitLabApi.getRepositoryFileApi().deleteFile(project.getId(), TEST_FILEPATH, TEST_BRANCH_NAME, "Testing deleteFile().");
@@ -194,7 +195,7 @@ public void testUpdateFile() throws GitLabApiException {
194195
RepositoryFile file = new RepositoryFile();
195196
file.setFilePath(TEST_FILEPATH);
196197
file.setContent(TEST_CONTENT);
197-
RepositoryFile createdFile = gitLabApi.getRepositoryFileApi().createFile(project.getId(), file, TEST_BRANCH_NAME, "Testing createFile().");
198+
RepositoryFileResponse createdFile = gitLabApi.getRepositoryFileApi().createFile(project.getId(), file, TEST_BRANCH_NAME, "Testing createFile().");
198199
assertNotNull(createdFile);
199200

200201
Optional<RepositoryFile> optionalFile = gitLabApi.getRepositoryFileApi().getOptionalFile(project, TEST_FILEPATH, TEST_BRANCH_NAME);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"file_path": "app/project.rb",
3+
"branch": "master"
4+
}

0 commit comments

Comments
 (0)