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());
}
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();
}
};
}
}
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);
}
}
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);
}
}
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());
}
Aggregations