23
23
24
24
package org .gitlab4j .api ;
25
25
26
+ import java .io .File ;
26
27
import java .io .UnsupportedEncodingException ;
27
28
import java .net .URLEncoder ;
28
29
import java .util .Date ;
36
37
import org .gitlab4j .api .GitLabApi .ApiVersion ;
37
38
import org .gitlab4j .api .models .AccessLevel ;
38
39
import org .gitlab4j .api .models .Event ;
40
+ import org .gitlab4j .api .models .FileUpload ;
39
41
import org .gitlab4j .api .models .Issue ;
40
42
import org .gitlab4j .api .models .Member ;
41
43
import org .gitlab4j .api .models .Project ;
42
44
import org .gitlab4j .api .models .ProjectHook ;
43
45
import org .gitlab4j .api .models .ProjectUser ;
46
+ import org .gitlab4j .api .models .PushRule ;
44
47
import org .gitlab4j .api .models .Snippet ;
45
48
import org .gitlab4j .api .models .Visibility ;
46
49
@@ -1883,4 +1886,191 @@ public Project unarchiveProject(Integer projectId)
1883
1886
Response response = post (Response .Status .CREATED , (new GitLabApiForm ()), "projects" , projectId , "unarchive" );
1884
1887
return (response .readEntity (Project .class ));
1885
1888
}
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
+ }
0 commit comments