Search in sources :

Example 1 with HttpException

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

the class GithubScm method getOrganizations.

@Override
public Container<ScmOrganization> getOrganizations() {
    StaplerRequest request = Stapler.getCurrentRequest();
    String credentialId = getCredentialIdFromRequest(request);
    User authenticatedUser = getAuthenticatedUser();
    final StandardUsernamePasswordCredentials credential = CredentialsUtils.findCredential(credentialId, StandardUsernamePasswordCredentials.class, new BlueOceanDomainRequirement());
    if (credential == null) {
        throw new ServiceException.BadRequestExpception(String.format("Credential id: %s not found for user %s", credentialId, authenticatedUser.getId()));
    }
    String accessToken = credential.getPassword().getPlainText();
    try {
        GitHub github = new GitHubBuilder().withOAuthToken(accessToken).withRateLimitHandler(new RateLimitHandlerImpl()).withEndpoint(getUri()).build();
        final Link link = getLink().rel("organizations");
        // preserve the same order that github org api returns
        Map<String, ScmOrganization> orgMap = new LinkedHashMap<>();
        for (Map.Entry<String, GHOrganization> entry : github.getMyOrganizations().entrySet()) {
            orgMap.put(entry.getKey(), new GithubOrganization(GithubScm.this, entry.getValue(), credential, link));
        }
        GHMyself user = github.getMyself();
        if (orgMap.get(user.getLogin()) == null) {
            //this is to take care of case if/when github starts reporting user login as org later on
            orgMap = new HashMap<>(orgMap);
            orgMap.put(user.getLogin(), new GithubUserOrganization(user, credential, this));
        }
        final Map<String, ScmOrganization> orgs = orgMap;
        return new Container<ScmOrganization>() {

            @Override
            public ScmOrganization get(String name) {
                ScmOrganization org = orgs.get(name);
                if (org == null) {
                    throw new ServiceException.NotFoundException(String.format("GitHub organization %s not found", name));
                }
                return org;
            }

            @Override
            public Link getLink() {
                return link;
            }

            @Override
            public Iterator<ScmOrganization> iterator() {
                return orgs.values().iterator();
            }
        };
    } catch (IOException e) {
        if (e instanceof HttpException) {
            HttpException ex = (HttpException) e;
            if (ex.getResponseCode() == 401) {
                throw new ServiceException.PreconditionRequired("Invalid Github accessToken", ex);
            } else if (ex.getResponseCode() == 403) {
                throw new ServiceException.PreconditionRequired("Github accessToken does not have required scopes. Expected scopes 'user:email, repo'", ex);
            }
        }
        throw new ServiceException.UnexpectedErrorException(e.getMessage(), e);
    }
}
Also used : GHMyself(org.kohsuke.github.GHMyself) GHUser(org.kohsuke.github.GHUser) User(hudson.model.User) GitHub(org.kohsuke.github.GitHub) StaplerRequest(org.kohsuke.stapler.StaplerRequest) GitHubBuilder(org.kohsuke.github.GitHubBuilder) LinkedHashMap(java.util.LinkedHashMap) StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) ScmOrganization(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmOrganization) Container(io.jenkins.blueocean.rest.model.Container) GHOrganization(org.kohsuke.github.GHOrganization) HttpException(org.kohsuke.github.HttpException) IOException(java.io.IOException) ServiceException(io.jenkins.blueocean.commons.ServiceException) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Link(io.jenkins.blueocean.rest.hal.Link)

Aggregations

StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 User (hudson.model.User)1 ServiceException (io.jenkins.blueocean.commons.ServiceException)1 Link (io.jenkins.blueocean.rest.hal.Link)1 BlueOceanDomainRequirement (io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement)1 ScmOrganization (io.jenkins.blueocean.rest.impl.pipeline.scm.ScmOrganization)1 Container (io.jenkins.blueocean.rest.model.Container)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 GHMyself (org.kohsuke.github.GHMyself)1 GHOrganization (org.kohsuke.github.GHOrganization)1 GHUser (org.kohsuke.github.GHUser)1 GitHub (org.kohsuke.github.GitHub)1 GitHubBuilder (org.kohsuke.github.GitHubBuilder)1 HttpException (org.kohsuke.github.HttpException)1 StaplerRequest (org.kohsuke.stapler.StaplerRequest)1