|
| 1 | +package org.gitlab4j.api; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Optional; |
| 7 | + |
| 8 | +import javax.ws.rs.core.Form; |
| 9 | +import javax.ws.rs.core.GenericType; |
| 10 | +import javax.ws.rs.core.Response; |
| 11 | + |
| 12 | +import org.gitlab4j.api.GitLabApi.ApiVersion; |
| 13 | +import org.gitlab4j.api.models.Release; |
| 14 | +import org.gitlab4j.api.models.Tag; |
| 15 | +import org.gitlab4j.api.utils.FileUtils; |
| 16 | + |
| 17 | +/** |
| 18 | + * This class provides an entry point to all the GitLab Tags API calls. |
| 19 | + * |
| 20 | + * See https://docs.gitlab.com/ce/api/tags.html for more information on the GitLab Tags API. |
| 21 | + */ |
| 22 | +public class TagsApi extends AbstractApi { |
| 23 | + |
| 24 | + public TagsApi(GitLabApi gitLabApi) { |
| 25 | + super(gitLabApi); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Get a list of repository tags from a project, sorted by name in reverse alphabetical order. |
| 30 | + * |
| 31 | + * GET /projects/:id/repository/tags |
| 32 | + * |
| 33 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 34 | + * @return the list of tags for the specified project ID |
| 35 | + * @throws GitLabApiException if any exception occurs |
| 36 | + */ |
| 37 | + public List<Tag> getTags(Object projectIdOrPath) throws GitLabApiException { |
| 38 | + Response response = get(Response.Status.OK, getDefaultPerPageParam(), |
| 39 | + "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags"); |
| 40 | + return (response.readEntity(new GenericType<List<Tag>>() { })); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get a list of repository tags from a project, sorted by name in reverse alphabetical order and in the specified page range. |
| 45 | + * |
| 46 | + * GET /projects/:id/repository/tags |
| 47 | + * |
| 48 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 49 | + * @param page the page to get |
| 50 | + * @param perPage the number of Tag instances per page |
| 51 | + * @return the list of tags for the specified project ID |
| 52 | + * @throws GitLabApiException if any exception occurs |
| 53 | + */ |
| 54 | + public List<Tag> getTags(Object projectIdOrPath, int page, int perPage) throws GitLabApiException { |
| 55 | + Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), |
| 56 | + "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags"); |
| 57 | + return (response.readEntity(new GenericType<List<Tag>>() { })); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get a list of repository tags from a project, sorted by name in reverse alphabetical order. |
| 62 | + * |
| 63 | + * GET /projects/:id/repository/tags |
| 64 | + * |
| 65 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 66 | + * @param itemsPerPage the number of Project instances that will be fetched per page |
| 67 | + * @return the list of tags for the specified project ID |
| 68 | + * @throws GitLabApiException if any exception occurs |
| 69 | + */ |
| 70 | + public Pager<Tag> getTags(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException { |
| 71 | + return (new Pager<Tag>(this, Tag.class, itemsPerPage, null, "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags")); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Get a specific repository tag determined by its name. |
| 76 | + * |
| 77 | + * GET /projects/:id/repository/tags/:tagName |
| 78 | + * |
| 79 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 80 | + * @param tagName the name of the tag to fetch the info for |
| 81 | + * @return a Tag instance with info on the specified tag |
| 82 | + * @throws GitLabApiException if any exception occurs |
| 83 | + */ |
| 84 | + public Tag getTag(Object projectIdOrPath, String tagName) throws GitLabApiException { |
| 85 | + Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags", tagName); |
| 86 | + return (response.readEntity(Tag.class)); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Get an Optional instance holding a Tag instance of a specific repository tag determined by its name. |
| 91 | + * |
| 92 | + * GET /projects/:id/repository/tags/:tagName |
| 93 | + * |
| 94 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 95 | + * @param tagName the name of the tag to fetch the info for |
| 96 | + * @return an Optional instance with the specified project tag as the value |
| 97 | + * @throws GitLabApiException if any exception occurs |
| 98 | + */ |
| 99 | + public Optional<Tag> getOptionalTag(Object projectIdOrPath, String tagName) throws GitLabApiException { |
| 100 | + try { |
| 101 | + return (Optional.ofNullable(getTag(projectIdOrPath, tagName))); |
| 102 | + } catch (GitLabApiException glae) { |
| 103 | + return (GitLabApi.createOptionalFromException(glae)); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Creates a tag on a particular ref of the given project. A message and release notes are optional. |
| 109 | + * |
| 110 | + * POST /projects/:id/repository/tags |
| 111 | + * |
| 112 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 113 | + * @param tagName The name of the tag Must be unique for the project |
| 114 | + * @param ref the git ref to place the tag on |
| 115 | + * @param message the message to included with the tag (optional) |
| 116 | + * @param releaseNotes the release notes for the tag (optional) |
| 117 | + * @return a Tag instance containing info on the newly created tag |
| 118 | + * @throws GitLabApiException if any exception occurs |
| 119 | + */ |
| 120 | + public Tag createTag(Object projectIdOrPath, String tagName, String ref, String message, String releaseNotes) throws GitLabApiException { |
| 121 | + |
| 122 | + Form formData = new GitLabApiForm() |
| 123 | + .withParam("tag_name", tagName, true) |
| 124 | + .withParam("ref", ref, true) |
| 125 | + .withParam("message", message) |
| 126 | + .withParam("release_description", releaseNotes); |
| 127 | + Response response = post(Response.Status.CREATED, formData.asMap(), |
| 128 | + "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags"); |
| 129 | + return (response.readEntity(Tag.class)); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Creates a tag on a particular ref of a given project. A message and a File instance containing the |
| 134 | + * release notes are optional. This method is the same as {@link #createTag(Object, String, String, String, String)}, |
| 135 | + * but instead allows the release notes to be supplied in a file. |
| 136 | + * |
| 137 | + * POST /projects/:id/repository/tags |
| 138 | + * |
| 139 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 140 | + * @param tagName the name of the tag, must be unique for the project |
| 141 | + * @param ref the git ref to place the tag on |
| 142 | + * @param message the message to included with the tag (optional) |
| 143 | + * @param releaseNotesFile a whose contents are the release notes (optional) |
| 144 | + * @return a Tag instance containing info on the newly created tag |
| 145 | + * @throws GitLabApiException if any exception occurs |
| 146 | + */ |
| 147 | + public Tag createTag(Object projectIdOrPath, String tagName, String ref, String message, File releaseNotesFile) throws GitLabApiException { |
| 148 | + |
| 149 | + String releaseNotes; |
| 150 | + if (releaseNotesFile != null) { |
| 151 | + try { |
| 152 | + releaseNotes = FileUtils.readFileContents(releaseNotesFile); |
| 153 | + } catch (IOException ioe) { |
| 154 | + throw (new GitLabApiException(ioe)); |
| 155 | + } |
| 156 | + } else { |
| 157 | + releaseNotes = null; |
| 158 | + } |
| 159 | + |
| 160 | + return (createTag(projectIdOrPath, tagName, ref, message, releaseNotes)); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Deletes the tag from a project with the specified tag name. |
| 165 | + * |
| 166 | + * DELETE /projects/:id/repository/tags/:tag_name |
| 167 | + * |
| 168 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 169 | + * @param tagName The name of the tag to delete |
| 170 | + * @throws GitLabApiException if any exception occurs |
| 171 | + */ |
| 172 | + public void deleteTag(Object projectIdOrPath, String tagName) throws GitLabApiException { |
| 173 | + Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT); |
| 174 | + delete(expectedStatus, null, "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags", tagName); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Add release notes to the existing git tag. |
| 179 | + * |
| 180 | + * POST /projects/:id/repository/tags/:tagName/release |
| 181 | + * |
| 182 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 183 | + * @param tagName the name of a tag |
| 184 | + * @param releaseNotes release notes with markdown support |
| 185 | + * @return a Tag instance containing info on the newly created tag |
| 186 | + * @throws GitLabApiException if any exception occurs |
| 187 | + */ |
| 188 | + public Release createRelease(Object projectIdOrPath, String tagName, String releaseNotes) throws GitLabApiException { |
| 189 | + Form formData = new GitLabApiForm().withParam("description", releaseNotes); |
| 190 | + Response response = post(Response.Status.CREATED, formData.asMap(), |
| 191 | + "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags", tagName, "release"); |
| 192 | + return (response.readEntity(Release.class)); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Updates the release notes of a given release. |
| 197 | + * |
| 198 | + * PUT /projects/:id/repository/tags/:tagName/release |
| 199 | + * |
| 200 | + * @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path |
| 201 | + * @param tagName the name of a tag |
| 202 | + * @param releaseNotes release notes with markdown support |
| 203 | + * @return a Tag instance containing info on the newly created tag |
| 204 | + * @throws GitLabApiException if any exception occurs |
| 205 | + */ |
| 206 | + public Release updateRelease(Object projectIdOrPath, String tagName, String releaseNotes) throws GitLabApiException { |
| 207 | + Form formData = new GitLabApiForm().withParam("description", releaseNotes); |
| 208 | + Response response = put(Response.Status.CREATED, formData.asMap(), |
| 209 | + "projects", getProjectIdOrPath(projectIdOrPath), "repository", "tags", tagName, "release"); |
| 210 | + return (response.readEntity(Release.class)); |
| 211 | + } |
| 212 | +} |
0 commit comments