GitLabApi.java 51.47 KiB
package org.gitlab4j.api;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.WeakHashMap;
import java.util.logging.Logger;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.Constants.TokenType;
import org.gitlab4j.api.models.OauthTokenResponse;
import org.gitlab4j.api.models.Session;
import org.gitlab4j.api.models.User;
import org.gitlab4j.api.models.Version;
import org.gitlab4j.api.utils.Oauth2LoginStreamingOutput;
import org.gitlab4j.api.utils.SecretString;
/**
 * This class is provides a simplified interface to a GitLab API server, and divides the API up into
 * a separate API class for each concern.
public class GitLabApi {
    private final static Logger LOG = Logger.getLogger(GitLabApi.class.getName());
    /** GitLab4J default per page.  GitLab will ignore anything over 100. */
    public static final int DEFAULT_PER_PAGE = 100;
    /** Specifies the version of the GitLab API to communicate with. */
    public enum ApiVersion {
        V3, V4, OAUTH2_CLIENT;
        public String getApiNamespace() {
            return ("/api/" + name().toLowerCase());
    // Used to keep track of GitLabApiExceptions on calls that return Optional<?>
    private static final Map<Optional<?>, GitLabApiException> optionalExceptionMap =
            Collections.synchronizedMap(new WeakHashMap<Optional<?>, GitLabApiException>());
    GitLabApiClient apiClient;
    private ApiVersion apiVersion;
    private String gitLabServerUrl;
    private Map<String, Object> clientConfigProperties;
    private int defaultPerPage = DEFAULT_PER_PAGE;
    private Session session;
    private CommitsApi commitsApi;
    private DeployKeysApi deployKeysApi;
    private GroupApi groupApi;
    private HealthCheckApi healthCheckApi;
    private IssuesApi issuesApi;
    private MergeRequestApi mergeRequestApi;
    private MilestonesApi milestonesApi;
    private NamespaceApi namespaceApi;
    private NotificationSettingsApi notificationSettingsApi;
    private PipelineApi pipelineApi;
    private ProjectApi projectApi;
    private ProtectedBranchesApi protectedBranchesApi;
    private RepositoryApi repositoryApi;
    private RepositoryFileApi repositoryFileApi;
    private RunnersApi runnersApi;
    private ServicesApi servicesApi;
    private SessionApi sessionApi;
    private SystemHooksApi systemHooksApi;
    private UserApi userApi;
    private JobApi jobApi;
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
private LabelsApi labelsApi; private NotesApi notesApi; private EventsApi eventsApi; private SnippetsApi snippetsApi; private WikisApi wikisApi; /** * Get the GitLab4J shared Logger instance. * * @return the GitLab4J shared Logger instance */ public static final Logger getLogger() { return (LOG); } /** * Create a new GitLabApi instance that is logically a duplicate of this instance, with the exception off sudo state. * * @return a new GitLabApi instance that is logically a duplicate of this instance, with the exception off sudo state. */ public final GitLabApi duplicate() { Integer sudoUserId = this.getSudoAsId(); GitLabApi gitLabApi = new GitLabApi(apiVersion, gitLabServerUrl, getTokenType(), getAuthToken(), getSecretToken(), clientConfigProperties); if (sudoUserId != null) { gitLabApi.apiClient.setSudoAsId(sudoUserId); } if (getIgnoreCertificateErrors()) { gitLabApi.setIgnoreCertificateErrors(true); } gitLabApi.defaultPerPage = this.defaultPerPage; return (gitLabApi); } /** * <p>Logs into GitLab using OAuth2 with the provided {@code username} and {@code password}, * and creates a new {@code GitLabApi} instance using returned access token.</p> * * @param url GitLab URL * @param username user name for which private token should be obtained * @param password password for a given {@code username} * @return new {@code GitLabApi} instance configured for a user-specific token * @throws GitLabApiException GitLabApiException if any exception occurs during execution * @deprecated As of release 4.8.7, replaced by {@link #oauth2Login(String, String, CharSequence)}, will be removed in 4.9.0 */ @Deprecated public static GitLabApi oauth2Login(String url, String username, String password) throws GitLabApiException { return (GitLabApi.oauth2Login(ApiVersion.V4, url, username, password, null, null, false)); } /** * <p>Logs into GitLab using OAuth2 with the provided {@code username} and {@code password}, * and creates a new {@code GitLabApi} instance using returned access token.</p> * * @param url GitLab URL * @param username user name for which private token should be obtained * @param password a CharSequence containing the password for a given {@code username} * @return new {@code GitLabApi} instance configured for a user-specific token * @throws GitLabApiException GitLabApiException if any exception occurs during execution */ public static GitLabApi oauth2Login(String url, String username, CharSequence password) throws GitLabApiException { return (GitLabApi.oauth2Login(ApiVersion.V4, url, username, password, null, null, false)); } /** * <p>Logs into GitLab using OAuth2 with the provided {@code username} and {@code password}, * and creates a new {@code GitLabApi} instance using returned access token.</p>