Skip to content

Commit fff854e

Browse files
committed
Added support for v3 getRawFile() (#282).
1 parent 257a06e commit fff854e

File tree

1 file changed

+133
-28
lines changed

1 file changed

+133
-28
lines changed

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

Lines changed: 133 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public RepositoryFileApi(GitLabApi gitLabApi) {
3434
* @param filePath (required) - Full path to the file. Ex. lib/class.rb
3535
* @param ref (required) - The name of branch, tag or commit
3636
* @return an Optional instance with the specified RepositoryFile as a value
37+
* @since GitLab-11.1.0
3738
*/
3839
public Optional<RepositoryFile> getOptionalFileInfo(Object projectIdOrPath, String filePath, String ref) {
3940
try {
@@ -127,13 +128,14 @@ public RepositoryFile getFile(Object projectIdOrPath, String filePath, String re
127128
* @throws GitLabApiException if any exception occurs
128129
* @deprecated Will be removed in version 5.0, replaced by {@link #getFile(Object, String, String)}
129130
*/
131+
@Deprecated
130132
public RepositoryFile getFile(String filePath, Integer projectId, String ref) throws GitLabApiException {
131133

132134
if (isApiVersion(ApiVersion.V3)) {
133135
return (getFileV3(filePath, projectId, ref));
136+
} else {
137+
return (getFile(projectId, filePath, ref, true));
134138
}
135-
136-
return (getFile(projectId, filePath, ref, true));
137139
}
138140

139141
/**
@@ -174,6 +176,7 @@ public RepositoryFile getFile(Object projectIdOrPath, String filePath, String re
174176
* @throws GitLabApiException if any exception occurs
175177
* @deprecated Will be removed in version 5.0
176178
*/
179+
@Deprecated
177180
protected RepositoryFile getFileV3(String filePath, Integer projectId, String ref) throws GitLabApiException {
178181
Form form = new Form();
179182
addFormParam(form, "file_path", filePath, true);
@@ -193,25 +196,52 @@ protected RepositoryFile getFileV3(String filePath, Integer projectId, String re
193196
* content (required) - File content
194197
* commit_message (required) - Commit message
195198
*
199+
* @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
196200
* @param file a ReposityoryFile instance with info for the file to create
197-
* @param projectId the project ID
198201
* @param branchName the name of branch
199202
* @param commitMessage the commit message
200203
* @return a RepositoryFile instance with the created file info
201204
* @throws GitLabApiException if any exception occurs
202205
*/
203-
public RepositoryFile createFile(RepositoryFile file, Integer projectId, String branchName, String commitMessage) throws GitLabApiException {
206+
public RepositoryFile createFile(Object projectIdOrPath, RepositoryFile file, String branchName, String commitMessage) throws GitLabApiException {
207+
204208
Form formData = createForm(file, branchName, commitMessage);
205209
Response response;
206210
if (isApiVersion(ApiVersion.V3)) {
207-
response = post(Response.Status.CREATED, formData, "projects", projectId, "repository", "files");
211+
response = post(Response.Status.CREATED, formData,
212+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "files");
208213
} else {
209-
response = post(Response.Status.CREATED, formData, "projects", projectId, "repository", "files", urlEncode(file.getFilePath()));
214+
response = post(Response.Status.CREATED, formData,
215+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(file.getFilePath()));
210216
}
211217

212218
return (response.readEntity(RepositoryFile.class));
213219
}
214220

221+
/**
222+
* Create new file in repository
223+
*
224+
* POST /projects/:id/repository/files
225+
*
226+
* file_path (required) - Full path to new file. Ex. lib/class.rb
227+
* branch_name (required) - The name of branch
228+
* encoding (optional) - 'text' or 'base64'. Text is default.
229+
* content (required) - File content
230+
* commit_message (required) - Commit message
231+
*
232+
* @param file a ReposityoryFile instance with info for the file to create
233+
* @param projectId the project ID
234+
* @param branchName the name of branch
235+
* @param commitMessage the commit message
236+
* @return a RepositoryFile instance with the created file info
237+
* @throws GitLabApiException if any exception occurs
238+
* @deprecated Will be removed in version 5.0, replaced by {@link #createFile(Object, RepositoryFile, String, String)}
239+
*/
240+
@Deprecated
241+
public RepositoryFile createFile(RepositoryFile file, Integer projectId, String branchName, String commitMessage) throws GitLabApiException {
242+
return (createFile(projectId, file, branchName, commitMessage));
243+
}
244+
215245
/**
216246
* Update existing file in repository
217247
*
@@ -223,25 +253,52 @@ public RepositoryFile createFile(RepositoryFile file, Integer projectId, String
223253
* content (required) - File content
224254
* commit_message (required) - Commit message
225255
*
256+
* @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
226257
* @param file a ReposityoryFile instance with info for the file to update
227-
* @param projectId the project ID
228258
* @param branchName the name of branch
229259
* @param commitMessage the commit message
230260
* @return a RepositoryFile instance with the updated file info
231261
* @throws GitLabApiException if any exception occurs
232262
*/
233-
public RepositoryFile updateFile(RepositoryFile file, Integer projectId, String branchName, String commitMessage) throws GitLabApiException {
263+
public RepositoryFile updateFile(Object projectIdOrPath, RepositoryFile file, String branchName, String commitMessage) throws GitLabApiException {
264+
234265
Form formData = createForm(file, branchName, commitMessage);
235266
Response response;
236267
if (isApiVersion(ApiVersion.V3)) {
237-
response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "files");
268+
response = put(Response.Status.OK, formData.asMap(),
269+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "files");
238270
} else {
239-
response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "files", urlEncode(file.getFilePath()));
271+
response = put(Response.Status.OK, formData.asMap(),
272+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(file.getFilePath()));
240273
}
241274

242275
return (response.readEntity(RepositoryFile.class));
243276
}
244277

278+
/**
279+
* Update existing file in repository
280+
*
281+
* PUT /projects/:id/repository/files
282+
*
283+
* file_path (required) - Full path to new file. Ex. lib/class.rb
284+
* branch_name (required) - The name of branch
285+
* encoding (optional) - 'text' or 'base64'. Text is default.
286+
* content (required) - File content
287+
* commit_message (required) - Commit message
288+
*
289+
* @param file a ReposityoryFile instance with info for the file to update
290+
* @param projectId the project ID
291+
* @param branchName the name of branch
292+
* @param commitMessage the commit message
293+
* @return a RepositoryFile instance with the updated file info
294+
* @throws GitLabApiException if any exception occurs
295+
* @deprecated Will be removed in version 5.0, replaced by {@link #updateFile(Object, RepositoryFile, String, String)}
296+
*/
297+
@Deprecated
298+
public RepositoryFile updateFile(RepositoryFile file, Integer projectId, String branchName, String commitMessage) throws GitLabApiException {
299+
return (updateFile(projectId, file, branchName, commitMessage));
300+
}
301+
245302
/**
246303
* Delete existing file in repository
247304
*
@@ -251,13 +308,13 @@ public RepositoryFile updateFile(RepositoryFile file, Integer projectId, String
251308
* branch_name (required) - The name of branch
252309
* commit_message (required) - Commit message
253310
*
311+
* @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
254312
* @param filePath full path to new file. Ex. lib/class.rb
255-
* @param projectId the project ID
256313
* @param branchName the name of branch
257314
* @param commitMessage the commit message
258315
* @throws GitLabApiException if any exception occurs
259316
*/
260-
public void deleteFile(String filePath, Integer projectId, String branchName, String commitMessage) throws GitLabApiException {
317+
public void deleteFile(Object projectIdOrPath, String filePath, String branchName, String commitMessage) throws GitLabApiException {
261318

262319
if (filePath == null) {
263320
throw new RuntimeException("filePath cannot be null");
@@ -270,17 +327,42 @@ public void deleteFile(String filePath, Integer projectId, String branchName, St
270327

271328
if (isApiVersion(ApiVersion.V3)) {
272329
addFormParam(form, "file_path", filePath, true);
273-
delete(expectedStatus, form.asMap(), "projects", projectId, "repository", "files");
330+
delete(expectedStatus, form.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files");
274331
} else {
275-
delete(expectedStatus, form.asMap(), "projects", projectId, "repository", "files", urlEncode(filePath));
332+
delete(expectedStatus, form.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(filePath));
276333
}
277334
}
278335

336+
/**
337+
* Delete existing file in repository
338+
*
339+
* DELETE /projects/:id/repository/files
340+
*
341+
* file_path (required) - Full path to file. Ex. lib/class.rb
342+
* branch_name (required) - The name of branch
343+
* commit_message (required) - Commit message
344+
*
345+
* @param filePath full path to new file. Ex. lib/class.rb
346+
* @param projectId the project ID
347+
* @param branchName the name of branch
348+
* @param commitMessage the commit message
349+
* @throws GitLabApiException if any exception occurs
350+
* @deprecated Will be removed in version 5.0, replaced by {@link #deleteFile(Object, RepositoryFile, String, String)}
351+
*/
352+
@Deprecated
353+
public void deleteFile(String filePath, Integer projectId, String branchName, String commitMessage) throws GitLabApiException {
354+
deleteFile(projectId, filePath, branchName, commitMessage);
355+
}
356+
279357
/**
280358
* Get the raw file for the file by commit sha and path. Thye file will be saved to the specified directory.
281359
* If the file already exists in the directory it will be overwritten.
282360
*
283-
* GET /projects/:id/repository/archive
361+
* V3:
362+
* GET /projects/:id/repository/blobs/:sha
363+
*
364+
* V4:
365+
* GET /projects/:id/repository/files/:filepath
284366
*
285367
* @param projectId the ID of the project
286368
* @param commitOrBranchName the commit or branch name to get the file for
@@ -289,48 +371,71 @@ public void deleteFile(String filePath, Integer projectId, String branchName, St
289371
* @return a File instance pointing to the download of the specified file
290372
* @throws GitLabApiException if any exception occurs
291373
*/
292-
public File getRawFile(Integer projectId, String commitOrBranchName, String filepath, File directory) throws GitLabApiException {
374+
public File getRawFile(Object projectIdOrPath, String commitOrBranchName, String filepath, File directory) throws GitLabApiException {
293375

294-
Form formData = new GitLabApiForm().withParam("ref", commitOrBranchName, true);
295-
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD,
296-
"projects", projectId, "repository", "files", urlEncode(filepath), "raw");
376+
InputStream in = getRawFile(projectIdOrPath, commitOrBranchName, filepath);
297377

298378
try {
299379

300-
if (directory == null)
380+
if (directory == null) {
301381
directory = new File(System.getProperty("java.io.tmpdir"));
382+
}
302383

303384
String filename = new File(filepath).getName();
304385
File file = new File(directory, filename);
305386

306-
InputStream in = response.readEntity(InputStream.class);
307387
Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
308388
return (file);
309389

310390
} catch (IOException ioe) {
311391
throw new GitLabApiException(ioe);
392+
} finally {
393+
try {
394+
in.close();
395+
} catch (IOException ignore) {
396+
}
312397
}
313398
}
314399

315400
/**
316401
* Get the raw file contents for a file by commit sha and path.
317402
*
403+
* V3:
318404
* GET /projects/:id/repository/blobs/:sha
319405
*
320-
* @param projectId the ID of the project
406+
* V4:
407+
* GET /projects/:id/repository/files/:filepath
408+
*
409+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
321410
* @param commitOrBranchName the commit or branch name to get the file contents for
322411
* @param filepath the path of the file to get
323412
* @return an InputStream to read the raw file from
324413
* @throws GitLabApiException if any exception occurs
325414
*/
326-
public InputStream getRawFile(Integer projectId, String commitOrBranchName, String filepath) throws GitLabApiException {
327-
Form formData = new GitLabApiForm().withParam("ref", commitOrBranchName, true);
328-
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD,
329-
"projects", projectId, "repository", "files", urlEncode(filepath), "raw");
330-
return (response.readEntity(InputStream.class));
415+
public InputStream getRawFile(Object projectIdOrPath, String commitOrBranchName, String filepath) throws GitLabApiException {
416+
417+
if (isApiVersion(ApiVersion.V3)) {
418+
Form formData = new GitLabApiForm().withParam("file_path", filepath, true);
419+
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD,
420+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "blobs", commitOrBranchName);
421+
return (response.readEntity(InputStream.class));
422+
} else {
423+
Form formData = new GitLabApiForm().withParam("ref", commitOrBranchName, true);
424+
Response response = getWithAccepts(Response.Status.OK, formData.asMap(), MediaType.MEDIA_TYPE_WILDCARD,
425+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(filepath), "raw");
426+
return (response.readEntity(InputStream.class));
427+
}
331428
}
332429

333-
private Form createForm(RepositoryFile file, String branchName, String commitMessage) {
430+
/**
431+
* Gets the query params based on the API version.
432+
*
433+
* @param file the RepositoryFile instance with the info for the query params
434+
* @param branchName the branch name
435+
* @param commitMessage the commit message
436+
* @return a Form instance with the correct query params.
437+
*/
438+
protected Form createForm(RepositoryFile file, String branchName, String commitMessage) {
334439

335440
Form form = new Form();
336441
if (isApiVersion(ApiVersion.V3)) {

0 commit comments

Comments
 (0)