Java Code Examples for org.gitlab.api.GitlabAPI#connect()

The following examples show how to use org.gitlab.api.GitlabAPI#connect() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitlabApiClient.java    From nexus3-gitlabauth-plugin with MIT License 6 votes vote down vote up
private GitlabPrincipal doAuthz(String loginName, char[] token) throws GitlabAuthenticationException {
    GitlabUser gitlabUser;
    List<GitlabGroup> groups = null;
    try {
        GitlabAPI gitlabAPI = GitlabAPI.connect(configuration.getGitlabApiUrl(), String.valueOf(token));
        gitlabUser = gitlabAPI.getUser();
    } catch (Exception e) {
        throw new GitlabAuthenticationException(e);
    }

    if (gitlabUser==null || !loginName.equals(gitlabUser.getEmail())) {
        throw new GitlabAuthenticationException("Given username not found or does not match Github Username!");
    }

    GitlabPrincipal principal = new GitlabPrincipal();

    principal.setUsername(gitlabUser.getEmail());
    principal.setGroups(getGroups((gitlabUser.getUsername())));

    return principal;
}
 
Example 2
Source File: GitLabProvider.java    From gocd-build-status-notifier with Apache License 2.0 6 votes vote down vote up
@Override
public void updateStatus(String url, PluginSettings pluginSettings, String prIdStr, String revision, String pipelineStage,
                         String result, String trackbackURL) throws Exception {

    String endPointToUse = pluginSettings.getEndPoint();
    String oauthAccessTokenToUse = pluginSettings.getOauthToken();

    if (StringUtils.isEmpty(endPointToUse)) {
        endPointToUse = System.getProperty("go.plugin.build.status.gitlab.endpoint");
    }
    if (StringUtils.isEmpty(oauthAccessTokenToUse)) {
        oauthAccessTokenToUse = System.getProperty("go.plugin.build.status.gitlab.oauth");
    }

    GitlabAPI api = GitlabAPI.connect(endPointToUse, oauthAccessTokenToUse);
    GitlabProject project = api.getProject(getRepository(url));
    api.createCommitStatus(project, revision, GitLabState.stateFor(result), prIdStr, "GoCD", trackbackURL, "");
}
 
Example 3
Source File: GitLabContext.java    From git-as-svn with GNU General Public License v2.0 6 votes vote down vote up
@NotNull
public static GitLabToken obtainAccessToken(@NotNull String gitlabUrl, @NotNull String username, @NotNull String password, boolean sudoScope) throws IOException {
  try {
    final OAuthGetAccessToken tokenServerUrl = new OAuthGetAccessToken(gitlabUrl + "/oauth/token?scope=api" + (sudoScope ? "%20sudo" : ""));
    final TokenResponse oauthResponse = new PasswordTokenRequest(transport, JacksonFactory.getDefaultInstance(), tokenServerUrl, username, password).execute();
    return new GitLabToken(TokenType.ACCESS_TOKEN, oauthResponse.getAccessToken());
  } catch (TokenResponseException e) {
    if (sudoScope && e.getStatusCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
      // Fallback for pre-10.2 gitlab versions
      final GitlabSession session = GitlabAPI.connect(gitlabUrl, username, password);
      return new GitLabToken(TokenType.PRIVATE_TOKEN, session.getPrivateToken());
    } else {
      throw new GitlabAPIException(e.getMessage(), e.getStatusCode(), e);
    }
  }
}
 
Example 4
Source File: GitlabRepositoryApiImpl.java    From bistoury with GNU General Public License v3.0 5 votes vote down vote up
private GitlabAPI createGitlabApi() {

        if (Strings.isNullOrEmpty(gitEndPoint)) {
            throw new RuntimeException("git 链接配置错误");
        }

        String userCode = LoginContext.getLoginContext().getLoginUser();
        Optional<PrivateToken> token = gitPrivateTokenService.queryToken(userCode);
        if (!token.isPresent()) {
            throw new RuntimeException("尚未设置 Git Private Token");
        }
        return GitlabAPI.connect(gitEndPoint, token.get().getPrivateToken());
    }
 
Example 5
Source File: GitlabService.java    From jhipster-online with Apache License 2.0 5 votes vote down vote up
/**
 * Connect to GitLab as the current logged in user.
 */
private GitlabAPI getConnection(User user) throws Exception {
    log.debug("Authenticating user `{}` on GitLab", user.getLogin());
    if (user.getGitlabOAuthToken() == null) {
        log.info("No GitLab token configured");
        throw new Exception("GitLab is not configured.");
    }
    GitlabAPI gitlab = GitlabAPI.connect(applicationProperties.getGitlab().getHost(), user.getGitlabOAuthToken(),
        TokenType.ACCESS_TOKEN);

    log.debug("User `{}` authenticated as `{}` on GitLab", user.getLogin(), gitlab.getUser().getUsername());
    return gitlab;
}
 
Example 6
Source File: GitLabAPI.java    From gitlab-branch-source-plugin with GNU General Public License v2.0 5 votes vote down vote up
public static GitLabAPI connect(String url, String token, boolean ignoreCertificateErrors, int requestTimeout) throws GitLabAPIException {
    try {
        GitlabAPI delegate = GitlabAPI.connect(url, token);
        delegate.ignoreCertificateErrors(ignoreCertificateErrors);
        return new GitLabAPI(delegate);
    } catch (Exception e) {
        throw new GitLabAPIException(e);
    }
}
 
Example 7
Source File: GitLabClient.java    From git-changelog-lib with Apache License 2.0 5 votes vote down vote up
private static List<GitlabIssue> getAllIssues(GitLabProjectIssuesCacheKey cacheKey)
    throws IOException {
  String hostUrl = cacheKey.getHostUrl();
  String apiToken = cacheKey.getApiToken();
  GitlabAPI gitLabApi = GitlabAPI.connect(hostUrl, apiToken);
  GitlabProject project = new GitlabProject();
  project.setId(cacheKey.getProjectId());
  return gitLabApi.getIssues(project);
}
 
Example 8
Source File: GitlabApiClient.java    From nexus3-gitlabauth-plugin with MIT License 4 votes vote down vote up
@PostConstruct
public void init() {
    client = GitlabAPI.connect(configuration.getGitlabApiUrl(), configuration.getGitlabApiKey());
    initPrincipalCache();
}
 
Example 9
Source File: GitLabContext.java    From git-as-svn with GNU General Public License v2.0 4 votes vote down vote up
@NotNull
public static GitlabAPI connect(@NotNull String gitlabUrl, @NotNull GitLabToken token) {
  return GitlabAPI.connect(gitlabUrl, token.getValue(), token.getType());
}
 
Example 10
Source File: GitlabConnection.java    From mylyn-gitlab with Eclipse Public License 1.0 4 votes vote down vote up
public GitlabAPI api() {
	return GitlabAPI.connect(host, token);
}