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
fff854e3
Commit
fff854e3
authored
Dec 12, 2018
by
Greg Messner
Browse files
Added support for v3 getRawFile() (#282).
parent
257a06ef
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/RepositoryFileApi.java
View file @
fff854e3
...
@@ -34,6 +34,7 @@ public class RepositoryFileApi extends AbstractApi {
...
@@ -34,6 +34,7 @@ public class RepositoryFileApi extends AbstractApi {
* @param filePath (required) - Full path to the file. Ex. lib/class.rb
* @param filePath (required) - Full path to the file. Ex. lib/class.rb
* @param ref (required) - The name of branch, tag or commit
* @param ref (required) - The name of branch, tag or commit
* @return an Optional instance with the specified RepositoryFile as a value
* @return an Optional instance with the specified RepositoryFile as a value
* @since GitLab-11.1.0
*/
*/
public
Optional
<
RepositoryFile
>
getOptionalFileInfo
(
Object
projectIdOrPath
,
String
filePath
,
String
ref
)
{
public
Optional
<
RepositoryFile
>
getOptionalFileInfo
(
Object
projectIdOrPath
,
String
filePath
,
String
ref
)
{
try
{
try
{
...
@@ -127,13 +128,14 @@ public class RepositoryFileApi extends AbstractApi {
...
@@ -127,13 +128,14 @@ public class RepositoryFileApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
* @deprecated Will be removed in version 5.0, replaced by {@link #getFile(Object, String, String)}
* @deprecated Will be removed in version 5.0, replaced by {@link #getFile(Object, String, String)}
*/
*/
@Deprecated
public
RepositoryFile
getFile
(
String
filePath
,
Integer
projectId
,
String
ref
)
throws
GitLabApiException
{
public
RepositoryFile
getFile
(
String
filePath
,
Integer
projectId
,
String
ref
)
throws
GitLabApiException
{
if
(
isApiVersion
(
ApiVersion
.
V3
))
{
if
(
isApiVersion
(
ApiVersion
.
V3
))
{
return
(
getFileV3
(
filePath
,
projectId
,
ref
));
return
(
getFileV3
(
filePath
,
projectId
,
ref
));
}
else
{
return
(
getFile
(
projectId
,
filePath
,
ref
,
true
));
}
}
return
(
getFile
(
projectId
,
filePath
,
ref
,
true
));
}
}
/**
/**
...
@@ -174,6 +176,7 @@ public class RepositoryFileApi extends AbstractApi {
...
@@ -174,6 +176,7 @@ public class RepositoryFileApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
* @deprecated Will be removed in version 5.0
* @deprecated Will be removed in version 5.0
*/
*/
@Deprecated
protected
RepositoryFile
getFileV3
(
String
filePath
,
Integer
projectId
,
String
ref
)
throws
GitLabApiException
{
protected
RepositoryFile
getFileV3
(
String
filePath
,
Integer
projectId
,
String
ref
)
throws
GitLabApiException
{
Form
form
=
new
Form
();
Form
form
=
new
Form
();
addFormParam
(
form
,
"file_path"
,
filePath
,
true
);
addFormParam
(
form
,
"file_path"
,
filePath
,
true
);
...
@@ -193,25 +196,52 @@ public class RepositoryFileApi extends AbstractApi {
...
@@ -193,25 +196,52 @@ public class RepositoryFileApi extends AbstractApi {
* content (required) - File content
* content (required) - File content
* commit_message (required) - Commit message
* commit_message (required) - Commit message
*
*
* @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
* @param file a ReposityoryFile instance with info for the file to create
* @param file a ReposityoryFile instance with info for the file to create
* @param projectId the project ID
* @param branchName the name of branch
* @param branchName the name of branch
* @param commitMessage the commit message
* @param commitMessage the commit message
* @return a RepositoryFile instance with the created file info
* @return a RepositoryFile instance with the created file info
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
RepositoryFile
createFile
(
RepositoryFile
file
,
Integer
projectId
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
public
RepositoryFile
createFile
(
Object
projectIdOrPath
,
RepositoryFile
file
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
Form
formData
=
createForm
(
file
,
branchName
,
commitMessage
);
Form
formData
=
createForm
(
file
,
branchName
,
commitMessage
);
Response
response
;
Response
response
;
if
(
isApiVersion
(
ApiVersion
.
V3
))
{
if
(
isApiVersion
(
ApiVersion
.
V3
))
{
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"projects"
,
projectId
,
"repository"
,
"files"
);
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"repository"
,
"files"
);
}
else
{
}
else
{
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"projects"
,
projectId
,
"repository"
,
"files"
,
urlEncode
(
file
.
getFilePath
()));
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"repository"
,
"files"
,
urlEncode
(
file
.
getFilePath
()));
}
}
return
(
response
.
readEntity
(
RepositoryFile
.
class
));
return
(
response
.
readEntity
(
RepositoryFile
.
class
));
}
}
/**
* Create new file in repository
*
* POST /projects/:id/repository/files
*
* file_path (required) - Full path to new file. Ex. lib/class.rb
* branch_name (required) - The name of branch
* encoding (optional) - 'text' or 'base64'. Text is default.
* content (required) - File content
* commit_message (required) - Commit message
*
* @param file a ReposityoryFile instance with info for the file to create
* @param projectId the project ID
* @param branchName the name of branch
* @param commitMessage the commit message
* @return a RepositoryFile instance with the created file info
* @throws GitLabApiException if any exception occurs
* @deprecated Will be removed in version 5.0, replaced by {@link #createFile(Object, RepositoryFile, String, String)}
*/
@Deprecated
public
RepositoryFile
createFile
(
RepositoryFile
file
,
Integer
projectId
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
return
(
createFile
(
projectId
,
file
,
branchName
,
commitMessage
));
}
/**
/**
* Update existing file in repository
* Update existing file in repository
*
*
...
@@ -223,25 +253,52 @@ public class RepositoryFileApi extends AbstractApi {
...
@@ -223,25 +253,52 @@ public class RepositoryFileApi extends AbstractApi {
* content (required) - File content
* content (required) - File content
* commit_message (required) - Commit message
* commit_message (required) - Commit message
*
*
* @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
* @param file a ReposityoryFile instance with info for the file to update
* @param file a ReposityoryFile instance with info for the file to update
* @param projectId the project ID
* @param branchName the name of branch
* @param branchName the name of branch
* @param commitMessage the commit message
* @param commitMessage the commit message
* @return a RepositoryFile instance with the updated file info
* @return a RepositoryFile instance with the updated file info
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
RepositoryFile
updateFile
(
RepositoryFile
file
,
Integer
projectId
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
public
RepositoryFile
updateFile
(
Object
projectIdOrPath
,
RepositoryFile
file
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
Form
formData
=
createForm
(
file
,
branchName
,
commitMessage
);
Form
formData
=
createForm
(
file
,
branchName
,
commitMessage
);
Response
response
;
Response
response
;
if
(
isApiVersion
(
ApiVersion
.
V3
))
{
if
(
isApiVersion
(
ApiVersion
.
V3
))
{
response
=
put
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
);
response
=
put
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"repository"
,
"files"
);
}
else
{
}
else
{
response
=
put
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
,
urlEncode
(
file
.
getFilePath
()));
response
=
put
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"repository"
,
"files"
,
urlEncode
(
file
.
getFilePath
()));
}
}
return
(
response
.
readEntity
(
RepositoryFile
.
class
));
return
(
response
.
readEntity
(
RepositoryFile
.
class
));
}
}
/**
* Update existing file in repository
*
* PUT /projects/:id/repository/files
*
* file_path (required) - Full path to new file. Ex. lib/class.rb
* branch_name (required) - The name of branch
* encoding (optional) - 'text' or 'base64'. Text is default.
* content (required) - File content
* commit_message (required) - Commit message
*
* @param file a ReposityoryFile instance with info for the file to update
* @param projectId the project ID
* @param branchName the name of branch
* @param commitMessage the commit message
* @return a RepositoryFile instance with the updated file info
* @throws GitLabApiException if any exception occurs
* @deprecated Will be removed in version 5.0, replaced by {@link #updateFile(Object, RepositoryFile, String, String)}
*/
@Deprecated
public
RepositoryFile
updateFile
(
RepositoryFile
file
,
Integer
projectId
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
return
(
updateFile
(
projectId
,
file
,
branchName
,
commitMessage
));
}
/**
/**
* Delete existing file in repository
* Delete existing file in repository
*
*
...
@@ -251,13 +308,13 @@ public class RepositoryFileApi extends AbstractApi {
...
@@ -251,13 +308,13 @@ public class RepositoryFileApi extends AbstractApi {
* branch_name (required) - The name of branch
* branch_name (required) - The name of branch
* commit_message (required) - Commit message
* commit_message (required) - Commit message
*
*
* @param projectIdOrPath the id, path of the project, or a Project instance holding the project ID or path
* @param filePath full path to new file. Ex. lib/class.rb
* @param filePath full path to new file. Ex. lib/class.rb
* @param projectId the project ID
* @param branchName the name of branch
* @param branchName the name of branch
* @param commitMessage the commit message
* @param commitMessage the commit message
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
void
deleteFile
(
String
filePath
,
Integer
projectId
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
public
void
deleteFile
(
Object
projectIdOrPath
,
String
filePath
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
if
(
filePath
==
null
)
{
if
(
filePath
==
null
)
{
throw
new
RuntimeException
(
"filePath cannot be null"
);
throw
new
RuntimeException
(
"filePath cannot be null"
);
...
@@ -270,17 +327,42 @@ public class RepositoryFileApi extends AbstractApi {
...
@@ -270,17 +327,42 @@ public class RepositoryFileApi extends AbstractApi {
if
(
isApiVersion
(
ApiVersion
.
V3
))
{
if
(
isApiVersion
(
ApiVersion
.
V3
))
{
addFormParam
(
form
,
"file_path"
,
filePath
,
true
);
addFormParam
(
form
,
"file_path"
,
filePath
,
true
);
delete
(
expectedStatus
,
form
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
);
delete
(
expectedStatus
,
form
.
asMap
(),
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"repository"
,
"files"
);
}
else
{
}
else
{
delete
(
expectedStatus
,
form
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
,
urlEncode
(
filePath
));
delete
(
expectedStatus
,
form
.
asMap
(),
"projects"
,
getProjectIdOrPath
(
projectId
OrPath
)
,
"repository"
,
"files"
,
urlEncode
(
filePath
));
}
}
}
}
/**
* Delete existing file in repository
*
* DELETE /projects/:id/repository/files
*
* file_path (required) - Full path to file. Ex. lib/class.rb
* branch_name (required) - The name of branch
* commit_message (required) - Commit message
*
* @param filePath full path to new file. Ex. lib/class.rb
* @param projectId the project ID
* @param branchName the name of branch
* @param commitMessage the commit message
* @throws GitLabApiException if any exception occurs
* @deprecated Will be removed in version 5.0, replaced by {@link #deleteFile(Object, RepositoryFile, String, String)}
*/
@Deprecated
public
void
deleteFile
(
String
filePath
,
Integer
projectId
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
deleteFile
(
projectId
,
filePath
,
branchName
,
commitMessage
);
}
/**
/**
* Get the raw file for the file by commit sha and path. Thye file will be saved to the specified directory.
* Get the raw file for the file by commit sha and path. Thye file will be saved to the specified directory.
* If the file already exists in the directory it will be overwritten.
* If the file already exists in the directory it will be overwritten.
*
*
* GET /projects/:id/repository/archive
* V3:
* GET /projects/:id/repository/blobs/:sha
*
* V4:
* GET /projects/:id/repository/files/:filepath
*
*
* @param projectId the ID of the project
* @param projectId the ID of the project
* @param commitOrBranchName the commit or branch name to get the file for
* @param commitOrBranchName the commit or branch name to get the file for
...
@@ -289,48 +371,71 @@ public class RepositoryFileApi extends AbstractApi {
...
@@ -289,48 +371,71 @@ public class RepositoryFileApi extends AbstractApi {
* @return a File instance pointing to the download of the specified file
* @return a File instance pointing to the download of the specified file
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
File
getRawFile
(
Integer
projectId
,
String
commitOrBranchName
,
String
filepath
,
File
directory
)
throws
GitLabApiException
{
public
File
getRawFile
(
Object
projectId
OrPath
,
String
commitOrBranchName
,
String
filepath
,
File
directory
)
throws
GitLabApiException
{
Form
formData
=
new
GitLabApiForm
().
withParam
(
"ref"
,
commitOrBranchName
,
true
);
InputStream
in
=
getRawFile
(
projectIdOrPath
,
commitOrBranchName
,
filepath
);
Response
response
=
getWithAccepts
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
MediaType
.
MEDIA_TYPE_WILDCARD
,
"projects"
,
projectId
,
"repository"
,
"files"
,
urlEncode
(
filepath
),
"raw"
);
try
{
try
{
if
(
directory
==
null
)
if
(
directory
==
null
)
{
directory
=
new
File
(
System
.
getProperty
(
"java.io.tmpdir"
));
directory
=
new
File
(
System
.
getProperty
(
"java.io.tmpdir"
));
}
String
filename
=
new
File
(
filepath
).
getName
();
String
filename
=
new
File
(
filepath
).
getName
();
File
file
=
new
File
(
directory
,
filename
);
File
file
=
new
File
(
directory
,
filename
);
InputStream
in
=
response
.
readEntity
(
InputStream
.
class
);
Files
.
copy
(
in
,
file
.
toPath
(),
StandardCopyOption
.
REPLACE_EXISTING
);
Files
.
copy
(
in
,
file
.
toPath
(),
StandardCopyOption
.
REPLACE_EXISTING
);
return
(
file
);
return
(
file
);
}
catch
(
IOException
ioe
)
{
}
catch
(
IOException
ioe
)
{
throw
new
GitLabApiException
(
ioe
);
throw
new
GitLabApiException
(
ioe
);
}
finally
{
try
{
in
.
close
();
}
catch
(
IOException
ignore
)
{
}
}
}
}
}
/**
/**
* Get the raw file contents for a file by commit sha and path.
* Get the raw file contents for a file by commit sha and path.
*
*
* V3:
* GET /projects/:id/repository/blobs/:sha
* GET /projects/:id/repository/blobs/:sha
*
*
* @param projectId the ID of the project
* V4:
* GET /projects/:id/repository/files/:filepath
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param commitOrBranchName the commit or branch name to get the file contents for
* @param commitOrBranchName the commit or branch name to get the file contents for
* @param filepath the path of the file to get
* @param filepath the path of the file to get
* @return an InputStream to read the raw file from
* @return an InputStream to read the raw file from
* @throws GitLabApiException if any exception occurs
* @throws GitLabApiException if any exception occurs
*/
*/
public
InputStream
getRawFile
(
Integer
projectId
,
String
commitOrBranchName
,
String
filepath
)
throws
GitLabApiException
{
public
InputStream
getRawFile
(
Object
projectIdOrPath
,
String
commitOrBranchName
,
String
filepath
)
throws
GitLabApiException
{
Form
formData
=
new
GitLabApiForm
().
withParam
(
"ref"
,
commitOrBranchName
,
true
);
Response
response
=
getWithAccepts
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
MediaType
.
MEDIA_TYPE_WILDCARD
,
if
(
isApiVersion
(
ApiVersion
.
V3
))
{
"projects"
,
projectId
,
"repository"
,
"files"
,
urlEncode
(
filepath
),
"raw"
);
Form
formData
=
new
GitLabApiForm
().
withParam
(
"file_path"
,
filepath
,
true
);
return
(
response
.
readEntity
(
InputStream
.
class
));
Response
response
=
getWithAccepts
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
MediaType
.
MEDIA_TYPE_WILDCARD
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"repository"
,
"blobs"
,
commitOrBranchName
);
return
(
response
.
readEntity
(
InputStream
.
class
));
}
else
{
Form
formData
=
new
GitLabApiForm
().
withParam
(
"ref"
,
commitOrBranchName
,
true
);
Response
response
=
getWithAccepts
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
MediaType
.
MEDIA_TYPE_WILDCARD
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"repository"
,
"files"
,
urlEncode
(
filepath
),
"raw"
);
return
(
response
.
readEntity
(
InputStream
.
class
));
}
}
}
private
Form
createForm
(
RepositoryFile
file
,
String
branchName
,
String
commitMessage
)
{
/**
* Gets the query params based on the API version.
*
* @param file the RepositoryFile instance with the info for the query params
* @param branchName the branch name
* @param commitMessage the commit message
* @return a Form instance with the correct query params.
*/
protected
Form
createForm
(
RepositoryFile
file
,
String
branchName
,
String
commitMessage
)
{
Form
form
=
new
Form
();
Form
form
=
new
Form
();
if
(
isApiVersion
(
ApiVersion
.
V3
))
{
if
(
isApiVersion
(
ApiVersion
.
V3
))
{
...
...
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