Skip to content

Commit f78d450

Browse files
committed
Add project Template list (#1127)
1 parent dff8509 commit f78d450

File tree

8 files changed

+281
-30
lines changed

8 files changed

+281
-30
lines changed

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

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,7 @@
4141
import jakarta.ws.rs.core.MultivaluedMap;
4242
import jakarta.ws.rs.core.Response;
4343

44-
import org.gitlab4j.api.models.AccessLevel;
45-
import org.gitlab4j.api.models.AccessRequest;
46-
import org.gitlab4j.api.models.ApprovalRule;
47-
import org.gitlab4j.api.models.ApprovalRuleParams;
48-
import org.gitlab4j.api.models.AuditEvent;
49-
import org.gitlab4j.api.models.Badge;
50-
import org.gitlab4j.api.models.CustomAttribute;
51-
import org.gitlab4j.api.models.Event;
52-
import org.gitlab4j.api.models.FileUpload;
53-
import org.gitlab4j.api.models.Issue;
54-
import org.gitlab4j.api.models.Iteration;
55-
import org.gitlab4j.api.models.IterationFilter;
56-
import org.gitlab4j.api.models.Member;
57-
import org.gitlab4j.api.models.Namespace;
58-
import org.gitlab4j.api.models.Project;
59-
import org.gitlab4j.api.models.ProjectAccessToken;
60-
import org.gitlab4j.api.models.ProjectApprovalsConfig;
61-
import org.gitlab4j.api.models.ProjectFetches;
62-
import org.gitlab4j.api.models.ProjectFilter;
63-
import org.gitlab4j.api.models.ProjectGroup;
64-
import org.gitlab4j.api.models.ProjectGroupsFilter;
65-
import org.gitlab4j.api.models.ProjectHook;
66-
import org.gitlab4j.api.models.ProjectUser;
67-
import org.gitlab4j.api.models.PullMirror;
68-
import org.gitlab4j.api.models.PushRules;
69-
import org.gitlab4j.api.models.RemoteMirror;
70-
import org.gitlab4j.api.models.Snippet;
71-
import org.gitlab4j.api.models.UploadedFile;
72-
import org.gitlab4j.api.models.Variable;
73-
import org.gitlab4j.api.models.Visibility;
44+
import org.gitlab4j.api.models.*;
7445
import org.gitlab4j.models.Constants;
7546
import org.gitlab4j.models.utils.ISO8601;
7647

@@ -4774,4 +4745,50 @@ public List<Iteration> listProjectIterations(Object projectIdOrPath, IterationFi
47744745
get(Response.Status.OK, queryParams, "projects", getProjectIdOrPath(projectIdOrPath), "iterations");
47754746
return (response.readEntity(new GenericType<List<Iteration>>() {}));
47764747
}
4748+
4749+
/**
4750+
* Get project templates of the specified type.
4751+
*
4752+
* <pre><code>GitLab Endpoint: GET /projects/:id/templates/:type</code></pre>
4753+
*
4754+
* @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance
4755+
* @param type type of the template. Accepted values are: dockerfiles, gitignores, gitlab_ci_ymls, licenses, issues, or merge_requests.
4756+
* @return the list of project templates
4757+
* @throws GitLabApiException if any exception occurs
4758+
*/
4759+
public List<ProjectTemplate> getProjectTemplates(Object projectIdOrPath, ProjectTemplateType type)
4760+
throws GitLabApiException {
4761+
Response response = get(
4762+
Response.Status.OK,
4763+
null,
4764+
"projects",
4765+
getProjectIdOrPath(projectIdOrPath),
4766+
"templates",
4767+
type.toString());
4768+
return (response.readEntity(new GenericType<List<ProjectTemplate>>() {}));
4769+
}
4770+
4771+
/**
4772+
* Get a specific project template of the specified type.
4773+
*
4774+
* <pre><code>GitLab Endpoint: GET /projects/:id/templates/:type/:name</code></pre>
4775+
*
4776+
* @param projectIdOrPath the project in the form of a Long(ID), String(path), or Project instance
4777+
* @param type type of the template. Accepted values are: dockerfiles, gitignores, gitlab_ci_ymls, licenses, issues, or merge_requests.
4778+
* @param templateName Key of the template
4779+
* @return the project template detail
4780+
* @throws GitLabApiException if any exception occurs
4781+
*/
4782+
public ProjectTemplateDetail getProjectTemplate(
4783+
Object projectIdOrPath, ProjectTemplateType type, String templateName) throws GitLabApiException {
4784+
Response response = get(
4785+
Response.Status.OK,
4786+
null,
4787+
"projects",
4788+
getProjectIdOrPath(projectIdOrPath),
4789+
"templates",
4790+
type.toString(),
4791+
templateName);
4792+
return (response.readEntity(ProjectTemplateDetail.class));
4793+
}
47774794
}

gitlab4j-api/src/test/java/org/gitlab4j/api/TestProjectApi.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import org.gitlab4j.api.models.Project;
5454
import org.gitlab4j.api.models.ProjectFetches;
5555
import org.gitlab4j.api.models.ProjectFilter;
56+
import org.gitlab4j.api.models.ProjectTemplate;
57+
import org.gitlab4j.api.models.ProjectTemplateType;
5658
import org.gitlab4j.api.models.User;
5759
import org.gitlab4j.api.models.Variable;
5860
import org.gitlab4j.api.models.Visibility;
@@ -1084,4 +1086,30 @@ public void testRotateProjectAccessToken() throws GitLabApiException {
10841086
// assertTrue(gitLabApi.getProjectApi().getProjectAccessToken(testProject.getId(),
10851087
// token.getId()).isRevoked());
10861088
}
1089+
1090+
@Test
1091+
public void testProjectTemplates() throws GitLabApiException {
1092+
1093+
assumeTrue(testProject != null);
1094+
1095+
// Act
1096+
List<ProjectTemplate> templates =
1097+
gitLabApi.getProjectApi().getProjectTemplates(testProject, ProjectTemplateType.LICENSES);
1098+
1099+
// Assert
1100+
assertNotNull(templates);
1101+
}
1102+
1103+
@Test
1104+
public void testProjectTemplate() throws GitLabApiException {
1105+
1106+
assumeTrue(testProject != null);
1107+
1108+
// Act
1109+
ProjectTemplate template =
1110+
gitLabApi.getProjectApi().getProjectTemplate(testProject, ProjectTemplateType.LICENSES, "mit");
1111+
1112+
// Assert
1113+
assertNotNull(template);
1114+
}
10871115
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.gitlab4j.api.models;
2+
3+
import java.io.Serializable;
4+
5+
import org.gitlab4j.models.utils.JacksonJson;
6+
7+
public class ProjectTemplate implements Serializable {
8+
private static final long serialVersionUID = 1L;
9+
10+
private String key;
11+
private String name;
12+
13+
public String getKey() {
14+
return key;
15+
}
16+
17+
public void setKey(String key) {
18+
this.key = key;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return (JacksonJson.toJsonString(this));
32+
}
33+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.gitlab4j.api.models;
2+
3+
import java.util.List;
4+
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
public class ProjectTemplateDetail extends ProjectTemplate {
8+
9+
private String nickname;
10+
private Boolean popular;
11+
12+
@JsonProperty("html_url")
13+
private String htmlUrl;
14+
15+
@JsonProperty("source_url")
16+
private String sourceUrl;
17+
18+
private String content;
19+
private String description;
20+
private List<String> conditions;
21+
private List<String> permissions;
22+
private List<String> limitations;
23+
24+
public String getNickname() {
25+
return nickname;
26+
}
27+
28+
public void setNickname(String nickname) {
29+
this.nickname = nickname;
30+
}
31+
32+
public Boolean getPopular() {
33+
return popular;
34+
}
35+
36+
public void setPopular(Boolean popular) {
37+
this.popular = popular;
38+
}
39+
40+
public String getHtmlUrl() {
41+
return htmlUrl;
42+
}
43+
44+
public void setHtmlUrl(String htmlUrl) {
45+
this.htmlUrl = htmlUrl;
46+
}
47+
48+
public String getSourceUrl() {
49+
return sourceUrl;
50+
}
51+
52+
public void setSourceUrl(String sourceUrl) {
53+
this.sourceUrl = sourceUrl;
54+
}
55+
56+
public String getContent() {
57+
return content;
58+
}
59+
60+
public void setContent(String content) {
61+
this.content = content;
62+
}
63+
64+
public String getDescription() {
65+
return description;
66+
}
67+
68+
public void setDescription(String description) {
69+
this.description = description;
70+
}
71+
72+
public List<String> getConditions() {
73+
return conditions;
74+
}
75+
76+
public void setConditions(List<String> conditions) {
77+
this.conditions = conditions;
78+
}
79+
80+
public List<String> getPermissions() {
81+
return permissions;
82+
}
83+
84+
public void setPermissions(List<String> permissions) {
85+
this.permissions = permissions;
86+
}
87+
88+
public List<String> getLimitations() {
89+
return limitations;
90+
}
91+
92+
public void setLimitations(List<String> limitations) {
93+
this.limitations = limitations;
94+
}
95+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.models.utils.JacksonJsonEnumHelper;
4+
5+
import com.fasterxml.jackson.annotation.JsonCreator;
6+
import com.fasterxml.jackson.annotation.JsonValue;
7+
8+
public enum ProjectTemplateType {
9+
DOCKERFILES,
10+
GITIGNORES,
11+
GITLAB_CI_YMLS,
12+
LICENSES,
13+
ISSUES,
14+
MERGE_REQUESTS;
15+
16+
private static final JacksonJsonEnumHelper<ProjectTemplateType> enumHelper =
17+
new JacksonJsonEnumHelper<>(ProjectTemplateType.class);
18+
19+
@JsonCreator
20+
public static ProjectTemplateType forValue(String value) {
21+
return enumHelper.forValue(value);
22+
}
23+
24+
@JsonValue
25+
public String toValue() {
26+
return (enumHelper.toString(this));
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return (enumHelper.toString(this));
32+
}
33+
}

gitlab4j-models/src/test/java/org/gitlab4j/models/TestGitLabApiBeans.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,18 @@ public void testProjectApprovalsCofig() throws Exception {
496496
assertTrue(compareJson(approvalsConfig, "project-approvals-config.json"));
497497
}
498498

499+
@Test
500+
public void testProjectTemplates() throws Exception {
501+
List<ProjectTemplate> projectTemplates = unmarshalResourceList(ProjectTemplate.class, "project-templates.json");
502+
assertTrue(compareJson(projectTemplates, "project-templates.json"));
503+
}
504+
505+
@Test
506+
public void testProjectTemplate() throws Exception {
507+
ProjectTemplateDetail projectTemplate = unmarshalResource(ProjectTemplateDetail.class, "project-template.json");
508+
assertTrue(compareJson(projectTemplate, "project-template.json"));
509+
}
510+
499511
@Test
500512
public void testProtectedBranch() throws Exception {
501513
ProtectedBranch protectedBranch = unmarshalResource(ProtectedBranch.class, "protected-branch.json");
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"key": "mit",
3+
"name": "MIT License",
4+
"nickname": "nickname",
5+
"popular": true,
6+
"html_url": "http://choosealicense.com/licenses/mit/",
7+
"source_url": "https://opensource.org/licenses/MIT",
8+
"description": "A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.",
9+
"conditions": [
10+
"include-copyright"
11+
],
12+
"permissions": [
13+
"commercial-use",
14+
"modifications",
15+
"distribution",
16+
"private-use"
17+
],
18+
"limitations": [
19+
"liability",
20+
"warranty"
21+
],
22+
"content": "MIT License content"
23+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"key": "epl-1.0",
4+
"name": "Eclipse Public License 1.0"
5+
},
6+
{
7+
"key": "lgpl-3.0",
8+
"name": "GNU Lesser General Public License v3.0"
9+
}
10+
]

0 commit comments

Comments
 (0)