Skip to content

Commit 63ee9bf

Browse files
committed
Added support for project snippets.
1 parent f336a43 commit 63ee9bf

File tree

9 files changed

+521
-32
lines changed

9 files changed

+521
-32
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To utilize the GitLab API for Java in your project, simply add the following dep
1111
```java
1212
dependencies {
1313
...
14-
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.4.7'
14+
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.4.8'
1515
}
1616
```
1717

@@ -20,11 +20,11 @@ dependencies {
2020
<dependency>
2121
<groupId>org.gitlab4j</groupId>
2222
<artifactId>gitlab4j-api</artifactId>
23-
<version>4.4.7</version>
23+
<version>4.4.8</version>
2424
</dependency>
2525
```
2626

27-
If you are not using Gradle or Maven you can download the latest gitlab4j-api JAR file here: [gitlab4j-api-4.4.7.jar](https://oss.sonatype.org/service/local/repositories/releases/content/org/gitlab4j/gitlab4j-api/4.4.7/gitlab4j-api-4.4.7.jar "Download JAR")
27+
If you are not using Gradle or Maven you can download the latest gitlab4j-api JAR file here: [gitlab4j-api-4.4.8.jar](https://oss.sonatype.org/service/local/repositories/releases/content/org/gitlab4j/gitlab4j-api/4.4.8/gitlab4j-api-4.4.8.jar "Download JAR")
2828

2929
Javadocs are available here: <a href="http://www.messners.com/gitlab4j-api/javadocs/index.html?org/gitlab4j/api/package-summary.html" target="_top">Javadocs</a>
3030

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

+167
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
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+
124
package org.gitlab4j.api;
225

326
import java.io.UnsupportedEncodingException;
@@ -14,6 +37,7 @@
1437
import org.gitlab4j.api.models.Member;
1538
import org.gitlab4j.api.models.Project;
1639
import org.gitlab4j.api.models.ProjectHook;
40+
import org.gitlab4j.api.models.Snippet;
1741
import org.gitlab4j.api.models.Visibility;
1842

1943
/**
@@ -1265,4 +1289,147 @@ public void deleteIssue(Integer projectId, Integer issueId) throws GitLabApiExce
12651289
Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
12661290
delete(expectedStatus, getDefaultPerPageParam(), "projects", projectId, "issues", issueId);
12671291
}
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+
}
12681435
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
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+
124
package org.gitlab4j.api.models;
225

3-
import java.util.HashMap;
4-
import java.util.Map;
26+
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
527

628
import com.fasterxml.jackson.annotation.JsonCreator;
729
import com.fasterxml.jackson.annotation.JsonValue;
@@ -13,24 +35,20 @@ public enum JobStatus {
1335

1436
RUNNING, PENDING, SUCCESS, FAILED, CANCELED, SKIPPED;
1537

16-
private static Map<String, JobStatus> valuesMap = new HashMap<>(6);
17-
static {
18-
for (JobStatus status : JobStatus.values())
19-
valuesMap.put(status.toValue(), status);
20-
}
38+
private static JacksonJsonEnumHelper<JobStatus> enumHelper = new JacksonJsonEnumHelper<>(JobStatus.class);
2139

2240
@JsonCreator
2341
public static JobStatus forValue(String value) {
24-
return valuesMap.get(value);
25-
}
42+
return enumHelper.forValue(value);
43+
}
2644

2745
@JsonValue
2846
public String toValue() {
29-
return (name().toLowerCase());
30-
}
47+
return (enumHelper.toString(this));
48+
}
3149

3250
@Override
3351
public String toString() {
34-
return (name().toLowerCase());
35-
}
52+
return (enumHelper.toString(this));
53+
}
3654
}

src/main/java/org/gitlab4j/api/models/ProjectSnippet.java renamed to src/main/java/org/gitlab4j/api/models/Snippet.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
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+
124
package org.gitlab4j.api.models;
225

326
import java.util.Date;
@@ -8,7 +31,7 @@
831

932
@XmlRootElement
1033
@XmlAccessorType(XmlAccessType.FIELD)
11-
public class ProjectSnippet {
34+
public class Snippet {
1235

1336
private Author author;
1437
private Date createdAt;
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
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+
124
package org.gitlab4j.api.models;
225

3-
import java.util.HashMap;
4-
import java.util.Map;
26+
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
527

628
import com.fasterxml.jackson.annotation.JsonCreator;
729
import com.fasterxml.jackson.annotation.JsonValue;
@@ -10,24 +32,20 @@ public enum Visibility {
1032

1133
PUBLIC, PRIVATE, INTERNAL;
1234

13-
private static Map<String, Visibility> valuesMap = new HashMap<>(3);
14-
static {
15-
for (Visibility visibility : Visibility.values())
16-
valuesMap.put(visibility.toValue(), visibility);
17-
}
35+
private static JacksonJsonEnumHelper<Visibility> enumHelper = new JacksonJsonEnumHelper<>(Visibility.class);
1836

1937
@JsonCreator
2038
public static Visibility forValue(String value) {
21-
return valuesMap.get(value);
39+
return enumHelper.forValue(value);
2240
}
2341

2442
@JsonValue
2543
public String toValue() {
26-
return (name().toLowerCase());
44+
return (enumHelper.toString(this));
2745
}
2846

2947
@Override
3048
public String toString() {
31-
return (name().toLowerCase());
49+
return (enumHelper.toString(this));
3250
}
3351
}

0 commit comments

Comments
 (0)