Skip to content

Added support for Uncyclos API #191

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 3 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
145 changes: 134 additions & 11 deletions src/main/java/org/gitlab4j/api/ProjectApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,7 @@
import javax.ws.rs.core.Response;

import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.Event;
import org.gitlab4j.api.models.FileUpload;
import org.gitlab4j.api.models.Issue;
import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProjectHook;
import org.gitlab4j.api.models.ProjectUser;
import org.gitlab4j.api.models.PushRules;
import org.gitlab4j.api.models.Snippet;
import org.gitlab4j.api.models.Visibility;
import org.gitlab4j.api.models.*;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We follow the practice of importing the individual items and avoid use of the wildcard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with the suggestion.


/**
* This class provides an entry point to all the GitLab API project calls.
Expand Down Expand Up @@ -1843,6 +1833,139 @@ public Optional<String> getOptionalRawSnippetContent(Integer projectId, Integer
}
}

/**
Copy link
Collaborator

@gmessner gmessner Jun 2, 2018

Choose a reason for hiding this comment

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

The convention we use is to have a one-to-one mapping of an API class to the GitLab API documentation page, in this case https://docs.gitlab.com/ce/api/wikis.html. The code that was added to the ProjectApi class should be moved to a new class called UncyclosApi, you'll then need to add a public UncyclosApi getUncyclosApi() method to the GitLabApi class.

Copy link
Contributor Author

@shuklaalok7 shuklaalok7 Jun 2, 2018

Choose a reason for hiding this comment

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

@gmessner Thanks for reviewing it.
Actually, I had added the UncyclosApi class, but later removed when I observed following pattern in the code.

  1. There is a SnippetsApi class for /snippets endpoints.
  2. Further implementation for the /projects/:id/snippets endpoints is added in the ProjectApi class.

As the endpoints for Uncyclos API are in-line with the second case, I added support for this API in ProjectApi class and removed UncyclosApi. The endpoints for Uncyclos API are /projects/:id/wikis. If you still suggest that I bring back UncyclosApi class, I'll do that this week.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The project snippets documentation use to be part of the Projects API at GitLab, it still is, but they moved the documentation to it's own page at https://docs.gitlab.com/ee/api/project_snippets.html

Notice that this is not called an API it is called "Project snippets". That being said, I will eventually deprecate the snippet methods in the ProjectApi and relocate them to SnippetsApi. You will also find a few others similar to this, I deprecate and relocate when I am making mods in the file for some other reason.

The GitLab API has evolved over the years, it is almost impossible to keep up with the changes. But keeping a one-to-one mapping of API classes to API documentation pages is how this project does it now, so please use the UncyclosApi approach.

* Get a list of pages in project wiki. This only returns the first page of wiki-pages.
*
* GET /projects/:id/wikis
*
* @param projectId the project ID to get the wiki-pages for
* @return a list of pages in the project's wiki
* @throws GitLabApiException if any exception occurs
*/
public List<UncycloPage> getUncycloPages(Integer projectId) throws GitLabApiException {
return getUncycloPages(projectId, 1, this.getDefaultPerPage());
}

/**
* Get a list of project snippets. This only returns the first page of snippets.
*
* GET /projects/:id/wikis
*
* @param projectId the project ID to get the wiki pages for
* @param page the page to get
* @param perPage the number of wiki-pages per page
* @return a list of pages in project's wiki for the specified range
* @throws GitLabApiException if any exception occurs
*/
public List<UncycloPage> getUncycloPages(Integer projectId, int page, int perPage) throws GitLabApiException {
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", projectId, "wikis");
return response.readEntity(new GenericType<List<UncycloPage>>() {});
}

/**
* Get a Pager of project's wiki pages.
*
* GET /projects/:id/wikis
*
* @param projectId the project ID to get the wiki-pages for
* @param itemsPerPage the number of wiki-pages per page
* @return the Pager of wiki-pages
* @throws GitLabApiException if any exception occurs
*/
public Pager<UncycloPage> getUncycloPages(Integer projectId, int itemsPerPage) throws GitLabApiException {
return (new Pager<>(this, UncycloPage.class, itemsPerPage, null, "projects", projectId, "wikis"));
}

/**
* Get a single page of project wiki.
*
* GET /projects/:id/wikis/:slug
*
* @param projectId the project ID to get the wiki page for
* @param slug the slug of the project's wiki page
* @return the specified project Snippet
* @throws GitLabApiException if any exception occurs
*/
public UncycloPage getUncycloPage(Integer projectId, String slug) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", projectId, "wikis", slug);
return (response.readEntity(UncycloPage.class));
}

/**
* Get a single page of project wiki as an Optional instance.
*
* GET /projects/:id/wikis/:slug
*
* @param projectId the project ID to get the snippet for
* @param slug the slug of the project's wiki page
* @return the specified project Snippet as an Optional instance
*/
public Optional<UncycloPage> getOptionalUncycloPage(Integer projectId, String slug) {
try {
return (Optional.ofNullable(getUncycloPage(projectId, slug)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}

/**
* Creates a new project wiki page. The user must have permission to create new wiki page.
*
* POST /projects/:id/wikis
*
* @param projectId the ID of the project owned by the authenticated user, required
* @param title the title of a snippet, required
* @param content the content of a wiki page, required
* @return a UncycloPage instance with info on the created page
* @throws GitLabApiException if any exception occurs
*/
public UncycloPage createUncycloPage(Integer projectId, String title, String content) throws GitLabApiException {
// one of title or content is required
GitLabApiForm formData = new GitLabApiForm()
.withParam("title", title)
.withParam("content", content);

Response response = post(Response.Status.CREATED, formData, "projects", projectId, "wikis");
return (response.readEntity(UncycloPage.class));
}

/**
* Updates an existing project wiki page. The user must have permission to change an existing wiki page.
*
* PUT /projects/:id/wikis/:slug
*
* @param projectId the ID of the project owned by the authenticated user, required
* @param slug the slug of the project's wiki page, required
* @param title the title of a snippet, optional
* @param content the content of a page, optional. Either title or content must be supplied.
* @return a UncycloPage instance with info on the updated page
* @throws GitLabApiException if any exception occurs
*/
public UncycloPage updateUncycloPage(Integer projectId, String slug, String title, String content) throws GitLabApiException {

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

Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "wikis", slug);
return (response.readEntity(UncycloPage.class));
}

/**
* Deletes an existing project wiki page. This is an idempotent function and deleting a non-existent page does
* not cause an error.
*
* DELETE /projects/:id/wikis/:slug
*
* @param projectId the project ID
* @param slug the slug of the project's wiki page
* @throws GitLabApiException if any exception occurs
*/
public void deleteUncycloPage(Integer projectId, String slug) throws GitLabApiException {
delete(Response.Status.NO_CONTENT, null, "projects", projectId, "wikis", slug);
}

/**
* Share a project with the specified group.
*
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/org/gitlab4j/api/models/UncycloPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Greg Messner <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package org.gitlab4j.api.models;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class UncycloPage {

private String title;
private String content;
private String slug;
private String format;

public UncycloPage() {
}

public UncycloPage(String title, String slug, String content) {
this.title = title;
this.slug = slug;
this.content = content;
}

public String getTitle() {
return this.title;
}

public void setTitle(String title) {
this.title = title;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getSlug() {
return slug;
}

public void setSlug(String slug) {
this.slug = slug;
}

public String getFormat() {
return format;
}

public void setFormat(String format) {
this.format = format;
}
}
Loading