use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketUser in project che-server by eclipse-che.
the class HttpBitbucketServerApiClientTest method testGetUser.
@Test
public void testGetUser() throws ScmItemNotFoundException, ScmUnauthorizedException, ScmCommunicationException {
stubFor(get(urlEqualTo("/rest/api/1.0/users/ksmster")).withHeader(HttpHeaders.AUTHORIZATION, equalTo(AUTHORIZATION_TOKEN)).willReturn(aResponse().withHeader("Content-Type", "application/json; charset=utf-8").withBodyFile("bitbucket/rest/api/1.0/users/ksmster/response.json")));
BitbucketUser user = bitbucketServer.getUser("ksmster");
assertNotNull(user);
}
use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketUser in project che-server by eclipse-che.
the class BitbucketServerPersonalAccessTokenFetcher method fetchPersonalAccessToken.
@Override
public PersonalAccessToken fetchPersonalAccessToken(Subject cheUser, String scmServerUrl) throws ScmUnauthorizedException, ScmCommunicationException {
if (!bitbucketServerApiClient.isConnected(scmServerUrl)) {
LOG.debug("not a valid url {} for current fetcher ", scmServerUrl);
return null;
}
final String tokenName = format(TOKEN_NAME_TEMPLATE, cheUser.getUserId(), apiEndpoint.getHost());
try {
BitbucketUser user = bitbucketServerApiClient.getUser(EnvironmentContext.getCurrent().getSubject());
LOG.debug("Current bitbucket user {} ", user);
// cleanup existed
List<BitbucketPersonalAccessToken> existingTokens = bitbucketServerApiClient.getPersonalAccessTokens(user.getSlug()).stream().filter(p -> p.getName().equals(tokenName)).collect(Collectors.toList());
for (BitbucketPersonalAccessToken existedToken : existingTokens) {
LOG.debug("Deleting existed che token {} {}", existedToken.getId(), existedToken.getName());
bitbucketServerApiClient.deletePersonalAccessTokens(user.getSlug(), existedToken.getId());
}
BitbucketPersonalAccessToken token = bitbucketServerApiClient.createPersonalAccessTokens(user.getSlug(), tokenName, DEFAULT_TOKEN_SCOPE);
LOG.debug("Token created = {} for {}", token.getId(), token.getUser());
return new PersonalAccessToken(scmServerUrl, EnvironmentContext.getCurrent().getSubject().getUserId(), user.getName(), valueOf(user.getId()), token.getName(), valueOf(token.getId()), token.getToken());
} catch (ScmBadRequestException | ScmItemNotFoundException e) {
throw new ScmCommunicationException(e.getMessage(), e);
}
}
use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketUser in project che-server by eclipse-che.
the class BitbucketServerPersonalAccessTokenFetcherTest method setup.
@BeforeMethod
public void setup() throws MalformedURLException {
URL apiEndpoint = new URL("https://che.server.com");
subject = new SubjectImpl("another_user", "user987", "token111", false);
bitbucketUser = new BitbucketUser("User", "user", 32423523, "NORMAL", true, "user", "user@users.com");
bitbucketPersonalAccessToken = new BitbucketPersonalAccessToken(234234, 234345345, 23534534, "che-token-<user987>-<che.server.com>", "2340590skdf3<0>945i0923i4jasoidfj934ui50", bitbucketUser, ImmutableSet.of("PROJECT_WRITE", "REPO_WRITE"));
bitbucketPersonalAccessToken2 = new BitbucketPersonalAccessToken(3647456, 234345345, 23534534, "che-token-<user987>-<che.server.com>", "34545<0>945i0923i4jasoidfj934ui50", bitbucketUser, ImmutableSet.of("REPO_READ"));
bitbucketPersonalAccessToken3 = new BitbucketPersonalAccessToken(132423, 234345345, 23534534, "che-token-<user987>-<che.server.com>", "3456\\<0>945//i0923i4jasoidfj934ui50", bitbucketUser, ImmutableSet.of("PROJECT_READ", "REPO_READ"));
fetcher = new BitbucketServerPersonalAccessTokenFetcher(bitbucketServerApiClient, apiEndpoint);
EnvironmentContext context = new EnvironmentContext();
context.setSubject(subject);
EnvironmentContext.setCurrent(context);
}
use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketUser in project devspaces-images by redhat-developer.
the class BitbucketServerPersonalAccessTokenFetcher method fetchPersonalAccessToken.
@Override
public PersonalAccessToken fetchPersonalAccessToken(Subject cheUser, String scmServerUrl) throws ScmUnauthorizedException, ScmCommunicationException {
if (!bitbucketServerApiClient.isConnected(scmServerUrl)) {
LOG.debug("not a valid url {} for current fetcher ", scmServerUrl);
return null;
}
final String tokenName = format(TOKEN_NAME_TEMPLATE, cheUser.getUserId(), apiEndpoint.getHost());
try {
BitbucketUser user = bitbucketServerApiClient.getUser(EnvironmentContext.getCurrent().getSubject());
LOG.debug("Current bitbucket user {} ", user);
// cleanup existed
List<BitbucketPersonalAccessToken> existingTokens = bitbucketServerApiClient.getPersonalAccessTokens(user.getSlug()).stream().filter(p -> p.getName().equals(tokenName)).collect(Collectors.toList());
for (BitbucketPersonalAccessToken existedToken : existingTokens) {
LOG.debug("Deleting existed che token {} {}", existedToken.getId(), existedToken.getName());
bitbucketServerApiClient.deletePersonalAccessTokens(user.getSlug(), existedToken.getId());
}
BitbucketPersonalAccessToken token = bitbucketServerApiClient.createPersonalAccessTokens(user.getSlug(), tokenName, DEFAULT_TOKEN_SCOPE);
LOG.debug("Token created = {} for {}", token.getId(), token.getUser());
return new PersonalAccessToken(scmServerUrl, EnvironmentContext.getCurrent().getSubject().getUserId(), user.getName(), valueOf(user.getId()), token.getName(), valueOf(token.getId()), token.getToken());
} catch (ScmBadRequestException | ScmItemNotFoundException e) {
throw new ScmCommunicationException(e.getMessage(), e);
}
}
use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketUser in project devspaces-images by redhat-developer.
the class BitbucketServerPersonalAccessTokenFetcherTest method setup.
@BeforeMethod
public void setup() throws MalformedURLException {
URL apiEndpoint = new URL("https://che.server.com");
subject = new SubjectImpl("another_user", "user987", "token111", false);
bitbucketUser = new BitbucketUser("User", "user", 32423523, "NORMAL", true, "user", "user@users.com");
bitbucketPersonalAccessToken = new BitbucketPersonalAccessToken(234234, 234345345, 23534534, "che-token-<user987>-<che.server.com>", "2340590skdf3<0>945i0923i4jasoidfj934ui50", bitbucketUser, ImmutableSet.of("PROJECT_WRITE", "REPO_WRITE"));
bitbucketPersonalAccessToken2 = new BitbucketPersonalAccessToken(3647456, 234345345, 23534534, "che-token-<user987>-<che.server.com>", "34545<0>945i0923i4jasoidfj934ui50", bitbucketUser, ImmutableSet.of("REPO_READ"));
bitbucketPersonalAccessToken3 = new BitbucketPersonalAccessToken(132423, 234345345, 23534534, "che-token-<user987>-<che.server.com>", "3456\\<0>945//i0923i4jasoidfj934ui50", bitbucketUser, ImmutableSet.of("PROJECT_READ", "REPO_READ"));
fetcher = new BitbucketServerPersonalAccessTokenFetcher(bitbucketServerApiClient, apiEndpoint);
EnvironmentContext context = new EnvironmentContext();
context.setSubject(subject);
EnvironmentContext.setCurrent(context);
}
Aggregations