|
| 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 | +} |
0 commit comments