Skip to content

Commit 6f5736c

Browse files
faustovazgmessner
authored andcommitted
Add support for Snippets API (#183)
1 parent 1382203 commit 6f5736c

File tree

4 files changed

+372
-0
lines changed

4 files changed

+372
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public String getApiNamespace() {
7171
private LabelsApi labelsApi;
7272
private NotesApi notesApi;
7373
private EventsApi eventsApi;
74+
private SnippetsApi snippetsApi;
7475

7576
/**
7677
* Get the GitLab4J shared Logger instance.
@@ -1276,4 +1277,22 @@ public static final <T> T orElseThrow(Optional<T> optional) throws GitLabApiExce
12761277

12771278
return (optional.get());
12781279
}
1280+
1281+
/**
1282+
* Gets the SnippetsApi instance owned by this GitLabApi instance. The SnippetsApi is used
1283+
* to perform all snippet related API calls.
1284+
*
1285+
* @return the SnippetsApi instance owned by this GitLabApi instance
1286+
*/
1287+
public SnippetsApi getSnippetApi() {
1288+
if (snippetsApi == null) {
1289+
synchronized (this) {
1290+
if (snippetsApi == null) {
1291+
snippetsApi = new SnippetsApi(this);
1292+
}
1293+
}
1294+
}
1295+
1296+
return snippetsApi;
1297+
}
12791298
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package org.gitlab4j.api;
2+
3+
import java.util.List;
4+
import javax.ws.rs.core.GenericType;
5+
import javax.ws.rs.core.Response;
6+
import org.gitlab4j.api.models.Snippet;
7+
import org.gitlab4j.api.models.Visibility;
8+
9+
/**
10+
* This class provides an entry point to all the GitLab Snippets API project calls.
11+
*/
12+
public class SnippetsApi extends AbstractApi {
13+
14+
public SnippetsApi(GitLabApi gitLabApi) {
15+
super(gitLabApi);
16+
}
17+
18+
/**
19+
* Create a new Snippet.
20+
*
21+
* @param title the title of the snippet
22+
* @param fileName the file name of the snippet
23+
* @param content the content of the snippet
24+
* @return the created Snippet
25+
* @throws GitLabApiException if any exception occurs
26+
*/
27+
public Snippet createSnippet(String title, String fileName, String content) throws GitLabApiException {
28+
GitLabApiForm formData = new GitLabApiForm()
29+
.withParam("title", title, true)
30+
.withParam("file_name", fileName, true)
31+
.withParam("content", content, true);
32+
33+
Response response = post(Response.Status.CREATED, formData, "snippets");
34+
return (response.readEntity(Snippet.class));
35+
}
36+
37+
/**
38+
* Create a new Snippet.
39+
*
40+
* @param title the title of the snippet
41+
* @param fileName the file name of the snippet
42+
* @param content the content of the snippet
43+
* @param visibility the visibility (Public, Internal, Private) of the snippet
44+
* @param description the description of the snippet
45+
* @return the created Snippet
46+
* @throws GitLabApiException if any exception occurs
47+
*/
48+
public Snippet createSnippet(String title, String fileName, String content, Visibility visibility, String description) throws GitLabApiException {
49+
GitLabApiForm formData = new GitLabApiForm()
50+
.withParam("title", title, true)
51+
.withParam("file_name", fileName, true)
52+
.withParam("content", content, true)
53+
.withParam("visibility", visibility)
54+
.withParam("description", description);
55+
56+
Response response = post(Response.Status.CREATED, formData, "snippets");
57+
return (response.readEntity(Snippet.class));
58+
}
59+
60+
/**
61+
* Removes Snippet
62+
*
63+
* DELETE /snippets/:id
64+
*
65+
* @param snippetId the snippet ID to remove
66+
* @throws GitLabApiException if any exception occurs
67+
*/
68+
public void deleteSnippet(Integer snippetId) throws GitLabApiException {
69+
if (snippetId == null) {
70+
throw new RuntimeException("snippetId can't be null");
71+
}
72+
delete(Response.Status.NO_CONTENT, null, "snippets", snippetId);
73+
}
74+
75+
/**
76+
* Get a list of Authenticated User's Snippets.
77+
*
78+
* GET /snippets
79+
*
80+
* @param downloadContent indicating whether to download the snippet content
81+
* @return a list of authenticated user's snippets
82+
* @throws GitLabApiException if any exception occurs
83+
*/
84+
public List<Snippet> getSnippets(boolean downloadContent) throws GitLabApiException {
85+
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "snippets");
86+
List<Snippet> snippets = (response.readEntity(new GenericType<List<Snippet>>() {}));
87+
88+
if(downloadContent) {
89+
for (Snippet snippet : snippets) {
90+
snippet.setContent(getSnippetContent(snippet.getId()));
91+
}
92+
}
93+
94+
return snippets;
95+
}
96+
97+
/**
98+
* Get a list of Authenticated User's Snippets.
99+
*
100+
* GET /snippets
101+
*
102+
* @return a list of authenticated user's snippets
103+
* @throws GitLabApiException if any exception occurs
104+
*/
105+
public List<Snippet> getSnippets() throws GitLabApiException {
106+
return getSnippets(false);
107+
}
108+
109+
/**
110+
* Get a the content of a Snippet
111+
*
112+
* GET /snippets/id/raw
113+
*
114+
* @param snippetId the snippet ID to remove
115+
* @return the content of snippet
116+
* @throws GitLabApiException if any exception occurs
117+
*/
118+
public String getSnippetContent(Integer snippetId) throws GitLabApiException {
119+
Response response = get(Response.Status.OK, null, "snippets", snippetId, "raw");
120+
return (response.readEntity(String.class));
121+
}
122+
123+
/**
124+
* Get a specific Snippet
125+
*
126+
* @param snippetId the snippet ID to remove
127+
* @param downloadContent indicating whether to download the snippet content
128+
* @return the snippet with the given id
129+
* @throws GitLabApiException if any exception occurs
130+
*/
131+
public Snippet getSnippet(Integer snippetId, boolean downloadContent) throws GitLabApiException {
132+
if (snippetId == null) {
133+
throw new RuntimeException("snippetId can't be null");
134+
}
135+
136+
Response response = get(Response.Status.OK, null, "snippets", snippetId);
137+
Snippet snippet = response.readEntity(Snippet.class);
138+
139+
if(downloadContent) {
140+
snippet.setContent(getSnippetContent(snippet.getId()));
141+
}
142+
143+
return snippet;
144+
}
145+
146+
/**
147+
* Get a specific Snippet
148+
*
149+
* @param snippetId the snippet ID to remove
150+
* @return the snippet with the given id
151+
* @throws GitLabApiException if any exception occurs
152+
*/
153+
public Snippet getSnippet(Integer snippetId) throws GitLabApiException {
154+
return getSnippet(snippetId, false);
155+
}
156+
157+
}

src/main/java/org/gitlab4j/api/models/Snippet.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,27 @@ public class Snippet {
4141
private String title;
4242
private String updatedAt;
4343
private String webUrl;
44+
private String content;
45+
private String rawUrl;
46+
private Visibility visibility;
47+
private String description;
4448

49+
public Snippet() {
50+
51+
}
52+
53+
public Snippet(String title, String fileName, String content, Visibility visibility, String description) {
54+
this(title, fileName, content);
55+
this.visibility = visibility;
56+
this.description = description;
57+
}
58+
59+
public Snippet(String title, String fileName, String content) {
60+
this.title = title;
61+
this.fileName = fileName;
62+
this.content = content;
63+
}
64+
4565
public Author getAuthor() {
4666
return this.author;
4767
}
@@ -105,4 +125,36 @@ public String getWebUrl() {
105125
public void setWebUrl(String webUrl) {
106126
this.webUrl = webUrl;
107127
}
128+
129+
public String getContent() {
130+
return content;
131+
}
132+
133+
public void setContent(String content) {
134+
this.content = content;
135+
}
136+
137+
public String getRawUrl() {
138+
return rawUrl;
139+
}
140+
141+
public void setRawUrl(String rawUrl) {
142+
this.rawUrl = rawUrl;
143+
}
144+
145+
public Visibility getVisibility() {
146+
return visibility;
147+
}
148+
149+
public void setVisibility(Visibility visibility) {
150+
this.visibility = visibility;
151+
}
152+
153+
public String getDescription() {
154+
return description;
155+
}
156+
157+
public void setDescription(String description) {
158+
this.description = description;
159+
}
108160
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package org.gitlab4j.api;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNull;
6+
import static org.junit.Assert.assertTrue;
7+
8+
import java.util.List;
9+
10+
import org.gitlab4j.api.GitLabApi.ApiVersion;
11+
import org.gitlab4j.api.models.Snippet;
12+
import org.gitlab4j.api.models.Visibility;
13+
import org.junit.BeforeClass;
14+
import org.junit.Test;
15+
16+
public class TestSnippetsApi {
17+
18+
private static final String TEST_HOST_URL;
19+
private static final String TEST_PRIVATE_TOKEN;
20+
21+
static {
22+
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
23+
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
24+
}
25+
26+
private static GitLabApi gitLabApi;
27+
private static final String TEST_SNIPPET_TITLE_1 = "test-snippet-title-1";
28+
private static final String TEST_SNIPPET_FILE_NAME_1 = "test-snippet-file-name-1";
29+
private static final String TEST_SNIPPET_CONTENT_1 = "test-snippet-content-1";
30+
private static final String TEST_SNIPPET_CONTENT_2 = "test-snippet-content-2";
31+
private static final String TEST_SNIPPET_DESCRIPTION_1 = "test-snippet-description-1";
32+
33+
34+
@BeforeClass
35+
public static void setup() {
36+
37+
String problems = "";
38+
39+
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) {
40+
problems += "TEST_HOST_URL cannot be empty\n";
41+
}
42+
43+
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) {
44+
problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
45+
}
46+
47+
if (problems.isEmpty()) {
48+
gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
49+
} else {
50+
System.err.print(problems);
51+
}
52+
}
53+
54+
@Test
55+
public void testCreate() throws GitLabApiException {
56+
Snippet snippet = createSnippet(new Snippet(TEST_SNIPPET_TITLE_1,
57+
TEST_SNIPPET_FILE_NAME_1,
58+
TEST_SNIPPET_CONTENT_1));
59+
assertEquals(TEST_SNIPPET_TITLE_1, snippet.getTitle());
60+
assertEquals(TEST_SNIPPET_FILE_NAME_1, snippet.getFileName());
61+
assertNull(snippet.getContent());
62+
63+
deleteSnippet(snippet);
64+
}
65+
66+
@Test
67+
public void testDelete() throws GitLabApiException {
68+
Snippet snippet = createSnippet(new Snippet(TEST_SNIPPET_TITLE_1,
69+
TEST_SNIPPET_FILE_NAME_1,
70+
TEST_SNIPPET_CONTENT_1));
71+
deleteSnippet(snippet);
72+
73+
SnippetsApi api = gitLabApi.getSnippetApi();
74+
List<Snippet> snippets = api.getSnippets();
75+
boolean found = snippets.stream().anyMatch(
76+
s -> s.getId().equals(snippet.getId()));
77+
assertFalse(found);
78+
}
79+
80+
@Test
81+
public void testList() throws GitLabApiException {
82+
Snippet snippet1 = createSnippet(new Snippet(TEST_SNIPPET_TITLE_1,
83+
TEST_SNIPPET_FILE_NAME_1,
84+
TEST_SNIPPET_CONTENT_1));
85+
Snippet snippet2 = createSnippet(new Snippet(TEST_SNIPPET_TITLE_1,
86+
TEST_SNIPPET_FILE_NAME_1,
87+
TEST_SNIPPET_CONTENT_2));
88+
89+
SnippetsApi api = gitLabApi.getSnippetApi();
90+
List<Snippet> snippets = api.getSnippets(true);
91+
92+
assertTrue(snippets.size() >= 2);
93+
assertTrue(snippets.stream().anyMatch(s -> s.getContent().equals(TEST_SNIPPET_CONTENT_1)));
94+
assertTrue(snippets.stream().anyMatch(s -> s.getContent().equals(TEST_SNIPPET_CONTENT_2)));
95+
96+
97+
deleteSnippet(snippet1);
98+
deleteSnippet(snippet2);
99+
}
100+
101+
@Test
102+
public void testSnippetContent() throws GitLabApiException {
103+
Snippet snippet = createSnippet(
104+
new Snippet(TEST_SNIPPET_TITLE_1, TEST_SNIPPET_FILE_NAME_1, TEST_SNIPPET_CONTENT_1));
105+
SnippetsApi api = gitLabApi.getSnippetApi();
106+
String snippetContent = api.getSnippetContent(snippet.getId());
107+
assertEquals(TEST_SNIPPET_CONTENT_1, snippetContent);
108+
deleteSnippet(snippet);
109+
}
110+
111+
@Test
112+
public void testRetrieveSnippet() throws GitLabApiException {
113+
Snippet snippet = createSnippet (new Snippet(TEST_SNIPPET_TITLE_1,
114+
TEST_SNIPPET_FILE_NAME_1,
115+
TEST_SNIPPET_CONTENT_1,
116+
Visibility.INTERNAL,
117+
TEST_SNIPPET_DESCRIPTION_1));
118+
119+
SnippetsApi api = gitLabApi.getSnippetApi();
120+
Snippet savedSnippet = api.getSnippet(snippet.getId(), true);
121+
122+
assertEquals(TEST_SNIPPET_TITLE_1, savedSnippet.getTitle());
123+
assertEquals(TEST_SNIPPET_FILE_NAME_1, savedSnippet.getFileName());
124+
assertEquals(TEST_SNIPPET_CONTENT_1, savedSnippet.getContent());
125+
assertEquals(TEST_SNIPPET_DESCRIPTION_1, savedSnippet.getDescription());
126+
127+
deleteSnippet(savedSnippet);
128+
}
129+
130+
public void deleteSnippet(Snippet snippet) throws GitLabApiException {
131+
SnippetsApi api = gitLabApi.getSnippetApi();
132+
api.deleteSnippet(snippet.getId());
133+
}
134+
135+
public Snippet createSnippet(Snippet snippet) throws GitLabApiException {
136+
SnippetsApi api = gitLabApi.getSnippetApi();
137+
return api.createSnippet(snippet.getTitle(),
138+
snippet.getFileName(),
139+
snippet.getContent(),
140+
snippet.getVisibility(),
141+
snippet.getDescription());
142+
}
143+
144+
}

0 commit comments

Comments
 (0)