Commit 6f5736c4 authored by Jefferson Fausto Vaz's avatar Jefferson Fausto Vaz Committed by Greg Messner
Browse files

Add support for Snippets API (#183)

parent 13822039
...@@ -71,6 +71,7 @@ public class GitLabApi { ...@@ -71,6 +71,7 @@ public class GitLabApi {
private LabelsApi labelsApi; private LabelsApi labelsApi;
private NotesApi notesApi; private NotesApi notesApi;
private EventsApi eventsApi; private EventsApi eventsApi;
private SnippetsApi snippetsApi;
/** /**
* Get the GitLab4J shared Logger instance. * Get the GitLab4J shared Logger instance.
...@@ -1276,4 +1277,22 @@ public class GitLabApi { ...@@ -1276,4 +1277,22 @@ public class GitLabApi {
return (optional.get()); return (optional.get());
} }
/**
* Gets the SnippetsApi instance owned by this GitLabApi instance. The SnippetsApi is used
* to perform all snippet related API calls.
*
* @return the SnippetsApi instance owned by this GitLabApi instance
*/
public SnippetsApi getSnippetApi() {
if (snippetsApi == null) {
synchronized (this) {
if (snippetsApi == null) {
snippetsApi = new SnippetsApi(this);
}
}
}
return snippetsApi;
}
} }
package org.gitlab4j.api;
import java.util.List;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.Snippet;
import org.gitlab4j.api.models.Visibility;
/**
* This class provides an entry point to all the GitLab Snippets API project calls.
*/
public class SnippetsApi extends AbstractApi {
public SnippetsApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
/**
* Create a new Snippet.
*
* @param title the title of the snippet
* @param fileName the file name of the snippet
* @param content the content of the snippet
* @return the created Snippet
* @throws GitLabApiException if any exception occurs
*/
public Snippet createSnippet(String title, String fileName, String content) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("title", title, true)
.withParam("file_name", fileName, true)
.withParam("content", content, true);
Response response = post(Response.Status.CREATED, formData, "snippets");
return (response.readEntity(Snippet.class));
}
/**
* Create a new Snippet.
*
* @param title the title of the snippet
* @param fileName the file name of the snippet
* @param content the content of the snippet
* @param visibility the visibility (Public, Internal, Private) of the snippet
* @param description the description of the snippet
* @return the created Snippet
* @throws GitLabApiException if any exception occurs
*/
public Snippet createSnippet(String title, String fileName, String content, Visibility visibility, String description) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("title", title, true)
.withParam("file_name", fileName, true)
.withParam("content", content, true)
.withParam("visibility", visibility)
.withParam("description", description);
Response response = post(Response.Status.CREATED, formData, "snippets");
return (response.readEntity(Snippet.class));
}
/**
* Removes Snippet
*
* DELETE /snippets/:id
*
* @param snippetId the snippet ID to remove
* @throws GitLabApiException if any exception occurs
*/
public void deleteSnippet(Integer snippetId) throws GitLabApiException {
if (snippetId == null) {
throw new RuntimeException("snippetId can't be null");
}
delete(Response.Status.NO_CONTENT, null, "snippets", snippetId);
}
/**
* Get a list of Authenticated User's Snippets.
*
* GET /snippets
*
* @param downloadContent indicating whether to download the snippet content
* @return a list of authenticated user's snippets
* @throws GitLabApiException if any exception occurs
*/
public List<Snippet> getSnippets(boolean downloadContent) throws GitLabApiException {
Response response = get(Response.Status.OK, getDefaultPerPageParam(), "snippets");
List<Snippet> snippets = (response.readEntity(new GenericType<List<Snippet>>() {}));
if(downloadContent) {
for (Snippet snippet : snippets) {
snippet.setContent(getSnippetContent(snippet.getId()));
}
}
return snippets;
}
/**
* Get a list of Authenticated User's Snippets.
*
* GET /snippets
*
* @return a list of authenticated user's snippets
* @throws GitLabApiException if any exception occurs
*/
public List<Snippet> getSnippets() throws GitLabApiException {
return getSnippets(false);
}
/**
* Get a the content of a Snippet
*
* GET /snippets/id/raw
*
* @param snippetId the snippet ID to remove
* @return the content of snippet
* @throws GitLabApiException if any exception occurs
*/
public String getSnippetContent(Integer snippetId) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "snippets", snippetId, "raw");
return (response.readEntity(String.class));
}
/**
* Get a specific Snippet
*
* @param snippetId the snippet ID to remove
* @param downloadContent indicating whether to download the snippet content
* @return the snippet with the given id
* @throws GitLabApiException if any exception occurs
*/
public Snippet getSnippet(Integer snippetId, boolean downloadContent) throws GitLabApiException {
if (snippetId == null) {
throw new RuntimeException("snippetId can't be null");
}
Response response = get(Response.Status.OK, null, "snippets", snippetId);
Snippet snippet = response.readEntity(Snippet.class);
if(downloadContent) {
snippet.setContent(getSnippetContent(snippet.getId()));
}
return snippet;
}
/**
* Get a specific Snippet
*
* @param snippetId the snippet ID to remove
* @return the snippet with the given id
* @throws GitLabApiException if any exception occurs
*/
public Snippet getSnippet(Integer snippetId) throws GitLabApiException {
return getSnippet(snippetId, false);
}
}
...@@ -41,7 +41,27 @@ public class Snippet { ...@@ -41,7 +41,27 @@ public class Snippet {
private String title; private String title;
private String updatedAt; private String updatedAt;
private String webUrl; private String webUrl;
private String content;
private String rawUrl;
private Visibility visibility;
private String description;
public Snippet() {
}
public Snippet(String title, String fileName, String content, Visibility visibility, String description) {
this(title, fileName, content);
this.visibility = visibility;
this.description = description;
}
public Snippet(String title, String fileName, String content) {
this.title = title;
this.fileName = fileName;
this.content = content;
}
public Author getAuthor() { public Author getAuthor() {
return this.author; return this.author;
} }
...@@ -105,4 +125,36 @@ public class Snippet { ...@@ -105,4 +125,36 @@ public class Snippet {
public void setWebUrl(String webUrl) { public void setWebUrl(String webUrl) {
this.webUrl = webUrl; this.webUrl = webUrl;
} }
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getRawUrl() {
return rawUrl;
}
public void setRawUrl(String rawUrl) {
this.rawUrl = rawUrl;
}
public Visibility getVisibility() {
return visibility;
}
public void setVisibility(Visibility visibility) {
this.visibility = visibility;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
} }
package org.gitlab4j.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Snippet;
import org.gitlab4j.api.models.Visibility;
import org.junit.BeforeClass;
import org.junit.Test;
public class TestSnippetsApi {
private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN;
static {
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
}
private static GitLabApi gitLabApi;
private static final String TEST_SNIPPET_TITLE_1 = "test-snippet-title-1";
private static final String TEST_SNIPPET_FILE_NAME_1 = "test-snippet-file-name-1";
private static final String TEST_SNIPPET_CONTENT_1 = "test-snippet-content-1";
private static final String TEST_SNIPPET_CONTENT_2 = "test-snippet-content-2";
private static final String TEST_SNIPPET_DESCRIPTION_1 = "test-snippet-description-1";
@BeforeClass
public static void setup() {
String problems = "";
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) {
problems += "TEST_HOST_URL cannot be empty\n";
}
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) {
problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
}
if (problems.isEmpty()) {
gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
} else {
System.err.print(problems);
}
}
@Test
public void testCreate() throws GitLabApiException {
Snippet snippet = createSnippet(new Snippet(TEST_SNIPPET_TITLE_1,
TEST_SNIPPET_FILE_NAME_1,
TEST_SNIPPET_CONTENT_1));
assertEquals(TEST_SNIPPET_TITLE_1, snippet.getTitle());
assertEquals(TEST_SNIPPET_FILE_NAME_1, snippet.getFileName());
assertNull(snippet.getContent());
deleteSnippet(snippet);
}
@Test
public void testDelete() throws GitLabApiException {
Snippet snippet = createSnippet(new Snippet(TEST_SNIPPET_TITLE_1,
TEST_SNIPPET_FILE_NAME_1,
TEST_SNIPPET_CONTENT_1));
deleteSnippet(snippet);
SnippetsApi api = gitLabApi.getSnippetApi();
List<Snippet> snippets = api.getSnippets();
boolean found = snippets.stream().anyMatch(
s -> s.getId().equals(snippet.getId()));
assertFalse(found);
}
@Test
public void testList() throws GitLabApiException {
Snippet snippet1 = createSnippet(new Snippet(TEST_SNIPPET_TITLE_1,
TEST_SNIPPET_FILE_NAME_1,
TEST_SNIPPET_CONTENT_1));
Snippet snippet2 = createSnippet(new Snippet(TEST_SNIPPET_TITLE_1,
TEST_SNIPPET_FILE_NAME_1,
TEST_SNIPPET_CONTENT_2));
SnippetsApi api = gitLabApi.getSnippetApi();
List<Snippet> snippets = api.getSnippets(true);
assertTrue(snippets.size() >= 2);
assertTrue(snippets.stream().anyMatch(s -> s.getContent().equals(TEST_SNIPPET_CONTENT_1)));
assertTrue(snippets.stream().anyMatch(s -> s.getContent().equals(TEST_SNIPPET_CONTENT_2)));
deleteSnippet(snippet1);
deleteSnippet(snippet2);
}
@Test
public void testSnippetContent() throws GitLabApiException {
Snippet snippet = createSnippet(
new Snippet(TEST_SNIPPET_TITLE_1, TEST_SNIPPET_FILE_NAME_1, TEST_SNIPPET_CONTENT_1));
SnippetsApi api = gitLabApi.getSnippetApi();
String snippetContent = api.getSnippetContent(snippet.getId());
assertEquals(TEST_SNIPPET_CONTENT_1, snippetContent);
deleteSnippet(snippet);
}
@Test
public void testRetrieveSnippet() throws GitLabApiException {
Snippet snippet = createSnippet (new Snippet(TEST_SNIPPET_TITLE_1,
TEST_SNIPPET_FILE_NAME_1,
TEST_SNIPPET_CONTENT_1,
Visibility.INTERNAL,
TEST_SNIPPET_DESCRIPTION_1));
SnippetsApi api = gitLabApi.getSnippetApi();
Snippet savedSnippet = api.getSnippet(snippet.getId(), true);
assertEquals(TEST_SNIPPET_TITLE_1, savedSnippet.getTitle());
assertEquals(TEST_SNIPPET_FILE_NAME_1, savedSnippet.getFileName());
assertEquals(TEST_SNIPPET_CONTENT_1, savedSnippet.getContent());
assertEquals(TEST_SNIPPET_DESCRIPTION_1, savedSnippet.getDescription());
deleteSnippet(savedSnippet);
}
public void deleteSnippet(Snippet snippet) throws GitLabApiException {
SnippetsApi api = gitLabApi.getSnippetApi();
api.deleteSnippet(snippet.getId());
}
public Snippet createSnippet(Snippet snippet) throws GitLabApiException {
SnippetsApi api = gitLabApi.getSnippetApi();
return api.createSnippet(snippet.getTitle(),
snippet.getFileName(),
snippet.getContent(),
snippet.getVisibility(),
snippet.getDescription());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment