GitLabApi.java 61.60 KiB
package org.gitlab4j.api;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.WeakHashMap;
import java.util.logging.Level;
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.User;
import org.gitlab4j.api.models.Version;
import org.gitlab4j.api.utils.MaskingLoggingFilter;
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 implements AutoCloseable {
    private final static Logger LOGGER = Logger.getLogger(GitLabApi.class.getName());
    /** GitLab4J default per page.  GitLab will ignore anything over 100. */
    public static final int DEFAULT_PER_PAGE = 96;
    /** Specifies the version of the GitLab API to communicate with. */
    public enum ApiVersion {
        V3, V4;
        public String getApiNamespace() {
            return ("/api/" + name().toLowerCase());
    // Used to keep track of GitLabApiExceptions on calls that return Optional<?>
    private static final Map<Integer, GitLabApiException> optionalExceptionMap =
            Collections.synchronizedMap(new WeakHashMap<Integer, GitLabApiException>());
    GitLabApiClient apiClient;
    private ApiVersion apiVersion;
    private String gitLabServerUrl;
    private Map<String, Object> clientConfigProperties;
    private int defaultPerPage = DEFAULT_PER_PAGE;
    private ApplicationsApi applicationsApi;
    private ApplicationSettingsApi applicationSettingsApi;
    private AuditEventApi auditEventApi;
    private AwardEmojiApi awardEmojiApi;
    private BoardsApi boardsApi;
    private CommitsApi commitsApi;
    private ContainerRegistryApi containerRegistryApi;
    private DiscussionsApi discussionsApi;
    private DeployKeysApi deployKeysApi;
    private DeploymentsApi deploymentsApi;
    private DeployTokensApi deployTokensApi;
    private EnvironmentsApi environmentsApi;
    private EpicsApi epicsApi;
    private EventsApi eventsApi;
    private GroupApi groupApi;
    private HealthCheckApi healthCheckApi;
    private ImportExportApi importExportApi;
    private IssuesApi issuesApi;
    private JobApi jobApi;
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
private LabelsApi labelsApi; private LicenseApi licenseApi; private LicenseTemplatesApi licenseTemplatesApi; private MarkdownApi markdownApi; private MergeRequestApi mergeRequestApi; private MilestonesApi milestonesApi; private NamespaceApi namespaceApi; private NotesApi notesApi; private NotificationSettingsApi notificationSettingsApi; private PackagesApi packagesApi; private PipelineApi pipelineApi; private ProjectApi projectApi; private ProtectedBranchesApi protectedBranchesApi; private ReleasesApi releasesApi; private RepositoryApi repositoryApi; private RepositoryFileApi repositoryFileApi; private ResourceLabelEventsApi resourceLabelEventsApi; private RunnersApi runnersApi; private SearchApi searchApi; private ServicesApi servicesApi; private SnippetsApi snippetsApi; private SystemHooksApi systemHooksApi; private TagsApi tagsApi; private TodosApi todosApi; private UserApi userApi; private WikisApi wikisApi; /** * Get the GitLab4J shared Logger instance. * * @return the GitLab4J shared Logger instance */ public static final Logger getLogger() { return (LOGGER); } /** * Constructs a GitLabApi instance set up to interact with the GitLab server * using GitLab API version 4. This is the primary way to authenticate with * the GitLab REST API. * * @param hostUrl the URL of the GitLab server * @param personalAccessToken the private token to use for access to the API */ public GitLabApi(String hostUrl, String personalAccessToken) { this(ApiVersion.V4, hostUrl, personalAccessToken, null); } /** * Constructs a GitLabApi instance set up to interact with the GitLab server using GitLab API version 4. * * @param hostUrl the URL of the GitLab server * @param personalAccessToken the private token to use for access to the API * @param secretToken use this token to validate received payloads */ public GitLabApi(String hostUrl, String personalAccessToken, String secretToken) { this(ApiVersion.V4, hostUrl, TokenType.PRIVATE, personalAccessToken, secretToken); } /** * <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 {