Search in sources :

Example 1 with BbOrg

use of io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbOrg in project blueocean-plugin by jenkinsci.

the class BitbucketApiTest method getProject.

@Test
public void getProject() throws JsonProcessingException {
    BbOrg project = api.getOrg("TESTP");
    assertEquals("TESTP", project.getKey());
    assertEquals("testproject1", project.getName());
}
Also used : BbOrg(io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbOrg) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with BbOrg

use of io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbOrg in project blueocean-plugin by jenkinsci.

the class AbstractBitbucketScm method getOrganizations.

@Override
public Container<ScmOrganization> getOrganizations() {
    User authenticatedUser = getAuthenticatedUser();
    StaplerRequest request = Stapler.getCurrentRequest();
    Preconditions.checkNotNull(request, "This request must be made in HTTP context");
    String credentialId = BitbucketCredentialUtils.computeCredentialId(getCredentialIdFromRequest(request), getId(), getUri());
    List<ErrorMessage.Error> errors = new ArrayList<>();
    StandardUsernamePasswordCredentials credential = null;
    if (credentialId == null) {
        errors.add(new ErrorMessage.Error("credentialId", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "Missing credential id. It must be provided either as HTTP header: " + X_CREDENTIAL_ID + " or as query parameter 'credentialId'"));
    } else {
        credential = CredentialsUtils.findCredential(credentialId, StandardUsernamePasswordCredentials.class, new BlueOceanDomainRequirement());
        if (credential == null) {
            errors.add(new ErrorMessage.Error("credentialId", ErrorMessage.Error.ErrorCodes.INVALID.toString(), String.format("credentialId: %s not found in user %s's credential store", credentialId, authenticatedUser.getId())));
        }
    }
    String apiUrl = request.getParameter("apiUrl");
    if (StringUtils.isBlank(apiUrl)) {
        errors.add(new ErrorMessage.Error("apiUrl", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "apiUrl is required parameter"));
    }
    if (!errors.isEmpty()) {
        throw new ServiceException.BadRequestException(new ErrorMessage(400, "Failed to return Bitbucket organizations").addAll(errors));
    } else {
        apiUrl = normalizeApiUrl(apiUrl);
        BitbucketApiFactory apiFactory = BitbucketApiFactory.resolve(this.getId());
        if (apiFactory == null) {
            throw new ServiceException.UnexpectedErrorException("BitbucketApiFactory to handle apiUrl " + apiUrl + " not found");
        }
        Preconditions.checkNotNull(credential);
        final BitbucketApi api = apiFactory.create(apiUrl, credential);
        return new Container<ScmOrganization>() {

            @Override
            public ScmOrganization get(String name) {
                return new BitbucketOrg(api.getOrg(name), api, getLink());
            }

            @Override
            public Link getLink() {
                return AbstractBitbucketScm.this.getLink().rel("organizations");
            }

            @Override
            public Iterator<ScmOrganization> iterator() {
                return iterator(0, 100);
            }

            @Override
            public Iterator<ScmOrganization> iterator(int start, int limit) {
                if (limit <= 0) {
                    limit = PagedResponse.DEFAULT_LIMIT;
                }
                if (start < 0) {
                    start = 0;
                }
                int page = (start / limit) + 1;
                return Lists.transform(api.getOrgs(page, limit).getValues(), new Function<BbOrg, ScmOrganization>() {

                    @Nullable
                    @Override
                    public ScmOrganization apply(@Nullable BbOrg input) {
                        if (input != null) {
                            return new BitbucketOrg(input, api, getLink());
                        }
                        return null;
                    }
                }).iterator();
            }
        };
    }
}
Also used : User(hudson.model.User) StaplerRequest(org.kohsuke.stapler.StaplerRequest) ArrayList(java.util.ArrayList) BbOrg(io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbOrg) StandardUsernamePasswordCredentials(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials) Function(com.google.common.base.Function) Container(io.jenkins.blueocean.rest.model.Container) ScmOrganization(io.jenkins.blueocean.rest.impl.pipeline.scm.ScmOrganization) BlueOceanDomainRequirement(io.jenkins.blueocean.rest.impl.pipeline.credential.BlueOceanDomainRequirement) ErrorMessage(io.jenkins.blueocean.commons.ErrorMessage) Nullable(javax.annotation.Nullable)

Example 3 with BbOrg

use of io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbOrg in project blueocean-plugin by jenkinsci.

the class BitbucketCloudApi method getOrgs.

@Nonnull
@Override
public BbPage<BbOrg> getOrgs(int pageNumber, int pageSize) {
    try {
        /*
             * Bitbucket teams API work with three roles: admin, contributor, member
             *
             * We default to 'contributor' role, because we only want to only list teams that have at least one repo
             * where user has WRITE access.
             *
             * see: https://developer.atlassian.com/bitbucket/api/2/reference/resource/teams
             */
        if (pageNumber <= 0) {
            pageNumber = 1;
        }
        if (pageSize <= 0) {
            pageSize = PagedResponse.DEFAULT_LIMIT;
        }
        InputStream inputStream = request.get(String.format("%s&page=%s&pagelen=%s", baseUrl + "teams/?role=contributor", pageNumber, pageSize)).getContent();
        BbPage<BbOrg> page = om.readValue(inputStream, new TypeReference<BbCloudPage<BbCloudTeam>>() {
        });
        if (pageNumber == 1) {
            // add user org as the first org on first page
            BbUser user = getUser();
            if (page instanceof BbCloudPage) {
                List<BbOrg> teams = new ArrayList<>();
                teams.add(new BbCloudTeam(user.getSlug(), user.getDisplayName(), user.getAvatar()));
                int newSize = page.getSize() + 1;
                int newPageLength = page.getLimit();
                if (page.getSize() > page.getLimit()) {
                    newPageLength++;
                }
                teams.addAll(page.getValues());
                return new BbCloudPage<>(newPageLength, pageNumber, newSize, ((BbCloudPage) page).getNext(), teams);
            }
        }
        return page;
    } catch (IOException e) {
        throw handleException(e);
    }
}
Also used : InputStream(java.io.InputStream) BbUser(io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbUser) ArrayList(java.util.ArrayList) BbCloudPage(io.jenkins.blueocean.blueocean_bitbucket_pipeline.cloud.model.BbCloudPage) BbOrg(io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbOrg) IOException(java.io.IOException) BbCloudTeam(io.jenkins.blueocean.blueocean_bitbucket_pipeline.cloud.model.BbCloudTeam) Nonnull(javax.annotation.Nonnull)

Example 4 with BbOrg

use of io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbOrg in project blueocean-plugin by jenkinsci.

the class BitbucketServerApi method getOrgs.

@Override
@Nonnull
public BbPage<BbOrg> getOrgs(int pageNumber, int pageSize) {
    try {
        if (pageNumber <= 0) {
            pageNumber = 1;
        }
        if (pageSize <= 0) {
            pageSize = PagedResponse.DEFAULT_LIMIT;
        }
        InputStream inputStream = request.get(String.format("%s?start=%s&limit=%s", baseUrl + "projects/", toStart(pageNumber, pageSize), pageSize)).getContent();
        BbPage<BbOrg> page = om.readValue(inputStream, new TypeReference<BbServerPage<BbServerProject>>() {
        });
        if (pageNumber == 1) {
            // add user org as the first org on first page
            BbServerUser user = getUser(userName);
            List<BbOrg> teams = new ArrayList<>();
            teams.add(user.toPersonalProject());
            teams.addAll(page.getValues());
            return new BbServerPage<>(page.getStart(), page.getLimit(), page.getSize() + 1, teams, page.isLastPage());
        }
        return page;
    } catch (IOException e) {
        throw handleException(e);
    }
}
Also used : BbServerPage(io.jenkins.blueocean.blueocean_bitbucket_pipeline.server.model.BbServerPage) InputStream(java.io.InputStream) BbServerUser(io.jenkins.blueocean.blueocean_bitbucket_pipeline.server.model.BbServerUser) ArrayList(java.util.ArrayList) BbOrg(io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbOrg) IOException(java.io.IOException) Nonnull(javax.annotation.Nonnull)

Example 5 with BbOrg

use of io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbOrg in project blueocean-plugin by jenkinsci.

the class BitbucketApiTest method getUserTeam.

@Test
public void getUserTeam() throws JsonProcessingException {
    BbOrg team = api.getOrg("vivekp7");
    assertEquals("vivekp7", team.getKey());
    assertEquals("Vivek Pandey", team.getName());
    assertEquals("https://bitbucket.org/account/vivekp7/avatar/50/", team.getAvatar());
}
Also used : BbOrg(io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbOrg) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

BbOrg (io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbOrg)7 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Nonnull (javax.annotation.Nonnull)2 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1 Function (com.google.common.base.Function)1 User (hudson.model.User)1 BbCloudPage (io.jenkins.blueocean.blueocean_bitbucket_pipeline.cloud.model.BbCloudPage)1 BbCloudTeam (io.jenkins.blueocean.blueocean_bitbucket_pipeline.cloud.model.BbCloudTeam)1 BbUser (io.jenkins.blueocean.blueocean_bitbucket_pipeline.model.BbUser)1 BbServerPage (io.jenkins.blueocean.blueocean_bitbucket_pipeline.server.model.BbServerPage)1 BbServerUser (io.jenkins.blueocean.blueocean_bitbucket_pipeline.server.model.BbServerUser)1 ErrorMessage (io.jenkins.blueocean.commons.ErrorMessage)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 Nullable (javax.annotation.Nullable)1