Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
17d2703f
Commit
17d2703f
authored
Jul 24, 2018
by
Greg Messner
Browse files
Added support to fetch file information without fetching the file content (#224).
parent
fbfa873e
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/AbstractApi.java
View file @
17d2703f
...
...
@@ -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.
...
...
src/main/java/org/gitlab4j/api/GitLabApiClient.java
View file @
17d2703f
...
...
@@ -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.
...
...
src/main/java/org/gitlab4j/api/RepositoryFileApi.java
View file @
17d2703f
...
...
@@ -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
();
...
...
src/test/java/org/gitlab4j/api/TestRepositoryApi.java
View file @
17d2703f
...
...
@@ -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
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment