Merge branch 'stable-2.16'

* stable-2.16:
  Add the standard AOSP license header and Copyright
  Adapt to the new related changes class on stable-2.15
  Adapt to the new related changes class on stable-2.16
  Fix submission of changes using the botUser identity
  Bazel: Include eclipse-out directory in .bazelignore

Change-Id: I2a993e4a562c39732918648406de7adcc8dd3644
diff --git a/.bazelignore b/.bazelignore
new file mode 100644
index 0000000..30f1613
--- /dev/null
+++ b/.bazelignore
@@ -0,0 +1 @@
+eclipse-out
diff --git a/src/main/java/com/criteo/gerrit/plugins/automerge/AtomicityHelper.java b/src/main/java/com/criteo/gerrit/plugins/automerge/AtomicityHelper.java
index ffedd94..faebf4b 100644
--- a/src/main/java/com/criteo/gerrit/plugins/automerge/AtomicityHelper.java
+++ b/src/main/java/com/criteo/gerrit/plugins/automerge/AtomicityHelper.java
@@ -1,18 +1,34 @@
+// Copyright (C) 2019 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.criteo.gerrit.plugins.automerge;
 
 import static com.google.gerrit.server.permissions.ChangePermission.READ;
 
 import com.google.gerrit.common.data.SubmitRecord;
+import com.google.gerrit.extensions.api.changes.RelatedChangeAndCommitInfo;
+import com.google.gerrit.extensions.api.changes.RelatedChangesInfo;
 import com.google.gerrit.extensions.api.changes.SubmitInput;
 import com.google.gerrit.extensions.client.ChangeStatus;
 import com.google.gerrit.extensions.restapi.AuthException;
-import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
 import com.google.gerrit.extensions.restapi.RestApiException;
 import com.google.gerrit.reviewdb.client.Account;
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.gerrit.reviewdb.server.ReviewDb;
 import com.google.gerrit.server.IdentifiedUser;
 import com.google.gerrit.server.account.Emails;
+import com.google.gerrit.server.change.ChangeResource;
 import com.google.gerrit.server.change.RevisionResource;
 import com.google.gerrit.server.notedb.ChangeNotes;
 import com.google.gerrit.server.permissions.PermissionBackend;
@@ -22,10 +38,7 @@
 import com.google.gerrit.server.project.SubmitRuleEvaluator;
 import com.google.gerrit.server.project.SubmitRuleOptions;
 import com.google.gerrit.server.query.change.ChangeData;
-import com.google.gerrit.server.restapi.change.ChangesCollection;
 import com.google.gerrit.server.restapi.change.GetRelated;
-import com.google.gerrit.server.restapi.change.GetRelated.ChangeAndCommit;
-import com.google.gerrit.server.restapi.change.GetRelated.RelatedInfo;
 import com.google.gerrit.server.restapi.change.Submit;
 import com.google.gwtorm.server.OrmException;
 import com.google.inject.Inject;
@@ -42,8 +55,6 @@
 
   @Inject ChangeData.Factory changeDataFactory;
 
-  @Inject private ChangesCollection collection;
-
   @Inject AutomergeConfig config;
 
   @Inject Provider<ReviewDb> db;
@@ -62,6 +73,8 @@
 
   @Inject SubmitRuleEvaluator.Factory submitRuleEvaluatorFactory;
 
+  @Inject ChangeResource.Factory changeResourceFactory;
+
   /**
    * Check if the current patchset of the specified change has dependent unmerged changes.
    *
@@ -78,13 +91,13 @@
       throws RestApiException, IOException, NoSuchChangeException, NoSuchProjectException,
           OrmException, PermissionBackendException {
     RevisionResource r = getRevisionResource(project, number);
-    RelatedInfo related = getRelated.apply(r);
+    RelatedChangesInfo related = getRelated.apply(r);
     log.debug(String.format("Checking for related changes on review %d", number));
 
     String checkedCommitSha1 = r.getPatchSet().getRevision().get();
     int firstParentIndex = 0;
     int i = 0;
-    for (ChangeAndCommit c : related.changes) {
+    for (RelatedChangeAndCommitInfo c : related.changes) {
       if (checkedCommitSha1.equals(c.commit.commit)) {
         firstParentIndex = i + 1;
         log.debug(
@@ -97,7 +110,8 @@
     }
 
     boolean hasNonMergedParent = false;
-    for (ChangeAndCommit c : related.changes.subList(firstParentIndex, related.changes.size())) {
+    for (RelatedChangeAndCommitInfo c :
+        related.changes.subList(firstParentIndex, related.changes.size())) {
       if (!ChangeStatus.MERGED.toString().equals(c.status)) {
         log.info(
             String.format(
@@ -166,10 +180,13 @@
       permissionBackend.user(getBotUser()).change(notes).database(db).check(READ);
       ChangeData changeData =
           changeDataFactory.create(db.get(), new Project.NameKey(project), changeId);
+
       RevisionResource r =
-          new RevisionResource(collection.parse(changeId), changeData.currentPatchSet());
+          new RevisionResource(
+              changeResourceFactory.create(changeData.notes(), getBotUser()),
+              changeData.currentPatchSet());
       return r;
-    } catch (ResourceNotFoundException | AuthException | PermissionBackendException e) {
+    } catch (AuthException | PermissionBackendException e) {
       throw new NoSuchChangeException(changeId);
     }
   }
diff --git a/src/main/java/com/criteo/gerrit/plugins/automerge/AutomaticMerger.java b/src/main/java/com/criteo/gerrit/plugins/automerge/AutomaticMerger.java
index d61a77b..b1e3c71 100644
--- a/src/main/java/com/criteo/gerrit/plugins/automerge/AutomaticMerger.java
+++ b/src/main/java/com/criteo/gerrit/plugins/automerge/AutomaticMerger.java
@@ -1,4 +1,5 @@
 // Copyright 2014 Criteo
+// Copyright (C) 2019 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.
diff --git a/src/main/java/com/criteo/gerrit/plugins/automerge/AutomergeConfig.java b/src/main/java/com/criteo/gerrit/plugins/automerge/AutomergeConfig.java
index 8eb1010..6fcc8bb 100644
--- a/src/main/java/com/criteo/gerrit/plugins/automerge/AutomergeConfig.java
+++ b/src/main/java/com/criteo/gerrit/plugins/automerge/AutomergeConfig.java
@@ -1,3 +1,17 @@
+// Copyright (C) 2019 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.criteo.gerrit.plugins.automerge;
 
 import com.google.gerrit.server.config.GerritServerConfig;
diff --git a/src/main/java/com/criteo/gerrit/plugins/automerge/AutomergeModule.java b/src/main/java/com/criteo/gerrit/plugins/automerge/AutomergeModule.java
index 0389810..68d5d9f 100644
--- a/src/main/java/com/criteo/gerrit/plugins/automerge/AutomergeModule.java
+++ b/src/main/java/com/criteo/gerrit/plugins/automerge/AutomergeModule.java
@@ -1,4 +1,5 @@
 // Copyright 2014 Criteo
+// Copyright (C) 2019 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.
diff --git a/src/main/java/com/criteo/gerrit/plugins/automerge/Change.java b/src/main/java/com/criteo/gerrit/plugins/automerge/Change.java
index 8728bed..ee986a1 100644
--- a/src/main/java/com/criteo/gerrit/plugins/automerge/Change.java
+++ b/src/main/java/com/criteo/gerrit/plugins/automerge/Change.java
@@ -1,3 +1,17 @@
+// Copyright (C) 2019 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.criteo.gerrit.plugins.automerge;
 
 import com.google.gerrit.extensions.common.ChangeInfo;
diff --git a/src/main/java/com/criteo/gerrit/plugins/automerge/PluginComment.java b/src/main/java/com/criteo/gerrit/plugins/automerge/PluginComment.java
index 0ac8aff..07fe96a 100644
--- a/src/main/java/com/criteo/gerrit/plugins/automerge/PluginComment.java
+++ b/src/main/java/com/criteo/gerrit/plugins/automerge/PluginComment.java
@@ -1,3 +1,17 @@
+// Copyright (C) 2019 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.criteo.gerrit.plugins.automerge;
 
 import com.google.common.base.Charsets;
diff --git a/src/main/java/com/criteo/gerrit/plugins/automerge/ReviewUpdater.java b/src/main/java/com/criteo/gerrit/plugins/automerge/ReviewUpdater.java
index 518e20b..2dc1c81 100644
--- a/src/main/java/com/criteo/gerrit/plugins/automerge/ReviewUpdater.java
+++ b/src/main/java/com/criteo/gerrit/plugins/automerge/ReviewUpdater.java
@@ -1,3 +1,17 @@
+// Copyright (C) 2019 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.criteo.gerrit.plugins.automerge;
 
 import com.google.gerrit.extensions.api.changes.ReviewInput;
diff --git a/src/test/java/com/criteo/gerrit/plugins/automerge/AutomaticMergerTest.java b/src/test/java/com/criteo/gerrit/plugins/automerge/AutomaticMergerTest.java
new file mode 100644
index 0000000..971659e
--- /dev/null
+++ b/src/test/java/com/criteo/gerrit/plugins/automerge/AutomaticMergerTest.java
@@ -0,0 +1,111 @@
+// Copyright (C) 2019 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.criteo.gerrit.plugins.automerge;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.gerrit.acceptance.GerritConfig;
+import com.google.gerrit.acceptance.LightweightPluginDaemonTest;
+import com.google.gerrit.acceptance.NoHttpd;
+import com.google.gerrit.acceptance.PushOneCommit;
+import com.google.gerrit.acceptance.TestAccount;
+import com.google.gerrit.acceptance.TestPlugin;
+import com.google.gerrit.extensions.api.changes.ChangeApi;
+import com.google.gerrit.extensions.api.changes.Changes;
+import com.google.gerrit.extensions.api.changes.ReviewInput;
+import com.google.gerrit.extensions.client.ChangeStatus;
+import com.google.gerrit.extensions.common.ChangeInfo;
+import com.google.gerrit.reviewdb.client.AccountGroup;
+import com.google.gerrit.server.account.externalids.ExternalIds;
+import com.google.inject.Inject;
+import java.util.Arrays;
+import java.util.List;
+import org.junit.Before;
+import org.junit.Test;
+
+@NoHttpd
+@TestPlugin(
+    name = "autosubmitter",
+    sysModule = "com.criteo.gerrit.plugins.automerge.AutomergeModule")
+public class AutomaticMergerTest extends LightweightPluginDaemonTest {
+  private static final String BOT_USERS = "Bot Users";
+  private static final String DEVELOPERS = "Developers";
+  private TestAccount botUser;
+  private TestAccount regularUser;
+  @Inject ExternalIds extIds;
+
+  @Before
+  public void setup() throws Exception {
+    gApi.groups().create(BOT_USERS);
+    gApi.groups().create(DEVELOPERS);
+    botUser = accountCreator.create("botuser", "botuser@mycompany.com", "Bot User", BOT_USERS);
+    regularUser =
+        accountCreator.create("developer", "developer@mycompany.com", "Developer", DEVELOPERS);
+    grant(project, "refs/*", "submit", false, groupUUID(BOT_USERS));
+    grantLabel("Code-Review", -2, 2, project, "refs/*", false, groupUUID(DEVELOPERS), false);
+  }
+
+  @Test
+  @GerritConfig(name = "automerge.botEmail", value = "botuser@mycompany.com")
+  public void changeReviewedShouldNotBeAutomaticallyMergedIfNotApproved() throws Exception {
+    int changeNum = createChangeNum(user);
+
+    assertThat(changesApi().id(changeNum).get().status).isEqualTo(ChangeStatus.NEW);
+  }
+
+  @Test
+  @GerritConfig(name = "automerge.botEmail", value = "botuser@mycompany.com")
+  public void changeReviewedShouldBeAutomaticallyMergedOnceApproved() throws Exception {
+    int changeNum = createChangeNum(user);
+    changesApi().id(changeNum).current().review(ReviewInput.approve());
+
+    assertThat(changesApi().id(changeNum).get().status).isEqualTo(ChangeStatus.MERGED);
+  }
+
+  @Test
+  @GerritConfig(name = "automerge.botEmail", value = "botuser@mycompany.com")
+  public void changeShouldBeAutomaticallyMergedByBotUser() throws Exception {
+    int changeNum = createChangeNum(user);
+    ChangeApi changeApi = changesApi().id(changeNum);
+    changeApi.current().review(ReviewInput.approve());
+
+    ChangeInfo changeInfo = changeApi.get();
+    assertThat(changeInfo.submitter).isNotNull();
+    assertThat(changeInfo.submitter._accountId).isEqualTo(new Integer(botUser.id.get()));
+    assertThat(changeInfo.submitter.email).isEqualTo(botUser.email);
+  }
+
+  private AccountGroup.UUID groupUUID(String name) {
+    return groupCache.get(new AccountGroup.NameKey(name)).get().getGroupUUID();
+  }
+
+  private Changes changesApi() {
+    setApiUser(regularUser);
+    return gApi.changes();
+  }
+
+  private int createChangeNum(TestAccount user) throws Exception {
+    List<String> msgs =
+        Arrays.asList(createChangeAsUser("refs/for/master", user).getMessage().split("\n"));
+    String changeUrl = msgs.get(msgs.size() - 1).trim().split(" ")[0];
+    return Integer.parseInt(changeUrl.substring(changeUrl.lastIndexOf('/') + 1));
+  }
+
+  protected PushOneCommit.Result createChangeAsUser(String ref, TestAccount user) throws Exception {
+    PushOneCommit.Result result = pushFactory.create(db, user.getIdent(), testRepo).to(ref);
+    result.assertOkStatus();
+    return result;
+  }
+}
diff --git a/src/test/java/com/criteo/gerrit/plugins/automerge/AutomergeConfigTest.java b/src/test/java/com/criteo/gerrit/plugins/automerge/AutomergeConfigTest.java
index 69e4ef9..548ee49 100644
--- a/src/test/java/com/criteo/gerrit/plugins/automerge/AutomergeConfigTest.java
+++ b/src/test/java/com/criteo/gerrit/plugins/automerge/AutomergeConfigTest.java
@@ -1,3 +1,17 @@
+// Copyright (C) 2019 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.criteo.gerrit.plugins.automerge;
 
 import static org.junit.Assert.assertEquals;