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

Conversation

faustovaz
Copy link
Contributor

This adds support for Gitlab Personal Snippets API (/snippets). If everything is ok, it would be nice to have this code merged to the main project.

@gmessner
Copy link
Collaborator

gmessner commented May 10, 2018

@faustovaz
This looks great and is very complete, thank you.

I have just one question, why call it the PersonalSnippetsApi and not simply the SnippetsApi? As much as possible, we keep a one-to-one mapping of sub-API names to the API names in the GitLab documentation, and this maps to https://docs.gitlab.com/ee/api/snippets.html which GitLab refers to as the Snippets API.

I would suggest renaming this to simply SnippetsApi so that it matches the GitLab documentation.

@faustovaz
Copy link
Contributor Author

Hi @gmessner ,
you are right. I chose PersonalSnippets to differentiate this kind of Snippet from Project's Snippet. But it's better to keep a one-to-one mapping, like you said.
I already submitted the code changes.
Thank you!

Copy link
Collaborator

@gmessner gmessner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@faustovaz
Just some small changes requested. Thanks again for your contributions.

GitLabApiForm formData = new GitLabApiForm()
.withParam("title", title)
.withParam("file_name", fileName)
.withParam("content", content);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setting title, file_name, and content must have a boolean third paramter specifying that they are required per the docs at: https://docs.gitlab.com/ee/api/snippets.html#create-new-snippet

Should look like this:

GitLabApiForm formData = new GitLabApiForm()
    .withParam("title", title, true)
    .withParam("file_name", fileName, true)
    .withParam("content", content, true);

@@ -71,6 +71,7 @@ public String getApiNamespace() {
private LabelsApi labelsApi;
private NotesApi notesApi;
private EventsApi eventsApi;
private SnippetsApi snippetApi;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename snippetApi to snippetsApi

GitLabApiForm formData = new GitLabApiForm()
.withParam("title", title)
.withParam("file_name", fileName)
.withParam("content", content)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above about title, file_name, and content being required parameters.

* @return a list of authenticated user's snippets
* @throws GitLabApiException if any exception occurs
*/
public List<Snippet> getSnippets() throws GitLabApiException {
Copy link
Collaborator

@gmessner gmessner May 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a parameter to this method indicating whether to download the content, i.e.:

public List<Snippet> getSnippets(boolean downloadContent) throws GitLabApiException {

and the make fetching the content optional based on this. You could add another method without the parameter that calls this method with downloadContent = false . The default should be false to make it act like the GitLab Snippets API.

* @throws GitLabApiException if any exception occurs
*/
public Snippet getSnippet(Integer snippetId) throws GitLabApiException {
if (snippetId == null) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the same here with a flag on whether to download the content or not.


assertTrue(snippets.size() >= 2);
assertTrue(snippets.stream().anyMatch(s -> s.getContent().equals("Java content")));
assertTrue(snippets.stream().anyMatch(s -> s.getContent().equals("Another java content")));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When comparing strings in unit tests it is a good idea to use constants, so you are certain it is the same when you do the assert below.

@Test
public void testSnippetContent() throws GitLabApiException {
Snippet snippet = createSnippet(
new Snippet("Snippet 1", "Snippet.java", "System.out.println(\"\");"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a constant to hold the content and use that instead of literals:
final String CONTENT = "System.out.println(\"\");";

"<parent><data>1</data></parent>",
Visibility.INTERNAL,
"Description"));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same recommendation for using a constant for the content.

@faustovaz
Copy link
Contributor Author

Thank you for reviewing my code, @gmessner .
Now it's much better!

@gmessner
Copy link
Collaborator

@faustovaz
The changes look good. I'm going to approve and merge and will get a release out later today.

@gmessner gmessner merged commit 6f5736c into gitlab4j:master May 10, 2018
@gmessner
Copy link
Collaborator

@faustovaz
I've released 4.8.19.

I did make some minor edits, mostly around formatting to bring the new files in-line with the formatting styles we use (mainly 4 spaces instead of tabs). Also added Pager and Optional support to the SnippetsApi.

Thanks again for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants