blob: dd8f7127935ae103384bc21b77056be578366024 [file] [log] [blame]
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.account;
import com.google.common.base.MoreObjects;
import com.google.gerrit.extensions.api.accounts.AccountInput;
import com.google.gerrit.extensions.common.AccountInfo;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.TopLevelResource;
import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Account.Id;
import com.google.gerrit.server.account.CreateAccount.Factory;
import com.google.gerrit.server.account.externalids.ExternalId;
import com.google.gerrit.server.account.externalids.ExternalIdsUpdate;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import java.io.IOException;
import org.apache.http.HttpStatus;
import org.eclipse.jgit.errors.ConfigInvalidException;
public class AccountImporter {
private final Factory createAccountFactory;
private final ExternalIdsUpdate.Server externalIdsUpdateServer;
@Inject
public AccountImporter(
final CreateAccount.Factory createAccountFactory,
ExternalIdsUpdate.Server externalIdsUpdateServer) {
this.createAccountFactory = createAccountFactory;
this.externalIdsUpdateServer = externalIdsUpdateServer;
}
public Account.Id importAccount(String login, String name, String email)
throws IOException, BadRequestException, ResourceConflictException,
UnprocessableEntityException, OrmException, ConfigInvalidException {
CreateAccount createAccount = createAccountFactory.create(login);
AccountInput accountInput = new AccountInput();
accountInput.email = email;
accountInput.username = login;
accountInput.name = MoreObjects.firstNonNull(name, login);
Response<AccountInfo> accountResponse =
createAccount.apply(TopLevelResource.INSTANCE, accountInput);
if (accountResponse.statusCode() != HttpStatus.SC_CREATED) {
throw new IOException(
"Cannot import GitHub account "
+ login
+ ": HTTP Status "
+ accountResponse.statusCode());
}
Id accountId = new Account.Id(accountResponse.value()._accountId.intValue());
externalIdsUpdateServer
.create()
.insert(ExternalId.create(ExternalId.SCHEME_GERRIT, login, accountId));
return accountId;
}
}