Skip to content

Commit 32d00b1

Browse files
DoubleDProjmini
andauthored
Add api for "GitLab CI YAML API" (#1026)
Co-authored-by: Jeremie Bresson <[email protected]>
1 parent ff037bd commit 32d00b1

File tree

8 files changed

+290
-0
lines changed

8 files changed

+290
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public String getApiNamespace() {
6565
private EpicsApi epicsApi;
6666
private EventsApi eventsApi;
6767
private ExternalStatusCheckApi externalStatusCheckApi;
68+
private GitLabCiYamlApi gitLabCiYaml;
6869
private GroupApi groupApi;
6970
private HealthCheckApi healthCheckApi;
7071
private ImportExportApi importExportApi;
@@ -1109,6 +1110,20 @@ public ExternalStatusCheckApi getExternalStatusCheckApi() {
11091110
return (externalStatusCheckApi);
11101111
}
11111112

1113+
/**
1114+
* Gets the GitLabCiYamlApi instance owned by this GitLabApi instance. The GitLabCiYamlApi is used to get Gitlab CI YAML templates.
1115+
*
1116+
* @return the GitLabCiYamlApi instance owned by this GitLabApi instance
1117+
*/
1118+
public GitLabCiYamlApi getGitLabCiYamlApi() {
1119+
synchronized (this) {
1120+
if (gitLabCiYaml == null) {
1121+
gitLabCiYaml = new GitLabCiYamlApi(this);
1122+
}
1123+
}
1124+
return gitLabCiYaml;
1125+
}
1126+
11121127

11131128
/**
11141129
* Gets the GroupApi instance owned by this GitLabApi instance. The GroupApi is used
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.gitlab4j.api;
2+
3+
import java.util.List;
4+
import javax.ws.rs.core.GenericType;
5+
import javax.ws.rs.core.Response;
6+
import javax.ws.rs.core.Response.Status;
7+
8+
import org.gitlab4j.api.models.GitLabCiTemplate;
9+
import org.gitlab4j.api.models.GitLabCiTemplateElement;
10+
11+
/**
12+
* This class provides an entry point to all the GitLab CI YAML API calls.
13+
*
14+
* @see <a href="https://docs.gitlab.com/ee/api/templates/gitlab_ci_ymls.html">GitLab CI YAML API</a>
15+
*/
16+
public class GitLabCiYamlApi extends AbstractApi {
17+
18+
public GitLabCiYamlApi(GitLabApi gitLabApi) {
19+
super(gitLabApi);
20+
}
21+
22+
/**
23+
* Get all GitLab CI/CD YAML templates.
24+
*
25+
* <pre><code>GitLab Endpoint: GET /templates/gitlab_ci_ymls</code></pre>
26+
*
27+
* @return a list of Gitlab CI YAML Templates
28+
* @throws GitLabApiException if any exception occurs
29+
*/
30+
public List<GitLabCiTemplateElement> getAllGitLabCiYamlTemplates() throws GitLabApiException {
31+
Response response = get(Response.Status.OK, null, "templates", "gitlab_ci_ymls");
32+
return (response.readEntity(new GenericType<List<GitLabCiTemplateElement>>() {}));
33+
}
34+
35+
/**
36+
* Get a single GitLab CI/CD YAML template.
37+
*
38+
* <pre><code>GitLab Endpoint: GET /templates/gitlab_ci_ymls/:key</code></pre>
39+
*
40+
* @param key The key of the GitLab CI YAML template
41+
* @return an Gitlab CI YAML Template
42+
* @throws GitLabApiException if any exception occurs
43+
*/
44+
public GitLabCiTemplate getSingleGitLabCiYamlTemplate(String key) throws GitLabApiException {
45+
Response response = get(Status.OK, null, "templates", "gitlab_ci_ymls", key);
46+
return (response.readEntity(GitLabCiTemplate.class));
47+
}
48+
49+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.api.utils.JacksonJson;
4+
5+
public class GitLabCiTemplate {
6+
7+
private String name;
8+
private String content;
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public void setName(String name) {
15+
this.name = name;
16+
}
17+
18+
public String getContent() {
19+
return content;
20+
}
21+
22+
public void setContent(String content) {
23+
this.content = content;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return (JacksonJson.toJsonString(this));
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.gitlab4j.api.models;
2+
3+
import org.gitlab4j.api.utils.JacksonJson;
4+
5+
public class GitLabCiTemplateElement {
6+
7+
private String key;
8+
private String name;
9+
10+
public String getKey() {
11+
return key;
12+
}
13+
14+
public void setKey(String key) {
15+
this.key = key;
16+
}
17+
18+
public String getName() {
19+
return name;
20+
}
21+
22+
public void setName(String name) {
23+
this.name = name;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return (JacksonJson.toJsonString(this));
29+
}
30+
}

src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
import org.gitlab4j.api.models.Board;
4747
import org.gitlab4j.api.models.Branch;
4848
import org.gitlab4j.api.models.ChildEpic;
49+
import org.gitlab4j.api.models.GitLabCiTemplate;
50+
import org.gitlab4j.api.models.GitLabCiTemplateElement;
4951
import org.gitlab4j.api.models.Comment;
5052
import org.gitlab4j.api.models.Commit;
5153
import org.gitlab4j.api.models.CommitPayload;
@@ -329,6 +331,18 @@ public void testFileUpload() throws Exception {
329331
assertTrue(compareJson(fileUpload, "file-upload.json"));
330332
}
331333

334+
@Test
335+
public void testGitLabCiTemplateElements() throws Exception {
336+
List<GitLabCiTemplateElement> ciYamlTemplatesElements = unmarshalResourceList(GitLabCiTemplateElement.class, "gitlab-ci-template-elements.json");
337+
assertTrue(compareJson(ciYamlTemplatesElements, "gitlab-ci-template-elements.json"));
338+
}
339+
340+
@Test
341+
public void testGitLabCiTemplate() throws Exception {
342+
GitLabCiTemplate ciYamlTemplate = unmarshalResource(GitLabCiTemplate.class, "gitlab-ci-template.json");
343+
assertTrue(compareJson(ciYamlTemplate, "gitlab-ci-template.json"));
344+
}
345+
332346
@Test
333347
public void testGpgSignature() throws Exception {
334348
GpgSignature gpgSignature = unmarshalResource(GpgSignature.class, "gpg-signature.json");
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.gitlab4j.api;
2+
3+
import static org.junit.jupiter.api.Assertions.assertAll;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
8+
9+
import java.util.List;
10+
import org.gitlab4j.api.models.GitLabCiTemplate;
11+
import org.gitlab4j.api.models.GitLabCiTemplateElement;
12+
import org.junit.jupiter.api.BeforeAll;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Tag;
15+
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.api.extension.ExtendWith;
17+
18+
/**
19+
* In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties
20+
*
21+
* TEST_PROJECT_NAME
22+
* TEST_HOST_URL
23+
*
24+
* If any of the above are NULL, all tests in this class will be skipped.
25+
*/
26+
@Tag("integration")
27+
@ExtendWith(SetupIntegrationTestExtension.class)
28+
public class TestGitLabCiYamlApi extends AbstractIntegrationTest {
29+
30+
private static GitLabApi gitLabApi;
31+
32+
@BeforeAll
33+
public static void setUp() {
34+
gitLabApi = baseTestSetup();
35+
}
36+
37+
@BeforeEach
38+
public void beforeMethod() {
39+
assumeTrue(gitLabApi != null);
40+
}
41+
42+
@Test
43+
public void testGetAllCiYamlTemplates() throws GitLabApiException {
44+
List<GitLabCiTemplateElement> ciYamlTemplatesElements = gitLabApi.getGitLabCiYamlApi().getAllGitLabCiYamlTemplates();
45+
assertAll(
46+
() -> assertNotNull(ciYamlTemplatesElements),
47+
() -> assertTrue(ciYamlTemplatesElements.size() > 0)
48+
);
49+
}
50+
51+
@Test
52+
public void testGetSingleCiYamlTemplate() throws GitLabApiException {
53+
List<GitLabCiTemplateElement> ciYamlTemplatesElements = gitLabApi.getGitLabCiYamlApi().getAllGitLabCiYamlTemplates();
54+
assumeTrue(ciYamlTemplatesElements != null);
55+
assumeTrue(ciYamlTemplatesElements.size() > 0);
56+
String templateKey = ciYamlTemplatesElements.get(0).getKey();
57+
GitLabCiTemplate ciYamlTemplate = gitLabApi.getGitLabCiYamlApi().getSingleGitLabCiYamlTemplate(templateKey);
58+
assertAll(
59+
() -> assertNotNull(ciYamlTemplate),
60+
() -> assertNotNull(ciYamlTemplate.getContent()),
61+
() -> assertEquals(templateKey, ciYamlTemplate.getName())
62+
);
63+
64+
}
65+
66+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
[
2+
{
3+
"key": "Android",
4+
"name": "Android"
5+
},
6+
{
7+
"key": "Android-Fastlane",
8+
"name": "Android-Fastlane"
9+
},
10+
{
11+
"key": "Auto-DevOps",
12+
"name": "Auto-DevOps"
13+
},
14+
{
15+
"key": "Bash",
16+
"name": "Bash"
17+
},
18+
{
19+
"key": "C++",
20+
"name": "C++"
21+
},
22+
{
23+
"key": "Chef",
24+
"name": "Chef"
25+
},
26+
{
27+
"key": "Clojure",
28+
"name": "Clojure"
29+
},
30+
{
31+
"key": "Code-Quality",
32+
"name": "Code-Quality"
33+
},
34+
{
35+
"key": "Crystal",
36+
"name": "Crystal"
37+
},
38+
{
39+
"key": "Deploy-ECS",
40+
"name": "Deploy-ECS"
41+
},
42+
{
43+
"key": "Django",
44+
"name": "Django"
45+
},
46+
{
47+
"key": "Docker",
48+
"name": "Docker"
49+
},
50+
{
51+
"key": "Elixir",
52+
"name": "Elixir"
53+
},
54+
{
55+
"key": "Go",
56+
"name": "Go"
57+
},
58+
{
59+
"key": "Gradle",
60+
"name": "Gradle"
61+
},
62+
{
63+
"key": "Grails",
64+
"name": "Grails"
65+
},
66+
{
67+
"key": "Julia",
68+
"name": "Julia"
69+
},
70+
{
71+
"key": "LaTeX",
72+
"name": "LaTeX"
73+
},
74+
{
75+
"key": "Laravel",
76+
"name": "Laravel"
77+
},
78+
{
79+
"key": "Managed-Cluster-Applications",
80+
"name": "Managed-Cluster-Applications"
81+
}
82+
]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "Ruby",
3+
"content": "# This file is a template, and might need editing before it works on your project.\n# To contribute improvements to CI/CD templates, please follow the Development guide at:\n# https://docs.gitlab.com/ee/development/cicd/templates.html\n# This specific template is located at:\n# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Ruby.gitlab-ci.yml\n\n# Official language image. Look for the different tagged releases at:\n# https://hub.docker.com/r/library/ruby/tags/\nimage: ruby:latest\n\n# Pick zero or more services to be used on all builds.\n# Only needed when using a docker container to run your tests in.\n# Check out: https://docs.gitlab.com/ee/ci/services/index.html\nservices:\n - mysql:latest\n - redis:latest\n - postgres:latest\n\nvariables:\n POSTGRES_DB: database_name\n\n# Cache gems in between builds\ncache:\n paths:\n - vendor/ruby\n\n# This is a basic example for a gem or script which doesn't use\n# services such as redis or postgres\nbefore_script:\n - ruby -v # Print out ruby version for debugging\n # Uncomment next line if your rails app needs a JS runtime:\n # - apt-get update -q \u0026\u0026 apt-get install nodejs -yqq\n - bundle config set --local deployment true # Install dependencies into ./vendor/ruby\n - bundle install -j $(nproc)\n\n# Optional - Delete if not using `rubocop`\nrubocop:\n script:\n - rubocop\n\nrspec:\n script:\n - rspec spec\n\nrails:\n variables:\n DATABASE_URL: \"postgresql://postgres:postgres@postgres:5432/$POSTGRES_DB\"\n script:\n - rails db:migrate\n - rails db:seed\n - rails test\n\n# This deploy job uses a simple deploy flow to Heroku, other providers, for example, AWS Elastic Beanstalk\n# are supported too: https://github.com/travis-ci/dpl\ndeploy:\n stage: deploy\n environment: production\n script:\n - gem install dpl\n - dpl --provider=heroku --app=$HEROKU_APP_NAME --api-key=$HEROKU_PRODUCTION_KEY\n"
4+
}

0 commit comments

Comments
 (0)