Support creating and updating find-owner OWNERS files Updates to code owner config files are defined by the new CodeOwnerConfigUpdate class. It follows the example of *Update classes in Gerrit core (e.g. InternalGroupUpdate). CodeOwnerConfigFile is now accepting a CodeOwnerConfigUpdate to create/update OWNERS files. Signed-off-by: Edwin Kempin <ekempin@google.com> Change-Id: Ie04f9b6d6d5c50d8975d93ab35c7052f985e7533
diff --git a/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnerConfig.java b/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnerConfig.java index 7ef5a5b..eb524bc 100644 --- a/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnerConfig.java +++ b/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnerConfig.java
@@ -38,6 +38,13 @@ public abstract ImmutableSet<CodeOwnerReference> codeOwners(); /** + * Creates a builder form this code owner config. + * + * @return builder that was created from this code owner config + */ + public abstract Builder toBuilder(); + + /** * Creates a builder for a code owner config. * * @param key the key of the code owner config
diff --git a/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnerConfigUpdate.java b/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnerConfigUpdate.java new file mode 100644 index 0000000..2ea2b03 --- /dev/null +++ b/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnerConfigUpdate.java
@@ -0,0 +1,79 @@ +// Copyright (C) 2020 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.plugins.codeowners.backend; + +import com.google.auto.value.AutoValue; +import com.google.common.collect.ImmutableSet; +import java.util.Set; + +/** + * Definition of an update to a {@link CodeOwnerConfig}. + * + * <p>A {@code CodeOwnerConfigUpdate} only specifies the modifications which should be applied to a + * code owner config. Each of the modifications and hence each call on {@link + * CodeOwnerConfigUpdate.Builder} is optional. + */ +@AutoValue +public abstract class CodeOwnerConfigUpdate { + /** Representation of a code owner modification as defined by {@link #apply(ImmutableSet)}. */ + @FunctionalInterface + public interface CodeOwnerModification { + + /** + * Applies the modification to the given code owners. + * + * @param originalCodeOwners current code owners of the code owner config. If used for a code + * owner config creation, this set is empty. + * @return the desired resulting code owners (not the diff of the code owners!) + */ + Set<CodeOwnerReference> apply(ImmutableSet<CodeOwnerReference> originalCodeOwners); + } + + /** + * Defines how the code owners of the code owner config should be modified. By default (that is if + * nothing is specified), the code owners remain unchanged. + * + * @return a {@link CodeOwnerModification} which gets the current code owners of the code owner + * config as input and outputs the desired resulting code owners + */ + public abstract CodeOwnerModification codeOwnerModification(); + + /** + * Creates a builder for a {@link CodeOwnerConfigUpdate}. + * + * @return builder for a {@link CodeOwnerConfigUpdate} + */ + public static Builder builder() { + return new AutoValue_CodeOwnerConfigUpdate.Builder() + .setCodeOwnerModification(codeOwners -> codeOwners); + } + + @AutoValue.Builder + public abstract static class Builder { + /** + * Sets the code owner modification. + * + * @see #codeOwnerModification() + */ + public abstract Builder setCodeOwnerModification(CodeOwnerModification codeOwnerModification); + + /** + * Builds the {@link CodeOwnerConfigUpdate} instance. + * + * @return the {@link CodeOwnerConfigUpdate} instance + */ + public abstract CodeOwnerConfigUpdate build(); + } +}
diff --git a/java/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigFile.java b/java/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigFile.java index a332cd9..a4a1b0b 100644 --- a/java/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigFile.java +++ b/java/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigFile.java
@@ -17,9 +17,11 @@ import static com.google.common.base.Preconditions.checkState; import com.google.common.annotations.VisibleForTesting; -import com.google.gerrit.extensions.restapi.NotImplementedException; +import com.google.common.collect.ImmutableSet; import com.google.gerrit.plugins.codeowners.backend.CodeOwnerConfig; +import com.google.gerrit.plugins.codeowners.backend.CodeOwnerConfigUpdate; import com.google.gerrit.server.git.GitRepositoryManager; +import com.google.gerrit.server.git.meta.MetaDataUpdate; import com.google.gerrit.server.git.meta.VersionedMetaData; import com.google.inject.Inject; import com.google.inject.Singleton; @@ -29,13 +31,19 @@ import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.lib.CommitBuilder; import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.treewalk.TreeWalk; /** * A representation of a code owner config that is stored as an {@code OWNERS} file in a source * branch. * - * <p>For reading code owner configs, refer to {@link Factory#load(CodeOwnerConfig.Key)}. + * <p>For reading code owner configs or creating/updating them, refer to {@link + * Factory#load(CodeOwnerConfig.Key)}. + * + * <p><strong>Note:</strong> Any modification (code owner config creation or update) only becomes + * permanent (and hence written to repository) if {@link + * #commit(com.google.gerrit.server.git.meta.MetaDataUpdate)} is called. */ class CodeOwnerConfigFile extends VersionedMetaData { /** Name of the file in which the code owner config for a folder in a branch is stored. */ @@ -62,6 +70,11 @@ * #getLoadedCodeOwnerConfig()} won't return any code owner config. Thus, the existence of a * code owner config can be easily tested. * + * <p>The code owner config represented by the returned {@link CodeOwnerConfigFile} can be + * created/updated by setting an {@link CodeOwnerConfigUpdate} via {@link + * #setCodeOwnerConfigUpdate(CodeOwnerConfigUpdate)} and committing the {@link + * CodeOwnerConfigUpdate} via {@link #commit(com.google.gerrit.server.git.meta.MetaDataUpdate)}. + * * @param codeOwnerConfigKey the key of the code owner config * @return a {@link CodeOwnerConfigFile} for the code owner config with the specified key * @throws IOException if the repository can't be accessed for some reason @@ -84,6 +97,7 @@ private boolean isLoaded = false; private Optional<CodeOwnerConfig> loadedCodeOwnersConfig = Optional.empty(); + private Optional<CodeOwnerConfigUpdate> codeOwnerConfigUpdate = Optional.empty(); private CodeOwnerConfigFile( CodeOwnerConfigParser codeOwnerConfigParser, CodeOwnerConfig.Key codeOwnerConfigKey) { @@ -103,6 +117,25 @@ return loadedCodeOwnersConfig; } + /** + * Specifies how the current code owner config should be updated. + * + * <p>If the code owner config is newly created, the {@link CodeOwnerConfigUpdate} can be used to + * specify optional properties. + * + * <p>If the update leads to an empty code owner config, the code owner config file is deleted. + * + * <p><strong>Note:</strong> This method doesn't perform the update. It only contains the + * instructions for the update. To apply the update for real, call {@link + * #commit(com.google.gerrit.server.git.meta.MetaDataUpdate)} on this {@link CodeOwnerConfigFile}. + * + * @param codeOwnerConfigUpdate an {@code CodeOwnerConfigUpdate} outlining the modifications which + * should be applied + */ + public void setCodeOwnerConfigUpdate(CodeOwnerConfigUpdate codeOwnerConfigUpdate) { + this.codeOwnerConfigUpdate = Optional.of(codeOwnerConfigUpdate); + } + @Override protected String getRefName() { return codeOwnerConfigKey.branch().branch(); @@ -144,11 +177,59 @@ } @Override + public RevCommit commit(MetaDataUpdate update) throws IOException { + // Reject the creation of a code owner config if the branch doesn't exist. + checkState( + update.getRepository().exactRef(getRefName()) != null, + "branch %s does not exist", + getRefName()); + + return super.commit(update); + } + + @Override protected boolean onSave(CommitBuilder commit) throws IOException, ConfigInvalidException { checkLoaded(); - // TODO(ekempin): Implement this method - throw new NotImplementedException(); + if (!codeOwnerConfigUpdate.isPresent()) { + // Code owner config was neither created nor changed. -> A new commit isn't necessary. + return false; + } + + // Update the code owner config. + CodeOwnerConfig originalCodeOwnerConfig = + loadedCodeOwnersConfig.orElse(CodeOwnerConfig.builder(codeOwnerConfigKey).build()); + CodeOwnerConfig updatedCodeOwnerConfig = + updateCodeOwnerConfig(originalCodeOwnerConfig, codeOwnerConfigUpdate.get()); + + // Do not create a new commit if the code owner config didn't change. + if (updatedCodeOwnerConfig.equals(originalCodeOwnerConfig)) { + return false; + } + + // Save the new code owner config. + String codeOwnerConfigFileContent = + codeOwnerConfigParser.formatAsString(updatedCodeOwnerConfig); + saveUTF8(codeOwnerConfigKey.filePathForJgit(FILE_NAME), codeOwnerConfigFileContent); + + // If the file content is empty, the update led to a deletion of the code owner config file. + loadedCodeOwnersConfig = + codeOwnerConfigFileContent.isEmpty() + ? Optional.empty() + : Optional.of(updatedCodeOwnerConfig); + codeOwnerConfigUpdate = Optional.empty(); + + return true; + } + + private CodeOwnerConfig updateCodeOwnerConfig( + CodeOwnerConfig codeOwnerConfig, CodeOwnerConfigUpdate codeOwnerConfigUpdate) { + return codeOwnerConfig + .toBuilder() + .setCodeOwners( + ImmutableSet.copyOf( + codeOwnerConfigUpdate.codeOwnerModification().apply(codeOwnerConfig.codeOwners()))) + .build(); } private void checkLoaded() {
diff --git a/javatests/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigFileTest.java b/javatests/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigFileTest.java index 1824b94..3a0cf8d 100644 --- a/javatests/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigFileTest.java +++ b/javatests/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigFileTest.java
@@ -14,18 +14,34 @@ package com.google.gerrit.plugins.codeowners.backend.findowners; +import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth8.assertThat; import static com.google.gerrit.plugins.codeowners.testing.CodeOwnerConfigSubject.assertThat; +import static com.google.gerrit.testing.GerritJUnit.assertThrows; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; import com.google.gerrit.plugins.codeowners.acceptance.AbstractCodeOwnersTest; import com.google.gerrit.plugins.codeowners.backend.CodeOwnerConfig; +import com.google.gerrit.plugins.codeowners.backend.CodeOwnerConfigUpdate; +import com.google.gerrit.plugins.codeowners.backend.CodeOwnerReference; +import com.google.gerrit.server.git.meta.MetaDataUpdate; +import com.google.inject.Inject; +import java.util.Optional; import org.eclipse.jgit.junit.TestRepository; +import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevObject; +import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.util.RawParseUtils; import org.junit.Before; import org.junit.Test; /** Tests for {@link CodeOwnerConfigFile}. */ public class CodeOwnerConfigFileTest extends AbstractCodeOwnersTest { + @Inject private MetaDataUpdate.Server metaDataUpdateServer; + private CodeOwnerConfigFile.Factory codeOwnerConfigFileFactory; private CodeOwnerConfigParser codeOwnerConfigParser; @@ -77,6 +93,270 @@ .containsExactly(admin.email()); } + @Test + public void createCodeOwnerConfigFile() throws Exception { + CodeOwnerConfig.Key codeOwnerConfigKey = + CodeOwnerConfig.Key.create(project, "master", "/foo/bar/"); + try (Repository repo = repoManager.openRepository(project); + MetaDataUpdate metaDataUpdate = metaDataUpdateServer.create(project)) { + CodeOwnerConfigFile codeOwnerConfigFile = codeOwnerConfigFileFactory.load(codeOwnerConfigKey); + + // Check that the code owner config doesn't exist yet. + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig()).isEmpty(); + assertThat(readCodeOwnerConfig(codeOwnerConfigKey)).isEmpty(); + + // Remember head so that we can check that the branch is changed. + RevCommit head = getHead(repo, codeOwnerConfigKey.ref()); + + // Create the code owner config. + codeOwnerConfigFile.setCodeOwnerConfigUpdate( + CodeOwnerConfigUpdate.builder() + .setCodeOwnerModification( + codeOwners -> + Sets.union( + codeOwners, ImmutableSet.of(CodeOwnerReference.create(admin.email())))) + .build()); + codeOwnerConfigFile.commit(metaDataUpdate); + + // Check that the loaded code owner config was updated. + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig()).isPresent(); + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig().get()) + .hasCodeOwnersEmailsThat() + .containsExactly(admin.email()); + + // Check that the code owner config was created in the repository. + Optional<CodeOwnerConfig> codeOwnerConfigInRepo = readCodeOwnerConfig(codeOwnerConfigKey); + assertThat(codeOwnerConfigInRepo).isPresent(); + assertThat(codeOwnerConfigInRepo.get()) + .hasCodeOwnersEmailsThat() + .containsExactly(admin.email()); + + // Check that the branch was changed. + assertThat(head).isNotEqualTo(getHead(repo, codeOwnerConfigKey.ref())); + } + } + + @Test + public void codeOwnerConfigFileIsNotCreatedIfNoCodeOwnerConfigUpdateSpecified() throws Exception { + CodeOwnerConfig.Key codeOwnerConfigKey = + CodeOwnerConfig.Key.create(project, "master", "/foo/bar/"); + try (Repository repo = repoManager.openRepository(project); + MetaDataUpdate metaDataUpdate = metaDataUpdateServer.create(project)) { + CodeOwnerConfigFile codeOwnerConfigFile = codeOwnerConfigFileFactory.load(codeOwnerConfigKey); + + // Check that the code owner config doesn't exist yet. + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig()).isEmpty(); + assertThat(readCodeOwnerConfig(codeOwnerConfigKey)).isEmpty(); + + // Remember head so that we can check that the branch doesn't change. + RevCommit head = getHead(repo, codeOwnerConfigKey.ref()); + + // Try to create the code owner config without setting a code owner config update. + codeOwnerConfigFile.commit(metaDataUpdate); + + // Check that the loaded code owner config was not created. + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig()).isEmpty(); + + // Check that no code owner config was created in the repository. + Optional<CodeOwnerConfig> codeOwnerConfigInRepo = readCodeOwnerConfig(codeOwnerConfigKey); + assertThat(codeOwnerConfigInRepo).isEmpty(); + + // Check that the branch didn't change. + assertThat(head).isEqualTo(getHead(repo, codeOwnerConfigKey.ref())); + } + } + + @Test + public void codeOwnerConfigFileIsNotCreatedIfEmpty() throws Exception { + CodeOwnerConfig.Key codeOwnerConfigKey = + CodeOwnerConfig.Key.create(project, "master", "/foo/bar/"); + try (Repository repo = repoManager.openRepository(project); + MetaDataUpdate metaDataUpdate = metaDataUpdateServer.create(project)) { + CodeOwnerConfigFile codeOwnerConfigFile = codeOwnerConfigFileFactory.load(codeOwnerConfigKey); + + // Check that the code owner config doesn't exist yet. + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig()).isEmpty(); + assertThat(readCodeOwnerConfig(codeOwnerConfigKey)).isEmpty(); + + // Remember head so that we can check that the branch doesn't change. + RevCommit head = getHead(repo, codeOwnerConfigKey.ref()); + + // Try to create the code owner config without specifying code owners. + codeOwnerConfigFile.setCodeOwnerConfigUpdate(CodeOwnerConfigUpdate.builder().build()); + codeOwnerConfigFile.commit(metaDataUpdate); + + // Check that the loaded code owner config was not created. + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig()).isEmpty(); + + // Check that no code owner config was created in the repository. + Optional<CodeOwnerConfig> codeOwnerConfigInRepo = readCodeOwnerConfig(codeOwnerConfigKey); + assertThat(codeOwnerConfigInRepo).isEmpty(); + + // Check that the branch didn't change. + assertThat(head).isEqualTo(getHead(repo, codeOwnerConfigKey.ref())); + } + } + + @Test + public void cannotCreateCodeOwnerConfigFileInNonExistingBranch() throws Exception { + CodeOwnerConfig.Key codeOwnerConfigKey = + CodeOwnerConfig.Key.create(project, "non-existing", "/foo/bar/"); + try (Repository repo = repoManager.openRepository(project); + MetaDataUpdate metaDataUpdate = metaDataUpdateServer.create(project)) { + CodeOwnerConfigFile codeOwnerConfigFile = codeOwnerConfigFileFactory.load(codeOwnerConfigKey); + + // Check that the code owner config doesn't exist yet. + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig()).isEmpty(); + assertThat(readCodeOwnerConfig(codeOwnerConfigKey)).isEmpty(); + + // Create the code owner config. + String email = "admin@example.com"; + codeOwnerConfigFile.setCodeOwnerConfigUpdate( + CodeOwnerConfigUpdate.builder() + .setCodeOwnerModification( + codeOwners -> + Sets.union(codeOwners, ImmutableSet.of(CodeOwnerReference.create(email)))) + .build()); + IllegalStateException exception = + assertThrows( + IllegalStateException.class, () -> codeOwnerConfigFile.commit(metaDataUpdate)); + assertThat(exception) + .hasMessageThat() + .isEqualTo(String.format("branch %s does not exist", codeOwnerConfigKey.ref())); + + // Check that the loaded code owner config is still empty. + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig()).isEmpty(); + + // Check that the code owner config was not created in the repository. + assertThat(readCodeOwnerConfig(codeOwnerConfigKey)).isEmpty(); + + // Check that the branch was not created. + assertThat(repo.exactRef(codeOwnerConfigKey.ref())).isNull(); + } + } + + @Test + public void updateCodeOwnerConfigFile() throws Exception { + CodeOwnerConfig.Key codeOwnerConfigKey = CodeOwnerConfig.Key.create(project, "master", "/"); + CodeOwnerConfig codeOwnerConfig = + CodeOwnerConfig.builder(codeOwnerConfigKey).addCodeOwnerEmail(admin.email()).build(); + writeCodeOwnerConfig(codeOwnerConfig); + + try (Repository repo = repoManager.openRepository(project); + MetaDataUpdate metaDataUpdate = metaDataUpdateServer.create(project)) { + CodeOwnerConfigFile codeOwnerConfigFile = codeOwnerConfigFileFactory.load(codeOwnerConfigKey); + + // Check that the code owner config exists. + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig()).isPresent(); + assertThat(readCodeOwnerConfig(codeOwnerConfigKey)).isPresent(); + + // Remember head so that we can check that the branch is changed. + RevCommit head = getHead(repo, codeOwnerConfigKey.ref()); + + // Update the code owner config. + codeOwnerConfigFile.setCodeOwnerConfigUpdate( + CodeOwnerConfigUpdate.builder() + .setCodeOwnerModification( + codeOwners -> + Sets.union( + codeOwners, ImmutableSet.of(CodeOwnerReference.create(user.email())))) + .build()); + codeOwnerConfigFile.commit(metaDataUpdate); + + // Check that the loaded code owner config was updated. + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig()).isPresent(); + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig().get()) + .hasCodeOwnersEmailsThat() + .containsExactly(admin.email(), user.email()); + + // Check that the code owner config was updated in the repository. + Optional<CodeOwnerConfig> codeOwnerConfigInRepo = readCodeOwnerConfig(codeOwnerConfigKey); + assertThat(codeOwnerConfigInRepo).isPresent(); + assertThat(codeOwnerConfigInRepo.get()) + .hasCodeOwnersEmailsThat() + .containsExactly(admin.email(), user.email()); + + // Check that the branch was changed. + assertThat(head).isNotEqualTo(getHead(repo, codeOwnerConfigKey.ref())); + } + } + + @Test + public void codeOwnerConfigFileIsNotUpdatedIfUpdateIsANoOp() throws Exception { + CodeOwnerConfig.Key codeOwnerConfigKey = CodeOwnerConfig.Key.create(project, "master", "/"); + CodeOwnerConfig codeOwnerConfig = + CodeOwnerConfig.builder(codeOwnerConfigKey).addCodeOwnerEmail(admin.email()).build(); + writeCodeOwnerConfig(codeOwnerConfig); + + try (Repository repo = repoManager.openRepository(project); + MetaDataUpdate metaDataUpdate = metaDataUpdateServer.create(project)) { + CodeOwnerConfigFile codeOwnerConfigFile = codeOwnerConfigFileFactory.load(codeOwnerConfigKey); + + // Check that the code owner config exists. + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig()).isPresent(); + assertThat(readCodeOwnerConfig(codeOwnerConfigKey)).isPresent(); + + // Remember head so that we can check that the branch doesn't change. + RevCommit head = getHead(repo, codeOwnerConfigKey.ref()); + + // Update the code owner config without changing it. + codeOwnerConfigFile.setCodeOwnerConfigUpdate(CodeOwnerConfigUpdate.builder().build()); + codeOwnerConfigFile.commit(metaDataUpdate); + + // Check that the loaded code owner config didn't change. + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig()).isPresent(); + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig().get()) + .hasCodeOwnersEmailsThat() + .containsExactly(admin.email()); + + // Check that the code owner config in the repository didn't change. + Optional<CodeOwnerConfig> codeOwnerConfigInRepo = readCodeOwnerConfig(codeOwnerConfigKey); + assertThat(codeOwnerConfigInRepo).isPresent(); + assertThat(codeOwnerConfigInRepo.get()) + .hasCodeOwnersEmailsThat() + .containsExactly(admin.email()); + + // Check that the branch didn't change. + assertThat(head).isEqualTo(getHead(repo, codeOwnerConfigKey.ref())); + } + } + + @Test + public void updateCodeOwnerConfigFileSoThatItBecomesEmptyAndIsDeleted() throws Exception { + CodeOwnerConfig.Key codeOwnerConfigKey = CodeOwnerConfig.Key.create(project, "master", "/"); + CodeOwnerConfig codeOwnerConfig = + CodeOwnerConfig.builder(codeOwnerConfigKey).addCodeOwnerEmail(admin.email()).build(); + writeCodeOwnerConfig(codeOwnerConfig); + + try (Repository repo = repoManager.openRepository(project); + MetaDataUpdate metaDataUpdate = metaDataUpdateServer.create(project)) { + CodeOwnerConfigFile codeOwnerConfigFile = codeOwnerConfigFileFactory.load(codeOwnerConfigKey); + + // Check that the code owner config exists. + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig()).isPresent(); + assertThat(readCodeOwnerConfig(codeOwnerConfigKey)).isPresent(); + + // Remember head so that we can check that the branch is changed. + RevCommit head = getHead(repo, codeOwnerConfigKey.ref()); + + // Update the code owner config so that it becomes empty. + codeOwnerConfigFile.setCodeOwnerConfigUpdate( + CodeOwnerConfigUpdate.builder() + .setCodeOwnerModification(codeOwners -> ImmutableSet.of()) + .build()); + codeOwnerConfigFile.commit(metaDataUpdate); + + // Check that the loaded code owner config was unset. + assertThat(codeOwnerConfigFile.getLoadedCodeOwnerConfig()).isEmpty(); + + // Check that the code owner config was deleted in the repository. + assertThat(readCodeOwnerConfig(codeOwnerConfigKey)).isEmpty(); + + // Check that the branch was changed. + assertThat(head).isNotEqualTo(getHead(repo, codeOwnerConfigKey.ref())); + } + } + private void writeCodeOwnerConfig(CodeOwnerConfig codeOwnerConfig) throws Exception { String formattedCodeOwnerConfig = codeOwnerConfigParser.formatAsString(codeOwnerConfig); try (TestRepository<Repository> testRepo = @@ -92,4 +372,30 @@ formattedCodeOwnerConfig)); } } + + private Optional<CodeOwnerConfig> readCodeOwnerConfig(CodeOwnerConfig.Key codeOwnerConfigKey) + throws Exception { + try (TestRepository<Repository> testRepo = + new TestRepository<>(repoManager.openRepository(project))) { + Ref ref = testRepo.getRepository().exactRef(codeOwnerConfigKey.ref()); + if (ref == null) { + // branch does not exist + return Optional.empty(); + } + + RevCommit commit = testRepo.getRevWalk().parseCommit(ref.getObjectId()); + String filePath = codeOwnerConfigKey.filePathForJgit(CodeOwnerConfigFile.FILE_NAME); + try (TreeWalk tw = + TreeWalk.forPath(testRepo.getRevWalk().getObjectReader(), filePath, commit.getTree())) { + if (tw == null) { + // file does not exist + return Optional.empty(); + } + } + RevObject blob = testRepo.get(commit.getTree(), filePath); + byte[] data = testRepo.getRepository().open(blob).getCachedBytes(Integer.MAX_VALUE); + return Optional.of( + codeOwnerConfigParser.parse(codeOwnerConfigKey, RawParseUtils.decode(data))); + } + } }