Skip to content

Conversation

jmini
Copy link
Collaborator

@jmini jmini commented Nov 1, 2024

@jmini
Copy link
Collaborator Author

jmini commented Nov 1, 2024

Tested with jbang:

///usr/bin/env jbang "$0" "$@" ; exit $?

//DEPS https://github.com/jmini/gitlab4j-api/commit/17020801279f8e1214dd24ccb0dc6a0401575d61

import java.util.List;
import java.util.logging.Level;

import org.gitlab4j.api.Constants.TokenType;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Pipeline;
import org.gitlab4j.api.models.Release;

public class HelloWorld {

    public static void main(String... args) throws Exception {
        System.out.println("--- Start ---");
        String gitLabUrl = getEnv("CI_API_V4_URL");
        if (gitLabUrl.endsWith("/api/v4")) {
            gitLabUrl = gitLabUrl.substring(0, gitLabUrl.length() - "/api/v4".length());
        }
        String jobToken = getEnv("CI_JOB_TOKEN");
        String projectId = getEnv("CI_PROJECT_ID");
        try (GitLabApi gitLabApi = new GitLabApi(ApiVersion.V4, gitLabUrl, TokenType.JOB_TOKEN, jobToken)
                .withRequestResponseLogging(Level.INFO)) {
            List<Release> releases = gitLabApi.getReleasesApi()
                    .getReleases(projectId);
            System.out.println(releases);

            Pipeline pipeline = gitLabApi.getPipelineApi()
                    .triggerPipeline(projectId, jobToken, "main", null);
            System.out.println(pipeline);
        }
        System.out.println("--- End ---");
    }

    private static String getEnv(String key) {
        String gitLabUrl = System.getenv(key);
        if (gitLabUrl == null) {
            System.out.println(key + " is not defined, please define it");
            System.exit(1);
            throw new RuntimeException("unreachable code");
        }
        return gitLabUrl;
    }
}

@jmini jmini merged commit c393f61 into gitlab4j:main Nov 6, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for CI/CD job tokens?
1 participant