org.eclipse.egit.github.core.service.UserService Java Examples

The following examples show how to use org.eclipse.egit.github.core.service.UserService. 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: GithubApi.java    From karamel with Apache License 2.0 6 votes vote down vote up
/**
 * Blindly accepts user credentials, no validation with github.
 *
 * @param user
 * @param password
 * @return primary github email for the user
 * @throws se.kth.karamel.common.exception.KaramelException
 */
public synchronized static GithubUser registerCredentials(String user, String password) throws KaramelException {
  try {
    GithubApi.user = user;
    GithubApi.password = password;
    client.setCredentials(user, password);
    client.getUser();
    Confs confs = Confs.loadKaramelConfs();
    confs.put(Settings.GITHUB_USER_KEY, user);
    confs.put(Settings.GITHUB_PASSWORD_KEY, password);
    confs.writeKaramelConfs();
    UserService us = new UserService(client);
    if (us == null) {
      throw new KaramelException("Could not find user or password incorret: " + user);
    }
    User u = us.getUser();
    if (u == null) {
      throw new KaramelException("Could not find user or password incorret: " + user);
    }
    GithubApi.email = u.getEmail();
  } catch (IOException ex) {
    logger.warn("Problem connecting to GitHub: " + ex.getMessage());
  }
  return new GithubUser(GithubApi.user, GithubApi.password, GithubApi.email);
}
 
Example #2
Source File: GithubApi.java    From karamel with Apache License 2.0 6 votes vote down vote up
/**
 * Create a repository in a given github user's local account.
 *
 * @param repoName
 * @param description
 * @throws KaramelException
 */
public synchronized static void createRepoForUser(String repoName, String description) throws KaramelException {
  try {
    UserService us = new UserService(client);
    RepositoryService rs = new RepositoryService(client);
    Repository r = new Repository();
    r.setName(repoName);
    r.setOwner(us.getUser());
    r.setDescription(description);
    rs.createRepository(r);
    cloneRepo(getUser(), repoName);
    cachedRepos.remove(GithubApi.getUser());
  } catch (IOException ex) {
    throw new KaramelException("Problem creating " + repoName + " for user " + ex.getMessage());
  }
}
 
Example #3
Source File: GitHubSourceConnector.java    From apicurio-studio with Apache License 2.0 5 votes vote down vote up
/**
 * @see io.apicurio.hub.api.github.IGitHubSourceConnector#getOrganizations()
 */
@Override
public Collection<GitHubOrganization> getOrganizations() throws GitHubException, SourceConnectorException {
    logger.debug("Getting organizations for current user.");
    Collection<GitHubOrganization> rval = new HashSet<>();
    try {
        GitHubClient client = githubClient();
        
        // Add the user's personal org
        UserService userService = new UserService(client);
        User user = userService.getUser();
        GitHubOrganization gho = new GitHubOrganization();
        gho.setUserOrg(true);
        gho.setId(user.getLogin());
        rval.add(gho);
        
        // Now all the user's orgs
        OrganizationService orgService = new OrganizationService(client);
        List<User> organizations = orgService.getOrganizations();
        for (User org : organizations) {
            gho = new GitHubOrganization();
            gho.setUserOrg(false);
            gho.setId(org.getLogin());
            rval.add(gho);
        }
    } catch (IOException e) {
        logger.error("Error getting GitHub organizations.", e);
        throw new GitHubException("Error getting GitHub organizations.", e);
    }
    return rval;
}
 
Example #4
Source File: GitHubSourceConnector.java    From apicurio-studio with Apache License 2.0 5 votes vote down vote up
/**
 * @see io.apicurio.hub.api.github.IGitHubSourceConnector#getRepositories(java.lang.String)
 */
@Override
public Collection<GitHubRepository> getRepositories(String org) throws GitHubException, SourceConnectorException {
    logger.debug("Getting the repositories from organization {}", org);
    Collection<GitHubRepository> rval = new HashSet<>();
    try {
        GitHubClient client = githubClient();

        // First get the user's login id
        UserService userService = new UserService(client);
        User user = userService.getUser();
        String userLogin = user.getLogin();

        // Get the Org/User repositories
        RepositoryService repoService = new RepositoryService(client);
        List<Repository> repositories = null;
        if (org.equals(userLogin)) {
            Map<String, String> filters = new HashMap<String, String>();
            filters.put("affiliation", "owner");
            filters.put("visibility", "all");
            repositories = repoService.getRepositories(filters);
        } else {
            repositories = repoService.getOrgRepositories(org);
        }
        
        for (Repository repository : repositories) {
            GitHubRepository ghrepo = new GitHubRepository();
            ghrepo.setName(repository.getName());
            ghrepo.setPriv(repository.isPrivate());
            rval.add(ghrepo);
        }
    } catch (IOException e) {
        logger.error("Error getting GitHub repositories.", e);
        throw new GitHubException("Error getting GitHub repositories.", e);
    }
    return rval;
}
 
Example #5
Source File: GitHubConnector.java    From tool.accelerate.core with Apache License 2.0 5 votes vote down vote up
/**
 * Must be called after #{createGitRepository()}
 */
public void pushAllChangesToGit() throws IOException {
    if (localRepository == null) {
        throw new IOException("Git has not been created, call createGitRepositoryFirst");
    }
    try {
        UserService userService = new UserService();
        userService.getClient().setOAuth2Token(oAuthToken);
        User user = userService.getUser();
        String name = user.getLogin();
        String email = user.getEmail();
        if (email == null) {
            // This is the e-mail addressed used by GitHub on web commits where the users mail is private. See:
            // https://github.com/settings/emails
            email = name + "@users.noreply.github.com";
        }

        localRepository.add().addFilepattern(".").call();
        localRepository.commit()
                .setMessage("Initial commit")
                .setCommitter(name, email)
                .call();
        PushCommand pushCommand = localRepository.push();
        addAuth(pushCommand);
        pushCommand.call();
    } catch (GitAPIException e) {
        throw new IOException("Error pushing changes to GitHub", e);
    }
}
 
Example #6
Source File: GitHubRepositoryCreationTest.java    From tool.accelerate.core with Apache License 2.0 5 votes vote down vote up
@Test
public void testGitHubRepositoryCreation() throws Exception {
    String oAuthToken = System.getProperty("gitHubOauth");
    assumeTrue(oAuthToken != null && !oAuthToken.isEmpty());
    String name = "TestAppAcceleratorProject";
    String port = System.getProperty("liberty.test.port");
    URI baseUri = new URI("http://localhost:" + port + "/start");
    ServiceConnector serviceConnector = new MockServiceConnector(baseUri);
    Services services = new Services();
    Service service = new Service();
    service.setId("wibble");
    List<Service> serviceList = Collections.singletonList(service);
    services.setServices(serviceList);
    ProjectConstructionInputData inputData = new ProjectConstructionInputData(services, serviceConnector, name, ProjectConstructor.DeployType.LOCAL, ProjectConstructor.BuildType.MAVEN, null, null, null, null, null, false);

    ProjectConstructor constructor = new ProjectConstructor(inputData);
    GitHubConnector connector = new GitHubConnector(oAuthToken);
    GitHubWriter writer = new GitHubWriter(constructor.buildFileMap(), inputData.appName, connector);
    writer.createProjectOnGitHub();

    RepositoryService repositoryService = new RepositoryService();
    repositoryService.getClient().setOAuth2Token(oAuthToken);
    UserService userService = new UserService();
    userService.getClient().setOAuth2Token(oAuthToken);
    ContentsService contentsService = new ContentsService();
    contentsService.getClient().setOAuth2Token(oAuthToken);
    Repository repository = repositoryService.getRepository(userService.getUser().getLogin(), name);
    checkFileExists(contentsService, repository, "pom.xml");
    checkFileExists(contentsService, repository, "README.md");
}
 
Example #7
Source File: CollaboratorServiceEx.java    From HubTurbo with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Gets the complete data for every user since most of GitHub APIs do not return users' complete data
 */
public static List<User> getCompleteUserData(List<User> users) {
    UserService service = new UserService();
    return users.stream()
            .map(user -> {
                try {
                    return service.getUser(user.getLogin());
                } catch (IOException e) {
                    logger.warn("Unable to get full details for user " + user.getLogin());
                    return user;
                }
            }).collect(Collectors.toList());
}
 
Example #8
Source File: GithubPullRequestManager.java    From cover-checker with Apache License 2.0 4 votes vote down vote up
public User getUser() throws IOException {
	return new UserService(ghClient).getUser();
}