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
c2f3c1b2
Commit
c2f3c1b2
authored
Jul 16, 2015
by
六幻
Browse files
Merge
https://github.com/gmessner/gitlab-api
merge
parents
302c26f7
be334cb3
Changes
6
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
c2f3c1b2
...
@@ -23,6 +23,7 @@ MergeRequestApi
...
@@ -23,6 +23,7 @@ MergeRequestApi
ProjectApi
ProjectApi
RepositoryApi
RepositoryApi
RepositoryFileApi
RepositoryFileApi
ServicesApi
SessionApi
SessionApi
UserApi
UserApi
```
```
...
@@ -63,6 +64,12 @@ RepositoryFileApi:
...
@@ -63,6 +64,12 @@ RepositoryFileApi:
RepositoryFile
file
=
gitLabApi
.
getRepositoryFileApi
().
getFile
(
"file-path"
,
1234
,
"ref"
);
RepositoryFile
file
=
gitLabApi
.
getRepositoryFileApi
().
getFile
(
"file-path"
,
1234
,
"ref"
);
```
```
ServicesApi:
```
java
// Activates the gitlab-ci service.
getLabApi
.
getServicesApi
().
setGitLabCI
(
"project-name"
,
"auth-token"
,
"project-ci-url"
);
```
SessionApi:
SessionApi:
```
java
```
java
// Log in to the GitLab server and get the session info
// Log in to the GitLab server and get the session info
...
...
pom.xml
View file @
c2f3c1b2
...
@@ -5,7 +5,7 @@
...
@@ -5,7 +5,7 @@
<groupId>
com.messners
</groupId>
<groupId>
com.messners
</groupId>
<artifactId>
gitlab-api
</artifactId>
<artifactId>
gitlab-api
</artifactId>
<packaging>
jar
</packaging>
<packaging>
jar
</packaging>
<version>
2.0.
2
-SNAPSHOT
</version>
<version>
2.0.
5
-SNAPSHOT
</version>
<name>
gitlab-api
</name>
<name>
gitlab-api
</name>
<description>
GitLab API provides a full featured Java API for working with GitLab repositories via the GitLab REST API
</description>
<description>
GitLab API provides a full featured Java API for working with GitLab repositories via the GitLab REST API
</description>
<url>
http://www.messners.com/#gitlab-api/gitlab-api.html
</url>
<url>
http://www.messners.com/#gitlab-api/gitlab-api.html
</url>
...
...
src/main/java/com/messners/gitlab/api/GitLabApi.java
View file @
c2f3c1b2
package
com.messners.gitlab.api
;
package
com.messners.gitlab.api
;
/**
/**
* This class is provides a simplified interface to a GitLab API server, and divides the API up into
* This class is provides a simplified interface to a GitLab API server, and divides the API up into
* a separate API class for each concern.
* a separate API class for each concern.
...
@@ -8,119 +7,113 @@ package com.messners.gitlab.api;
...
@@ -8,119 +7,113 @@ package com.messners.gitlab.api;
* @author Greg Messner <greg@messners.com>
* @author Greg Messner <greg@messners.com>
*/
*/
public
class
GitLabApi
{
public
class
GitLabApi
{
GitLabApiClient
apiClient
;
private
CommitsApi
commitsApi
;
private
GroupApi
groupApi
;
private
MergeRequestApi
mergeRequestApi
;
private
ProjectApi
projectApi
;
private
RepositoryApi
repositoryApi
;
private
SessionApi
sessoinApi
;
private
UserApi
userApi
;
private
RepositoryFileApi
repositoryFileApi
;
/**
GitLabApiClient
apiClient
;
* Logs into GitLab using provided {@code username} and {@code password}, and creates a new
private
CommitsApi
commitsApi
;
* {@code GitLabApi} instance using returned private token
private
GroupApi
groupApi
;
* @param url GitLab URL
private
MergeRequestApi
mergeRequestApi
;
* @param username user name for which private token should be obtained
private
ProjectApi
projectApi
;
* @param password password for a given {@code username}
private
RepositoryApi
repositoryApi
;
* @return new {@code GitLabApi} instance configured for a user-specific token
private
RepositoryFileApi
repositoryFileApi
;
*/
private
ServicesApi
servicesApi
;
static
public
GitLabApi
create
(
String
url
,
String
username
,
String
password
)
throws
GitLabApiException
{
private
SessionApi
sessoinApi
;
String
token
=
new
SessionApi
(
new
GitLabApi
(
url
,
null
)).
login
(
username
,
null
,
password
).
getPrivateToken
();
private
UserApi
userApi
;
return
new
GitLabApi
(
url
,
token
);
}
/**
* Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance using returned private token
*
* @param url GitLab URL
* @param username user name for which private token should be obtained
* @param password password for a given {@code username}
* @return new {@code GitLabApi} instance configured for a user-specific token
*/
static
public
GitLabApi
create
(
String
url
,
String
username
,
String
password
)
throws
GitLabApiException
{
String
token
=
new
SessionApi
(
new
GitLabApi
(
url
,
null
)).
login
(
username
,
null
,
password
).
getPrivateToken
();
return
new
GitLabApi
(
url
,
token
);
}
/**
* Constructs a GitLabApi instance set up to interact with the GitLab server
* specified by hostUrl.
*
* @param hostUrl
* @param privateToken
*/
public
GitLabApi
(
String
hostUrl
,
String
privateToken
)
{
apiClient
=
new
GitLabApiClient
(
hostUrl
,
privateToken
);
commitsApi
=
new
CommitsApi
(
this
);
groupApi
=
new
GroupApi
(
this
);
mergeRequestApi
=
new
MergeRequestApi
(
this
);
projectApi
=
new
ProjectApi
(
this
);
repositoryApi
=
new
RepositoryApi
(
this
);
servicesApi
=
new
ServicesApi
(
this
);
sessoinApi
=
new
SessionApi
(
this
);
userApi
=
new
UserApi
(
this
);
repositoryFileApi
=
new
RepositoryFileApi
(
this
);
}
/**
/**
* Constructs a GitLabApi instance set up to interact with the GitLab server
* Return the GitLabApiClient associated with this instance. This is used by all the sub API classes
* specified by hostUrl.
* to communicate with the GitLab API.
*
*
* @param hostUrl
* @return the GitLabApiClient associated with this instance
* @param privateToken
*/
*/
GitLabApiClient
getApiClient
()
{
public
GitLabApi
(
String
hostUrl
,
String
privateToken
)
{
return
(
apiClient
);
apiClient
=
new
GitLabApiClient
(
hostUrl
,
privateToken
);
}
commitsApi
=
new
CommitsApi
(
this
);
groupApi
=
new
GroupApi
(
this
);
mergeRequestApi
=
new
MergeRequestApi
(
this
);
projectApi
=
new
ProjectApi
(
this
);
repositoryApi
=
new
RepositoryApi
(
this
);
sessoinApi
=
new
SessionApi
(
this
);
userApi
=
new
UserApi
(
this
);
repositoryFileApi
=
new
RepositoryFileApi
(
this
);
}
/**
* Return the GitLabApiClient associated with this instance. This is used by all the sub API classes
* to communicate with the GitLab API.
*
* @return the GitLabApiClient associated with this instance
*/
GitLabApiClient
getApiClient
()
{
return
(
apiClient
);
}
/**
* Gets the CommitsApi instance owned by this GitLabApi instance. The CommitsApi is used
* to perform all commit related API calls.
*
* @return the CommitsApi instance owned by this GitLabApi instance
*/
public
CommitsApi
getCommitsApi
()
{
return
(
commitsApi
);
}
/**
* Gets the MergeRequestApi instance owned by this GitLabApi instance. The MergeRequestApi is used
* to perform all merge request related API calls.
*
* @return the MergeRequestApi instance owned by this GitLabApi instance
*/
public
MergeRequestApi
getMergeRequestApi
()
{
return
(
mergeRequestApi
);
}
/**
* Gets the GroupApi instance owned by this GitLabApi instance. The GroupApi is used
* to perform all group related API calls.
*
* @return the GroupApi instance owned by this GitLabApi instance
*/
public
GroupApi
getGroupApi
()
{
return
(
groupApi
);
}
/**
/**
* Gets the ProjectApi instance owned by this GitLabApi instance. The ProjectApi is used
* Gets the CommitsApi instance owned by this GitLabApi instance. The CommitsApi is used
* to perform all project related API calls.
* to perform all commit related API calls.
*
*
* @return the ProjectApi instance owned by this GitLabApi instance
* @return the CommitsApi instance owned by this GitLabApi instance
*/
*/
public
ProjectApi
getProjectApi
()
{
public
CommitsApi
getCommitsApi
()
{
return
(
projectApi
);
return
(
commitsApi
);
}
}
/**
* Gets the RepositoryApi instance owned by this GitLabApi instance. The RepositoryApi is used
* to perform all repository related API calls.
*
* @return the RepositoryApi instance owned by this GitLabApi instance
*/
public
RepositoryApi
getRepositoryApi
()
{
return
(
repositoryApi
);
}
/**
* Gets the MergeRequestApi instance owned by this GitLabApi instance. The MergeRequestApi is used
* to perform all merge request related API calls.
*
* @return the MergeRequestApi instance owned by this GitLabApi instance
*/
public
MergeRequestApi
getMergeRequestApi
()
{
return
(
mergeRequestApi
);
}
/**
/**
* Gets the RepositoryFileApi instance owned by this GitLabApi instance. The RepositoryFileApi is used
* Gets the GroupApi instance owned by this GitLabApi instance. The GroupApi is used
* to perform all group related API calls.
*
* @return the GroupApi instance owned by this GitLabApi instance
*/
public
GroupApi
getGroupApi
()
{
return
(
groupApi
);
}
/**
* Gets the ProjectApi instance owned by this GitLabApi instance. The ProjectApi is used
* to perform all project related API calls.
*
* @return the ProjectApi instance owned by this GitLabApi instance
*/
public
ProjectApi
getProjectApi
()
{
return
(
projectApi
);
}
/**
* Gets the RepositoryApi instance owned by this GitLabApi instance. The RepositoryApi is used
* to perform all repository related API calls.
*
* @return the RepositoryApi instance owned by this GitLabApi instance
*/
public
RepositoryApi
getRepositoryApi
()
{
return
(
repositoryApi
);
}
/**
* Gets the RepositoryFileApi instance owned by this GitLabApi instance. The RepositoryFileApi is used
* to perform all repository files related API calls.
* to perform all repository files related API calls.
*
*
* @return the RepositoryFileApi instance owned by this GitLabApi instance
* @return the RepositoryFileApi instance owned by this GitLabApi instance
...
@@ -129,25 +122,33 @@ public class GitLabApi {
...
@@ -129,25 +122,33 @@ public class GitLabApi {
return
repositoryFileApi
;
return
repositoryFileApi
;
}
}
/**
* Gets the ServicesApi instance owned by this GitLabApi instance. The ServicesApi is used
* to perform all services related API calls.
*
* @return the ServicesApi instance owned by this GitLabApi instance
*/
public
ServicesApi
getServicesApi
()
{
return
(
servicesApi
);
}
/**
/**
* Gets the SessionApi instance owned by this GitLabApi instance. The SessionApi is used
* Gets the SessionApi instance owned by this GitLabApi instance. The SessionApi is used
* to perform a login to the GitLab API.
* to perform a login to the GitLab API.
*
*
* @return the SessionApi instance owned by this GitLabApi instance
* @return the SessionApi instance owned by this GitLabApi instance
*/
*/
public
SessionApi
getSessionApi
()
{
public
SessionApi
getSessionApi
()
{
return
(
sessoinApi
);
return
(
sessoinApi
);
}
}
/**
/**
* Gets the UserApi instance owned by this GitLabApi instance. The UserApi is used
* Gets the UserApi instance owned by this GitLabApi instance. The UserApi is used
* to perform all user related API calls.
* to perform all user related API calls.
*
*
* @return the UserApi instance owned by this GitLabApi instance
* @return the UserApi instance owned by this GitLabApi instance
*/
*/
public
UserApi
getUserApi
()
{
public
UserApi
getUserApi
()
{
return
(
userApi
);
return
(
userApi
);
}
}
}
}
src/main/java/com/messners/gitlab/api/RepositoryApi.java
View file @
c2f3c1b2
...
@@ -9,7 +9,6 @@ import javax.ws.rs.core.GenericType;
...
@@ -9,7 +9,6 @@ import javax.ws.rs.core.GenericType;
import
javax.ws.rs.core.Response
;
import
javax.ws.rs.core.Response
;
import
java.util.List
;
import
java.util.List
;
/**
/**
* This class provides an entry point to all the GitLab API repository calls.
* This class provides an entry point to all the GitLab API repository calls.
*
*
...
@@ -17,125 +16,120 @@ import java.util.List;
...
@@ -17,125 +16,120 @@ import java.util.List;
*/
*/
public
class
RepositoryApi
extends
AbstractApi
{
public
class
RepositoryApi
extends
AbstractApi
{
public
RepositoryApi
(
GitLabApi
gitLabApi
)
{
public
RepositoryApi
(
GitLabApi
gitLabApi
)
{
super
(
gitLabApi
);
super
(
gitLabApi
);
}
}
/**
* Get a list of repository branches from a project, sorted by name alphabetically.
*
* GET /projects/:id/repository/branches
*
* @param projectId
* @return the list of repository branches for mthe specified project ID
* @throws GitLabApiException
*/
public
List
<
Branch
>
getBranches
(
Integer
projectId
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Branch
>>()
{}));
}
/**
/**
* Get a
single project
repository branch.
* Get a
list of
repository branch
es from a project, sorted by name alphabetically
.
*
*
* GET /projects/:id/repository/branches
/:branch
* GET /projects/:id/repository/branches
*
*
* @param projectId
* @param projectId
* @param branchName
* @return the list of repository branches for the specified project ID
* @return the branch info for the specified project ID/branch name pair
* @throws GitLabApiException
* @throws GitLabApiException
*/
*/
public
List
<
Branch
>
getBranches
(
Integer
projectId
)
throws
GitLabApiException
{
public
Branch
getBranch
(
Integer
projectId
,
String
branchName
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
);
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
,
branchName
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Branch
>>()
{
return
(
response
.
readEntity
(
Branch
.
class
));
}
));
}
}
/**
* Get a single project repository branch.
*
* GET /projects/:id/repository/branches/:branch
*
* @param projectId
* @param branchName
* @return the branch info for the specified project ID/branch name pair
* @throws GitLabApiException
*/
public
Branch
getBranch
(
Integer
projectId
,
String
branchName
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
,
branchName
);
return
(
response
.
readEntity
(
Branch
.
class
));
}
/**
/**
* Creates a branch for the project. Support as of version 6.8.x
* Creates a branch for the project. Support as of version 6.8.x
*
*
* POST /projects/:id/repository/branches
* POST /projects/:id/repository/branches
*
*
* @param projectId the project to create the branch for
* @param projectId the project to create the branch for
* @param branchName the name of the branch to create
* @param branchName the name of the branch to create
* @param ref Source to create the branch from, can be an existing branch, tag or commit SHA
* @param ref Source to create the branch from, can be an existing branch, tag or commit SHA
* @return
the branch info for the created branch
* @return the branch info for the created branch
* @throws GitLabApiException
* @throws GitLabApiException
*/
*/
public
Branch
createBranch
(
Integer
projectId
,
String
branchName
,
String
ref
)
throws
GitLabApiException
{
public
Branch
createBranch
(
Integer
projectId
,
String
branchName
,
String
ref
)
throws
GitLabApiException
{
Form
formData
=
new
Form
();
Form
formData
=
new
Form
();
formData
.
param
(
"branch_name "
,
branchName
);
formData
.
param
(
"branch_name "
,
branchName
);
formData
.
param
(
"ref "
,
ref
);
formData
.
param
(
"ref "
,
ref
);
Response
response
=
post
(
Response
.
Status
.
OK
,
formData
,
"projects"
,
projectId
,
"repository"
,
"branches"
);
Response
response
=
post
(
Response
.
Status
.
OK
,
formData
,
"projects"
,
projectId
,
"repository"
,
"branches"
);
return
(
response
.
readEntity
(
Branch
.
class
));
return
(
response
.
readEntity
(
Branch
.
class
));
}
}
/**
* Protects a single project repository branch. This is an idempotent function,
* protecting an already protected repository branch will not produce an error.
*
* PUT /projects/:id/repository/branches/:branch/protect
*
* @param projectId
* @param branchName
* @return the branch info for the protected branch
* @throws GitLabApiException
*/
public
Branch
protectBranch
(
Integer
projectId
,
String
branchName
)
throws
GitLabApiException
{
Response
response
=
put
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
,
branchName
,
"protect"
);
return
(
response
.
readEntity
(
Branch
.
class
));
}
/**
* Unprotects a single project repository branch. This is an idempotent function, unprotecting an
* already unprotected repository branch will not produce an error.
*
* PUT /projects/:id/repository/branches/:branch/unprotect
*
* @param projectId
* @param branchName
* @return the branch info for the unprotected branch
* @throws GitLabApiException
*/
public
Branch
unprotectBranch
(
Integer
projectId
,
String
branchName
)
throws
GitLabApiException
{
Response
response
=
put
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
,
branchName
,
"unprotect"
);
return
(
response
.
readEntity
(
Branch
.
class
));
}
/**
* Get a list of repository tags from a project, sorted by name in reverse alphabetical order.
*
* GET /projects/:id/repository/tags
*
* @param projectId
* @return the list of tags for the specified project ID
* @throws GitLabApiException
*/
public
List
<
Tag
>
getTags
(
Integer
projectId
)
throws
GitLabApiException
{
Response
response
=
put
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"tags"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Tag
>>()
{}));
}
/**
* Get a list of repository files and directories in a project.
*
* GET /projects/:id/repository/tree
*
* @param projectId
* @return a tree with the root directories and files of a project
* @throws GitLabApiException
*/
public
List
<
TreeItem
>
getTree
(
Integer
projectId
)
throws
GitLabApiException
{
return
this
.
getTree
(
projectId
,
"/"
,
"master"
);
}
/**
/**
* Protects a single project repository branch. This is an idempotent function,
* protecting an already protected repository branch will not produce an error.
*
* PUT /projects/:id/repository/branches/:branch/protect
*
* @param projectId
* @param branchName
* @return the branch info for the protected branch
* @throws GitLabApiException
*/
public
Branch
protectBranch
(
Integer
projectId
,
String
branchName
)
throws
GitLabApiException
{
Response
response
=
put
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
,
branchName
,
"protect"
);
return
(
response
.
readEntity
(
Branch
.
class
));
}
/**
* Unprotects a single project repository branch. This is an idempotent function, unprotecting an
* already unprotected repository branch will not produce an error.
*
* PUT /projects/:id/repository/branches/:branch/unprotect
*
* @param projectId
* @param branchName
* @return the branch info for the unprotected branch
* @throws GitLabApiException
*/
public
Branch
unprotectBranch
(
Integer
projectId
,
String
branchName
)
throws
GitLabApiException
{
Response
response
=
put
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"branches"
,
branchName
,
"unprotect"
);
return
(
response
.
readEntity
(
Branch
.
class
));
}
/**
* Get a list of repository tags from a project, sorted by name in reverse alphabetical order.
*
* GET /projects/:id/repository/tags
*
* @param projectId
* @return the list of tags for the specified project ID
* @throws GitLabApiException
*/
public
List
<
Tag
>
getTags
(
Integer
projectId
)
throws
GitLabApiException
{
Response
response
=
put
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"tags"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
Tag
>>()
{
}));
}
/**
* Get a list of repository files and directories in a project.
*
* GET /projects/:id/repository/tree
*
* @param projectId
* @return a tree with the root directories and files of a project
* @throws GitLabApiException
*/
public
List
<
TreeItem
>
getTree
(
Integer
projectId
)
throws
GitLabApiException
{
return
this
.
getTree
(
projectId
,
"/"
,
"master"
);
}
/**
* Get a list of repository files and directories in a project.
* Get a list of repository files and directories in a project.
*
*
* GET /projects/:id/repository/tree
* GET /projects/:id/repository/tree
...
@@ -143,49 +137,52 @@ public class RepositoryApi extends AbstractApi {
...
@@ -143,49 +137,52 @@ public class RepositoryApi extends AbstractApi {
* id (required) - The ID of a project
* id (required) - The ID of a project
* path (optional) - The path inside repository. Used to get contend of subdirectories
* path (optional) - The path inside repository. Used to get contend of subdirectories
* ref_name (optional) - The name of a repository branch or tag or if not given the default branch
* ref_name (optional) - The name of a repository branch or tag or if not given the default branch
*
* @param projectId
* @param filePath
* @param refName
* @return a tree with the directories and files of a project
* @return a tree with the directories and files of a project
* @throws GitLabApiException
* @throws GitLabApiException
*/
*/
public
List
<
TreeItem
>
getTree
(
Integer
projectId
,
String
filePath
,
String
refName
)
throws
GitLabApiException
{
public
List
<
TreeItem
>
getTree
(
Integer
projectId
,
String
filePath
,
String
refName
)
throws
GitLabApiException
{
Form
formData
=
new
Form
();
Form
formData
=
new
Form
();
addFormParam
(
formData
,
"id"
,
projectId
,
true
);
addFormParam
(
formData
,
"id"
,
projectId
,
true
);
addFormParam
(
formData
,
"path"
,
filePath
,
false
);
addFormParam
(
formData
,
"path"
,
filePath
,
false
);
addFormParam
(
formData
,
"ref_name"
,
refName
,
false
);
addFormParam
(
formData
,
"ref_name"
,
refName
,
false
);
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"tree"
);
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"tree"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
TreeItem
>>()
{}));
return
(
response
.
readEntity
(
new
GenericType
<
List
<
TreeItem
>>()
{
}));
}
}
/**
* Get the raw file contents for a file by commit sha and path.
*
* GET /projects/:id/repository/blobs/:sha
*
* @param projectId
* @param commitOrBranchName
* @return a string with the file content for the specified file
* @throws GitLabApiException
*/
public
String
getRawFileContent
(
Integer
projectId
,
String
commitOrBranchName
,
String
filepath
)
throws
GitLabApiException
{
Form
formData
=
new
Form
();
addFormParam
(
formData
,
"filepath"
,
filepath
,
true
);
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"blobs"
,
commitOrBranchName
);
return
(
response
.
readEntity
(
String
.
class
));
}
/**
/**
* Get the raw file contents for a file by commit sha and path.
* Get the raw file contents for a blob by blob SHA.
*
*
* GET /projects/:id/repository/blobs/:sha
* GET /projects/:id/repository/raw_blobs/:sha
*
*
* @param projectId
* @param projectId
* @param commitOrBranchName
* @param sha
* @return a string with the file content for the specified file
* @return the raw file contents for the blob
* @throws GitLabApiException
* @throws GitLabApiException
*/
*/
public
String
getRawFileContent
(
Integer
projectId
,
String
commitOrBranchName
,
String
filepath
)
throws
GitLabApiException
{
public
String
getRawBlobCotent
(
Integer
projectId
,
String
sha
)
throws
GitLabApiException
{
Form
formData
=
new
Form
();
addFormParam
(
formData
,
"filepath"
,
filepath
,
true
);
Response
response
=
get
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"blobs"
,
commitOrBranchName
);
return
(
response
.
readEntity
(
String
.
class
));
}
/**
* Get the raw file contents for a blob by blob SHA.
*
* GET /projects/:id/repository/raw_blobs/:sha
*
* @param projectId
* @param sha
* @return the raw file contents for the blob
* @throws GitLabApiException
*/
public
String
getRawBlobCotent
(
Integer
projectId
,
String
sha
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"raw_blobs"
,
sha
);
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"repository"
,
"raw_blobs"
,
sha
);
return
(
response
.
readEntity
(
String
.
class
));
return
(
response
.
readEntity
(
String
.
class
));
}
}
}
}
src/main/java/com/messners/gitlab/api/RepositoryFileApi.java
View file @
c2f3c1b2
...
@@ -5,7 +5,6 @@ import javax.ws.rs.core.Response;
...
@@ -5,7 +5,6 @@ import javax.ws.rs.core.Response;
import
com.messners.gitlab.api.models.RepositoryFile
;
import
com.messners.gitlab.api.models.RepositoryFile
;
/**
/**
* This class provides an entry point to all the GitLab API repository files calls.
* This class provides an entry point to all the GitLab API repository files calls.
*
*
...
@@ -13,95 +12,94 @@ import com.messners.gitlab.api.models.RepositoryFile;
...
@@ -13,95 +12,94 @@ import com.messners.gitlab.api.models.RepositoryFile;
*/
*/
public
class
RepositoryFileApi
extends
AbstractApi
{
public
class
RepositoryFileApi
extends
AbstractApi
{
public
RepositoryFileApi
(
GitLabApi
gitLabApi
)
{
public
RepositoryFileApi
(
GitLabApi
gitLabApi
)
{
super
(
gitLabApi
);
super
(
gitLabApi
);
}
}
/**
/**
* Get file from repository
* Get file from repository. Allows you to receive information about file in repository like name, size, content.
* Allows you to receive information about file in repository like name, size, content.
* Note that file content is Base64 encoded.
* Note that file content is Base64 encoded.
*
*
* GET /projects/:id/repository/files
* GET /projects/:id/repository/files
*
*
* @param file
_p
ath (required) - Full path to new file. Ex. lib/class.rb
* @param file
P
ath (required) - Full path to new file. Ex. lib/class.rb
* @param projectId
* @param projectId
* @param ref (required) - The name of branch, tag or commit
* @param ref (required) - The name of branch, tag or commit
* @return
* @return
a RepositoryFile instance with the file info
* @throws GitLabApiException
* @throws GitLabApiException
*/
*/
public
RepositoryFile
getFile
(
String
filePath
,
Integer
projectId
,
String
ref
)
throws
GitLabApiException
{
public
RepositoryFile
getFile
(
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
);
addFormParam
(
form
,
"ref"
,
ref
,
true
);
addFormParam
(
form
,
"ref"
,
ref
,
true
);
Response
response
=
get
(
Response
.
Status
.
OK
,
form
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
);
Response
response
=
get
(
Response
.
Status
.
OK
,
form
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
);
return
(
response
.
readEntity
(
RepositoryFile
.
class
));
return
(
response
.
readEntity
(
RepositoryFile
.
class
));
}
}
/**
/**
* Create new file in repository
* Create new file in repository
*
*
* POST /projects/:id/repository/files
* POST /projects/:id/repository/files
*
*
* file_path (required) - Full path to new file. Ex. lib/class.rb
* file_path (required) - Full path to new file. Ex. lib/class.rb
* branch_name (required) - The name of branch
* branch_name (required) - The name of branch
* encoding (optional) - 'text' or 'base64'. Text is default.
* encoding (optional) - 'text' or 'base64'. Text is default.
* content (required) - File content
* content (required) - File content
* commit_message (required) - Commit message
* commit_message (required) - Commit message
*
*
* @param file
* @param file
* @param projectId
* @param projectId
* @param branchName
* @param branchName
* @param commitMessage
* @param commitMessage
* @return
* @return
a RepositoryFile instance with the created file info
* @throws GitLabApiException
* @throws GitLabApiException
*/
*/
public
RepositoryFile
createFile
(
RepositoryFile
file
,
Integer
projectId
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
public
RepositoryFile
createFile
(
RepositoryFile
file
,
Integer
projectId
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
Form
formData
=
file2form
(
file
,
branchName
,
commitMessage
);
Form
formData
=
file2form
(
file
,
branchName
,
commitMessage
);
Response
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"projects"
,
projectId
,
"repository"
,
"files"
);
Response
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"projects"
,
projectId
,
"repository"
,
"files"
);
return
(
response
.
readEntity
(
RepositoryFile
.
class
));
return
(
response
.
readEntity
(
RepositoryFile
.
class
));
}
}
/**
/**
* Update existing file in repository
* Update existing file in repository
*
*
* PUT /projects/:id/repository/files
* PUT /projects/:id/repository/files
*
*
* file_path (required) - Full path to new file. Ex. lib/class.rb
* file_path (required) - Full path to new file. Ex. lib/class.rb
* branch_name (required) - The name of branch
* branch_name (required) - The name of branch
* encoding (optional) - 'text' or 'base64'. Text is default.
* encoding (optional) - 'text' or 'base64'. Text is default.
* content (required) - File content
* content (required) - File content
* commit_message (required) - Commit message
* commit_message (required) - Commit message
*
*
* @param file
* @param file
* @param projectId
* @param projectId
* @param branchName
* @param branchName
* @param commitMessage
* @param commitMessage
* @return
* @return
a RepositoryFile instance with the updated file info
* @throws GitLabApiException
* @throws GitLabApiException
*/
*/
public
RepositoryFile
updateFile
(
RepositoryFile
file
,
Integer
projectId
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
public
RepositoryFile
updateFile
(
RepositoryFile
file
,
Integer
projectId
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
Form
form
=
file2form
(
file
,
branchName
,
commitMessage
);
Form
form
=
file2form
(
file
,
branchName
,
commitMessage
);
Response
response
=
put
(
Response
.
Status
.
OK
,
form
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
);
Response
response
=
put
(
Response
.
Status
.
OK
,
form
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
);
return
(
response
.
readEntity
(
RepositoryFile
.
class
));
return
(
response
.
readEntity
(
RepositoryFile
.
class
));
}
}
/**
/**
* Delete existing file in repository
* Delete existing file in repository
*
*
* DELETE /projects/:id/repository/files
* DELETE /projects/:id/repository/files
*
*
* file_path (required) - Full path to file. Ex. lib/class.rb
* file_path (required) - Full path to file. Ex. lib/class.rb
* 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 filePath
* @param filePath
* @param projectId
* @param projectId
* @param branchName
* @param branchName
* @param commitMessage
* @param commitMessage
* @throws GitLabApiException
* @throws GitLabApiException
*/
*/
public
void
deleteFile
(
String
filePath
,
Integer
projectId
,
String
branchName
,
String
commitMessage
)
throws
GitLabApiException
{
public
void
deleteFile
(
String
filePath
,
Integer
projectId
,
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"
);
...
@@ -114,13 +112,13 @@ public class RepositoryFileApi extends AbstractApi {
...
@@ -114,13 +112,13 @@ public class RepositoryFileApi extends AbstractApi {
delete
(
Response
.
Status
.
OK
,
form
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
);
delete
(
Response
.
Status
.
OK
,
form
.
asMap
(),
"projects"
,
projectId
,
"repository"
,
"files"
);
}
}
private
Form
file2form
(
RepositoryFile
file
,
String
branchName
,
String
commitMessage
){
private
Form
file2form
(
RepositoryFile
file
,
String
branchName
,
String
commitMessage
)
{
Form
form
=
new
Form
();
Form
form
=
new
Form
();
addFormParam
(
form
,
"file_path"
,
file
.
getFilePath
(),
true
);
addFormParam
(
form
,
"file_path"
,
file
.
getFilePath
(),
true
);
addFormParam
(
form
,
"branch_name"
,
branchName
,
true
);
addFormParam
(
form
,
"branch_name"
,
branchName
,
true
);
addFormParam
(
form
,
"encoding"
,
file
.
getEncoding
(),
false
);
addFormParam
(
form
,
"encoding"
,
file
.
getEncoding
(),
false
);
addFormParam
(
form
,
"content"
,
file
.
getContent
(),
true
);
addFormParam
(
form
,
"content"
,
file
.
getContent
(),
true
);
addFormParam
(
form
,
"commit_message"
,
commitMessage
,
true
);
addFormParam
(
form
,
"commit_message"
,
commitMessage
,
true
);
return
form
;
return
form
;
}
}
}
}
src/main/java/com/messners/gitlab/api/ServicesApi.java
0 → 100644
View file @
c2f3c1b2
package
com.messners.gitlab.api
;
import
com.messners.gitlab.api.models.Project
;
import
javax.ws.rs.core.Form
;
import
javax.ws.rs.core.Response
;
/**
* Access for the services API.
* See
* <a href="https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/services.md">GitLab documentation</a>.
* It is quite restricted as you may not retrieve the API but only set or delete.
*/
public
class
ServicesApi
extends
AbstractApi
{
public
ServicesApi
(
GitLabApi
gitLabApi
)
{
super
(
gitLabApi
);
}
/**
* Activates the gitlab-ci service.
*
* PUT /projects/:id/services/gitlab-ci
* @param projectId id of the project
* @param token for authentication
* @param projectCIUrl URL of the GitLab-CI project
*/
public
void
setGitLabCI
(
Integer
projectId
,
String
token
,
String
projectCIUrl
)
throws
GitLabApiException
{
final
Form
formData
=
new
Form
();
formData
.
param
(
"token"
,
token
);
formData
.
param
(
"project_url"
,
projectCIUrl
);
put
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
projectId
,
"services"
,
"gitlab-ci"
);
}
/**
* Activates the gitlab-ci service.
*
* PUT /projects/:id/services/gitlab-ci
* @param project the project
* @param token for authentication
* @param projectCIUrl URL of the GitLab-CI project
*/
public
void
setGitLabCI
(
Project
project
,
String
token
,
String
projectCIUrl
)
throws
GitLabApiException
{
setGitLabCI
(
project
.
getId
(),
token
,
projectCIUrl
);
}
/**
* Deletes the gitlab-ci service.
*
* DELETE /projects/:id/services/gitlab-ci
*
* @param projectId id of the project
* @throws GitLabApiException
*/
public
void
deleteGitLabCI
(
Integer
projectId
)
throws
GitLabApiException
{
delete
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"services"
,
"gitlab-ci"
);
}
/**
* DELETE /projects/:id/services/gitlab-ci
* @param project to delete
* @throws GitLabApiException
*/
public
void
deleteGitLabCI
(
Project
project
)
throws
GitLabApiException
{
deleteGitLabCI
(
project
.
getId
());
}
/**
* Activates HipChat notifications.
*
* PUT /projects/:id/services/hipchat
*
* @param projectId id of the project
* @param token for authentication
* @param room HipChat Room
* @param server HipChat Server URL
*
* @throws GitLabApiException
*/
public
void
setHipChat
(
Integer
projectId
,
String
token
,
String
room
,
String
server
)
throws
GitLabApiException
{
final
Form
formData
=
new
Form
();
formData
.
param
(
"token"
,
token
);
formData
.
param
(
"room"
,
room
);
formData
.
param
(
"server"
,
server
);
put
(
Response
.
Status
.
OK
,
formData
.
asMap
(),
"projects"
,
projectId
,
"services"
,
"hipchat"
);
}
/**
* Activates HipChat notifications.
*
* PUT /projects/:id/services/hipchat
*
* @param project
* @param token
* @param room
* @param server
* @throws GitLabApiException
*/
public
void
setHipChat
(
Project
project
,
String
token
,
String
room
,
String
server
)
throws
GitLabApiException
{
setHipChat
(
project
.
getId
(),
token
,
room
,
server
);
}
/**
* Deletes the gitlab-ci service.
*
* DELETE /projects/:id/services/hipchat
*
* @param projectId id of the project
* @throws GitLabApiException
*/
public
void
deleteHipChat
(
Integer
projectId
)
throws
GitLabApiException
{
delete
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"services"
,
"hipchat"
);
}
/**
* Deletes the gitlab-ci service.
*
* DELETE /projects/:id/services/hipchat
*
* @param project the project
* @throws GitLabApiException
*/
public
void
deleteHipChat
(
Project
project
)
throws
GitLabApiException
{
deleteHipChat
(
project
.
getId
());
}
}
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