Skip to content

Commit 4080db2

Browse files
committed
Initial commit (#317).
1 parent 2f2d2b4 commit 4080db2

File tree

6 files changed

+403
-0
lines changed

6 files changed

+403
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017 Greg Messner <greg@messners.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software is furnished to do so,
11+
* subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package org.gitlab4j.api;
25+
26+
import java.util.List;
27+
import java.util.stream.Stream;
28+
29+
import javax.ws.rs.core.GenericType;
30+
import javax.ws.rs.core.Response;
31+
32+
import org.gitlab4j.api.models.Package;
33+
import org.gitlab4j.api.models.PackageFile;
34+
35+
/**
36+
* <p>This class implements the client side API for the GitLab Packages API.
37+
* See <a href="https://docs.gitlab.com/ee/api/packages.html">Packages API at GitLab</a> for more information.</p>
38+
*
39+
* NOTE: This API is not available in the Community edition of GitLab.
40+
*/
41+
public class PackagesApi extends AbstractApi {
42+
43+
public PackagesApi(GitLabApi gitLabApi) {
44+
super(gitLabApi);
45+
}
46+
47+
/**
48+
* Get a list of project packages. Both Maven and NPM packages are included in results.
49+
* When accessed without authentication, only packages of public projects are returned.
50+
*
51+
* <pre><code>GitLab Endpoint: GET /projects/:id/packages</code></pre>
52+
*
53+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
54+
* @return a list of pages in the project's packages
55+
* @throws GitLabApiException if any exception occurs
56+
*/
57+
public List<Package> getPackages(Object projectIdOrPath) throws GitLabApiException {
58+
return (getPackages(projectIdOrPath, getDefaultPerPage()).all());
59+
}
60+
61+
/**
62+
* Get a list of project packages for the specified page. Both Maven and NPM packages are included in results.
63+
* When accessed without authentication, only packages of public projects are returned.
64+
*
65+
* <pre><code>GitLab Endpoint: GET /projects/:id/packages</code></pre>
66+
*
67+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
68+
* @param page the page to get
69+
* @param perPage the number of Package instances per page
70+
* @return a list of project packages for the specified range
71+
* @throws GitLabApiException if any exception occurs
72+
*/
73+
public List<Package> getPackages(Object projectIdOrPath, int page, int perPage) throws GitLabApiException {
74+
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage),
75+
"projects", getProjectIdOrPath(projectIdOrPath), "packages");
76+
return response.readEntity(new GenericType<List<Package>>() {});
77+
}
78+
79+
/**
80+
* Get a Pager of project packages. Both Maven and NPM packages are included in results.
81+
* When accessed without authentication, only packages of public projects are returned.
82+
*
83+
* <pre><code>GitLab Endpoint: GET /projects/:id/packages</code></pre>
84+
*
85+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
86+
* @param itemsPerPage the number of Package instances per page
87+
* @return a Pager of project packages for the specified range
88+
* @throws GitLabApiException if any exception occurs
89+
*/
90+
public Pager<Package> getPackages(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
91+
return (new Pager<Package>(this, Package.class, itemsPerPage, null,
92+
"projects", getProjectIdOrPath(projectIdOrPath), "packages"));
93+
}
94+
95+
/**
96+
* Get a Stream of project packages. Both Maven and NPM packages are included in results.
97+
* When accessed without authentication, only packages of public projects are returned.
98+
*
99+
* <pre><code>GitLab Endpoint: GET /projects/:id/packages</code></pre>
100+
*
101+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
102+
* @return a Stream of pages in the project's packages
103+
* @throws GitLabApiException if any exception occurs
104+
*/
105+
public Stream<Package> getPackagesStream(Object projectIdOrPath) throws GitLabApiException {
106+
return (getPackages(projectIdOrPath, getDefaultPerPage()).stream());
107+
}
108+
109+
/**
110+
* Get a single project package.
111+
*
112+
* <pre><code>GitLab Endpoint: GET /projects/:id/packages/:package_id</code></pre>
113+
*
114+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
115+
* @param packageId the ID of the package to get
116+
* @return a Package instance for the specified package ID
117+
* @throws GitLabApiException if any exception occurs
118+
*/
119+
public Package getPackage(Object projectIdOrPath, Integer packageId) throws GitLabApiException {
120+
Response response = get(Response.Status.OK, null,
121+
"projects", getProjectIdOrPath(projectIdOrPath), "packages", packageId);
122+
return (response.readEntity(Package.class));
123+
}
124+
125+
/**
126+
* Get a list of package files of a single package.
127+
*
128+
* <pre><code>GitLab Endpoint: GET /projects/:id/packages/:package_id/package_files</code></pre>
129+
*
130+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
131+
* @param packageId the ID of the package to get the package files for
132+
* @return a list of PackageFile instances for the specified package ID
133+
* @throws GitLabApiException if any exception occurs
134+
*/
135+
public List<PackageFile> getPackageFiles(Object projectIdOrPath, Integer packageId) throws GitLabApiException {
136+
return (getPackageFiles(projectIdOrPath, packageId, getDefaultPerPage()).all());
137+
}
138+
139+
/**
140+
* Get a list of package files of a single package for the specified page.
141+
*
142+
* <pre><code>GitLab Endpoint: GET /projects/:id/packages/:package_id/package_files</code></pre>
143+
*
144+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
145+
* @param packageId the ID of the package to get the package files for
146+
* @param page the page to get
147+
* @param perPage the number of PackageFile instances per page
148+
* @return a list of PackageFile instances for the specified package ID
149+
* @throws GitLabApiException if any exception occurs
150+
*/
151+
public List<PackageFile> getPackageFiles(Object projectIdOrPath, Integer packageId, int page, int perPage) throws GitLabApiException {
152+
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage),
153+
"projects", getProjectIdOrPath(projectIdOrPath), "packages", packageId, "package_files");
154+
return response.readEntity(new GenericType<List<PackageFile>>() {});
155+
}
156+
157+
/**
158+
* Get a Pager of project package files.
159+
*
160+
* <pre><code>GitLab Endpoint: GET /projects/:id/packages/:package_id/package_files</code></pre>
161+
*
162+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
163+
* @param packageId the ID of the package to get the package files for
164+
* @param itemsPerPage the number of PackageFile instances per page
165+
* @return a Pager of PackageFile instances for the specified package ID
166+
* @throws GitLabApiException if any exception occurs
167+
*/
168+
public Pager<PackageFile> getPackageFiles(Object projectIdOrPath, Integer packageId, int itemsPerPage) throws GitLabApiException {
169+
return (new Pager<PackageFile>(this, PackageFile.class, itemsPerPage, null,
170+
"projects", getProjectIdOrPath(projectIdOrPath), "packages", packageId, "package_files"));
171+
}
172+
173+
/**
174+
* Get a Stream of project package files.
175+
*
176+
* <pre><code>GitLab Endpoint: GET /projects/:id/packages/:package_id/package_files</code></pre>
177+
*
178+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
179+
* @param packageId the ID of the package to get the package files for
180+
* @return a Stream of PackageFile instances for the specified package ID
181+
* @throws GitLabApiException if any exception occurs
182+
*/
183+
public Stream<PackageFile> getPackagesStream(Object projectIdOrPath, Integer packageId) throws GitLabApiException {
184+
return (getPackageFiles(projectIdOrPath, packageId, getDefaultPerPage()).stream());
185+
}
186+
187+
/**
188+
* Deletes a project package.
189+
*
190+
* <pre><code>GitLab Endpoint: DELETE /projects/:id/packages/:package_id</code></pre>
191+
*
192+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
193+
* @param packageId the ID of the package to delete
194+
* @throws GitLabApiException if any exception occurs
195+
*/
196+
public void deletePackage(Object projectIdOrPath, Integer packageId) throws GitLabApiException {
197+
198+
if (packageId == null) {
199+
throw new RuntimeException("packageId cannot be null");
200+
}
201+
202+
delete(Response.Status.NO_CONTENT, null,"projects", getProjectIdOrPath(projectIdOrPath), "packages", packageId);
203+
}
204+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.gitlab4j.api.models;
2+
3+
import javax.xml.bind.annotation.XmlRootElement;
4+
5+
import org.gitlab4j.api.utils.JacksonJson;
6+
7+
@XmlRootElement
8+
public class Package {
9+
10+
private Integer id;
11+
private String name;
12+
private String version;
13+
private PackageType packageType;
14+
15+
public Integer getId() {
16+
return id;
17+
}
18+
19+
public void setId(Integer id) {
20+
this.id = id;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
31+
public String getVersion() {
32+
return version;
33+
}
34+
35+
public void setVersion(String version) {
36+
this.version = version;
37+
}
38+
39+
public PackageType getPackageType() {
40+
return packageType;
41+
}
42+
43+
public void setPackageType(PackageType packageType) {
44+
this.packageType = packageType;
45+
}
46+
47+
@Override
48+
public String toString() {
49+
return (JacksonJson.toJsonString(this));
50+
}
51+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.gitlab4j.api.models;
2+
3+
import java.util.Date;
4+
5+
import org.gitlab4j.api.utils.JacksonJson;
6+
7+
public class PackageFile {
8+
9+
private Integer id;
10+
private Integer packageId;
11+
private Date created_at;
12+
private String fileName;
13+
private Long size;
14+
private String fileMd5;
15+
private String fileSha1;
16+
17+
public Integer getId() {
18+
return id;
19+
}
20+
21+
public void setId(Integer id) {
22+
this.id = id;
23+
}
24+
25+
public Integer getPackageId() {
26+
return packageId;
27+
}
28+
29+
public void setPackageId(Integer packageId) {
30+
this.packageId = packageId;
31+
}
32+
33+
public Date getCreated_at() {
34+
return created_at;
35+
}
36+
37+
public void setCreated_at(Date created_at) {
38+
this.created_at = created_at;
39+
}
40+
41+
public String getFileName() {
42+
return fileName;
43+
}
44+
45+
public void setFileName(String fileName) {
46+
this.fileName = fileName;
47+
}
48+
49+
public Long getSize() {
50+
return size;
51+
}
52+
53+
public void setSize(Long size) {
54+
this.size = size;
55+
}
56+
57+
public String getFileMd5() {
58+
return fileMd5;
59+
}
60+
61+
public void setFileMd5(String fileMd5) {
62+
this.fileMd5 = fileMd5;
63+
}
64+
65+
public String getFileSha1() {
66+
return fileSha1;
67+
}
68+
69+
public void setFileSha1(String fileSha1) {
70+
this.fileSha1 = fileSha1;
71+
}
72+
73+
@Override
74+
public String toString() {
75+
return (JacksonJson.toJsonString(this));
76+
}
77+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
4+
5+
import com.fasterxml.jackson.annotation.JsonCreator;
6+
import com.fasterxml.jackson.annotation.JsonValue;
7+
8+
public enum PackageType {
9+
10+
MAVEN, NPM;
11+
12+
private static JacksonJsonEnumHelper<PackageType> enumHelper = new JacksonJsonEnumHelper<>(PackageType.class);
13+
14+
@JsonCreator
15+
public static PackageType forValue(String value) {
16+
return enumHelper.forValue(value);
17+
}
18+
19+
@JsonValue
20+
public String toValue() {
21+
return (enumHelper.toString(this));
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return (enumHelper.toString(this));
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[
2+
{
3+
"id": 25,
4+
"package_id": 4,
5+
"created_at": "2018-11-07T15:25:52.199Z",
6+
"file_name": "my-app-1.5-20181107.152550-1.jar",
7+
"size": 2421,
8+
"file_md5": "58e6a45a629910c6ff99145a688971ac",
9+
"file_sha1": "ebd193463d3915d7e22219f52740056dfd26cbfe"
10+
},
11+
{
12+
"id": 26,
13+
"package_id": 4,
14+
"created_at": "2018-11-07T15:25:56.776Z",
15+
"file_name": "my-app-1.5-20181107.152550-1.pom",
16+
"size": 1122,
17+
"file_md5": "d90f11d851e17c5513586b4a7e98f1b2",
18+
"file_sha1": "9608d068fe88aff85781811a42f32d97feb440b5"
19+
},
20+
{
21+
"id": 27,
22+
"package_id": 4,
23+
"created_at": "2018-11-07T15:26:00.556Z",
24+
"file_name": "maven-metadata.xml",
25+
"size": 767,
26+
"file_md5": "6dfd0cce1203145a927fef5e3a1c650c",
27+
"file_sha1": "d25932de56052d320a8ac156f745ece73f6a8cd2"
28+
}
29+
]

0 commit comments

Comments
 (0)