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
1d8b0a75
Commit
1d8b0a75
authored
May 14, 2015
by
六幻
Browse files
Add api: Create, read, update and delete repository files.
parent
48c8adfa
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/main/java/com/messners/gitlab/api/RepositoryFileApi.java
0 → 100644
View file @
1d8b0a75
package
com.messners.gitlab.api
;
import
javax.ws.rs.core.Form
;
import
javax.ws.rs.core.Response
;
import
com.messners.gitlab.api.models.RepositoryFile
;
/**
* This class provides an entry point to all the GitLab API repository files calls.
*
* @author lonfee88 <lonfee88@gmail.com>
*/
public
class
RepositoryFileApi
extends
AbstractApi
{
public
RepositoryFileApi
(
GitLabApi
gitLabApi
)
{
super
(
gitLabApi
);
}
/**
* 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
* @param projectId
* @param ref
* @return
* @throws GitLabApiException
*/
public
RepositoryFile
getFile
(
String
filePath
,
Integer
projectId
,
String
ref
)
throws
GitLabApiException
{
Form
form
=
new
Form
();
addFormParam
(
form
,
"file_path"
,
filePath
,
true
);
addFormParam
(
form
,
"ref"
,
ref
,
true
);
Response
response
=
get
(
Response
.
Status
.
OK
,
form
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
);
return
(
response
.
readEntity
(
RepositoryFile
.
class
));
}
/**
* Create new file in repository
*
* POST /projects/:id/repository/files
*
* @param file
* @param projectId
* @param branchName
* @param commitMessage
* @return
* @throws GitLabApiException
*/
public
RepositoryFile
createFile
(
RepositoryFile
file
,
Integer
projectId
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
Form
formData
=
file2form
(
file
,
branchName
,
commitMessage
);
Response
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"projects"
,
projectId
,
"repository"
,
"files"
);
return
(
response
.
readEntity
(
RepositoryFile
.
class
));
}
/**
* Update existing file in repository
*
* PUT /projects/:id/repository/files
*
* @param file
* @param projectId
* @param branchName
* @param commitMessage
* @return
* @throws GitLabApiException
*/
public
RepositoryFile
updateFile
(
RepositoryFile
file
,
Integer
projectId
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
Form
form
=
file2form
(
file
,
branchName
,
commitMessage
);
Response
response
=
put
(
Response
.
Status
.
OK
,
form
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
);
return
(
response
.
readEntity
(
RepositoryFile
.
class
));
}
/**
* Delete existing file in repository
*
* DELETE /projects/:id/repository/files
*
* @param filePath
* @param projectId
* @param branchName
* @param commitMessage
* @throws GitLabApiException
*/
public
void
deleteFile
(
String
filePath
,
Integer
projectId
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
if
(
filePath
==
null
)
{
throw
new
RuntimeException
(
"filePath cannot be null"
);
}
Form
form
=
new
Form
();
addFormParam
(
form
,
"file_path"
,
filePath
,
true
);
addFormParam
(
form
,
"branch_name"
,
branchName
,
true
);
addFormParam
(
form
,
"commit_message"
,
commitMessage
,
true
);
delete
(
Response
.
Status
.
OK
,
form
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
);
}
private
Form
file2form
(
RepositoryFile
file
,
String
branchName
,
String
commitMessage
){
Form
form
=
new
Form
();
addFormParam
(
form
,
"file_path"
,
file
.
getFilePath
(),
true
);
addFormParam
(
form
,
"branch_name"
,
branchName
,
true
);
addFormParam
(
form
,
"encoding"
,
file
.
getEncoding
(),
false
);
addFormParam
(
form
,
"content"
,
file
.
getContent
(),
true
);
addFormParam
(
form
,
"commit_message"
,
commitMessage
,
true
);
return
form
;
}
}
src/main/java/com/messners/gitlab/api/models/RepositoryFile.java
0 → 100644
View file @
1d8b0a75
package
com.messners.gitlab.api.models
;
import
javax.xml.bind.annotation.XmlAccessType
;
import
javax.xml.bind.annotation.XmlAccessorType
;
import
javax.xml.bind.annotation.XmlRootElement
;
@XmlRootElement
@XmlAccessorType
(
XmlAccessType
.
FIELD
)
public
class
RepositoryFile
{
private
String
fileName
;
//file name only, Ex. lib/class.rb
private
String
filePath
;
//full path to file. Ex. lib/class.rb
private
Integer
size
;
private
String
encoding
;
private
String
content
;
private
String
ref
;
private
String
blobId
;
private
String
commitId
;
public
String
getFileName
()
{
return
fileName
;
}
public
void
setFileName
(
String
fileName
)
{
this
.
fileName
=
fileName
;
}
public
String
getFilePath
()
{
return
filePath
;
}
public
void
setFilePath
(
String
filePath
)
{
this
.
filePath
=
filePath
;
}
public
Integer
getSize
()
{
return
size
;
}
public
void
setSize
(
Integer
size
)
{
this
.
size
=
size
;
}
public
String
getEncoding
()
{
return
encoding
;
}
public
void
setEncoding
(
String
encoding
)
{
this
.
encoding
=
encoding
;
}
public
String
getContent
()
{
return
content
;
}
public
void
setContent
(
String
content
)
{
this
.
content
=
content
;
}
public
String
getRef
()
{
return
ref
;
}
public
void
setRef
(
String
ref
)
{
this
.
ref
=
ref
;
}
public
String
getBlobId
()
{
return
blobId
;
}
public
void
setBlobId
(
String
blobId
)
{
this
.
blobId
=
blobId
;
}
public
String
getCommitId
()
{
return
commitId
;
}
public
void
setCommitId
(
String
commitId
)
{
this
.
commitId
=
commitId
;
}
}
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