hub4j / github-api

Java API for GitHub
https://github-api.kohsuke.org/
MIT License
1.12k stars 715 forks source link

Add support for fetching a single organisation member by username #1849

Closed fv316 closed 3 weeks ago

fv316 commented 1 month ago

Right now, fetching a single GitHub organisation member by username requires listing all members in that organisation using the listMembers method and filtering the response by the username. A similar approach is already encoded in the library for the getTeamByName which lists all teams and then filters by the team name. However, there exists a Get Organisation Memberships For A User endpoint on the GitHub API that could be used to fetch the user and other useful attributes.

This new method would be very similar to the hasMember method in the GHOrganisation.class (copied below) but calls /orgs/{org}/memberships/{username} and needs to map the response to the appropriate GHUser object.

    /**
     * Checks if this organization has the specified user as a member.
     *
     * @param user
     *            the user
     * @return the boolean
     */
    public boolean hasMember(GHUser user) {
        try {
            root().createRequest().withUrlPath("/orgs/" + login + "/members/" + user.getLogin()).send();
            return true;
        } catch (IOException ignore) {
            return false;
        }
    }

I'm happy to create a PR if you think this would be valuable.

bitwiseman commented 1 month ago

PR with tests welcome.

fv316 commented 1 month ago

Could you give me access to hub4j-test-org? Unless the PR with my mock organisation is fine.

bitwiseman commented 4 weeks ago

@fv316 Invite sent