Skip to content

Commit 05e5f53

Browse files
committed
Mods for file upload and push rules (#171).
1 parent 03d69b8 commit 05e5f53

File tree

3 files changed

+217
-3
lines changed

3 files changed

+217
-3
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To utilize the GitLab API for Java in your project, simply add the following dep
1111
```java
1212
dependencies {
1313
...
14-
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.8.9'
14+
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.8.10'
1515
}
1616
```
1717

@@ -20,7 +20,7 @@ dependencies {
2020
<dependency>
2121
<groupId>org.gitlab4j</groupId>
2222
<artifactId>gitlab4j-api</artifactId>
23-
<version>4.8.9</version>
23+
<version>4.8.10</version>
2424
</dependency>
2525
```
2626

src/main/java/org/gitlab4j/api/ProjectApi.java

+191-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
package org.gitlab4j.api;
2525

26+
import java.io.File;
2627
import java.io.UnsupportedEncodingException;
2728
import java.net.URLEncoder;
2829
import java.util.Date;
@@ -36,11 +37,13 @@
3637
import org.gitlab4j.api.GitLabApi.ApiVersion;
3738
import org.gitlab4j.api.models.AccessLevel;
3839
import org.gitlab4j.api.models.Event;
40+
import org.gitlab4j.api.models.FileUpload;
3941
import org.gitlab4j.api.models.Issue;
4042
import org.gitlab4j.api.models.Member;
4143
import org.gitlab4j.api.models.Project;
4244
import org.gitlab4j.api.models.ProjectHook;
4345
import org.gitlab4j.api.models.ProjectUser;
46+
import org.gitlab4j.api.models.PushRule;
4447
import org.gitlab4j.api.models.Snippet;
4548
import org.gitlab4j.api.models.Visibility;
4649

@@ -1883,4 +1886,191 @@ public Project unarchiveProject(Integer projectId)
18831886
Response response = post(Response.Status.CREATED, (new GitLabApiForm()), "projects", projectId, "unarchive");
18841887
return (response.readEntity(Project.class));
18851888
}
1886-
}
1889+
1890+
/**
1891+
* Uploads a file to the specified project to be used in an issue or merge request description, or a comment.
1892+
*
1893+
* POST /projects/:id/uploads
1894+
*
1895+
* @param projectId the ID of the project to upload the file to, required
1896+
* @param fileToUpload the File instance of the file to upload, required
1897+
* @return a FileUpload instance with information on the just uploaded file
1898+
* @throws GitLabApiException if any exception occurs
1899+
*/
1900+
public FileUpload uploadFile(Integer projectId, File fileToUpload) throws GitLabApiException {
1901+
return (uploadFile(projectId, fileToUpload, null));
1902+
}
1903+
1904+
/**
1905+
* Uploads a file to the specified project to be used in an issue or merge request description, or a comment.
1906+
*
1907+
* POST /projects/:id/uploads
1908+
*
1909+
* @param projectId the ID of the project to upload the file to, required
1910+
* @param fileToUpload the File instance of the file to upload, required
1911+
* @param mediaType the media type of the file to upload, optional
1912+
* @return a FileUpload instance with information on the just uploaded file
1913+
* @throws GitLabApiException if any exception occurs
1914+
*/
1915+
public FileUpload uploadFile(Integer projectId, File fileToUpload, String mediaType) throws GitLabApiException {
1916+
Response response = upload(Response.Status.CREATED, "file", fileToUpload, mediaType, "projects", projectId, "uploads");
1917+
return (response.readEntity(FileUpload.class));
1918+
}
1919+
1920+
/**
1921+
* Get a list of project's push rules. Only returns the first page
1922+
*
1923+
* GET /projects/:id/push_rule
1924+
*
1925+
* @param projectId the project ID to get the push rules for
1926+
* @return a list of project's push rules
1927+
* @throws GitLabApiException if any exception occurs
1928+
*/
1929+
public List<PushRule> getPushRules(Integer projectId) throws GitLabApiException {
1930+
return (getPushRules(projectId, 1, getDefaultPerPage()));
1931+
}
1932+
1933+
/**
1934+
* Get a list of project's push rules using the specified page and per page settings.
1935+
*
1936+
* GET /projects/:id/push_rule
1937+
*
1938+
* @param projectId the project ID to get the push rules for
1939+
* @param page the page to get
1940+
* @param perPage the number of push rules per page
1941+
* @return the list of push rules in the specified range
1942+
* @throws GitLabApiException if any exception occurs
1943+
*/
1944+
public List<PushRule> getPushRules(Integer projectId, int page, int perPage) throws GitLabApiException {
1945+
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", projectId, "push_rule");
1946+
return (response.readEntity(new GenericType<List<PushRule>>() {}));
1947+
}
1948+
1949+
/**
1950+
* Get a Pager of project's push rules.
1951+
*
1952+
* GET /projects/:id/push_rule
1953+
*
1954+
* @param projectId the project ID to get the push rules for
1955+
* @param itemsPerPage the number of push rules per page
1956+
* @return a Pager instance for paging over the project's push rules
1957+
* @throws GitLabApiException if any exception occurs
1958+
*/
1959+
public Pager<PushRule> getPushRules(Integer projectId, int itemsPerPage) throws GitLabApiException {
1960+
return (new Pager<PushRule>(this, PushRule.class, itemsPerPage, null, "projects", projectId, "push_rule"));
1961+
}
1962+
1963+
/**
1964+
* Creates new push rule for the specified project.
1965+
*
1966+
* POST /projects/:id/push_rule
1967+
*
1968+
* The following properties on the PushRule instance are utilized in the creation of the push rule:
1969+
*
1970+
*<code>
1971+
* denyDeleteTag (optional) - Deny deleting a tag
1972+
* memberCheck (optional) - Restrict commits by author (email) to existing GitLab users
1973+
* preventSecrets (optional) - GitLab will reject any files that are likely to contain secrets
1974+
* commitMessageRegex (optional) - All commit messages must match this, e.g. Fixed \d+\..*
1975+
* branchNameRegex (optional) - All branch names must match this, e.g. `(feature
1976+
* authorEmailRegex (optional) - All commit author emails must match this, e.g. @my-company.com$
1977+
* fileNameRegex (optional) - All committed filenames must not match this, e.g. `(jar
1978+
* maxFileSize (optional) - Maximum file size (MB
1979+
*</code>
1980+
*
1981+
* @param projectId the project ID to add the push rule to
1982+
* @param pushRule the PUshRule instance containing the push rule configuration to add
1983+
* @return a PushRule instance with the newly created push rule info
1984+
* @throws GitLabApiException if any exception occurs
1985+
*/
1986+
public PushRule createPushRule(Integer projectId, PushRule pushRule) throws GitLabApiException {
1987+
1988+
if (projectId == null) {
1989+
throw new RuntimeException("projectId cannot be null");
1990+
}
1991+
1992+
GitLabApiForm formData = new GitLabApiForm()
1993+
.withParam("deny_delete_tag", pushRule.getDenyDeleteTag())
1994+
.withParam("member_check", pushRule.getMemberCheck())
1995+
.withParam("prevent_secrets", pushRule.getPreventSecrets())
1996+
.withParam("commit_message_regex", pushRule.getCommitMessageRegex())
1997+
.withParam("branch_name_regex", pushRule.getBranchNameRegex())
1998+
.withParam("author_email_regex", pushRule.getAuthorEmailRegex())
1999+
.withParam("file_name_regex", pushRule.getFileNameRegex())
2000+
.withParam("max_file_size", pushRule.getMaxFileSize());
2001+
2002+
Response response = post(Response.Status.CREATED, formData, "projects", projectId, "push_rule");
2003+
return (response.readEntity(PushRule.class));
2004+
}
2005+
2006+
/**
2007+
* Updates a push rule for the specified project.
2008+
*
2009+
* PUT /projects/:id/push_rule/:push_rule_id
2010+
*
2011+
* The following properties on the PushRule instance are utilized when updating the push rule:
2012+
*
2013+
*<code>
2014+
* denyDeleteTag (optional) - Deny deleting a tag
2015+
* memberCheck (optional) - Restrict commits by author (email) to existing GitLab users
2016+
* preventSecrets (optional) - GitLab will reject any files that are likely to contain secrets
2017+
* commitMessageRegex (optional) - All commit messages must match this, e.g. Fixed \d+\..*
2018+
* branchNameRegex (optional) - All branch names must match this, e.g. `(feature
2019+
* authorEmailRegex (optional) - All commit author emails must match this, e.g. @my-company.com$
2020+
* fileNameRegex (optional) - All committed filenames must not match this, e.g. `(jar
2021+
* maxFileSize (optional) - Maximum file size (MB
2022+
*</code>
2023+
*
2024+
* @param projectId the project ID to update the push rule for
2025+
* @param pushRuleId the push rule ID to update
2026+
* @param pushRule the PUshRule instance containing the push rule configuration to update
2027+
* @return a PushRule instance with the newly created push rule info
2028+
* @throws GitLabApiException if any exception occurs
2029+
*/
2030+
public PushRule updatePushRule(Integer projectId, Integer pushRuleId, PushRule pushRule) throws GitLabApiException {
2031+
2032+
if (projectId == null) {
2033+
throw new RuntimeException("projectId cannot be null");
2034+
}
2035+
2036+
if (pushRuleId == null) {
2037+
throw new RuntimeException("pushRuleId cannot be null");
2038+
}
2039+
2040+
GitLabApiForm formData = new GitLabApiForm()
2041+
.withParam("deny_delete_tag", pushRule.getDenyDeleteTag())
2042+
.withParam("member_check", pushRule.getMemberCheck())
2043+
.withParam("prevent_secrets", pushRule.getPreventSecrets())
2044+
.withParam("commit_message_regex", pushRule.getCommitMessageRegex())
2045+
.withParam("branch_name_regex", pushRule.getBranchNameRegex())
2046+
.withParam("author_email_regex", pushRule.getAuthorEmailRegex())
2047+
.withParam("file_name_regex", pushRule.getFileNameRegex())
2048+
.withParam("max_file_size", pushRule.getMaxFileSize());
2049+
2050+
Response response = post(Response.Status.OK, formData, "projects", projectId, "push_rule", pushRuleId);
2051+
return (response.readEntity(PushRule.class));
2052+
}
2053+
2054+
/**
2055+
* Removes a push rule from a project. This is an idempotent method and can be
2056+
* called multiple times. Either the push rule is available or not.
2057+
*
2058+
* DELETE /projects/:id/push_rule/:push_rule_id
2059+
*
2060+
* @param projectId the project ID to delete the push rule from
2061+
* @param pushRuleId the push rule ID to delete
2062+
* @throws GitLabApiException if any exception occurs
2063+
*/
2064+
public void deletePushRule(Integer projectId, Integer pushRuleId) throws GitLabApiException {
2065+
2066+
if (projectId == null) {
2067+
throw new RuntimeException("projectId cannot be null");
2068+
}
2069+
2070+
if (pushRuleId == null) {
2071+
throw new RuntimeException("pushRuleId cannot be null");
2072+
}
2073+
2074+
delete(Response.Status.OK, null, "projects", projectId, "push_rule", pushRuleId);
2075+
}
2076+
}

src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java

+24
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.gitlab4j.api.models.DeployKey;
3939
import org.gitlab4j.api.models.Diff;
4040
import org.gitlab4j.api.models.Event;
41+
import org.gitlab4j.api.models.FileUpload;
4142
import org.gitlab4j.api.models.Group;
4243
import org.gitlab4j.api.models.ImpersonationToken;
4344
import org.gitlab4j.api.models.Issue;
@@ -55,6 +56,7 @@
5556
import org.gitlab4j.api.models.ProjectHook;
5657
import org.gitlab4j.api.models.ProjectUser;
5758
import org.gitlab4j.api.models.ProtectedBranch;
59+
import org.gitlab4j.api.models.PushRule;
5860
import org.gitlab4j.api.models.Runner;
5961
import org.gitlab4j.api.models.RunnerDetail;
6062
import org.gitlab4j.api.models.Session;
@@ -174,6 +176,17 @@ public void testEvent() {
174176
}
175177
}
176178

179+
@Test
180+
public void testFileUpload() {
181+
182+
try {
183+
FileUpload fileUpload = makeFakeApiCall(FileUpload.class, "file-upload");
184+
assertTrue(compareJson(fileUpload, "file-upload"));
185+
} catch (Exception e) {
186+
e.printStackTrace();
187+
}
188+
}
189+
177190
@Test
178191
public void testGroup() {
179192

@@ -280,6 +293,17 @@ public void testProtectedBranch() {
280293
}
281294
}
282295

296+
@Test
297+
public void testPushRule() {
298+
299+
try {
300+
PushRule pushRule = makeFakeApiCall(PushRule.class, "push-rule");
301+
assertTrue(compareJson(pushRule, "push-rule"));
302+
} catch (Exception e) {
303+
e.printStackTrace();
304+
}
305+
}
306+
283307
@Test
284308
public void testRunnerDetail() {
285309

0 commit comments

Comments
 (0)