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
05e5f53c
Commit
05e5f53c
authored
Apr 14, 2018
by
Greg Messner
Browse files
Mods for file upload and push rules (#171).
parent
03d69b81
Changes
3
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
05e5f53c
...
...
@@ -11,7 +11,7 @@ To utilize the GitLab API for Java in your project, simply add the following dep
```
java
dependencies
{
...
compile
group:
'
org
.
gitlab4j
'
,
name:
'
gitlab4j
-
api
'
,
version:
'
4.8
.
9
'
compile
group:
'
org
.
gitlab4j
'
,
name:
'
gitlab4j
-
api
'
,
version:
'
4.8
.
10
'
}
```
...
...
@@ -20,7 +20,7 @@ dependencies {
<dependency>
<groupId>
org.gitlab4j
</groupId>
<artifactId>
gitlab4j-api
</artifactId>
<version>
4.8.
9
</version>
<version>
4.8.
10
</version>
</dependency>
```
...
...
src/main/java/org/gitlab4j/api/ProjectApi.java
View file @
05e5f53c
...
...
@@ -23,6 +23,7 @@
package
org.gitlab4j.api
;
import
java.io.File
;
import
java.io.UnsupportedEncodingException
;
import
java.net.URLEncoder
;
import
java.util.Date
;
...
...
@@ -36,11 +37,13 @@ import javax.ws.rs.core.Response;
import
org.gitlab4j.api.GitLabApi.ApiVersion
;
import
org.gitlab4j.api.models.AccessLevel
;
import
org.gitlab4j.api.models.Event
;
import
org.gitlab4j.api.models.FileUpload
;
import
org.gitlab4j.api.models.Issue
;
import
org.gitlab4j.api.models.Member
;
import
org.gitlab4j.api.models.Project
;
import
org.gitlab4j.api.models.ProjectHook
;
import
org.gitlab4j.api.models.ProjectUser
;
import
org.gitlab4j.api.models.PushRule
;
import
org.gitlab4j.api.models.Snippet
;
import
org.gitlab4j.api.models.Visibility
;
...
...
@@ -1883,4 +1886,191 @@ public class ProjectApi extends AbstractApi implements Constants {
Response
response
=
post
(
Response
.
Status
.
CREATED
,
(
new
GitLabApiForm
()),
"projects"
,
projectId
,
"unarchive"
);
return
(
response
.
readEntity
(
Project
.
class
));
}
}
/**
* Uploads a file to the specified project to be used in an issue or merge request description, or a comment.
*
* POST /projects/:id/uploads
*
* @param projectId the ID of the project to upload the file to, required
* @param fileToUpload the File instance of the file to upload, required
* @return a FileUpload instance with information on the just uploaded file
* @throws GitLabApiException if any exception occurs
*/
public
FileUpload
uploadFile
(
Integer
projectId
,
File
fileToUpload
)
throws
GitLabApiException
{
return
(
uploadFile
(
projectId
,
fileToUpload
,
null
));
}
/**
* Uploads a file to the specified project to be used in an issue or merge request description, or a comment.
*
* POST /projects/:id/uploads
*
* @param projectId the ID of the project to upload the file to, required
* @param fileToUpload the File instance of the file to upload, required
* @param mediaType the media type of the file to upload, optional
* @return a FileUpload instance with information on the just uploaded file
* @throws GitLabApiException if any exception occurs
*/
public
FileUpload
uploadFile
(
Integer
projectId
,
File
fileToUpload
,
String
mediaType
)
throws
GitLabApiException
{
Response
response
=
upload
(
Response
.
Status
.
CREATED
,
"file"
,
fileToUpload
,
mediaType
,
"projects"
,
projectId
,
"uploads"
);
return
(
response
.
readEntity
(
FileUpload
.
class
));
}
/**
* Get a list of project's push rules. Only returns the first page
*
* GET /projects/:id/push_rule
*
* @param projectId the project ID to get the push rules for
* @return a list of project's push rules
* @throws GitLabApiException if any exception occurs
*/
public
List
<
PushRule
>
getPushRules
(
Integer
projectId
)
throws
GitLabApiException
{
return
(
getPushRules
(
projectId
,
1
,
getDefaultPerPage
()));
}
/**
* Get a list of project's push rules using the specified page and per page settings.
*
* GET /projects/:id/push_rule
*
* @param projectId the project ID to get the push rules for
* @param page the page to get
* @param perPage the number of push rules per page
* @return the list of push rules in the specified range
* @throws GitLabApiException if any exception occurs
*/
public
List
<
PushRule
>
getPushRules
(
Integer
projectId
,
int
page
,
int
perPage
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getPageQueryParams
(
page
,
perPage
),
"projects"
,
projectId
,
"push_rule"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
PushRule
>>()
{}));
}
/**
* Get a Pager of project's push rules.
*
* GET /projects/:id/push_rule
*
* @param projectId the project ID to get the push rules for
* @param itemsPerPage the number of push rules per page
* @return a Pager instance for paging over the project's push rules
* @throws GitLabApiException if any exception occurs
*/
public
Pager
<
PushRule
>
getPushRules
(
Integer
projectId
,
int
itemsPerPage
)
throws
GitLabApiException
{
return
(
new
Pager
<
PushRule
>(
this
,
PushRule
.
class
,
itemsPerPage
,
null
,
"projects"
,
projectId
,
"push_rule"
));
}
/**
* Creates new push rule for the specified project.
*
* POST /projects/:id/push_rule
*
* The following properties on the PushRule instance are utilized in the creation of the push rule:
*
*<code>
* denyDeleteTag (optional) - Deny deleting a tag
* memberCheck (optional) - Restrict commits by author (email) to existing GitLab users
* preventSecrets (optional) - GitLab will reject any files that are likely to contain secrets
* commitMessageRegex (optional) - All commit messages must match this, e.g. Fixed \d+\..*
* branchNameRegex (optional) - All branch names must match this, e.g. `(feature
* authorEmailRegex (optional) - All commit author emails must match this, e.g. @my-company.com$
* fileNameRegex (optional) - All committed filenames must not match this, e.g. `(jar
* maxFileSize (optional) - Maximum file size (MB
*</code>
*
* @param projectId the project ID to add the push rule to
* @param pushRule the PUshRule instance containing the push rule configuration to add
* @return a PushRule instance with the newly created push rule info
* @throws GitLabApiException if any exception occurs
*/
public
PushRule
createPushRule
(
Integer
projectId
,
PushRule
pushRule
)
throws
GitLabApiException
{
if
(
projectId
==
null
)
{
throw
new
RuntimeException
(
"projectId cannot be null"
);
}
GitLabApiForm
formData
=
new
GitLabApiForm
()
.
withParam
(
"deny_delete_tag"
,
pushRule
.
getDenyDeleteTag
())
.
withParam
(
"member_check"
,
pushRule
.
getMemberCheck
())
.
withParam
(
"prevent_secrets"
,
pushRule
.
getPreventSecrets
())
.
withParam
(
"commit_message_regex"
,
pushRule
.
getCommitMessageRegex
())
.
withParam
(
"branch_name_regex"
,
pushRule
.
getBranchNameRegex
())
.
withParam
(
"author_email_regex"
,
pushRule
.
getAuthorEmailRegex
())
.
withParam
(
"file_name_regex"
,
pushRule
.
getFileNameRegex
())
.
withParam
(
"max_file_size"
,
pushRule
.
getMaxFileSize
());
Response
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"projects"
,
projectId
,
"push_rule"
);
return
(
response
.
readEntity
(
PushRule
.
class
));
}
/**
* Updates a push rule for the specified project.
*
* PUT /projects/:id/push_rule/:push_rule_id
*
* The following properties on the PushRule instance are utilized when updating the push rule:
*
*<code>
* denyDeleteTag (optional) - Deny deleting a tag
* memberCheck (optional) - Restrict commits by author (email) to existing GitLab users
* preventSecrets (optional) - GitLab will reject any files that are likely to contain secrets
* commitMessageRegex (optional) - All commit messages must match this, e.g. Fixed \d+\..*
* branchNameRegex (optional) - All branch names must match this, e.g. `(feature
* authorEmailRegex (optional) - All commit author emails must match this, e.g. @my-company.com$
* fileNameRegex (optional) - All committed filenames must not match this, e.g. `(jar
* maxFileSize (optional) - Maximum file size (MB
*</code>
*
* @param projectId the project ID to update the push rule for
* @param pushRuleId the push rule ID to update
* @param pushRule the PUshRule instance containing the push rule configuration to update
* @return a PushRule instance with the newly created push rule info
* @throws GitLabApiException if any exception occurs
*/
public
PushRule
updatePushRule
(
Integer
projectId
,
Integer
pushRuleId
,
PushRule
pushRule
)
throws
GitLabApiException
{
if
(
projectId
==
null
)
{
throw
new
RuntimeException
(
"projectId cannot be null"
);
}
if
(
pushRuleId
==
null
)
{
throw
new
RuntimeException
(
"pushRuleId cannot be null"
);
}
GitLabApiForm
formData
=
new
GitLabApiForm
()
.
withParam
(
"deny_delete_tag"
,
pushRule
.
getDenyDeleteTag
())
.
withParam
(
"member_check"
,
pushRule
.
getMemberCheck
())
.
withParam
(
"prevent_secrets"
,
pushRule
.
getPreventSecrets
())
.
withParam
(
"commit_message_regex"
,
pushRule
.
getCommitMessageRegex
())
.
withParam
(
"branch_name_regex"
,
pushRule
.
getBranchNameRegex
())
.
withParam
(
"author_email_regex"
,
pushRule
.
getAuthorEmailRegex
())
.
withParam
(
"file_name_regex"
,
pushRule
.
getFileNameRegex
())
.
withParam
(
"max_file_size"
,
pushRule
.
getMaxFileSize
());
Response
response
=
post
(
Response
.
Status
.
OK
,
formData
,
"projects"
,
projectId
,
"push_rule"
,
pushRuleId
);
return
(
response
.
readEntity
(
PushRule
.
class
));
}
/**
* Removes a push rule from a project. This is an idempotent method and can be
* called multiple times. Either the push rule is available or not.
*
* DELETE /projects/:id/push_rule/:push_rule_id
*
* @param projectId the project ID to delete the push rule from
* @param pushRuleId the push rule ID to delete
* @throws GitLabApiException if any exception occurs
*/
public
void
deletePushRule
(
Integer
projectId
,
Integer
pushRuleId
)
throws
GitLabApiException
{
if
(
projectId
==
null
)
{
throw
new
RuntimeException
(
"projectId cannot be null"
);
}
if
(
pushRuleId
==
null
)
{
throw
new
RuntimeException
(
"pushRuleId cannot be null"
);
}
delete
(
Response
.
Status
.
OK
,
null
,
"projects"
,
projectId
,
"push_rule"
,
pushRuleId
);
}
}
\ No newline at end of file
src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java
View file @
05e5f53c
...
...
@@ -38,6 +38,7 @@ import org.gitlab4j.api.models.CompareResults;
import
org.gitlab4j.api.models.DeployKey
;
import
org.gitlab4j.api.models.Diff
;
import
org.gitlab4j.api.models.Event
;
import
org.gitlab4j.api.models.FileUpload
;
import
org.gitlab4j.api.models.Group
;
import
org.gitlab4j.api.models.ImpersonationToken
;
import
org.gitlab4j.api.models.Issue
;
...
...
@@ -55,6 +56,7 @@ import org.gitlab4j.api.models.Project;
import
org.gitlab4j.api.models.ProjectHook
;
import
org.gitlab4j.api.models.ProjectUser
;
import
org.gitlab4j.api.models.ProtectedBranch
;
import
org.gitlab4j.api.models.PushRule
;
import
org.gitlab4j.api.models.Runner
;
import
org.gitlab4j.api.models.RunnerDetail
;
import
org.gitlab4j.api.models.Session
;
...
...
@@ -174,6 +176,17 @@ public class TestGitLabApiBeans {
}
}
@Test
public
void
testFileUpload
()
{
try
{
FileUpload
fileUpload
=
makeFakeApiCall
(
FileUpload
.
class
,
"file-upload"
);
assertTrue
(
compareJson
(
fileUpload
,
"file-upload"
));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
@Test
public
void
testGroup
()
{
...
...
@@ -280,6 +293,17 @@ public class TestGitLabApiBeans {
}
}
@Test
public
void
testPushRule
()
{
try
{
PushRule
pushRule
=
makeFakeApiCall
(
PushRule
.
class
,
"push-rule"
);
assertTrue
(
compareJson
(
pushRule
,
"push-rule"
));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
@Test
public
void
testRunnerDetail
()
{
...
...
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