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
f6edda9a
Commit
f6edda9a
authored
Aug 21, 2018
by
Greg Messner
Browse files
Added encode/decode convenience methods for content (#240).
parent
0398df3c
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/models/RepositoryFile.java
View file @
f6edda9a
package
org.gitlab4j.api.models
;
package
org.gitlab4j.api.models
;
import
java.util.Base64
;
import
javax.xml.bind.annotation.XmlAccessType
;
import
javax.xml.bind.annotation.XmlAccessType
;
import
javax.xml.bind.annotation.XmlAccessorType
;
import
javax.xml.bind.annotation.XmlAccessorType
;
import
javax.xml.bind.annotation.XmlRootElement
;
import
javax.xml.bind.annotation.XmlRootElement
;
import
com.fasterxml.jackson.annotation.JsonIgnore
;
@XmlRootElement
@XmlRootElement
@XmlAccessorType
(
XmlAccessType
.
FIELD
)
@XmlAccessorType
(
XmlAccessType
.
FIELD
)
public
class
RepositoryFile
{
public
class
RepositoryFile
{
...
@@ -90,4 +94,73 @@ public class RepositoryFile {
...
@@ -90,4 +94,73 @@ public class RepositoryFile {
public
void
setLastCommitId
(
String
lastCommitId
)
{
public
void
setLastCommitId
(
String
lastCommitId
)
{
this
.
lastCommitId
=
lastCommitId
;
this
.
lastCommitId
=
lastCommitId
;
}
}
/**
* Returns the content as a String, base64 decoding it if necessary.
* For binary files it is recommended to use getDecodedContentAsBytes()
*
* @return the content as a String, base64 decoding it if necessary
*/
@JsonIgnore
public
String
getDecodedContentAsString
()
{
if
(
content
==
null
)
{
return
(
null
);
}
if
(
"base64"
.
equalsIgnoreCase
(
encoding
))
{
return
(
new
String
(
Base64
.
getDecoder
().
decode
(
content
)));
}
return
(
content
);
}
/**
* Returns the content as a byte array, decoding from base64 if necessary.
* For String content it is recommended to use getDecodedContent().
*
* @return the content as a byte array, decoding from base64 if necessary
*/
@JsonIgnore
public
byte
[]
getDecodedContentAsBytes
()
{
if
(
content
==
null
)
{
return
(
null
);
}
if
(
"base64"
.
equalsIgnoreCase
(
encoding
))
{
return
(
Base64
.
getDecoder
().
decode
(
content
));
}
return
(
content
.
getBytes
());
}
/**
* Encodes the provided String using Base64 and sets it as the content. The encoding
* property of this instance will be set to base64.
*
* @param content the String content to encode and set as the base64 encoded String content
*/
@JsonIgnore
public
void
encodeAndSetContent
(
String
content
)
{
encodeAndSetContent
(
content
!=
null
?
content
.
getBytes
()
:
null
);
}
/**
* Encodes the provided byte array using Base64 and sets it as the content. The encoding
* property of this instance will be set to base64.
*
* @param content the byte[] content to encode and set as the base64 encoded String content
*/
@JsonIgnore
public
void
encodeAndSetContent
(
byte
[]
byteContent
)
{
if
(
byteContent
==
null
)
{
byteContent
=
null
;
return
;
}
this
.
content
=
Base64
.
getEncoder
().
encodeToString
(
byteContent
);
encoding
=
"base64"
;
}
}
}
src/test/java/org/gitlab4j/api/TestRepositoryFileApi.java
View file @
f6edda9a
...
@@ -44,7 +44,6 @@ public class TestRepositoryFileApi {
...
@@ -44,7 +44,6 @@ public class TestRepositoryFileApi {
private
static
final
String
TEST_NAMESPACE
;
private
static
final
String
TEST_NAMESPACE
;
private
static
final
String
TEST_HOST_URL
;
private
static
final
String
TEST_HOST_URL
;
private
static
final
String
TEST_PRIVATE_TOKEN
;
private
static
final
String
TEST_PRIVATE_TOKEN
;
static
{
static
{
TEST_NAMESPACE
=
TestUtils
.
getProperty
(
"TEST_NAMESPACE"
);
TEST_NAMESPACE
=
TestUtils
.
getProperty
(
"TEST_NAMESPACE"
);
TEST_PROJECT_NAME
=
TestUtils
.
getProperty
(
"TEST_PROJECT_NAME"
);
TEST_PROJECT_NAME
=
TestUtils
.
getProperty
(
"TEST_PROJECT_NAME"
);
...
@@ -52,6 +51,9 @@ public class TestRepositoryFileApi {
...
@@ -52,6 +51,9 @@ public class TestRepositoryFileApi {
TEST_PRIVATE_TOKEN
=
TestUtils
.
getProperty
(
"TEST_PRIVATE_TOKEN"
);
TEST_PRIVATE_TOKEN
=
TestUtils
.
getProperty
(
"TEST_PRIVATE_TOKEN"
);
}
}
private
static
final
String
TEST_CONTENT
=
"This is some content to test file content 1234567890 !@#$%^&()."
;
private
static
final
String
TEST_ADDITIONAL_CONTENT
=
"\n\nThis is an additional line"
;
private
static
final
String
TEST_BRANCH_NAME
=
"feature/test_branch_for_files"
;
private
static
final
String
TEST_BRANCH_NAME
=
"feature/test_branch_for_files"
;
private
static
final
String
TEST_FILEPATH
=
"test-file.txt"
;
private
static
final
String
TEST_FILEPATH
=
"test-file.txt"
;
private
static
GitLabApi
gitLabApi
;
private
static
GitLabApi
gitLabApi
;
...
@@ -183,7 +185,7 @@ public class TestRepositoryFileApi {
...
@@ -183,7 +185,7 @@ public class TestRepositoryFileApi {
RepositoryFile
file
=
new
RepositoryFile
();
RepositoryFile
file
=
new
RepositoryFile
();
file
.
setFilePath
(
TEST_FILEPATH
);
file
.
setFilePath
(
TEST_FILEPATH
);
file
.
setContent
(
"This is a test file."
);
file
.
setContent
(
TEST_CONTENT
);
RepositoryFile
createdFile
=
gitLabApi
.
getRepositoryFileApi
().
createFile
(
file
,
project
.
getId
(),
TEST_BRANCH_NAME
,
"Testing createFile()."
);
RepositoryFile
createdFile
=
gitLabApi
.
getRepositoryFileApi
().
createFile
(
file
,
project
.
getId
(),
TEST_BRANCH_NAME
,
"Testing createFile()."
);
assertNotNull
(
createdFile
);
assertNotNull
(
createdFile
);
...
@@ -209,4 +211,39 @@ public class TestRepositoryFileApi {
...
@@ -209,4 +211,39 @@ public class TestRepositoryFileApi {
gitLabApi
.
getRepositoryFileApi
().
deleteFile
(
TEST_FILEPATH
,
project
.
getId
(),
TEST_BRANCH_NAME
,
"Testing deleteFile()."
);
gitLabApi
.
getRepositoryFileApi
().
deleteFile
(
TEST_FILEPATH
,
project
.
getId
(),
TEST_BRANCH_NAME
,
"Testing deleteFile()."
);
gitLabApi
.
getRepositoryApi
().
deleteBranch
(
project
.
getId
(),
TEST_BRANCH_NAME
);
gitLabApi
.
getRepositoryApi
().
deleteBranch
(
project
.
getId
(),
TEST_BRANCH_NAME
);
}
}
@Test
public
void
testUpdateFile
()
throws
GitLabApiException
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME
);
assertNotNull
(
project
);
Branch
branch
=
gitLabApi
.
getRepositoryApi
().
createBranch
(
project
.
getId
(),
TEST_BRANCH_NAME
,
"master"
);
assertNotNull
(
branch
);
RepositoryFile
file
=
new
RepositoryFile
();
file
.
setFilePath
(
TEST_FILEPATH
);
file
.
setContent
(
TEST_CONTENT
);
RepositoryFile
createdFile
=
gitLabApi
.
getRepositoryFileApi
().
createFile
(
file
,
project
.
getId
(),
TEST_BRANCH_NAME
,
"Testing createFile()."
);
assertNotNull
(
createdFile
);
Optional
<
RepositoryFile
>
optionalFile
=
gitLabApi
.
getRepositoryFileApi
().
getOptionalFile
(
project
,
TEST_FILEPATH
,
TEST_BRANCH_NAME
);
assertTrue
(
optionalFile
.
isPresent
());
RepositoryFile
newFile
=
optionalFile
.
get
();
assertEquals
(
TEST_CONTENT
,
newFile
.
getDecodedContentAsString
());
String
newContent
=
TEST_CONTENT
+
TEST_ADDITIONAL_CONTENT
;
file
=
new
RepositoryFile
();
file
.
setFilePath
(
TEST_FILEPATH
);
file
.
encodeAndSetContent
(
newContent
);
gitLabApi
.
getRepositoryFileApi
().
updateFile
(
file
,
project
.
getId
(),
TEST_BRANCH_NAME
,
"Testing updateFile()."
);
optionalFile
=
gitLabApi
.
getRepositoryFileApi
().
getOptionalFile
(
project
,
TEST_FILEPATH
,
TEST_BRANCH_NAME
);
assertTrue
(
optionalFile
.
isPresent
());
RepositoryFile
updatedFile
=
optionalFile
.
get
();
assertEquals
(
newContent
,
updatedFile
.
getDecodedContentAsString
());
gitLabApi
.
getRepositoryFileApi
().
deleteFile
(
TEST_FILEPATH
,
project
.
getId
(),
TEST_BRANCH_NAME
,
"Testing deleteFile()."
);
gitLabApi
.
getRepositoryApi
().
deleteBranch
(
project
.
getId
(),
TEST_BRANCH_NAME
);
}
}
}
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