Merge "Remove unused imports in PullRequestImportJob"
diff --git a/github-plugin/src/main/java/com/google/gerrit/server/project/ChangeControlDelegate.java b/github-plugin/src/main/java/com/google/gerrit/server/project/ChangeControlDelegate.java
deleted file mode 100644
index 7ccaa69..0000000
--- a/github-plugin/src/main/java/com/google/gerrit/server/project/ChangeControlDelegate.java
+++ /dev/null
@@ -1,189 +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.google.gerrit.server.project;
-
-import java.util.List;
-
-import com.google.gerrit.common.data.LabelTypes;
-import com.google.gerrit.common.data.PermissionRange;
-import com.google.gerrit.common.data.SubmitRecord;
-import com.google.gerrit.common.data.SubmitTypeRecord;
-import com.google.gerrit.reviewdb.client.Account.Id;
-import com.google.gerrit.reviewdb.client.Change;
-import com.google.gerrit.reviewdb.client.PatchSet;
-import com.google.gerrit.reviewdb.client.PatchSetApproval;
-import com.google.gerrit.reviewdb.client.Project;
-import com.google.gerrit.reviewdb.server.ReviewDb;
-import com.google.gerrit.server.CurrentUser;
-import com.google.gerrit.server.query.change.ChangeData;
-import com.google.gwtorm.server.OrmException;
-import com.googlecode.prolog_cafe.lang.Term;
-
-public class ChangeControlDelegate extends ChangeControl {
-
-  private final ChangeControl realChangeControl;
-  private final CurrentUser delegatedUser;
-
-  private ChangeControlDelegate(ChangeControl realChangeControl, CurrentUser delegatedUser) {
-    // All methods are delegated, super class constructor needs to be invoked anyway with dummy values
-    super(null, null, null, null);
-    this.realChangeControl = realChangeControl;
-    this.delegatedUser = delegatedUser;
-  }
-
-  public static ChangeControl wrap(ChangeControl realChangeControl, CurrentUser delegatedUser) {
-    return new ChangeControlDelegate(realChangeControl, delegatedUser);
-  }
-
-  public int hashCode() {
-    return realChangeControl.hashCode();
-  }
-
-  public boolean equals(Object obj) {
-    return realChangeControl.equals(obj);
-  }
-
-  public ChangeControl forUser(CurrentUser who) {
-    return realChangeControl.forUser(who);
-  }
-
-  public RefControl getRefControl() {
-    return realChangeControl.getRefControl();
-  }
-
-  public CurrentUser getCurrentUser() {
-    return delegatedUser;
-  }
-
-  public ProjectControl getProjectControl() {
-    return realChangeControl.getProjectControl();
-  }
-
-  public Project getProject() {
-    return realChangeControl.getProject();
-  }
-
-  public Change getChange() {
-    return realChangeControl.getChange();
-  }
-
-  public boolean isVisible(ReviewDb db) throws OrmException {
-    return realChangeControl.isVisible(db);
-  }
-
-  public boolean isRefVisible() {
-    return realChangeControl.isRefVisible();
-  }
-
-  public boolean isPatchVisible(PatchSet ps, ReviewDb db) throws OrmException {
-    return realChangeControl.isPatchVisible(ps, db);
-  }
-
-  public boolean canAbandon() {
-    return realChangeControl.canAbandon();
-  }
-
-  public boolean canPublish(ReviewDb db) throws OrmException {
-    return realChangeControl.canPublish(db);
-  }
-
-  public boolean canDeleteDraft(ReviewDb db) throws OrmException {
-    return realChangeControl.canDeleteDraft(db);
-  }
-
-  public boolean canRebase() {
-    return realChangeControl.canRebase();
-  }
-
-  public boolean canRestore() {
-    return realChangeControl.canRestore();
-  }
-
-  public LabelTypes getLabelTypes() {
-    return realChangeControl.getLabelTypes();
-  }
-
-  public String toString() {
-    return realChangeControl.toString();
-  }
-
-  public List<PermissionRange> getLabelRanges() {
-    return realChangeControl.getLabelRanges();
-  }
-
-  public PermissionRange getRange(String permission) {
-    return realChangeControl.getRange(permission);
-  }
-
-  public boolean canAddPatchSet() {
-    return realChangeControl.canAddPatchSet();
-  }
-
-  public boolean isOwner() {
-    return realChangeControl.isOwner();
-  }
-
-  public boolean isReviewer(ReviewDb db) throws OrmException {
-    return realChangeControl.isReviewer(db);
-  }
-
-  public boolean isReviewer(ReviewDb db, ChangeData cd) throws OrmException {
-    return realChangeControl.isReviewer(db, cd);
-  }
-
-  public boolean canRemoveReviewer(PatchSetApproval approval) {
-    return realChangeControl.canRemoveReviewer(approval);
-  }
-
-  public boolean canRemoveReviewer(Id reviewer, int value) {
-    return realChangeControl.canRemoveReviewer(reviewer, value);
-  }
-
-  public boolean canEditTopicName() {
-    return realChangeControl.canEditTopicName();
-  }
-
-  public List<SubmitRecord> getSubmitRecords(ReviewDb db, PatchSet patchSet) {
-    return realChangeControl.getSubmitRecords(db, patchSet);
-  }
-
-  public boolean canSubmit() {
-    return realChangeControl.canSubmit();
-  }
-
-  public List<SubmitRecord> canSubmit(ReviewDb db, PatchSet patchSet) {
-    return realChangeControl.canSubmit(db, patchSet);
-  }
-
-  public List<SubmitRecord> canSubmit(ReviewDb db, PatchSet patchSet,
-      ChangeData cd, boolean fastEvalLabels, boolean allowClosed,
-      boolean allowDraft) {
-    return realChangeControl.canSubmit(db, patchSet, cd, fastEvalLabels,
-        allowClosed, allowDraft);
-  }
-
-  public List<SubmitRecord> resultsToSubmitRecord(Term submitRule,
-      List<Term> results) {
-    return realChangeControl.resultsToSubmitRecord(submitRule, results);
-  }
-
-  public SubmitTypeRecord getSubmitTypeRecord(ReviewDb db, PatchSet patchSet) {
-    return realChangeControl.getSubmitTypeRecord(db, patchSet);
-  }
-
-  public SubmitTypeRecord getSubmitTypeRecord(ReviewDb db, PatchSet patchSet,
-      ChangeData cd) {
-    return realChangeControl.getSubmitTypeRecord(db, patchSet, cd);
-  }
-}
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
index e04c3d6..5d3b929 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestCreateChange.java
@@ -53,7 +53,6 @@
 import com.google.gerrit.server.git.validators.CommitValidationException;
 import com.google.gerrit.server.git.validators.CommitValidators;
 import com.google.gerrit.server.project.ChangeControl;
-import com.google.gerrit.server.project.ChangeControlDelegate;
 import com.google.gerrit.server.project.InvalidChangeOperationException;
 import com.google.gerrit.server.project.NoSuchChangeException;
 import com.google.gerrit.server.project.NoSuchProjectException;
@@ -164,7 +163,7 @@
 
           ChangeControl changeControl =
               projectControlFactory.controlFor(project.getNameKey()).controlFor(
-                  destChange);
+                  destChange).forUser(userFactory.create(pullRequestOwner));
 
           return insertPatchSet(git, revWalk, destChange, pullRequestCommit,
               changeControl, pullRequestOwner, pullRequestMesage, doValidation);
@@ -194,8 +193,7 @@
       boolean doValidation) throws InvalidChangeOperationException,
       IOException, OrmException, NoSuchChangeException {
     PatchSetInserter patchSetInserter =
-        patchSetInserterFactory.create(git, revWalk, ChangeControlDelegate.wrap(changeControl,
-            userFactory.create(pullRequestOwnerId)), cherryPickCommit);
+        patchSetInserterFactory.create(git, revWalk, changeControl, cherryPickCommit);
     // This apparently useless method call is made for triggering
     // the creation of patchSet inside PatchSetInserter and thus avoiding a NPE
     patchSetInserter.getPatchSetId();
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
index 5ec2d4d..8fe0468 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
@@ -151,10 +151,10 @@
       } catch (OrmException e1) {
         LOG.error("Error rolling back transation", e1);
       }
-    } catch (Exception e) {
+    } catch (Throwable e) {
       LOG.error("Pull request " + prId + " into repository " + organisation
           + "/" + repoName + " was failed", e);
-      status.update(GitJobStatus.Code.FAILED, "Failed", getErrorDescription(e));
+      status.update(GitJobStatus.Code.FAILED, "Failed",  e.getLocalizedMessage());
       try {
         db.rollback();
       } catch (OrmException e1) {
@@ -165,10 +165,6 @@
     }
   }
 
-  private String getErrorDescription(Exception e) {
-    return e.getLocalizedMessage();
-  }
-
   private List<Id> addPullRequestToChange(ReviewDb db, GHPullRequest pr,
       Repository gitRepo) throws Exception {
     String destinationBranch = pr.getBase().getRef();
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/AccountController.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/AccountController.java
index 3d87a6c..4235308 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/AccountController.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/AccountController.java
@@ -36,6 +36,7 @@
 import com.google.common.collect.Sets;
 import com.google.gerrit.extensions.restapi.RawInput;
 import com.google.gerrit.reviewdb.client.Account;
+import com.google.gerrit.reviewdb.client.Account.Id;
 import com.google.gerrit.reviewdb.server.ReviewDb;
 import com.google.gerrit.server.IdentifiedUser;
 import com.google.gerrit.server.account.AccountCache;
@@ -44,6 +45,7 @@
 import com.google.gerrit.server.account.AccountResource;
 import com.google.gerrit.server.account.AddSshKey;
 import com.google.gerrit.server.account.AuthRequest;
+import com.google.gerrit.server.account.AuthResult;
 import com.google.gerrit.server.account.GetSshKeys;
 import com.google.gerrit.server.account.GetSshKeys.SshKeyInfo;
 import com.google.gwtorm.server.OrmException;
@@ -90,12 +92,22 @@
     String fullName = req.getParameter("fullname");
     String email = req.getParameter("email");
     try {
-      accountManager.link(user.getAccountId(), AuthRequest.forEmail(email));
-      Account a = dbProvider.get().accounts().get(user.getAccountId());
+      Id accountId = user.getAccountId();
+      AuthResult result =
+          accountManager.link(accountId, AuthRequest.forEmail(email));
+      log.debug("Account {} linked to email {}: result = {}", accountId, email,
+          result);
+
+      Account a = dbProvider.get().accounts().get(accountId);
       a.setPreferredEmail(email);
       a.setFullName(fullName);
       dbProvider.get().accounts().update(Collections.singleton(a));
-      accountCache.evict(user.getAccountId());
+      log.debug(
+          "Account {} updated with preferredEmail = {} and fullName = {}",
+          accountId, email, fullName);
+
+      accountCache.evict(accountId);
+      log.debug("Account cache evicted for {}", accountId);
     } catch (AccountException | OrmException e) {
       throw new ServletException("Cannot associate email '" + email
           + "' to current user '" + user + "'", e);