Merge branch 'stable-2.11'

Change-Id: I5b4404e20a7159f519f705af57436a9c0682df50
diff --git a/github-oauth/pom.xml b/github-oauth/pom.xml
index b4e9a2b..fe43a82 100644
--- a/github-oauth/pom.xml
+++ b/github-oauth/pom.xml
@@ -21,7 +21,7 @@
   <parent>
     <groupId>com.googlesource.gerrit.plugins.github</groupId>
     <artifactId>github-parent</artifactId>
-    <version>2.11</version>
+    <version>2.12-SNAPSHOT</version>
   </parent>
   <artifactId>github-oauth</artifactId>
   <name>Gerrit Code Review - GitHub OAuth login</name>
diff --git a/github-oauth/src/main/java/com/googlesource/gerrit/plugins/github/oauth/OAuthWebFilter.java b/github-oauth/src/main/java/com/googlesource/gerrit/plugins/github/oauth/OAuthWebFilter.java
index 6929d16..6291481 100644
--- a/github-oauth/src/main/java/com/googlesource/gerrit/plugins/github/oauth/OAuthWebFilter.java
+++ b/github-oauth/src/main/java/com/googlesource/gerrit/plugins/github/oauth/OAuthWebFilter.java
@@ -26,8 +26,8 @@
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
-import java.text.SimpleDateFormat;
-import java.util.Date;
+import java.nio.file.Files;
+import java.nio.file.attribute.FileTime;
 import java.util.Random;
 import java.util.Set;
 
@@ -183,8 +183,8 @@
       String user, String access_token) throws IOException,
       ConfigInvalidException {
     FileBasedConfig currentSecureConfig =
-        new FileBasedConfig(sites.secure_config, FS.DETECTED);
-    long currentSecureConfigUpdateTs = sites.secure_config.lastModified();
+        new FileBasedConfig(sites.secure_config.toFile(), FS.DETECTED);
+    FileTime currentSecureConfigUpdateTs = Files.getLastModifiedTime(sites.secure_config);
     currentSecureConfig.load();
 
     boolean configUpdate =
@@ -202,19 +202,17 @@
     log.info("Updating " + sites.secure_config + " credentials for user "
         + user);
 
-    if (sites.secure_config.lastModified() != currentSecureConfigUpdateTs) {
+    FileTime secureConfigCurrentModifiedTime =
+        Files.getLastModifiedTime(sites.secure_config);
+    if (!secureConfigCurrentModifiedTime.equals(currentSecureConfigUpdateTs)) {
       throw new ConcurrentFileBasedConfigWriteException("File "
           + sites.secure_config + " was written at "
-          + formatTS(sites.secure_config.lastModified())
+          + secureConfigCurrentModifiedTime
           + " while was trying to update security for user " + user);
     }
     currentSecureConfig.save();
   }
 
-  private String formatTS(long ts) {
-    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(ts));
-  }
-
   private boolean updateConfigSection(FileBasedConfig config, String section,
       String user, String password) {
     String configUser = config.getString("remote", section, "username");
diff --git a/github-plugin/pom.xml b/github-plugin/pom.xml
index e8a1daa..adb635b 100644
--- a/github-plugin/pom.xml
+++ b/github-plugin/pom.xml
@@ -20,7 +20,7 @@
   <parent>
     <artifactId>github-parent</artifactId>
     <groupId>com.googlesource.gerrit.plugins.github</groupId>
-    <version>2.11</version>
+    <version>2.12-SNAPSHOT</version>
   </parent>
 
   <artifactId>github-plugin</artifactId>
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GitHubConfig.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GitHubConfig.java
index 17c3b9e..44a4b4e 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GitHubConfig.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GitHubConfig.java
@@ -26,8 +26,8 @@
 
 import org.eclipse.jgit.lib.Config;
 
-import java.io.File;
 import java.net.MalformedURLException;
+import java.nio.file.Path;
 import java.util.HashMap;
 
 @Singleton
@@ -48,7 +48,7 @@
   private static final String CONF_PUBLIC_BASE_PROJECT = "publicBaseProject";
   private static final String CONF_PRIVATE_BASE_PROJECT = "privateBaseProject";
 
-  public final File gitDir;
+  public final Path gitDir;
   public final int jobPoolLimit;
   public final int jobExecTimeout;
   public final int pullRequestListLimit;
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/InitGitHub.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/InitGitHub.java
index 907f73d..5a78252 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/InitGitHub.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/InitGitHub.java
@@ -15,6 +15,7 @@
 
 import java.net.URISyntaxException;
 
+import com.google.common.base.Strings;
 import com.google.gerrit.pgm.init.api.ConsoleUI;
 import com.google.gerrit.pgm.init.api.InitStep;
 import com.google.gerrit.pgm.init.api.InitUtil;
@@ -79,16 +80,22 @@
       auth.string("HTTP Authentication Header", "httpHeader", "GITHUB_USER");
       httpd.set("filterClass",
           "com.googlesource.gerrit.plugins.github.oauth.OAuthFilter");
-      auth.set("httpExternalIdHeader", "GITHUB_OAUTH_TOKEN");
-      auth.set("loginUrl","/login");
-      auth.set("loginText", "Sign-in with GitHub");
-      auth.set("registerPageUrl", "/#/register");
+      authSetDefault("httpExternalIdHeader", "GITHUB_OAUTH_TOKEN");
+      authSetDefault("loginUrl","/login");
+      authSetDefault("loginText", "Sign-in with GitHub");
+      authSetDefault("registerPageUrl", "/#/register");
     } else {
       httpd.unset("filterClass");
       httpd.unset("httpHeader");
     }
   }
 
+  private void authSetDefault(String key, String defValue) {
+    if (Strings.isNullOrEmpty(auth.get(key))) {
+      auth.set(key, defValue);
+    }
+  }
+
   private String getAssumedCanonicalWebUrl() {
     String url = gerrit.get("canonicalWebUrl");
     if (url != null) {
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 4b6cfb7..1ca982d 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
@@ -53,7 +53,7 @@
       GitDestinationNotWritableException {
     super(gitConfig.gitHubUrl, organisation, repository, gitHubRepoFactory);
     LOG.debug("GitHub Clone " + organisation + "/" + repository);
-    this.gitDir = gitConfig.gitDir;
+    this.gitDir = gitConfig.gitDir.toFile();
     this.destinationDirectory =
         getDestinationDirectory(organisation, repository);
   }
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 88a32fd..169f92a 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
@@ -181,7 +181,7 @@
                   doValidation));
         }
       } finally {
-        revWalk.release();
+        revWalk.close();
         if (newChange == null) {
           db.rollback();
         }
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 b13cc13..9834c2c 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
@@ -173,34 +173,35 @@
     ObjectId baseObjectId = ObjectId.fromString(pr.getBase().getSha());
     ObjectId prHeadObjectId = ObjectId.fromString(pr.getHead().getSha());
 
-    RevWalk walk = new RevWalk(gitRepo);
-    walk.markUninteresting(walk.lookupCommit(baseObjectId));
-    walk.markStart(walk.lookupCommit(prHeadObjectId));
-    walk.sort(RevSort.REVERSE);
+    try (RevWalk walk = new RevWalk(gitRepo)) {
+      walk.markUninteresting(walk.lookupCommit(baseObjectId));
+      walk.markStart(walk.lookupCommit(prHeadObjectId));
+      walk.sort(RevSort.REVERSE);
 
-    int patchNr = 1;
-    for (GHPullRequestCommitDetail ghCommitDetail : pr.listCommits()) {
-      status.update(Code.SYNC, "Patch #" + patchNr, "Patch#" + patchNr
-          + ": Inserting PullRequest into Gerrit");
-      RevCommit revCommit =
-          walk.parseCommit(ObjectId.fromString(ghCommitDetail.getSha()));
+      int patchNr = 1;
+      for (GHPullRequestCommitDetail ghCommitDetail : pr.listCommits()) {
+        status.update(Code.SYNC, "Patch #" + patchNr, "Patch#" + patchNr
+            + ": Inserting PullRequest into Gerrit");
+        RevCommit revCommit =
+            walk.parseCommit(ObjectId.fromString(ghCommitDetail.getSha()));
 
-      GHUser prUser = pr.getUser();
-      GitUser commitAuthor = ghCommitDetail.getCommit().getAuthor();
-      GitHubUser gitHubUser = GitHubUser.from(prUser, commitAuthor);
+        GHUser prUser = pr.getUser();
+        GitUser commitAuthor = ghCommitDetail.getCommit().getAuthor();
+        GitHubUser gitHubUser = GitHubUser.from(prUser, commitAuthor);
 
-      Account.Id pullRequestOwner = getOrRegisterAccount(db, gitHubUser);
-      Id changeId =
-          createChange.addCommitToChange(db, project, gitRepo,
-              destinationBranch, pullRequestOwner, revCommit,
-              getChangeMessage(pr),
-              String.format(TOPIC_FORMAT, pr.getNumber()), false);
-      if (changeId != null) {
-        prChanges.add(changeId);
+        Account.Id pullRequestOwner = getOrRegisterAccount(db, gitHubUser);
+        Id changeId =
+            createChange.addCommitToChange(db, project, gitRepo,
+                destinationBranch, pullRequestOwner, revCommit,
+                getChangeMessage(pr),
+                String.format(TOPIC_FORMAT, pr.getNumber()), false);
+        if (changeId != null) {
+          prChanges.add(changeId);
+        }
       }
-    }
 
-    return prChanges;
+      return prChanges;
+    }
   }
 
   private com.google.gerrit.reviewdb.client.Account.Id getOrRegisterAccount(
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/ReplicationConfig.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/ReplicationConfig.java
index 1784e0e..60e8502 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/ReplicationConfig.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/ReplicationConfig.java
@@ -35,9 +35,9 @@
   @Inject
   public ReplicationConfig(final SitePaths site) {
     replicationConf =
-        new FileBasedConfig(new File(site.etc_dir, "replication.config"),
+        new FileBasedConfig(new File(site.etc_dir.toFile(), "replication.config"),
             FS.DETECTED);
-    secureConf = new FileBasedConfig(site.secure_config, FS.DETECTED);
+    secureConf = new FileBasedConfig(site.secure_config.toFile(), FS.DETECTED);
   }
 
   public synchronized void addSecureCredentials(String organisation,
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/replication/GitHubDestinations.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/replication/GitHubDestinations.java
index e383fe1..ab8fa24 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/replication/GitHubDestinations.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/replication/GitHubDestinations.java
@@ -34,9 +34,10 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -79,7 +80,7 @@
     replicationUserFactory = ruf;
     gitRepositoryManager = grm;
     groupBackend = gb;
-    configs = getDestinations(new File(site.etc_dir, "replication.config"));
+    configs = getDestinations(site.etc_dir.resolve("replication.config"));
     organisations = getOrganisations(configs);
   }
 
@@ -94,13 +95,13 @@
     return organisations;
   }
 
-  private List<Destination> getDestinations(File cfgPath)
+  private List<Destination> getDestinations(Path cfgPath)
       throws ConfigInvalidException, IOException {
-    FileBasedConfig cfg = new FileBasedConfig(cfgPath, FS.DETECTED);
-    if (!cfg.getFile().exists() || cfg.getFile().length() == 0) {
+    if (!Files.exists(cfgPath) || Files.size(cfgPath) == 0) {
       return Collections.emptyList();
-    }
-
+    }    
+    
+    FileBasedConfig cfg = new FileBasedConfig(cfgPath.toFile(), FS.DETECTED);
     try {
       cfg.load();
     } catch (ConfigInvalidException e) {
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 57b4780..431a438 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
@@ -26,7 +26,6 @@
 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
 import org.apache.velocity.runtime.resource.loader.JarResourceLoader;
 
-import java.io.File;
 import java.util.Properties;
 
 @Singleton
@@ -65,13 +64,14 @@
     p.setProperty(VELOCITY_FILE_RESOURCE_LOADER_CLASS, pkg
         + ".FileResourceLoader");
     p.setProperty(VELOCITY_FILE_RESOURCE_LOADER_PATH,
-        new File(site.static_dir.getAbsolutePath(), "..").getAbsolutePath());
+        site.static_dir.getParent().toAbsolutePath().toString());
     p.setProperty(VELOCITY_CLASS_RESOURCE_LOADER_CLASS,
         ClasspathResourceLoader.class.getName());
     p.setProperty(VELOCITY_JAR_RESOURCE_LOADER_CLASS,
         JarResourceLoader.class.getName());
     p.setProperty(VELOCITY_JAR_RESOURCE_LOADER_PATH, "jar:file:"
-        + new File(site.plugins_dir, pluginName + ".jar").getAbsolutePath());
+        + site.plugins_dir.resolve(pluginName + ".jar").toAbsolutePath()
+            .toString());
 
     RuntimeInstance ri = new RuntimeInstance();
     try {
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/velocity/VelocityViewServlet.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/velocity/VelocityViewServlet.java
index cd1a202..75e3c47 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/velocity/VelocityViewServlet.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/velocity/VelocityViewServlet.java
@@ -19,8 +19,6 @@
 import com.google.inject.Provider;
 import com.google.inject.Singleton;
 import com.google.inject.name.Named;
-
-import com.googlesource.gerrit.plugins.github.GitHubConfig.NextPage;
 import com.googlesource.gerrit.plugins.github.oauth.GitHubLogin;
 import com.googlesource.gerrit.plugins.github.oauth.ScopedProvider;
 
@@ -47,7 +45,7 @@
 public class VelocityViewServlet extends HttpServlet {
   private static final Logger log = LoggerFactory
       .getLogger(VelocityViewServlet.class);
-  private static final String STATIC_PREFIX = "/static";
+  private static final String STATIC_PREFIX = "/static/";
   private static final long serialVersionUID = 529071287765413268L;
   private final RuntimeInstance velocityRuntime;
   private final Provider<PluginVelocityModel> modelProvider;
@@ -73,16 +71,11 @@
     HttpServletRequest req = (HttpServletRequest) request;
     HttpServletResponse resp = (HttpServletResponse) response;
 
-    String servletPath = req.getPathInfo();
-    NextPage nextPage = (NextPage) req.getAttribute("destUrl");
-    String destUrl = null;
-    if (nextPage != null && !nextPage.uri.startsWith("/")) {
-      destUrl =
-          servletPath.substring(0, servletPath.lastIndexOf("/")) + "/"
-              + nextPage.uri;
-    }
-
-    String pathInfo = STATIC_PREFIX + MoreObjects.firstNonNull(destUrl, servletPath);
+    String pathInfo =
+        STATIC_PREFIX
+            + MoreObjects.firstNonNull(
+                (String) req.getAttribute("destUrl"),
+                req.getPathInfo());
 
     try {
       Template template = velocityRuntime.getTemplate(pathInfo, "UTF-8");
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
index c661242..2d34231 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/PullRequestListController.java
@@ -205,30 +205,31 @@
       Repository gitRepo, GHPullRequest ghPullRequest)
       throws IncorrectObjectTypeException, IOException {
     boolean pullRequestToImport = false;
-    RevWalk gitWalk = new RevWalk(gitRepo);
-    for (GHPullRequestCommitDetail pullRequestCommit : ghPullRequest
-        .listCommits()) {
-      ObjectId pullRequestHeadObjectId =
-          ObjectId.fromString(pullRequestCommit.getSha());
+    try (RevWalk gitWalk = new RevWalk(gitRepo)) {
+      for (GHPullRequestCommitDetail pullRequestCommit : ghPullRequest
+          .listCommits()) {
+        ObjectId pullRequestHeadObjectId =
+            ObjectId.fromString(pullRequestCommit.getSha());
 
-      try {
-        gitWalk.parseCommit(pullRequestHeadObjectId);
-
-        ResultSet<PatchSet> patchSets;
         try {
-          patchSets =
-              db.patchSets().byRevision(new RevId(pullRequestCommit.getSha()));
-        } catch (OrmException e) {
-          LOG.error("Error whilst fetching patch-sets from DB associated to commit "
-              + pullRequestCommit.getSha());
-          return false;
+          gitWalk.parseCommit(pullRequestHeadObjectId);
+
+          ResultSet<PatchSet> patchSets;
+          try {
+            patchSets =
+                db.patchSets().byRevision(new RevId(pullRequestCommit.getSha()));
+          } catch (OrmException e) {
+            LOG.error("Error whilst fetching patch-sets from DB associated to commit "
+                + pullRequestCommit.getSha());
+            return false;
+          }
+          pullRequestToImport = !patchSets.iterator().hasNext();
+          patchSets.close();
+        } catch (MissingObjectException e) {
+          pullRequestToImport = true;
         }
-        pullRequestToImport = !patchSets.iterator().hasNext();
-        patchSets.close();
-      } catch (MissingObjectException e) {
-        pullRequestToImport = true;
       }
+      return pullRequestToImport;
     }
-    return pullRequestToImport;
   }
 }
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/VelocityControllerServlet.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/VelocityControllerServlet.java
index 1d7b65e..561fbd0 100644
--- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/VelocityControllerServlet.java
+++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/VelocityControllerServlet.java
@@ -122,7 +122,7 @@
 
   private void redirectToNextStep(HttpServletRequest req,
       HttpServletResponse resp) throws IOException, ServletException {
-    String sourceUri = req.getRequestURI();
+    String sourceUri = req.getPathInfo();
     int pathPos = sourceUri.lastIndexOf('/') + 1;
     String sourcePage = sourceUri.substring(pathPos);
     String sourcePath = sourceUri.substring(0, pathPos);
@@ -137,7 +137,7 @@
       } else {
         RequestDispatcher requestDispatcher =
             req.getRequestDispatcher(nextPageURL(sourcePath, nextPage));
-        req.setAttribute("destUrl", nextPage);
+        req.setAttribute("destUrl", nextPage.uri);
         requestDispatcher.forward(req, resp);
       }
     }
diff --git a/pom.xml b/pom.xml
index 575be55..05bb64c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -18,7 +18,7 @@
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.googlesource.gerrit.plugins.github</groupId>
   <artifactId>github-parent</artifactId>
-  <version>2.11</version>
+  <version>2.12-SNAPSHOT</version>
   <name>Gerrit Code Review - GitHub integration</name>
   <url>http://www.gerritforge.com</url>
   <packaging>pom</packaging>
@@ -292,7 +292,7 @@
     <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
-        <version>1.14.0</version>
+        <version>1.16.2</version>
         <scope>provided</scope>
     </dependency>
   </dependencies>