Commit f75f0da0 authored by zhengrenjie's avatar zhengrenjie Committed by Greg Messner
Browse files

Added LicenseApi and MarkdownApi (#204)

1) Added a method to get languages used in a project.
2) Added LicensesApi & MarkdownApi classes and methods
parent 2ae52935
......@@ -53,6 +53,8 @@ public class GitLabApi {
private GroupApi groupApi;
private HealthCheckApi healthCheckApi;
private IssuesApi issuesApi;
private LicensesApi licensesApi;
private MarkdownApi markdownApi;
private MergeRequestApi mergeRequestApi;
private MilestonesApi milestonesApi;
private NamespaceApi namespaceApi;
......@@ -951,6 +953,44 @@ public class GitLabApi {
return (labelsApi);
}
/**
* Gets the LicensesApi instance owned by this GitLabApi instance. The LicensesApi is used
* to perform all license related API calls.
*
* @return the LicensesApi instance owned by this GitLabApi instance
*/
public LicensesApi getLicensesApi() {
if (licensesApi == null) {
synchronized (this) {
if (licensesApi == null) {
licensesApi = new LicensesApi(this);
}
}
}
return (licensesApi);
}
/**
* Gets the MarkdownApi instance owned by this GitLabApi instance. The MarkdownApi is used
* to perform all markdown related API calls.
*
* @return the MarkdownApi instance owned by this GitLabApi instance
*/
public MarkdownApi getMarkdownApi() {
if (markdownApi == null) {
synchronized (this) {
if (markdownApi == null) {
markdownApi = new MarkdownApi(this);
}
}
}
return (markdownApi);
}
/**
* Gets the MergeRequestApi instance owned by this GitLabApi instance. The MergeRequestApi is used
* to perform all merge request related API calls.
......
package org.gitlab4j.api;
import java.util.List;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.LicenseTemplate;
/**
* This class provides an entry point to all the GitLab API licenses calls.
*/
public class LicensesApi extends AbstractApi {
public LicensesApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
/**
* Get all license templates.
*
* GET /licenses
*
* @return a list of LicenseTemplate instances
* @throws GitLabApiException if any exception occurs
*/
public List<LicenseTemplate> getAllLicenseTemplates() throws GitLabApiException {
GitLabApiForm formData = null;
Response response = get(Response.Status.OK, formData.asMap(), "licenses");
return (response.readEntity(new GenericType<List<LicenseTemplate>>() {}));
}
/**
* Get popular license templates.
*
* GET /licenses
*
* @return a list of LicenseTemplate instances
* @throws GitLabApiException if any exception occurs
*/
public List<LicenseTemplate> getPopularLicenseTemplates() throws GitLabApiException {
Form formData = new GitLabApiForm().withParam("popular", true, true);
Response response = get(Response.Status.OK, formData.asMap(), "licenses");
return (response.readEntity(new GenericType<List<LicenseTemplate>>() {}));
}
/**
* Get a single license template.
*
* GET /licenses
*
* @param key The key of the license template
* @return a LicenseTemplate instance
* @throws GitLabApiException if any exception occurs
*/
public LicenseTemplate getSingleLicenseTemplate(String key) throws GitLabApiException {
GitLabApiForm formData = null;
Response response = get(Response.Status.OK, formData.asMap(), "licenses", key);
return (response.readEntity(LicenseTemplate.class));
}
}
\ No newline at end of file
package org.gitlab4j.api;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Markdown;
/**
* This class provides an entry point to all the GitLab API markdown calls.
*/
public class MarkdownApi extends AbstractApi {
public MarkdownApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
/**
* Render an arbitrary Markdown document.
*
* POST /api/v4/markdown
*
* @param text text to be transformed
* @return a Markdown instance with transformed info
* @throws GitLabApiException if any exception occurs
* @since GitLab 11.0
*/
public Markdown getMarkdown(String text) throws GitLabApiException {
if(!isApiVersion(ApiVersion.V4)){
throw new GitLabApiException("Api version must be v4");
}
Form formData = new GitLabApiForm().withParam("text", text, true);
Response response = post(Response.Status.OK, formData.asMap(), "markdown");
return (response.readEntity(Markdown.class));
}
}
\ No newline at end of file
......@@ -28,6 +28,7 @@ import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.ws.rs.core.Form;
......@@ -2176,4 +2177,24 @@ public class ProjectApi extends AbstractApi implements Constants {
Response response = post(expectedStatus, (Form) null, "projects", getProjectIdOrPath(projectIdOrPath), "unstar");
return (response.readEntity(Project.class));
}
/**
* Get languages used in a project with percentage value.
*
* Get /projects/:id/languages
*
* @param projectId the ID of the project
* @return a Map instance with the language as the key and the percentage as the value
* @throws GitLabApiException if any exception occurs
* @since GitLab 10.8
*/
public Map<String, Float> getProjectLanguages(Integer projectId) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
Response response = get(Response.Status.OK, null, "projects", projectId, "languages");
return (response.readEntity(new GenericType<Map<String, Float>>() {}));
}
}
\ No newline at end of file
......@@ -93,7 +93,7 @@ public class RepositoryFileApi extends AbstractApi {
} else {
response = post(Response.Status.CREATED, formData, "projects", projectId, "repository", "files", urlEncode(file.getFilePath()));
}
return (response.readEntity(RepositoryFile.class));
}
......@@ -123,7 +123,7 @@ public class RepositoryFileApi extends AbstractApi {
} else {
response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "files", urlEncode(file.getFilePath()));
}
return (response.readEntity(RepositoryFile.class));
}
......@@ -237,4 +237,4 @@ public class RepositoryFileApi extends AbstractApi {
addFormParam(form, "commit_message", commitMessage, true);
return (form);
}
}
}
\ No newline at end of file
package org.gitlab4j.api.models;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class LicenseTemplate {
private String key;
private String name;
private String nickname;
private boolean featured;
private String htmlUrl;
private String sourceUrl;
private String description;
private List<String> conditions;
private List<String> permissions;
private List<String> limitations;
private String content;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public boolean isFeatured() {
return featured;
}
public void setFeatured(boolean featured) {
this.featured = featured;
}
public String getHtmlUrl() {
return htmlUrl;
}
public void setHtmlUrl(String htmlUrl) {
this.htmlUrl = htmlUrl;
}
public String getSourceUrl() {
return sourceUrl;
}
public void setSourceUrl(String sourceUrl) {
this.sourceUrl = sourceUrl;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<String> getConditions() {
return conditions;
}
public void setConditions(List<String> conditions) {
this.conditions = conditions;
}
public List<String> getPermissions() {
return permissions;
}
public void setPermissions(List<String> permissions) {
this.permissions = permissions;
}
public List<String> getLimitations() {
return limitations;
}
public void setLimitations(List<String> limitations) {
this.limitations = limitations;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
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 Markdown {
private String html;
public String getHtml() {
return html;
}
public void setHtml(String html) {
this.html = html;
}
}
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