Skip to content

Commit d4b0e56

Browse files
aalmirayjmini
andauthored
Added ReleaseLinks API (#639) (#640)
* Added ReleaseLinks API (#639) * Deprecate the "external" attribute in Link * Add tests * Rename to ReleaseLinksApi and add the getter in GitLabApi --------- Co-authored-by: Jeremie Bresson <[email protected]>
1 parent 3407ec9 commit d4b0e56

File tree

7 files changed

+438
-0
lines changed

7 files changed

+438
-0
lines changed

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

+20
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public String getApiNamespace() {
8383
private PipelineApi pipelineApi;
8484
private ProjectApi projectApi;
8585
private ProtectedBranchesApi protectedBranchesApi;
86+
private ReleaseLinksApi releaseLinksApi;
8687
private ReleasesApi releasesApi;
8788
private RepositoryApi repositoryApi;
8889
private RepositoryFileApi repositoryFileApi;
@@ -1444,6 +1445,25 @@ public ProtectedBranchesApi getProtectedBranchesApi() {
14441445
return (this.protectedBranchesApi);
14451446
}
14461447

1448+
/**
1449+
* Gets the ReleaseLinksApi instance owned by this GitLabApi instance. The ReleaseLinksApi is used
1450+
* to perform all Release Links related API calls.
1451+
*
1452+
* @return the ReleaseLinksApi instance owned by this GitLabApi instance
1453+
*/
1454+
public ReleaseLinksApi getReleaseLinksApi() {
1455+
1456+
if (releaseLinksApi == null) {
1457+
synchronized (this) {
1458+
if (releaseLinksApi == null) {
1459+
releaseLinksApi = new ReleaseLinksApi(this);
1460+
}
1461+
}
1462+
}
1463+
1464+
return releaseLinksApi;
1465+
}
1466+
14471467
/**
14481468
* Gets the ReleasesApi instance owned by this GitLabApi instance. The ReleasesApi is used
14491469
* to perform all release related API calls.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package org.gitlab4j.api;
2+
3+
import org.gitlab4j.api.models.Link;
4+
import org.gitlab4j.api.models.ReleaseLinkParams;
5+
6+
import javax.ws.rs.core.Response;
7+
import java.util.List;
8+
import java.util.Optional;
9+
import java.util.stream.Stream;
10+
11+
12+
/**
13+
* This class provides an entry point to all the GitLab ReleaseLinks API calls.
14+
* @see <a href="https://docs.gitlab.com/ce/api/releases/links.html">ReleaseLinks API at GitLab</a>
15+
*/
16+
public class ReleaseLinksApi extends AbstractApi {
17+
18+
public ReleaseLinksApi(GitLabApi gitLabApi) {
19+
super(gitLabApi);
20+
}
21+
22+
/**
23+
* Get assets as Links from a Release.
24+
*
25+
* <pre><code>GitLab Endpoint: GET /projects/:id/releases/:tagName/assets/links</code></pre>
26+
*
27+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
28+
* @param tagName the tag name that the release was created from
29+
* @return the list of assets for the specified release
30+
* @throws GitLabApiException if any exception occurs
31+
*/
32+
public List<Link> getLinks(Object projectIdOrPath, String tagName) throws GitLabApiException {
33+
return (getLinks(projectIdOrPath, tagName, getDefaultPerPage()).all());
34+
}
35+
36+
/**
37+
* Get assets as Links from a Release.
38+
*
39+
* <pre><code>GitLab Endpoint: GET /projects/:id/releases/:tagName/assets/links</code></pre>
40+
*
41+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
42+
* @param tagName the tag name that the release was created from
43+
* @param itemsPerPage the number of Link instances that will be fetched per page
44+
* @return the Pager of Link instances for the specified project ID
45+
* @throws GitLabApiException if any exception occurs
46+
*/
47+
public Pager<Link> getLinks(Object projectIdOrPath, String tagName, int itemsPerPage) throws GitLabApiException {
48+
return (new Pager<Link>(this, Link.class, itemsPerPage, null, "projects", getProjectIdOrPath(projectIdOrPath), "releases", urlEncode(tagName), "assets", "links"));
49+
}
50+
51+
/**
52+
* Get a Stream of assets as Links from a Release.
53+
*
54+
* <pre><code>GitLab Endpoint: GET /projects/:id/releases/:tagName/assets/links</code></pre>
55+
*
56+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
57+
* @param tagName the tag name that the release was created from
58+
* @return a Stream of Link instances for the specified project ID
59+
* @throws GitLabApiException if any exception occurs
60+
*/
61+
public Stream<Link> getLinksStream(Object projectIdOrPath, String tagName) throws GitLabApiException {
62+
return (getLinks(projectIdOrPath, tagName, getDefaultPerPage()).stream());
63+
}
64+
65+
/**
66+
* Get a Link for the given tag name and link id.
67+
*
68+
* <pre><code>GitLab Endpoint: GET /projects/:id/releases/:tagName/assets/links/:linkId</code></pre>
69+
*
70+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
71+
* @param tagName the name of the tag to fetch the Link for
72+
* @param linkId the id of the Link to fetch for
73+
* @return a Link instance with info on the specified tag and id
74+
* @throws GitLabApiException if any exception occurs
75+
*/
76+
public Link getLink(Object projectIdOrPath, String tagName, Integer linkId) throws GitLabApiException {
77+
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "releases", urlEncode(tagName), "assets", "links", linkId);
78+
return (response.readEntity(Link.class));
79+
}
80+
81+
/**
82+
* Get an Optional instance holding a Link instance for the specific tag name and link id.
83+
*
84+
* <pre><code>GitLab Endpoint: GET /projects/:id/releases/:tagName/assets/links/:linkId</code></pre>
85+
*
86+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
87+
* @param tagName the name of the tag to fetch the Link for
88+
* @param linkId the id of the Link to fetch for
89+
* @return an Optional instance with the specified Link as the value
90+
* @throws GitLabApiException if any exception occurs
91+
*/
92+
public Optional<Link> getOptionalLink(Object projectIdOrPath, String tagName, Integer linkId) throws GitLabApiException {
93+
try {
94+
return (Optional.ofNullable(getLink(projectIdOrPath, tagName, linkId)));
95+
} catch (GitLabApiException glae) {
96+
return (GitLabApi.createOptionalFromException(glae));
97+
}
98+
}
99+
100+
/**
101+
* Create a Link. You need push access to the repository to create a Link.
102+
*
103+
* <pre><code>GitLab Endpoint: POST /projects/:id/releases/:tagName/assets/links</code></pre>
104+
*
105+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
106+
* @param params a ReleaseLinksParams instance holding the parameters for the link
107+
* @return a Link instance containing the newly created Link info
108+
* @throws GitLabApiException if any exception occurs
109+
*/
110+
public Link createLink(Object projectIdOrPath, ReleaseLinkParams params) throws GitLabApiException {
111+
String tagName = params.getTagName();
112+
if (tagName == null || tagName.trim().isEmpty()) {
113+
throw new RuntimeException("params.tagName cannot be null or empty");
114+
}
115+
116+
String name = params.getName();
117+
if (name == null || name.trim().isEmpty()) {
118+
throw new RuntimeException("params.name cannot be null or empty");
119+
}
120+
121+
String url = params.getUrl();
122+
if (url == null || url.trim().isEmpty()) {
123+
throw new RuntimeException("params.url cannot be null or empty");
124+
}
125+
126+
Response response = post(Response.Status.CREATED, params,
127+
"projects", getProjectIdOrPath(projectIdOrPath), "releases", urlEncode(tagName), "assets", "links");
128+
return (response.readEntity(Link.class));
129+
}
130+
131+
/**
132+
* Updates the attributes of a given Link.
133+
*
134+
* <pre><code>GitLab Endpoint: PUT /projects/:id/releases/:tagName/assets/links/:linkId</code></pre>
135+
*
136+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
137+
* @param linkId the id of the Link to fetch for
138+
* @param params a ReleaseLinksParams instance holding the parameters for the Link
139+
* @return a Link instance containing info on the updated Link
140+
* @throws GitLabApiException if any exception occurs
141+
*/
142+
public Link updateLink(Object projectIdOrPath, Integer linkId, ReleaseLinkParams params) throws GitLabApiException {
143+
String tagName = params.getTagName();
144+
if (tagName == null || tagName.trim().isEmpty()) {
145+
throw new RuntimeException("params.tagName cannot be null or empty");
146+
}
147+
148+
if (linkId == null) {
149+
throw new RuntimeException("linkId cannot be null");
150+
}
151+
152+
Response response = put(Response.Status.OK, params,
153+
"projects", getProjectIdOrPath(projectIdOrPath), "releases", urlEncode(tagName), "assets", "links", linkId);
154+
return (response.readEntity(Link.class));
155+
}
156+
157+
/**
158+
* Delete a Link.
159+
*
160+
* <pre><code>GitLab Endpoint: DELETE /projects/:id/releases/:tagName/assets/links/:linkId</code></pre>
161+
*
162+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
163+
* @param tagName the tag name that the link was created from
164+
* @param linkId the id of the Link to delete
165+
* @throws GitLabApiException if any exception occurs
166+
*/
167+
public void deleteLink(Object projectIdOrPath, String tagName, Integer linkId) throws GitLabApiException {
168+
delete(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "releases", urlEncode(tagName), "assets", "links", linkId);
169+
}
170+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.api.utils.JacksonJson;
4+
5+
import java.util.Date;
6+
import java.util.List;
7+
8+
public class Link {
9+
10+
private Integer id;
11+
private String name;
12+
private String url;
13+
/**
14+
* @deprecated deprecated in GitLab 15.9, will be removed in GitLab 16.0.
15+
*/
16+
@Deprecated
17+
private Boolean external;
18+
private String linkType;
19+
20+
public Integer getId() {
21+
return id;
22+
}
23+
24+
public void setId(Integer id) {
25+
this.id = id;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
public String getUrl() {
37+
return url;
38+
}
39+
40+
public void setUrl(String url) {
41+
this.url = url;
42+
}
43+
44+
/**
45+
* @deprecated deprecated in GitLab 15.9, will be removed in GitLab 16.0.
46+
*/
47+
@Deprecated
48+
public Boolean getExternal() {
49+
return external;
50+
}
51+
52+
/**
53+
* @deprecated deprecated in GitLab 15.9, will be removed in GitLab 16.0.
54+
*/
55+
@Deprecated
56+
public void setExternal(Boolean external) {
57+
this.external = external;
58+
}
59+
60+
public String getLinkType() {
61+
return linkType;
62+
}
63+
64+
public void setLinkType(String linkType) {
65+
this.linkType = linkType;
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return (JacksonJson.toJsonString(this));
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.api.utils.JacksonJson;
4+
5+
import java.util.Date;
6+
import java.util.List;
7+
8+
public class ReleaseLinkParams {
9+
10+
private String name;
11+
private String tagName;
12+
private String url;
13+
private String filepath;
14+
private String linkType;
15+
16+
public String getName() {
17+
return name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public ReleaseLinkParams withName(String name) {
25+
this.name = name;
26+
return (this);
27+
}
28+
29+
public String getTagName() {
30+
return tagName;
31+
}
32+
33+
public void setTagName(String tagName) {
34+
this.tagName = tagName;
35+
}
36+
37+
public ReleaseLinkParams withTagName(String tagName) {
38+
this.tagName = tagName;
39+
return (this);
40+
}
41+
42+
public String getUrl() {
43+
return url;
44+
}
45+
46+
public void setUrl(String url) {
47+
this.url = url;
48+
}
49+
50+
public ReleaseLinkParams withUrl(String url) {
51+
this.url = url;
52+
return (this);
53+
}
54+
55+
public String getFilepath() {
56+
return filepath;
57+
}
58+
59+
public void setFilepath(String filepath) {
60+
this.filepath = filepath;
61+
}
62+
63+
public ReleaseLinkParams withFilepath(String filepath) {
64+
this.filepath = filepath;
65+
return (this);
66+
}
67+
68+
public String getLinkType() {
69+
return linkType;
70+
}
71+
72+
public void setLinkType(String linkType) {
73+
this.linkType = linkType;
74+
}
75+
76+
public ReleaseLinkParams withLinkType(String linkType) {
77+
this.linkType = linkType;
78+
return (this);
79+
}
80+
81+
@Override
82+
public String toString() {
83+
return (JacksonJson.toJsonString(this));
84+
}
85+
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import org.gitlab4j.api.models.Key;
7979
import org.gitlab4j.api.models.Label;
8080
import org.gitlab4j.api.models.LabelEvent;
81+
import org.gitlab4j.api.models.Link;
8182
import org.gitlab4j.api.models.Member;
8283
import org.gitlab4j.api.models.MergeRequest;
8384
import org.gitlab4j.api.models.MergeRequestDiff;
@@ -373,6 +374,12 @@ public void testLinkedIssues() throws Exception {
373374
assertTrue(compareJson(linkedIssues, "linked-issues.json"));
374375
}
375376

377+
@Test
378+
public void testLinks() throws Exception {
379+
List<Link> links = unmarshalResourceList(Link.class, "links.json");
380+
assertTrue(compareJson(links, "links.json"));
381+
}
382+
376383
@Test
377384
public void testCommitDiscussions() throws Exception {
378385
List<Discussion> discussions = unmarshalResourceList(Discussion.class, "commit-discussions.json");

0 commit comments

Comments
 (0)