Skip to content

Add personal snippets api #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/org/gitlab4j/api/GitLabApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public String getApiNamespace() {
private LabelsApi labelsApi;
private NotesApi notesApi;
private EventsApi eventsApi;
private SnippetsApi snippetsApi;

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

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;
}
}
157 changes: 157 additions & 0 deletions src/main/java/org/gitlab4j/api/SnippetsApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
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);
}

}
52 changes: 52 additions & 0 deletions src/main/java/org/gitlab4j/api/models/Snippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,27 @@ public class Snippet {
private String title;
private String updatedAt;
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() {
return this.author;
}
Expand Down Expand Up @@ -105,4 +125,36 @@ public String getWebUrl() {
public void setWebUrl(String 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;
}
}
144 changes: 144 additions & 0 deletions src/test/java/org/gitlab4j/api/TestSnippetsApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
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());
}

}