Commit 17d2703f authored by Greg Messner's avatar Greg Messner
Browse files

Added support to fetch file information without fetching the file content (#224).

parent fbfa873e
......@@ -194,6 +194,24 @@ public abstract class AbstractApi implements Constants {
}
}
/**
* Perform an HTTP HEAD call with the specified query parameters and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param queryParams multivalue map of request parameters
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException if any exception occurs during execution
*/
protected Response head(Response.Status expectedStatus, MultivaluedMap<String, String> queryParams, Object... pathArgs) throws GitLabApiException {
try {
return validate(getApiClient().head(queryParams, pathArgs), expectedStatus);
} catch (Exception e) {
throw handle(e);
}
}
/**
* Perform an HTTP POST call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
......
......@@ -390,6 +390,32 @@ public class GitLabApiClient {
return (invocation(url, queryParams, accepts).get());
}
/**
* Perform an HTTP HEAD call with the specified query parameters and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams multivalue map of request parameters
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected Response head(MultivaluedMap<String, String> queryParams, Object... pathArgs) throws IOException {
URL url = getApiUrl(pathArgs);
return (head(queryParams, url));
}
/**
* Perform an HTTP HEAD call with the specified query parameters and URL, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param queryParams multivalue map of request parameters
* @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint
*/
protected Response head(MultivaluedMap<String, String> queryParams, URL url) {
return (invocation(url, queryParams).head());
}
/**
* Perform an HTTP POST call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
......
......@@ -22,17 +22,69 @@ public class RepositoryFileApi extends AbstractApi {
super(gitLabApi);
}
/**
* Get information on a file in the repository. Allows you to receive information about file in repository like name, size.
* Only works with GitLab 11.1.0+, returns an empty object for earlier versions of GitLab.
*
* HEAD /projects/:id/repository/files
*
* @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
* @param filePath (required) - Full path to the file. Ex. lib/class.rb
* @param ref (required) - The name of branch, tag or commit
* @return a RepositoryFile instance with the file info
* @throws GitLabApiException if any exception occurs
* @since GitLab-11.1.0
*/
public RepositoryFile getFileInfo(Object projectIdOrPath, String filePath, String ref) throws GitLabApiException {
Form form = new Form();
addFormParam(form, "ref", ref, true);
Response response = head(Response.Status.OK, form.asMap(),
"projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(filePath));
RepositoryFile file = new RepositoryFile();
file.setBlobId(response.getHeaderString("X-Gitlab-Blob-Id"));
file.setCommitId(response.getHeaderString("X-Gitlab-Commit-Id"));
file.setEncoding(response.getHeaderString("X-Gitlab-Encoding"));
file.setFileName(response.getHeaderString("X-Gitlab-File-Name"));
file.setFilePath(response.getHeaderString("X-Gitlab-File-Path"));
file.setLastCommitId(response.getHeaderString("X-Gitlab-Last-Commit-Id"));
file.setRef(response.getHeaderString("X-Gitlab-Ref"));
String sizeStr = response.getHeaderString("X-Gitlab-Size");
file.setSize(sizeStr != null ? Integer.valueOf(sizeStr) : -1);
return (file);
}
/**
* Get file from repository. Allows you to receive information about file in repository like name, size, content.
* Note that file content is Base64 encoded.
*
* GET /projects/:id/repository/files
*
* @param filePath (required) - Full path to new file. Ex. lib/class.rb
* @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
* @param filePath (required) - Full path to the file. Ex. lib/class.rb
* @param ref (required) - The name of branch, tag or commit
* @return a RepositoryFile instance with the file info and file content
* @throws GitLabApiException if any exception occurs
*/
public RepositoryFile getFile(Object projectIdOrPath, String filePath, String ref) throws GitLabApiException {
return (getFile(projectIdOrPath, filePath, ref, true));
}
/**
* Get file from repository. Allows you to receive information about file in repository like name, size, content.
* Note that file content is Base64 encoded.
*
* GET /projects/:id/repository/files
*
* @param filePath (required) - Full path to the file. Ex. lib/class.rb
* @param projectId (required) - the project ID
* @param ref (required) - The name of branch, tag or commit
* @return a RepositoryFile instance with the file info
* @return a RepositoryFile instance with the file info and file content
* @throws GitLabApiException if any exception occurs
* @deprecated Will be removed in version 5.0, replaced by {@link #getFile(Object, String, String)}
*/
public RepositoryFile getFile(String filePath, Integer projectId, String ref) throws GitLabApiException {
......@@ -40,10 +92,31 @@ public class RepositoryFileApi extends AbstractApi {
return (getFileV3(filePath, projectId, ref));
}
return (getFile(projectId, filePath, ref, true));
}
/**
* Get file from repository. Allows you to receive information about file in repository like name, size, and optionally content.
* Note that file content is Base64 encoded.
*
* GET /projects/:id/repository/files
*
* @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
* @param filePath (required) - Full path to the file. Ex. lib/class.rb
* @param ref (required) - The name of branch, tag or commit
* @param includeContent if true will also fetch file content
* @return a RepositoryFile instance with the file info and optionally file content
* @throws GitLabApiException if any exception occurs
*/
public RepositoryFile getFile(Object projectIdOrPath, String filePath, String ref, boolean includeContent) throws GitLabApiException {
if (!includeContent) {
return (getFileInfo(projectIdOrPath, filePath, ref));
}
Form form = new Form();
addFormParam(form, "ref", ref, true);
Response response = get(Response.Status.OK, form.asMap(),
"projects", projectId, "repository", "files", urlEncode(filePath));
Response response = get(Response.Status.OK, form.asMap(), "projects", getProjectIdOrPath(projectIdOrPath), "repository", "files", urlEncode(filePath));
return (response.readEntity(RepositoryFile.class));
}
......@@ -58,6 +131,7 @@ public class RepositoryFileApi extends AbstractApi {
* @param ref (required) - The name of branch, tag or commit
* @return a RepositoryFile instance with the file info
* @throws GitLabApiException if any exception occurs
* @deprecated Will be removed in version 5.0
*/
protected RepositoryFile getFileV3(String filePath, Integer projectId, String ref) throws GitLabApiException {
Form form = new Form();
......
......@@ -202,6 +202,16 @@ public class TestRepositoryApi {
Files.delete(target);
}
@Test
public void testRepositoryFileGetFile() throws GitLabApiException, IOException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
RepositoryFile fileInfo = gitLabApi.getRepositoryFileApi().getFileInfo(project.getId(), "README", "master");
assertNotNull(fileInfo);
}
@Test
public void testCompare() throws GitLabApiException {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment