Skip to content

Commit 4788373

Browse files
committed
Added getOptionalFile() and getOptionalFileInfo() methods (#224).
1 parent 19094fc commit 4788373

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.InputStream;
66
import java.nio.file.Files;
77
import java.nio.file.StandardCopyOption;
8+
import java.util.Optional;
89

910
import javax.ws.rs.core.Form;
1011
import javax.ws.rs.core.MediaType;
@@ -22,6 +23,26 @@ public RepositoryFileApi(GitLabApi gitLabApi) {
2223
super(gitLabApi);
2324
}
2425

26+
/**
27+
* Get an Optional instance with the value holding information on a file in the repository.
28+
* Allows you to receive information about file in repository like name, size.
29+
* Only works with GitLab 11.1.0+, value will be an empty object for earlier versions of GitLab.
30+
*
31+
* HEAD /projects/:id/repository/files
32+
*
33+
* @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
34+
* @param filePath (required) - Full path to the file. Ex. lib/class.rb
35+
* @param ref (required) - The name of branch, tag or commit
36+
* @return an Optional instance with the specified RepositoryFile as a value
37+
*/
38+
public Optional<RepositoryFile> getOptionalFileInfo(Object projectIdOrPath, String filePath, String ref) {
39+
try {
40+
return (Optional.ofNullable(getFileInfo(projectIdOrPath, filePath, ref)));
41+
} catch (GitLabApiException glae) {
42+
return (GitLabApi.createOptionalFromException(glae));
43+
}
44+
}
45+
2546
/**
2647
* Get information on a file in the repository. Allows you to receive information about file in repository like name, size.
2748
* Only works with GitLab 11.1.0+, returns an empty object for earlier versions of GitLab.
@@ -57,6 +78,26 @@ public RepositoryFile getFileInfo(Object projectIdOrPath, String filePath, Strin
5778
return (file);
5879
}
5980

81+
/**
82+
* Get an Optional instance with the value holding information and content for a file in the repository.
83+
* Allows you to receive information about file in repository like name, size, and content.
84+
* Only works with GitLab 11.1.0+, value will be an empty object for earlier versions of GitLab.
85+
*
86+
* HEAD /projects/:id/repository/files
87+
*
88+
* @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
89+
* @param filePath (required) - Full path to the file. Ex. lib/class.rb
90+
* @param ref (required) - The name of branch, tag or commit
91+
* @return an Optional instance with the specified RepositoryFile as a value
92+
*/
93+
public Optional<RepositoryFile> getOptionalFile(Object projectIdOrPath, String filePath, String ref) {
94+
try {
95+
return (Optional.ofNullable(getFile(projectIdOrPath, filePath, ref, true)));
96+
} catch (GitLabApiException glae) {
97+
return (GitLabApi.createOptionalFromException(glae));
98+
}
99+
}
100+
60101
/**
61102
* Get file from repository. Allows you to receive information about file in repository like name, size, content.
62103
* Note that file content is Base64 encoded.

0 commit comments

Comments
 (0)