Use new Gerrit 2.9-SNAPSHOT patch-set inserter

The new Gerrit master patch-set inserter relies
on the ChangeControl to get the current user for 
being used as patch-set submitter.

This is typically correct but in case of GitHub
the patch is created "on behalf" of the pull request
on GitHub: we need to then simulate this delegation
of responsibilities as we want Gerrit to notify the
original Pull Request user and NOT the "robot" used
for creating the patches in Gerrit.

Change-Id: Ibbafeb680e007d635944dba48924fbb840dbfb4a
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
new file mode 100644
index 0000000..6c11593
--- /dev/null
+++ b/github-plugin/src/main/java/com/google/gerrit/server/project/ChangeControlDelegate.java
@@ -0,0 +1,190 @@
+// 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);
+
+    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/googlesrouce/gerrit/plugins/github/git/PullRequestCreateChange.java b/github-plugin/src/main/java/com/googlesrouce/gerrit/plugins/github/git/PullRequestCreateChange.java
index c73bdc4..0d21ea8 100644
--- a/github-plugin/src/main/java/com/googlesrouce/gerrit/plugins/github/git/PullRequestCreateChange.java
+++ b/github-plugin/src/main/java/com/googlesrouce/gerrit/plugins/github/git/PullRequestCreateChange.java
@@ -15,14 +15,12 @@
 package com.googlesrouce.gerrit.plugins.github.git;
 
 import java.io.IOException;
-import java.sql.Timestamp;
 import java.util.Iterator;
 import java.util.List;
 
 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
 import org.eclipse.jgit.errors.MissingObjectException;
 import org.eclipse.jgit.lib.ObjectId;
-import org.eclipse.jgit.lib.PersonIdent;
 import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.RefUpdate;
 import org.eclipse.jgit.lib.Repository;
@@ -35,17 +33,16 @@
 import org.slf4j.LoggerFactory;
 
 import com.google.gerrit.common.errors.EmailException;
+import com.google.gerrit.reviewdb.client.Account;
 import com.google.gerrit.reviewdb.client.Branch;
 import com.google.gerrit.reviewdb.client.Change;
 import com.google.gerrit.reviewdb.client.Change.Id;
-import com.google.gerrit.reviewdb.client.Account;
 import com.google.gerrit.reviewdb.client.ChangeMessage;
 import com.google.gerrit.reviewdb.client.PatchSet;
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.gerrit.reviewdb.client.RevId;
 import com.google.gerrit.reviewdb.server.ReviewDb;
 import com.google.gerrit.server.ChangeUtil;
-import com.google.gerrit.server.GerritPersonIdent;
 import com.google.gerrit.server.IdentifiedUser;
 import com.google.gerrit.server.IdentifiedUser.GenericFactory;
 import com.google.gerrit.server.change.ChangeInserter;
@@ -56,6 +53,8 @@
 import com.google.gerrit.server.git.MergeUtil;
 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;
@@ -67,7 +66,6 @@
 import com.google.gwtorm.server.OrmException;
 import com.google.gwtorm.server.ResultSet;
 import com.google.inject.Inject;
-import com.google.inject.Provider;
 
 public class PullRequestCreateChange {
   private static final Logger LOG = LoggerFactory
@@ -168,8 +166,13 @@
           // The change key exists on the destination branch: adding a new
           // patch-set
           Change destChange = destChanges.get(0);
+
+          ChangeControl changeControl =
+              projectControlFactor.controlFor(project.getNameKey()).controlFor(
+                  destChange);
+
           return insertPatchSet(git, revWalk, destChange, pullRequestCommit,
-              refControl, pullRequestOwner, pullRequestMesage, doValidation);
+              changeControl, pullRequestOwner, pullRequestMesage, doValidation);
         } else {
           // Change key not found on destination branch. We can create a new
           // change.
@@ -191,13 +194,13 @@
   }
 
   private Change.Id insertPatchSet(Repository git, RevWalk revWalk,
-      Change change, RevCommit cherryPickCommit, RefControl refControl,
+      Change change, RevCommit cherryPickCommit, ChangeControl changeControl,
       Account.Id pullRequestOwnerId, String pullRequestMessage,
       boolean doValidation) throws InvalidChangeOperationException,
       IOException, OrmException, NoSuchChangeException {
     PatchSetInserter patchSetInserter =
-        patchSetInserterFactory.create(git, revWalk, refControl,
-            userFactory.create(pullRequestOwnerId), change, cherryPickCommit);
+        patchSetInserterFactory.create(git, revWalk, ChangeControlDelegate.wrap(changeControl,
+            userFactory.create(pullRequestOwnerId)), cherryPickCommit);
     // This apparently useless method call is made for triggering
     // the creation of patchSet inside PatchSetInserter and thus avoiding a NPE
     patchSetInserter.getPatchSetId();