Skip to content

Commit 17d2703

Browse files
committed
Added support to fetch file information without fetching the file content (#224).
1 parent fbfa873 commit 17d2703

File tree

4 files changed

+132
-4
lines changed

4 files changed

+132
-4
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,24 @@ protected Response get(Response.Status expectedStatus, MultivaluedMap<String, St
194194
}
195195
}
196196

197+
/**
198+
* Perform an HTTP HEAD call with the specified query parameters and path objects, returning
199+
* a ClientResponse instance with the data returned from the endpoint.
200+
*
201+
* @param expectedStatus the HTTP status that should be returned from the server
202+
* @param queryParams multivalue map of request parameters
203+
* @param pathArgs variable list of arguments used to build the URI
204+
* @return a ClientResponse instance with the data returned from the endpoint
205+
* @throws GitLabApiException if any exception occurs during execution
206+
*/
207+
protected Response head(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object... pathArgs) throws GitLabApiException {
208+
try {
209+
return validate(getApiClient().head(queryParams, pathArgs), expectedStatus);
210+
} catch (Exception e) {
211+
throw handle(e);
212+
}
213+
}
214+
197215
/**
198216
* Perform an HTTP POST call with the specified form data and path objects, returning
199217
* a ClientResponse instance with the data returned from the endpoint.

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,32 @@ protected Response getWithAccepts(MultivaluedMap<String, String> queryParams, UR
390390
return (invocation(url, queryParams, accepts).get());
391391
}
392392

393+
/**
394+
* Perform an HTTP HEAD call with the specified query parameters and path objects, returning
395+
* a ClientResponse instance with the data returned from the endpoint.
396+
*
397+
* @param queryParams multivalue map of request parameters
398+
* @param pathArgs variable list of arguments used to build the URI
399+
* @return a ClientResponse instance with the data returned from the endpoint
400+
* @throws IOException if an error occurs while constructing the URL
401+
*/
402+
protected Response head(MultivaluedMap<String, String> queryParams, Object... pathArgs) throws IOException {
403+
URL url = getApiUrl(pathArgs);
404+
return (head(queryParams, url));
405+
}
406+
407+
/**
408+
* Perform an HTTP HEAD call with the specified query parameters and URL, returning
409+
* a ClientResponse instance with the data returned from the endpoint.
410+
*
411+
* @param queryParams multivalue map of request parameters
412+
* @param url the fully formed path to the GitLab API endpoint
413+
* @return a ClientResponse instance with the data returned from the endpoint
414+
*/
415+
protected Response head(MultivaluedMap<String, String> queryParams, URL url) {
416+
return (invocation(url, queryParams).head());
417+
}
418+
393419
/**
394420
* Perform an HTTP POST call with the specified form data and path objects, returning
395421
* a ClientResponse instance with the data returned from the endpoint.

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

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,101 @@ public RepositoryFileApi(GitLabApi gitLabApi) {
2222
super(gitLabApi);
2323
}
2424

25+
/**
26+
* Get information on a file in the repository. Allows you to receive information about file in repository like name, size.
27+
* Only works with GitLab 11.1.0+, returns an empty object for earlier versions of GitLab.
28+
*
29+
* HEAD /projects/:id/repository/files
30+
*
31+
* @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
32+
* @param filePath (required) - Full path to the file. Ex. lib/class.rb
33+
* @param ref (required) - The name of branch, tag or commit
34+
* @return a RepositoryFile instance with the file info
35+
* @throws GitLabApiException if any exception occurs
36+
* @since GitLab-11.1.0
37+
*/
38+
public RepositoryFile getFileInfo(Object projectIdOrPath, String filePath, String ref) throws GitLabApiException {
39+
40+
Form form = new Form();
41+
addFormParam(form, "ref", ref, true);
42+
Response response = head(Response.Status.OK, form.asMap(),
43+
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(filePath));
44+
45+
RepositoryFile file = new RepositoryFile();
46+
file.setBlobId(response.getHeaderString("X-Gitlab-Blob-Id"));
47+
file.setCommitId(response.getHeaderString("X-Gitlab-Commit-Id"));
48+
file.setEncoding(response.getHeaderString("X-Gitlab-Encoding"));
49+
file.setFileName(response.getHeaderString("X-Gitlab-File-Name"));
50+
file.setFilePath(response.getHeaderString("X-Gitlab-File-Path"));
51+
file.setLastCommitId(response.getHeaderString("X-Gitlab-Last-Commit-Id"));
52+
file.setRef(response.getHeaderString("X-Gitlab-Ref"));
53+
54+
String sizeStr = response.getHeaderString("X-Gitlab-Size");
55+
file.setSize(sizeStr != null ? Integer.valueOf(sizeStr) : -1);
56+
57+
return (file);
58+
}
59+
2560
/**
2661
* Get file from repository. Allows you to receive information about file in repository like name, size, content.
2762
* Note that file content is Base64 encoded.
2863
*
2964
* GET /projects/:id/repository/files
3065
*
31-
* @param filePath (required) - Full path to new file. Ex. lib/class.rb
66+
* @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
67+
* @param filePath (required) - Full path to the file. Ex. lib/class.rb
68+
* @param ref (required) - The name of branch, tag or commit
69+
* @return a RepositoryFile instance with the file info and file content
70+
* @throws GitLabApiException if any exception occurs
71+
*/
72+
public RepositoryFile getFile(Object projectIdOrPath, String filePath, String ref) throws GitLabApiException {
73+
return (getFile(projectIdOrPath, filePath, ref, true));
74+
}
75+
76+
/**
77+
* Get file from repository. Allows you to receive information about file in repository like name, size, content.
78+
* Note that file content is Base64 encoded.
79+
*
80+
* GET /projects/:id/repository/files
81+
*
82+
* @param filePath (required) - Full path to the file. Ex. lib/class.rb
3283
* @param projectId (required) - the project ID
3384
* @param ref (required) - The name of branch, tag or commit
34-
* @return a RepositoryFile instance with the file info
85+
* @return a RepositoryFile instance with the file info and file content
3586
* @throws GitLabApiException if any exception occurs
87+
* @deprecated Will be removed in version 5.0, replaced by {@link #getFile(Object, String, String)}
3688
*/
3789
public RepositoryFile getFile(String filePath, Integer projectId, String ref) throws GitLabApiException {
3890

3991
if (isApiVersion(ApiVersion.V3)) {
4092
return (getFileV3(filePath, projectId, ref));
4193
}
4294

95+
return (getFile(projectId, filePath, ref, true));
96+
}
97+
98+
/**
99+
* Get file from repository. Allows you to receive information about file in repository like name, size, and optionally content.
100+
* Note that file content is Base64 encoded.
101+
*
102+
* GET /projects/:id/repository/files
103+
*
104+
* @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
105+
* @param filePath (required) - Full path to the file. Ex. lib/class.rb
106+
* @param ref (required) - The name of branch, tag or commit
107+
* @param includeContent if true will also fetch file content
108+
* @return a RepositoryFile instance with the file info and optionally file content
109+
* @throws GitLabApiException if any exception occurs
110+
*/
111+
public RepositoryFile getFile(Object projectIdOrPath, String filePath, String ref, boolean includeContent) throws GitLabApiException {
112+
113+
if (!includeContent) {
114+
return (getFileInfo(projectIdOrPath, filePath, ref));
115+
}
116+
43117
Form form = new Form();
44118
addFormParam(form, "ref", ref, true);
45-
Response response = get(Response.Status.OK, form.asMap(),
46-
"projects", projectId, "repository", "files", urlEncode(filePath));
119+
Response response = get(Response.Status.OK, form.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(filePath));
47120
return (response.readEntity(RepositoryFile.class));
48121
}
49122

@@ -58,6 +131,7 @@ public RepositoryFile getFile(String filePath, Integer projectId, String ref) th
58131
* @param ref (required) - The name of branch, tag or commit
59132
* @return a RepositoryFile instance with the file info
60133
* @throws GitLabApiException if any exception occurs
134+
* @deprecated Will be removed in version 5.0
61135
*/
62136
protected RepositoryFile getFileV3(String filePath, Integer projectId, String ref) throws GitLabApiException {
63137
Form form = new Form();

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,16 @@ public void testRepositoryFileViaInputStream() throws GitLabApiException, IOExce
202202
Files.delete(target);
203203
}
204204

205+
@Test
206+
public void testRepositoryFileGetFile() throws GitLabApiException, IOException {
207+
208+
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
209+
assertNotNull(project);
210+
211+
RepositoryFile fileInfo = gitLabApi.getRepositoryFileApi().getFileInfo(project.getId(), "README", "master");
212+
assertNotNull(fileInfo);
213+
}
214+
205215
@Test
206216
public void testCompare() throws GitLabApiException {
207217

0 commit comments

Comments
 (0)