Skip to content

#1010 get .gitlab-ci.yml template #1026

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 4 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 17 additions & 1 deletion src/main/java/org/gitlab4j/api/GitLabApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.ws.rs.core.Response;

import org.gitlab4j.api.Constants.TokenType;
import org.gitlab4j.api.models.CiYamlTemplatesApi;
import org.gitlab4j.api.models.OauthTokenResponse;
import org.gitlab4j.api.models.User;
import org.gitlab4j.api.models.Version;
Expand Down Expand Up @@ -99,6 +100,7 @@ public String getApiNamespace() {
private UserApi userApi;
private UncyclosApi wikisApi;
private KeysApi keysApi;
private CiYamlTemplatesApi ciYamlTemplatesApi;

/**
* Get the GitLab4J shared Logger instance.
Expand Down Expand Up @@ -1463,7 +1465,7 @@ public ReleaseLinksApi getReleaseLinksApi() {

return releaseLinksApi;
}

/**
* Gets the ReleasesApi instance owned by this GitLabApi instance. The ReleasesApi is used
* to perform all release related API calls.
Expand Down Expand Up @@ -1739,6 +1741,20 @@ public KeysApi getKeysAPI() {
return keysApi;
}

/**
* Gets the CiYamlTemplatesAPI instance owned by this GitLabApi instance. The CiYamlTemplatesAPI is used to get Gitlab CI YAML templates.
*
* @return the CiYamlTemplatesAPI instance owned by this GitLabApi instance
*/
public CiYamlTemplatesApi getCiYamlTemplatesApi() {
synchronized (this) {
if (ciYamlTemplatesApi == null) {
ciYamlTemplatesApi = new CiYamlTemplatesApi(this);
}
}
return ciYamlTemplatesApi;
}


/**
* Create and return an Optional instance associated with a GitLabApiException.
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/gitlab4j/api/models/CiYamlTemplate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.gitlab4j.api.models;

import org.gitlab4j.api.utils.JacksonJson;

public class CiYamlTemplate {

private String name;
private String content;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getContent() {
return content;
}

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

@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
}
49 changes: 49 additions & 0 deletions src/main/java/org/gitlab4j/api/models/CiYamlTemplatesApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package 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.

Wrong package


import java.util.List;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.gitlab4j.api.AbstractApi;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;

/**
* This class provides an entry point to all the GitLab CI YAML API calls.
*
* @see <a href="https://https://docs.gitlab.com/ee/api/templates/gitlab_ci_ymls.html">GitLab CI YAML API</a>
Copy link
Collaborator

Choose a reason for hiding this comment

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

link is wrong

*/
public class CiYamlTemplatesApi extends AbstractApi {
Copy link
Collaborator

@jmini jmini Sep 5, 2023

Choose a reason for hiding this comment

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

I think the class should be named CiYaml or GitLabCiYaml

to match the title of the docs GitLab CI YAML API
https://docs.gitlab.com/ee/api/templates/gitlab_ci_ymls.html


public CiYamlTemplatesApi(GitLabApi gitLabApi) {
super(gitLabApi);
}

/**
* Get all GitLab CI/CD YAML templates.
*
* <pre><code>GitLab Endpoint: GET /templates/gitlab_ci_ymls</code></pre>
*
* @return a list of Gitlab CI YAML Templates
* @throws GitLabApiException if any exception occurs
*/
public List<CiYamlTemplatesElement> getAllCiYamlTemplates() throws GitLabApiException {
Response response = get(Response.Status.OK, null, "templates", "gitlab_ci_ymls");
return (response.readEntity(new GenericType<List<CiYamlTemplatesElement>>() {}));
}

/**
* Get a single GitLab CI/CD YAML template.
*
* <pre><code>GitLab Endpoint: GET /templates/gitlab_ci_ymls/:key</code></pre>
*
* @param key The key of the GitLab CI YAML template
* @return an Gitlab CI YAML Template
* @throws GitLabApiException if any exception occurs
*/
public CiYamlTemplate getSingleCiYamlTemplate(String key) throws GitLabApiException {
Response response = get(Status.OK, null, "templates", "gitlab_ci_ymls", key);
return (response.readEntity(CiYamlTemplate.class));
}

}
30 changes: 30 additions & 0 deletions src/main/java/org/gitlab4j/api/models/CiYamlTemplatesElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.gitlab4j.api.models;

import org.gitlab4j.api.utils.JacksonJson;

public class CiYamlTemplatesElement {

private String key;
private String name;

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;
}

@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
}
66 changes: 66 additions & 0 deletions src/test/java/org/gitlab4j/api/TestCiYamlTemplatesApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.gitlab4j.api;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.util.List;
import org.gitlab4j.api.models.CiYamlTemplate;
import org.gitlab4j.api.models.CiYamlTemplatesElement;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties
*
* TEST_PROJECT_NAME
* TEST_HOST_URL
*
* If any of the above are NULL, all tests in this class will be skipped.
*/
@Tag("integration")
@ExtendWith(SetupIntegrationTestExtension.class)
public class TestCiYamlTemplatesApi extends AbstractIntegrationTest {

private static GitLabApi gitLabApi;

@BeforeAll
public static void setUp() {
gitLabApi = baseTestSetup();
}

@BeforeEach
public void beforeMethod() {
assumeTrue(gitLabApi != null);
}

@Test
public void testGetAllCiYamlTemplates() throws GitLabApiException {
List<CiYamlTemplatesElement> ciYamlTemplatesElements = gitLabApi.getCiYamlTemplatesApi().getAllCiYamlTemplates();
assertAll(
() -> assertNotNull(ciYamlTemplatesElements),
() -> assertTrue(ciYamlTemplatesElements.size() > 0)
);
}

@Test
public void testGetSingleCiYamlTemplate() throws GitLabApiException {
List<CiYamlTemplatesElement> ciYamlTemplatesElements = gitLabApi.getCiYamlTemplatesApi().getAllCiYamlTemplates();
assumeTrue(ciYamlTemplatesElements != null);
assumeTrue(ciYamlTemplatesElements.size() > 0);
String templateKey = ciYamlTemplatesElements.get(0).getKey();
CiYamlTemplate ciYamlTemplate = gitLabApi.getCiYamlTemplatesApi().getSingleCiYamlTemplate(templateKey);
assertAll(
() -> assertNotNull(ciYamlTemplate),
() -> assertNotNull(ciYamlTemplate.getContent()),
() -> assertEquals(templateKey, ciYamlTemplate.getName())
);

}

}
14 changes: 14 additions & 0 deletions src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import org.gitlab4j.api.models.Board;
import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.ChildEpic;
import org.gitlab4j.api.models.CiYamlTemplate;
import org.gitlab4j.api.models.CiYamlTemplatesElement;
import org.gitlab4j.api.models.Comment;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.CommitPayload;
Expand Down Expand Up @@ -790,4 +792,16 @@ public void testProjectAccessToken() throws Exception {
ProjectAccessToken token = unmarshalResource(ProjectAccessToken.class, "project-access-token.json");
assertTrue(compareJson(token, "project-access-token.json"));
}

@Test
public void testCiYamlTemplatesElements() throws Exception {
List<CiYamlTemplatesElement> ciYamlTemplatesElements = unmarshalResourceList(CiYamlTemplatesElement.class, "ci-yaml-templates-elements.json");
assertTrue(compareJson(ciYamlTemplatesElements, "ci-yaml-templates-elements.json"));
}

@Test
public void testCiYamlTemplate() throws Exception {
CiYamlTemplate ciYamlTemplate = unmarshalResource(CiYamlTemplate.class, "ci-yaml-template.json");
assertTrue(compareJson(ciYamlTemplate, "ci-yaml-template.json"));
}
}
4 changes: 4 additions & 0 deletions src/test/resources/org/gitlab4j/api/ci-yaml-template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "Ruby",
"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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
[
{
"key": "Android",
"name": "Android"
},
{
"key": "Android-Fastlane",
"name": "Android-Fastlane"
},
{
"key": "Auto-DevOps",
"name": "Auto-DevOps"
},
{
"key": "Bash",
"name": "Bash"
},
{
"key": "C++",
"name": "C++"
},
{
"key": "Chef",
"name": "Chef"
},
{
"key": "Clojure",
"name": "Clojure"
},
{
"key": "Code-Quality",
"name": "Code-Quality"
},
{
"key": "Crystal",
"name": "Crystal"
},
{
"key": "Deploy-ECS",
"name": "Deploy-ECS"
},
{
"key": "Django",
"name": "Django"
},
{
"key": "Docker",
"name": "Docker"
},
{
"key": "Elixir",
"name": "Elixir"
},
{
"key": "Go",
"name": "Go"
},
{
"key": "Gradle",
"name": "Gradle"
},
{
"key": "Grails",
"name": "Grails"
},
{
"key": "Julia",
"name": "Julia"
},
{
"key": "LaTeX",
"name": "LaTeX"
},
{
"key": "Laravel",
"name": "Laravel"
},
{
"key": "Managed-Cluster-Applications",
"name": "Managed-Cluster-Applications"
}
]