|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2017 Greg Messner <[email protected]> |
| 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 | + |
1 | 24 | package org.gitlab4j.api;
|
2 | 25 |
|
3 | 26 | import java.io.UnsupportedEncodingException;
|
|
14 | 37 | import org.gitlab4j.api.models.Member;
|
15 | 38 | import org.gitlab4j.api.models.Project;
|
16 | 39 | import org.gitlab4j.api.models.ProjectHook;
|
| 40 | +import org.gitlab4j.api.models.Snippet; |
17 | 41 | import org.gitlab4j.api.models.Visibility;
|
18 | 42 |
|
19 | 43 | /**
|
@@ -1265,4 +1289,147 @@ public void deleteIssue(Integer projectId, Integer issueId) throws GitLabApiExce
|
1265 | 1289 | Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
|
1266 | 1290 | delete(expectedStatus, getDefaultPerPageParam(), "projects", projectId, "issues", issueId);
|
1267 | 1291 | }
|
| 1292 | + |
| 1293 | + /** |
| 1294 | + * Get a list of project snippets. This only returns the first page of snippets. |
| 1295 | + * |
| 1296 | + * GET /projects/:id/snippets |
| 1297 | + * |
| 1298 | + * @param projectId the project ID to get the snippets for |
| 1299 | + * @return a list of project's snippets |
| 1300 | + * @throws GitLabApiException if any exception occurs |
| 1301 | + */ |
| 1302 | + public List<Snippet> getSnippets(Integer projectId) throws GitLabApiException { |
| 1303 | + return (getSnippets(projectId, 1, this.getDefaultPerPage())); |
| 1304 | + } |
| 1305 | + |
| 1306 | + /** |
| 1307 | + * Get a list of project snippets. This only returns the first page of snippets. |
| 1308 | + * |
| 1309 | + * GET /projects/:id/snippets |
| 1310 | + * |
| 1311 | + * @param projectId the project ID to get the snippets for |
| 1312 | + * @param page the page to get |
| 1313 | + * @param perPage the number of snippets per page |
| 1314 | + * @return a list of project's snippets for the specified range |
| 1315 | + * @throws GitLabApiException if any exception occurs |
| 1316 | + */ |
| 1317 | + public List<Snippet> getSnippets(Integer projectId, int page, int perPage) throws GitLabApiException { |
| 1318 | + Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", projectId, "snippets"); |
| 1319 | + return (response.readEntity(new GenericType<List<Snippet>>() {})); |
| 1320 | + } |
| 1321 | + |
| 1322 | + /** |
| 1323 | + * Get a Pager of project's snippets. |
| 1324 | + * |
| 1325 | + * GET /projects/:id/snippets |
| 1326 | + * |
| 1327 | + * @param projectId the project ID to get the issues for |
| 1328 | + * @param itemsPerPage the number of snippets per page |
| 1329 | + * @return the Pager of snippets |
| 1330 | + * @throws GitLabApiException if any exception occurs |
| 1331 | + */ |
| 1332 | + public Pager<Snippet> getSnippets(Integer projectId, int itemsPerPage) throws GitLabApiException { |
| 1333 | + return (new Pager<Snippet>(this, Snippet.class, itemsPerPage, null, "projects", projectId, "snippets")); |
| 1334 | + } |
| 1335 | + |
| 1336 | + /** |
| 1337 | + * Get a single of project snippet. |
| 1338 | + * |
| 1339 | + * GET /projects/:id/snippets/:snippet_id |
| 1340 | + * |
| 1341 | + * @param projectId the project ID to get the snippet for |
| 1342 | + * @param snippetId the ID of the project's snippet |
| 1343 | + * @return the specified project Snippet |
| 1344 | + * @throws GitLabApiException if any exception occurs |
| 1345 | + */ |
| 1346 | + public Snippet getSnippet(Integer projectId, Integer snippetId) throws GitLabApiException { |
| 1347 | + Response response = get(Response.Status.OK, null, "projects", projectId, "snippets", snippetId); |
| 1348 | + return (response.readEntity(Snippet.class)); |
| 1349 | + } |
| 1350 | + |
| 1351 | + /** |
| 1352 | + * Creates a new project snippet. The user must have permission to create new snippets. |
| 1353 | + * |
| 1354 | + * POST /projects/:id/snippets |
| 1355 | + * |
| 1356 | + * @param id the ID of the project owned by the authenticated user, required |
| 1357 | + * @param title the title of a snippet, required |
| 1358 | + * @param fileName the name of a snippet file, required |
| 1359 | + * @param description the description of a snippet, optional |
| 1360 | + * @param code the content of a snippet, required |
| 1361 | + * @param visibility the snippet's visibility, required |
| 1362 | + * @return a Snippet instance with info on the created snippet |
| 1363 | + * @throws GitLabApiException if any exception occurs |
| 1364 | + */ |
| 1365 | + public Snippet createSnippet(Integer projectId, String title, String filename, String description, |
| 1366 | + String code, Visibility visibility) throws GitLabApiException { |
| 1367 | + |
| 1368 | + GitLabApiForm formData = new GitLabApiForm() |
| 1369 | + .withParam("title", title, true) |
| 1370 | + .withParam("file_name", filename, true) |
| 1371 | + .withParam("description", description) |
| 1372 | + .withParam("code", code, true) |
| 1373 | + .withParam("visibility", visibility, true); |
| 1374 | + |
| 1375 | + Response response = post(Response.Status.CREATED, formData, "projects", projectId, "snippets"); |
| 1376 | + return (response.readEntity(Snippet.class)); |
| 1377 | + } |
| 1378 | + |
| 1379 | + /** |
| 1380 | + * Updates an existing project snippet. The user must have permission to change an existing snippet. |
| 1381 | + * |
| 1382 | + * PUT /projects/:id/snippets/:snippet_id |
| 1383 | + * |
| 1384 | + * @param id the ID of the project owned by the authenticated user, required |
| 1385 | + * @param snippetId the ID of a project's snippet, required |
| 1386 | + * @param title the title of a snippet, optional |
| 1387 | + * @param fileName the name of a snippet file, optional |
| 1388 | + * @param description the description of a snippet, optioptionalonal |
| 1389 | + * @param code the content of a snippet, optional |
| 1390 | + * @param visibility the snippet's visibility, reqoptionaluired |
| 1391 | + * @return a Snippet instance with info on the updated snippet |
| 1392 | + * @throws GitLabApiException if any exception occurs |
| 1393 | + */ |
| 1394 | + public Snippet updateSnippet(Integer projectId, Integer snippetId, String title, String filename, String description, |
| 1395 | + String code, Visibility visibility) throws GitLabApiException { |
| 1396 | + |
| 1397 | + GitLabApiForm formData = new GitLabApiForm() |
| 1398 | + .withParam("title", title) |
| 1399 | + .withParam("file_name", filename) |
| 1400 | + .withParam("description", description) |
| 1401 | + .withParam("code", code) |
| 1402 | + .withParam("visibility", visibility); |
| 1403 | + |
| 1404 | + Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "snippets", snippetId); |
| 1405 | + return (response.readEntity(Snippet.class)); |
| 1406 | + } |
| 1407 | + |
| 1408 | + /* |
| 1409 | + * Deletes an existing project snippet. This is an idempotent function and deleting a |
| 1410 | + * non-existent snippet does not cause an error. |
| 1411 | + * |
| 1412 | + * DELETE /projects/:id/snippets/:snippet_id |
| 1413 | + * |
| 1414 | + * @param projectId the project ID of the snippet |
| 1415 | + * @param snippetId the ID of the project's snippet |
| 1416 | + * @throws GitLabApiException if any exception occurs |
| 1417 | + */ |
| 1418 | + public void deleteSnippet(Integer projectId, Integer snippetId) throws GitLabApiException { |
| 1419 | + delete(Response.Status.NO_CONTENT, null, "projects", projectId, "snippets", snippetId); |
| 1420 | + } |
| 1421 | + |
| 1422 | + /* |
| 1423 | + * Get the raw project snippet as plain text. |
| 1424 | + * |
| 1425 | + * GET /projects/:id/snippets/:snippet_id/raw |
| 1426 | + * |
| 1427 | + * @param projectId the project ID of the snippet |
| 1428 | + * @param snippetId the ID of the project's snippet |
| 1429 | + * @throws GitLabApiException if any exception occurs |
| 1430 | + */ |
| 1431 | + public String getRawSnippetContent(Integer projectId, Integer snippetId) throws GitLabApiException { |
| 1432 | + Response response = get(Response.Status.OK, null, "projects", projectId, "snippets", snippetId, "raw"); |
| 1433 | + return (response.readEntity(String.class)); |
| 1434 | + } |
1268 | 1435 | }
|
0 commit comments