Allows self-provisioned accounts to login to Gerrit.
When accounts are self-provisioned during a Pull Request
import (because they are the owner of the request),
generate the gerrit scheme for account_external_ids
so that they can log-in using their GitHub credentials.
Change-Id: I4834aef2d94f56c6618b83288fa2bce3b9cfb2e1
diff --git a/github-plugin/src/main/java/com/google/gerrit/server/account/AccountImpoter.java b/github-plugin/src/main/java/com/google/gerrit/server/account/AccountImpoter.java
index 6f643b9..2ff0137 100644
--- a/github-plugin/src/main/java/com/google/gerrit/server/account/AccountImpoter.java
+++ b/github-plugin/src/main/java/com/google/gerrit/server/account/AccountImpoter.java
@@ -14,41 +14,59 @@
package com.google.gerrit.server.account;
import java.io.IOException;
+import java.util.Arrays;
import org.apache.http.HttpStatus;
import org.kohsuke.github.GHUser;
+import com.google.common.base.Objects;
+import com.google.common.collect.Iterables;
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.reviewdb.client.AccountExternalId;
+import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.account.CreateAccount.Factory;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
+import com.google.inject.Provider;
public class AccountImpoter {
- private Factory createAccountFactory;
+ private final Factory createAccountFactory;
+ private final Provider<ReviewDb> schema;
@Inject
- public AccountImpoter(CreateAccount.Factory createAccountFactory) {
+ public AccountImpoter(final CreateAccount.Factory createAccountFactory,
+ final Provider<ReviewDb> schema) {
this.createAccountFactory = createAccountFactory;
+ this.schema = schema;
}
- public Account.Id importAccount(String login, String name, String email) throws IOException,
- BadRequestException, ResourceConflictException,
+ @SuppressWarnings("unchecked")
+ public Account.Id importAccount(String login, String name, String email)
+ throws IOException, BadRequestException, ResourceConflictException,
UnprocessableEntityException, OrmException {
+ ReviewDb db = schema.get();
CreateAccount createAccount = createAccountFactory.create(login);
CreateAccount.Input accountInput = new CreateAccount.Input();
accountInput.email = email;
- accountInput.name = name;
accountInput.username = login;
+ accountInput.name = Objects.firstNonNull(name, login);
Response<AccountInfo> accountResponse =
(Response<AccountInfo>) createAccount.apply(TopLevelResource.INSTANCE,
accountInput);
if (accountResponse.statusCode() == HttpStatus.SC_CREATED) {
- return accountResponse.value()._id;
+ Id accountId = accountResponse.value()._id;
+ db.accountExternalIds().insert(
+ Arrays
+ .asList(new AccountExternalId(accountId,
+ new AccountExternalId.Key(AccountExternalId.SCHEME_GERRIT,
+ login))));
+ return accountId;
} else {
throw new IOException("Cannot import GitHub account " + login
+ ": HTTP Status " + accountResponse.statusCode());
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GuiceHttpModule.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GuiceHttpModule.java
index cc00a73..38ae784 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GuiceHttpModule.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GuiceHttpModule.java
@@ -19,7 +19,6 @@
import com.google.inject.servlet.ServletModule;
import com.googlesource.gerrit.plugins.github.filters.GitHubOAuthFilter;
import com.googlesource.gerrit.plugins.github.oauth.GitHubHttpProvider;
-import com.googlesource.gerrit.plugins.github.pullsync.PullRequestsServlet;
import com.googlesource.gerrit.plugins.github.replication.RemoteSiteUser;
import com.googlesource.gerrit.plugins.github.velocity.VelocityStaticServlet;
import com.googlesource.gerrit.plugins.github.velocity.VelocityViewServlet;
@@ -33,7 +32,6 @@
install(new FactoryModuleBuilder().build(RemoteSiteUser.Factory.class));
install(new GuiceModule());
- serve("/").with(PullRequestsServlet.class);
serve("*.css","*.js","*.png","*.jpg","*.woff","*.gif","*.ttf").with(VelocityStaticServlet.class);
serve("*.html").with(VelocityViewServlet.class);
serve("*.gh").with(VelocityControllerServlet.class);
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/pullsync/PullRequestsServlet.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/pullsync/PullRequestsServlet.java
deleted file mode 100644
index eb6f4f9..0000000
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/pullsync/PullRequestsServlet.java
+++ /dev/null
@@ -1,115 +0,0 @@
-// 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.googlesource.gerrit.plugins.github.pullsync;
-
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.List;
-
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.velocity.Template;
-import org.apache.velocity.VelocityContext;
-import org.apache.velocity.exception.ParseErrorException;
-import org.apache.velocity.exception.ResourceNotFoundException;
-import org.apache.velocity.runtime.RuntimeInstance;
-import org.kohsuke.github.GHCommitPointer;
-import org.kohsuke.github.GHIssueState;
-import org.kohsuke.github.GHOrganization;
-import org.kohsuke.github.GHPullRequest;
-import org.kohsuke.github.GHRepository;
-import org.kohsuke.github.GitHub;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.gerrit.reviewdb.client.Project.NameKey;
-import com.google.gerrit.server.project.ProjectCache;
-import com.google.gerrit.server.project.ProjectState;
-import com.google.inject.Inject;
-import com.google.inject.Provider;
-import com.google.inject.Singleton;
-import com.google.inject.name.Named;
-import com.googlesource.gerrit.plugins.github.oauth.GitHubLogin;
-import com.googlesource.gerrit.plugins.github.replication.GitHubDestinations;
-
-@Singleton
-public class PullRequestsServlet extends HttpServlet {
- private static final long serialVersionUID = 3635343057427548273L;
- private static final Logger log = LoggerFactory
- .getLogger(PullRequestsServlet.class);
- private Provider<GitHubLogin> loginProvider;
- private GitHubDestinations destinations;
- private ProjectCache projects;
- private RuntimeInstance velocityRuntime;
-
- @Inject
- public PullRequestsServlet(Provider<GitHubLogin> loginProvider,
- GitHubDestinations destinations, ProjectCache projects,
- @Named("PluginRuntimeInstance") RuntimeInstance velocityRuntime) {
- this.loginProvider = loginProvider;
- this.destinations = destinations;
- this.projects = projects;
- this.velocityRuntime = velocityRuntime;
- }
-
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
-
- PrintWriter out = null;
- try {
- GitHub hub = loginProvider.get().hub;
- out = resp.getWriter();
-
- for (String orgName : destinations.getOrganisations()) {
- GHOrganization org = hub.getOrganization(orgName);
- for (GHRepository repo : org.getRepositories().values()) {
- String repoName = repo.getName();
- ProjectState project = projects.get(new NameKey(repoName));
- if (project == null) {
- log.debug("GitHub repo " + orgName + "/" + repoName
- + " does not have a correspondant Gerrit Project: skipped");
- continue;
- }
-
- List<GHPullRequest> pullRequests =
- repo.getPullRequests(GHIssueState.OPEN);
- for (GHPullRequest pullRequest : pullRequests) {
-
- GHCommitPointer pullHead = pullRequest.getHead();
- GHCommitPointer pullBase = pullRequest.getBase();
- }
-
- }
- org.getRepositories();
- };
- } catch (ResourceNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ParseErrorException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- if (out != null) {
- out.close();
- }
- }
- }
-}