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

The following examples show how to use org.kohsuke.github.GitHub#getOrganization() . 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: MembershipChecker.java    From github-oauth-authorization-plugin with Apache License 2.0 6 votes vote down vote up
private boolean checkTeamMembershipUsingPersonalAccessToken(GHUser ghUser, AuthConfig authConfig, Map<String, List<String>> organizationAndTeamsAllowed) throws IOException {
    final GitHub gitHubForPersonalAccessToken = clientBuilder.from(authConfig.gitHubConfiguration());

    for (String organizationName : organizationAndTeamsAllowed.keySet()) {
        final GHOrganization organization = gitHubForPersonalAccessToken.getOrganization(organizationName);

        if (organization != null) {
            final List<String> allowedTeamsFromRole = organizationAndTeamsAllowed.get(organizationName);
            final Map<String, GHTeam> teamsFromGitHub = organization.getTeams();

            for (GHTeam team : teamsFromGitHub.values()) {
                if (allowedTeamsFromRole.contains(team.getName().toLowerCase()) && team.hasMember(ghUser)) {
                    LOG.info(format("[MembershipChecker] User `{0}` is a member of `{1}` team.", ghUser.getLogin(), team.getName()));
                    return true;
                }
            }
        }
    }

    return false;
}
 
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: GithubApi.java    From karamel with Apache License 2.0 6 votes vote down vote up
public synchronized static void removeRepo(String owner, String repoName) throws KaramelException {

    try {
      GitHub gitHub = GitHub.connectUsingPassword(GithubApi.getUser(), GithubApi.getPassword());
      if (!gitHub.isCredentialValid()) {
        throw new KaramelException("Invalid GitHub credentials");
      }
      GHRepository repo = null;
      if (owner.compareToIgnoreCase(GithubApi.getUser()) != 0) {
        GHOrganization org = gitHub.getOrganization(owner);
        repo = org.getRepository(repoName);
      } else {
        repo = gitHub.getRepository(owner + "/" + repoName);
      }
      repo.delete();

    } catch (IOException ex) {
      throw new KaramelException("Problem authenticating with gihub-api when trying to remove a repository");
    }
  }
 
Example 4
Source File: ArchetypeBuilder.java    From ipaas-quickstarts with Apache License 2.0 6 votes vote down vote up
/**
 * Iterates through all projects in the given github organisation and generates an archetype for it
 */
public void generateArchetypesFromGithubOrganisation(String githubOrg, File outputDir, List<String> dirs) throws IOException {
    GitHub github = GitHub.connectAnonymously();
    GHOrganization organization = github.getOrganization(githubOrg);
    Objects.notNull(organization, "No github organisation found for: " + githubOrg);
    Map<String, GHRepository> repositories = organization.getRepositories();
    Set<Map.Entry<String, GHRepository>> entries = repositories.entrySet();

    File cloneParentDir = new File(outputDir, "../git-clones");
    if (cloneParentDir.exists()) {
        Files.recursiveDelete(cloneParentDir);
    }
    for (Map.Entry<String, GHRepository> entry : entries) {
        String repoName = entry.getKey();
        GHRepository repo = entry.getValue();
        String url = repo.getGitTransportUrl();

        generateArchetypeFromGitRepo(outputDir, dirs, cloneParentDir, repoName, url, null);
    }
}
 
Example 5
Source File: MembershipChecker.java    From github-oauth-authorization-plugin with Apache License 2.0 5 votes vote down vote up
private boolean checkMembershipUsingPersonalAccessToken(GHUser ghUser, AuthConfig authConfig, List<String> organizationsAllowed) throws IOException {
    final GitHub gitHubForPersonalAccessToken = clientBuilder.from(authConfig.gitHubConfiguration());

    for (String organizationName : organizationsAllowed) {
        final GHOrganization organization = gitHubForPersonalAccessToken.getOrganization(organizationName);
        if (organization != null && organization.hasMember(ghUser)) {
            LOG.info(format("[MembershipChecker] User `{0}` is a member of `{1}` organization.", ghUser.getLogin(), organizationName));
            return true;
        }
    }

    return false;
}
 
Example 6
Source File: GitHubSCMNavigator.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
private GHOrganization getGhOrganization(final GitHub github) throws IOException {
    try {
        return github.getOrganization(repoOwner);
    } catch (RateLimitExceededException rle) {
        throw new AbortException(rle.getMessage());
    } catch (FileNotFoundException fnf) {
        // may be an user... ok to ignore
    }
    return null;
}