Merge branch 'stable-3.6' into stable-3.7

* stable-3.6:
  PullRequestCreateChange: Remove unused import
  Set plugin version to 3.4.7
  Fire git ref update events for all imported refs
  PluginVelocityRuntimeProvider: Fix warning flagged by error prone
  Fix default scopes resolution

Change-Id: Ia574c521bbe3a65a882b766dc39920acd836df6b
diff --git a/github-oauth/src/main/java/com/googlesource/gerrit/plugins/github/oauth/GitHubOAuthConfig.java b/github-oauth/src/main/java/com/googlesource/gerrit/plugins/github/oauth/GitHubOAuthConfig.java
index 7ef81d1..151f636 100644
--- a/github-oauth/src/main/java/com/googlesource/gerrit/plugins/github/oauth/GitHubOAuthConfig.java
+++ b/github-oauth/src/main/java/com/googlesource/gerrit/plugins/github/oauth/GitHubOAuthConfig.java
@@ -56,6 +56,7 @@
   public static final String GERRIT_LOGIN = "/login";
   public static final String GERRIT_LOGOUT = "/logout";
   public static final String GITHUB_PLUGIN_OAUTH_SCOPE = "/plugins/github-plugin/static/scope.html";
+  public static final ScopeKey GITHUB_DEFAULT_SCOPES_KEY = new ScopeKey("scopes", null, 0);
 
   public final String gitHubUrl;
   public final String gitHubApiUrl;
@@ -193,10 +194,10 @@
   }
 
   public Scope[] getDefaultScopes() {
-    if (scopes == null || scopes.get("scopes") == null) {
+    if (scopes == null || scopes.get(GITHUB_DEFAULT_SCOPES_KEY) == null) {
       return new Scope[0];
     }
-    return scopes.get("scopes").toArray(new Scope[0]);
+    return scopes.get(GITHUB_DEFAULT_SCOPES_KEY).toArray(new Scope[0]);
   }
 
   public KeyConfig getCurrentKeyConfig() {
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/GitCloneStep.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/GitCloneStep.java
index 0e0e72d..12e5101 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/GitCloneStep.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/GitCloneStep.java
@@ -21,11 +21,15 @@
 import com.google.gerrit.extensions.registration.DynamicSet;
 import com.google.gerrit.extensions.restapi.ResourceConflictException;
 import com.google.gerrit.extensions.restapi.RestApiException;
+import com.google.gerrit.server.IdentifiedUser;
+import com.google.gerrit.server.account.AccountState;
+import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
 import com.google.gerrit.server.git.GitRepositoryManager;
 import com.google.gerrit.server.project.ProjectCache;
 import com.google.gerrit.server.util.ManualRequestContext;
 import com.google.gerrit.server.util.OneOffRequestContext;
 import com.google.inject.Inject;
+import com.google.inject.Provider;
 import com.google.inject.assistedinject.Assisted;
 import com.googlesource.gerrit.plugins.github.GitHubConfig;
 import java.io.File;
@@ -34,7 +38,9 @@
 import org.eclipse.jgit.api.FetchCommand;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.ProgressMonitor;
+import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.lib.RepositoryCache;
 import org.slf4j.Logger;
@@ -54,6 +60,8 @@
   private final ProjectCache projectCache;
   private final GitRepositoryManager repoManager;
   private final String projectName;
+  private final GitReferenceUpdated referenceUpdated;
+  private final Provider<IdentifiedUser> identifiedUser;
 
   public interface Factory {
     GitCloneStep create(
@@ -69,6 +77,8 @@
       DynamicSet<ProjectDeletedListener> deletedListeners,
       ProjectCache projectCache,
       GitRepositoryManager repoManager,
+      GitReferenceUpdated referenceUpdated,
+      Provider<IdentifiedUser> identifiedUser,
       @Assisted("organisation") String organisation,
       @Assisted("name") String repository)
       throws GitException {
@@ -86,6 +96,8 @@
     this.deletedListeners = deletedListeners;
     this.projectCache = projectCache;
     this.repoManager = repoManager;
+    this.referenceUpdated = referenceUpdated;
+    this.identifiedUser = identifiedUser;
   }
 
   private static File prepareTargetGitDirectory(File gitDir, String projectName)
@@ -113,6 +125,7 @@
   @Override
   public void doImport(ProgressMonitor progress) throws GitException {
     createNewProject();
+    Project.NameKey key = Project.nameKey(projectName);
     String sourceUri = getSourceUri();
     try (Git git = Git.open(destinationDirectory)) {
       FetchCommand fetch = git.fetch().setRefSpecs("refs/*:refs/*").setRemote(sourceUri);
@@ -122,6 +135,11 @@
       }
       LOG.info(sourceUri + "| Clone into " + destinationDirectory);
       fetch.call();
+      AccountState accountState = identifiedUser.get().state();
+      for (Ref ref : git.getRepository().getRefDatabase().getRefs()) {
+        referenceUpdated.fire(
+            key, ref.getName(), ObjectId.zeroId(), ref.getObjectId(), accountState);
+      }
     } catch (IOException | GitAPIException e) {
       LOG.error("Unable to fetch from {} into {}", sourceUri, destinationDirectory, e);
       throw new GitCloneFailedException(sourceUri, e);
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 3d0ec26..f2a7f06 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
@@ -39,7 +39,6 @@
 import com.google.gerrit.server.query.change.InternalChangeQuery;
 import com.google.gerrit.server.update.BatchUpdate;
 import com.google.gerrit.server.update.UpdateException;
-import com.google.gerrit.server.util.time.TimeUtil;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 import java.io.IOException;
@@ -101,9 +100,7 @@
           RestApiException {
     try (BatchUpdate bu =
         updateFactory.create(
-            project.getNameKey(),
-            userFactory.create(pullRequestOwner),
-            Instant.now())) {
+            project.getNameKey(), userFactory.create(pullRequestOwner), Instant.now())) {
 
       return internalAddCommitToChange(
           bu,
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/velocity/PluginVelocityRuntimeProvider.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/velocity/PluginVelocityRuntimeProvider.java
index e1b4d9b..144e77a 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/velocity/PluginVelocityRuntimeProvider.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/velocity/PluginVelocityRuntimeProvider.java
@@ -21,6 +21,7 @@
 import com.google.inject.Singleton;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.util.Arrays;
 import java.util.Properties;
 import org.apache.velocity.runtime.RuntimeConstants;
 import org.apache.velocity.runtime.RuntimeInstance;
@@ -90,7 +91,7 @@
         "Cannot find any Jar file in "
             + pluginName
             + " plugin class loader URLs "
-            + jarUrls
+            + Arrays.toString(jarUrls)
             + ": unable to initialize Velocity resource loading.");
   }
 }