Search in sources :

Example 1 with GHUser

use of org.kohsuke.github.GHUser in project blueocean-plugin by jenkinsci.

the class GithubScm method validateAndCreate.

@Override
public HttpResponse validateAndCreate(@JsonBody JSONObject request) {
    String accessToken = (String) request.get("accessToken");
    if (accessToken == null) {
        throw new ServiceException.BadRequestExpception("accessToken is required");
    }
    try {
        User authenticatedUser = getAuthenticatedUser();
        HttpURLConnection connection = connect(String.format("%s/%s", getUri(), "user"), accessToken);
        validateAccessTokenScopes(connection);
        String data = IOUtils.toString(connection.getInputStream());
        GHUser user = GithubScm.om.readValue(data, GHUser.class);
        if (user.getEmail() != null) {
            Mailer.UserProperty p = authenticatedUser.getProperty(Mailer.UserProperty.class);
            // the one from Github?
            if (p == null) {
                authenticatedUser.addProperty(new Mailer.UserProperty(user.getEmail()));
            }
        }
        //Now we know the token is valid. Lets find credential
        StandardUsernamePasswordCredentials githubCredential = CredentialsUtils.findCredential(getId(), StandardUsernamePasswordCredentials.class, new BlueOceanDomainRequirement());
        final StandardUsernamePasswordCredentials credential = new UsernamePasswordCredentialsImpl(CredentialsScope.USER, "github", "Github Access Token", authenticatedUser.getId(), accessToken);
        if (githubCredential == null) {
            CredentialsUtils.createCredentialsInUserStore(credential, authenticatedUser, getCredentialsDomainName(getUri()), ImmutableList.<DomainSpecification>of(new BlueOceanDomainSpecification()));
        } else {
            CredentialsUtils.updateCredentialsInUserStore(githubCredential, credential, authenticatedUser, getCredentialsDomainName(getUri()), ImmutableList.<DomainSpecification>of(new BlueOceanDomainSpecification()));
        }
        return createResponse(credential.getId());
    } catch (IOException e) {
        throw new ServiceException.UnexpectedErrorException(e.getMessage());
    }
}
Also used : GHUser(org.kohsuke.github.GHUser) User(hudson.model.User) BlueOceanDomainSpecification(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification) Mailer(hudson.tasks.Mailer) GHUser(org.kohsuke.github.GHUser) IOException(java.io.IOException) UsernamePasswordCredentialsImpl(com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) HttpURLConnection(java.net.HttpURLConnection) ServiceException(io.jenkins.blueocean.commons.ServiceException) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)

Example 2 with GHUser

use of org.kohsuke.github.GHUser in project Gargoyle by callakrsos.

the class GitTest method getGargoyleRepository.

@Test
public void getGargoyleRepository() throws IOException {
    GitHub github = GitHub.connect();
    GHUser user = github.getUser("callakrsos");
    Map<String, GHRepository> repositories = user.getRepositories();
    //Repository를 모두 출력
    Iterator<Entry<String, GHRepository>> iterator = repositories.entrySet().iterator();
    while (iterator.hasNext()) {
        Entry<String, GHRepository> next = iterator.next();
        System.out.println(next.getKey() + " - " + next.getValue().getName());
    }
    //Gargoyle Repository 정보 출력
    GHRepository repository = user.getRepository("Gargoyle");
    System.out.println(repository);
    List<GHContent> directoryContent = repository.getDirectoryContent("/");
    //루트 디렉토리 정보 출력
    directoryContent.forEach(con -> {
        System.out.println(String.format("Path : %s\t\t\tsize : %d\t\t\t\tUrl:%s ", con.getPath(), con.getSize(), con.getUrl()));
    });
}
Also used : Entry(java.util.Map.Entry) GHRepository(org.kohsuke.github.GHRepository) GitHub(org.kohsuke.github.GitHub) GHContent(org.kohsuke.github.GHContent) GHUser(org.kohsuke.github.GHUser) Test(org.junit.Test)

Example 3 with GHUser

use of org.kohsuke.github.GHUser in project Gargoyle by callakrsos.

the class GitTest method readContent.

@Test
public void readContent() throws IOException {
    GitHub github = GitHub.connect();
    GHUser user = github.getUser("callakrsos");
    //Gargoyle Repository 정보 출력
    GHRepository repository = user.getRepository("Gargoyle");
    System.out.println(repository);
    GHContent fileContent = repository.getFileContent(".project", "master");
    String string = fileContent.toString();
    System.out.println(string);
    String downloadUrl = fileContent.getDownloadUrl();
    System.out.println(downloadUrl);
    String gitUrl = fileContent.getGitUrl();
    System.out.println(gitUrl);
    try (BufferedReader br = new BufferedReader(new InputStreamReader(fileContent.read()))) {
        String readLine = null;
        while ((readLine = br.readLine()) != null) {
            System.out.println(readLine);
        }
    }
}
Also used : GHRepository(org.kohsuke.github.GHRepository) InputStreamReader(java.io.InputStreamReader) GitHub(org.kohsuke.github.GitHub) GHContent(org.kohsuke.github.GHContent) BufferedReader(java.io.BufferedReader) GHUser(org.kohsuke.github.GHUser) Test(org.junit.Test)

Aggregations

GHUser (org.kohsuke.github.GHUser)3 Test (org.junit.Test)2 GHContent (org.kohsuke.github.GHContent)2 GHRepository (org.kohsuke.github.GHRepository)2 GitHub (org.kohsuke.github.GitHub)2 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1 UsernamePasswordCredentialsImpl (com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl)1 User (hudson.model.User)1 Mailer (hudson.tasks.Mailer)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)1 BlueOceanDomainSpecification (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainSpecification)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1 Entry (java.util.Map.Entry)1