Skip to content

Commit f75f0da

Browse files
zhengrenjiegmessner
authored andcommitted
Added LicenseApi and MarkdownApi (#204)
1) Added a method to get languages used in a project. 2) Added LicensesApi & MarkdownApi classes and methods
1 parent 2ae5293 commit f75f0da

File tree

7 files changed

+292
-3
lines changed

7 files changed

+292
-3
lines changed

src/main/java/org/gitlab4j/api/GitLabApi.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public String getApiNamespace() {
5353
private GroupApi groupApi;
5454
private HealthCheckApi healthCheckApi;
5555
private IssuesApi issuesApi;
56+
private LicensesApi licensesApi;
57+
private MarkdownApi markdownApi;
5658
private MergeRequestApi mergeRequestApi;
5759
private MilestonesApi milestonesApi;
5860
private NamespaceApi namespaceApi;
@@ -951,6 +953,44 @@ public LabelsApi getLabelsApi() {
951953
return (labelsApi);
952954
}
953955

956+
/**
957+
* Gets the LicensesApi instance owned by this GitLabApi instance. The LicensesApi is used
958+
* to perform all license related API calls.
959+
*
960+
* @return the LicensesApi instance owned by this GitLabApi instance
961+
*/
962+
public LicensesApi getLicensesApi() {
963+
964+
if (licensesApi == null) {
965+
synchronized (this) {
966+
if (licensesApi == null) {
967+
licensesApi = new LicensesApi(this);
968+
}
969+
}
970+
}
971+
972+
return (licensesApi);
973+
}
974+
975+
/**
976+
* Gets the MarkdownApi instance owned by this GitLabApi instance. The MarkdownApi is used
977+
* to perform all markdown related API calls.
978+
*
979+
* @return the MarkdownApi instance owned by this GitLabApi instance
980+
*/
981+
public MarkdownApi getMarkdownApi() {
982+
983+
if (markdownApi == null) {
984+
synchronized (this) {
985+
if (markdownApi == null) {
986+
markdownApi = new MarkdownApi(this);
987+
}
988+
}
989+
}
990+
991+
return (markdownApi);
992+
}
993+
954994
/**
955995
* Gets the MergeRequestApi instance owned by this GitLabApi instance. The MergeRequestApi is used
956996
* to perform all merge request related API calls.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.gitlab4j.api;
2+
3+
import java.util.List;
4+
import javax.ws.rs.core.Form;
5+
import javax.ws.rs.core.GenericType;
6+
import javax.ws.rs.core.Response;
7+
import org.gitlab4j.api.models.LicenseTemplate;
8+
9+
/**
10+
* This class provides an entry point to all the GitLab API licenses calls.
11+
*/
12+
public class LicensesApi extends AbstractApi {
13+
14+
public LicensesApi(GitLabApi gitLabApi) {
15+
super(gitLabApi);
16+
}
17+
18+
/**
19+
* Get all license templates.
20+
*
21+
* GET /licenses
22+
*
23+
* @return a list of LicenseTemplate instances
24+
* @throws GitLabApiException if any exception occurs
25+
*/
26+
public List<LicenseTemplate> getAllLicenseTemplates() throws GitLabApiException {
27+
GitLabApiForm formData = null;
28+
Response response = get(Response.Status.OK, formData.asMap(), "licenses");
29+
return (response.readEntity(new GenericType<List<LicenseTemplate>>() {}));
30+
}
31+
32+
/**
33+
* Get popular license templates.
34+
*
35+
* GET /licenses
36+
*
37+
* @return a list of LicenseTemplate instances
38+
* @throws GitLabApiException if any exception occurs
39+
*/
40+
public List<LicenseTemplate> getPopularLicenseTemplates() throws GitLabApiException {
41+
Form formData = new GitLabApiForm().withParam("popular", true, true);
42+
Response response = get(Response.Status.OK, formData.asMap(), "licenses");
43+
return (response.readEntity(new GenericType<List<LicenseTemplate>>() {}));
44+
}
45+
46+
/**
47+
* Get a single license template.
48+
*
49+
* GET /licenses
50+
*
51+
* @param key The key of the license template
52+
* @return a LicenseTemplate instance
53+
* @throws GitLabApiException if any exception occurs
54+
*/
55+
public LicenseTemplate getSingleLicenseTemplate(String key) throws GitLabApiException {
56+
GitLabApiForm formData = null;
57+
Response response = get(Response.Status.OK, formData.asMap(), "licenses", key);
58+
return (response.readEntity(LicenseTemplate.class));
59+
}
60+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.gitlab4j.api;
2+
3+
import javax.ws.rs.core.Form;
4+
import javax.ws.rs.core.Response;
5+
import org.gitlab4j.api.GitLabApi.ApiVersion;
6+
import org.gitlab4j.api.models.Markdown;
7+
8+
/**
9+
* This class provides an entry point to all the GitLab API markdown calls.
10+
*/
11+
public class MarkdownApi extends AbstractApi {
12+
13+
public MarkdownApi(GitLabApi gitLabApi) {
14+
super(gitLabApi);
15+
}
16+
17+
/**
18+
* Render an arbitrary Markdown document.
19+
*
20+
* POST /api/v4/markdown
21+
*
22+
* @param text text to be transformed
23+
* @return a Markdown instance with transformed info
24+
* @throws GitLabApiException if any exception occurs
25+
* @since GitLab 11.0
26+
*/
27+
public Markdown getMarkdown(String text) throws GitLabApiException {
28+
29+
if(!isApiVersion(ApiVersion.V4)){
30+
throw new GitLabApiException("Api version must be v4");
31+
}
32+
33+
Form formData = new GitLabApiForm().withParam("text", text, true);
34+
Response response = post(Response.Status.OK, formData.asMap(), "markdown");
35+
return (response.readEntity(Markdown.class));
36+
}
37+
}

src/main/java/org/gitlab4j/api/ProjectApi.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.net.URLEncoder;
2929
import java.util.Date;
3030
import java.util.List;
31+
import java.util.Map;
3132
import java.util.Optional;
3233

3334
import javax.ws.rs.core.Form;
@@ -2176,4 +2177,24 @@ public Project unstarProject(Object projectIdOrPath) throws GitLabApiException {
21762177
Response response = post(expectedStatus, (Form) null, "projects", getProjectIdOrPath(projectIdOrPath), "unstar");
21772178
return (response.readEntity(Project.class));
21782179
}
2180+
2181+
/**
2182+
* Get languages used in a project with percentage value.
2183+
*
2184+
* Get /projects/:id/languages
2185+
*
2186+
* @param projectId the ID of the project
2187+
* @return a Map instance with the language as the key and the percentage as the value
2188+
* @throws GitLabApiException if any exception occurs
2189+
* @since GitLab 10.8
2190+
*/
2191+
public Map<String, Float> getProjectLanguages(Integer projectId) throws GitLabApiException {
2192+
2193+
if (projectId == null) {
2194+
throw new RuntimeException("projectId cannot be null");
2195+
}
2196+
2197+
Response response = get(Response.Status.OK, null, "projects", projectId, "languages");
2198+
return (response.readEntity(new GenericType<Map<String, Float>>() {}));
2199+
}
21792200
}

src/main/java/org/gitlab4j/api/RepositoryFileApi.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public RepositoryFile createFile(RepositoryFile file, Integer projectId, String
9393
} else {
9494
response = post(Response.Status.CREATED, formData, "projects", projectId, "repository", "files", urlEncode(file.getFilePath()));
9595
}
96-
96+
9797
return (response.readEntity(RepositoryFile.class));
9898
}
9999

@@ -123,7 +123,7 @@ public RepositoryFile updateFile(RepositoryFile file, Integer projectId, String
123123
} else {
124124
response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "repository", "files", urlEncode(file.getFilePath()));
125125
}
126-
126+
127127
return (response.readEntity(RepositoryFile.class));
128128
}
129129

@@ -237,4 +237,4 @@ private Form createForm(RepositoryFile file, String branchName, String commitMes
237237
addFormParam(form, "commit_message", commitMessage, true);
238238
return (form);
239239
}
240-
}
240+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package org.gitlab4j.api.models;
2+
3+
import java.util.List;
4+
import javax.xml.bind.annotation.XmlAccessType;
5+
import javax.xml.bind.annotation.XmlAccessorType;
6+
import javax.xml.bind.annotation.XmlRootElement;
7+
8+
@XmlRootElement
9+
@XmlAccessorType(XmlAccessType.FIELD)
10+
public class LicenseTemplate {
11+
12+
private String key;
13+
private String name;
14+
private String nickname;
15+
private boolean featured;
16+
private String htmlUrl;
17+
private String sourceUrl;
18+
private String description;
19+
private List<String> conditions;
20+
private List<String> permissions;
21+
private List<String> limitations;
22+
private String content;
23+
24+
public String getKey() {
25+
return key;
26+
}
27+
28+
public void setKey(String key) {
29+
this.key = key;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public String getNickname() {
41+
return nickname;
42+
}
43+
44+
public void setNickname(String nickname) {
45+
this.nickname = nickname;
46+
}
47+
48+
public boolean isFeatured() {
49+
return featured;
50+
}
51+
52+
public void setFeatured(boolean featured) {
53+
this.featured = featured;
54+
}
55+
56+
public String getHtmlUrl() {
57+
return htmlUrl;
58+
}
59+
60+
public void setHtmlUrl(String htmlUrl) {
61+
this.htmlUrl = htmlUrl;
62+
}
63+
64+
public String getSourceUrl() {
65+
return sourceUrl;
66+
}
67+
68+
public void setSourceUrl(String sourceUrl) {
69+
this.sourceUrl = sourceUrl;
70+
}
71+
72+
public String getDescription() {
73+
return description;
74+
}
75+
76+
public void setDescription(String description) {
77+
this.description = description;
78+
}
79+
80+
public List<String> getConditions() {
81+
return conditions;
82+
}
83+
84+
public void setConditions(List<String> conditions) {
85+
this.conditions = conditions;
86+
}
87+
88+
public List<String> getPermissions() {
89+
return permissions;
90+
}
91+
92+
public void setPermissions(List<String> permissions) {
93+
this.permissions = permissions;
94+
}
95+
96+
public List<String> getLimitations() {
97+
return limitations;
98+
}
99+
100+
public void setLimitations(List<String> limitations) {
101+
this.limitations = limitations;
102+
}
103+
104+
public String getContent() {
105+
return content;
106+
}
107+
108+
public void setContent(String content) {
109+
this.content = content;
110+
}
111+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.gitlab4j.api.models;
2+
3+
import javax.xml.bind.annotation.XmlAccessType;
4+
import javax.xml.bind.annotation.XmlAccessorType;
5+
import javax.xml.bind.annotation.XmlRootElement;
6+
7+
@XmlRootElement
8+
@XmlAccessorType(XmlAccessType.FIELD)
9+
public class Markdown {
10+
11+
private String html;
12+
13+
public String getHtml() {
14+
return html;
15+
}
16+
17+
public void setHtml(String html) {
18+
this.html = html;
19+
}
20+
}

0 commit comments

Comments
 (0)