Java Code Examples for org.kohsuke.github.GitHub#getUser()

The following examples show how to use org.kohsuke.github.GitHub#getUser() . 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: GetRolesExecutor.java    From github-oauth-authorization-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public GoPluginApiResponse execute() throws IOException {
    if (request.getRoles().isEmpty()) {
        LOG.debug("[Get User Roles] Server sent empty roles config. Nothing to do!.");
        return DefaultGoPluginApiResponse.success("[]");
    }

    GitHub gitHub = clientBuilder.from(request.getAuthConfig().gitHubConfiguration());
    GHUser user = gitHub.getUser(request.getUsername());

    if (user == null) {
        LOG.error(format("[Get User Roles] User %s does not exist in GitHub.", request.getUsername()));
        return DefaultGoPluginApiResponse.error("");
    }

    List<String> roles = gitHubAuthorizer.authorize(user, request.getAuthConfig(), request.getRoles());

    LOG.debug(format("[Get User Roles] User %s has %s roles.", request.getUsername(), roles));
    return DefaultGoPluginApiResponse.success(GSON.toJson(roles));
}
 
Example 2
Source File: GitHubProvider.java    From gocd-oauth-login with Apache License 2.0 6 votes vote down vote up
private boolean isAMemberOfOrganization(GithubPluginSettings pluginSettings, User user) {
    try {
        GitHub github = getGitHub(pluginSettings);
        GHUser ghUser = github.getUser(user.getUsername());

        if(ghUser == null) return false;

        for(String orgName: pluginSettings.getGithubOrganizations()) {
            GHOrganization organization = github.getOrganization(orgName);

            if(organization != null && ghUser.isMemberOf(organization)){
                return true;
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Error occurred while trying to check if user is member of organization", e);
    }
    return false;
}
 
Example 3
Source File: ValidateUserRequestExecutor.java    From github-oauth-authorization-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public GoPluginApiResponse execute() throws Exception {
    GitHub gitHub = clientBuilder.from(request.getAuthConfig().gitHubConfiguration());
    GHUser user = gitHub.getUser(request.getUsername());
    if (user == null) {
        LOG.error(format("[Is Valid User] User %s does not exist in GitHub.", request.getUsername()));
        return DefaultGoPluginApiResponse.error(String.format("User '%s' does not exist in GitHub.", request.getUsername()));
    } else {
        LOG.debug(format("[Is Valid User] %s is valid user.", request.getUsername()));
        return DefaultGoPluginApiResponse.success("");
    }
}
 
Example 4
Source File: GitHubSCMNavigator.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@NonNull
@Override
public List<Action> retrieveActions(@NonNull SCMNavigatorOwner owner,
                                    @CheckForNull SCMNavigatorEvent event,
                                    @NonNull TaskListener listener) throws IOException, InterruptedException {
    // TODO when we have support for trusted events, use the details from event if event was from trusted source
    listener.getLogger().printf("Looking up details of %s...%n", getRepoOwner());
    List<Action> result = new ArrayList<>();
    StandardCredentials credentials = Connector.lookupScanCredentials((Item)owner, getApiUri(), credentialsId);
    GitHub hub = Connector.connect(getApiUri(), credentials);
    try {
        Connector.checkApiRateLimit(listener, hub);
        GHUser u = hub.getUser(getRepoOwner());
        String objectUrl = u.getHtmlUrl() == null ? null : u.getHtmlUrl().toExternalForm();
        result.add(new ObjectMetadataAction(
                Util.fixEmpty(u.getName()),
                null,
                objectUrl)
        );
        result.add(new GitHubOrgMetadataAction(u));
        result.add(new GitHubLink("icon-github-logo", u.getHtmlUrl()));
        if (objectUrl == null) {
            listener.getLogger().println("Organization URL: unspecified");
        } else {
            listener.getLogger().printf("Organization URL: %s%n",
                    HyperlinkNote.encodeTo(objectUrl, StringUtils.defaultIfBlank(u.getName(), objectUrl)));
        }
        return result;
    } finally {
        Connector.release(hub);
    }
}