Use project specific settings Change-Id: I415058b3b9b6cfd040248fa287feb0d2715c2b9e Signed-off-by: Jan Srnicek <jsrnicek@cisco.com>
diff --git a/src/main/java/io/fd/maintainer/plugin/events/OnCommittersToBeAddedListener.java b/src/main/java/io/fd/maintainer/plugin/events/OnCommittersToBeAddedListener.java index a0df89f..a310b50 100644 --- a/src/main/java/io/fd/maintainer/plugin/events/OnCommittersToBeAddedListener.java +++ b/src/main/java/io/fd/maintainer/plugin/events/OnCommittersToBeAddedListener.java
@@ -18,6 +18,7 @@ import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.PatchSet; +import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gerrit.server.change.ChangesCollection; import com.google.gerrit.server.change.PostReview; @@ -95,8 +96,9 @@ final PatchSetCreatedEvent patchSetCreatedEvent = PatchSetCreatedEvent.class.cast(event); final ChangeAttribute changeAttributes = patchSetCreatedEvent.change.get(); + final Project.NameKey projectKey = new Project.NameKey(changeAttributes.project); final PluginBranchSpecificSettings settings = - settingsProvider.getBranchSpecificSettings(changeAttributes.branch); + settingsProvider.getBranchSpecificSettings(changeAttributes.branch, projectKey); if (!settings.isAutoAddReviewers()) { LOG.warn("Auto add reviewers option turned off"); @@ -104,7 +106,7 @@ } try (final ReviewDb reviewDb = schemaFactory.open()) { - final Change.Id changeId = new Change.Id(changeAttributes.number); + final Change.Id changeId = new Change.Id(Integer.valueOf(changeAttributes.number)); final Change change = reviewDb.changes().get(changeId); final PatchSet mostCurrentPatchSet = reviewDb.patchSets().get(change.currentPatchSetId()); @@ -112,7 +114,8 @@ LOG.info("Processing change {} | patchset {}", change.getId(), mostCurrentPatchSet.getId()); final MaintainersIndex index = new MaintainersIndex( - maintainersProvider.getMaintainersInfo(changeAttributes.branch, changeAttributes.number)); + maintainersProvider.getMaintainersInfo(changeAttributes.branch, + Integer.valueOf(changeAttributes.number), projectKey)); reviewerPusher.addRelevantReviewers(index, change, mostCurrentPatchSet, settings.getPluginUserName()); LOG.info("Reviewers for change {} successfully added", change.getId());
diff --git a/src/main/java/io/fd/maintainer/plugin/events/OnPatchsetVerifiedListener.java b/src/main/java/io/fd/maintainer/plugin/events/OnPatchsetVerifiedListener.java index 194d626..9063859 100644 --- a/src/main/java/io/fd/maintainer/plugin/events/OnPatchsetVerifiedListener.java +++ b/src/main/java/io/fd/maintainer/plugin/events/OnPatchsetVerifiedListener.java
@@ -24,6 +24,7 @@ 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.change.ChangesCollection; import com.google.gerrit.server.data.AccountAttribute; @@ -83,8 +84,9 @@ protected void consumeDescribedEvent(final Event event) { CommentAddedEvent commentAddedEvent = CommentAddedEvent.class.cast(event); + final Project.NameKey projectKey = new Project.NameKey(commentAddedEvent.change.get().project); final PluginBranchSpecificSettings settings = - settingsProvider.getBranchSpecificSettings(commentAddedEvent.change.get().branch); + settingsProvider.getBranchSpecificSettings(commentAddedEvent.change.get().branch, projectKey); if (!settings.isAllowMaintainersSubmit()) { LOG.warn("Maintainers submit is turned off"); @@ -125,7 +127,7 @@ final MaintainersIndex maintainersIndex = new MaintainersIndex(maintainersProvider .getMaintainersInfo(commentAddedEvent.getBranchNameKey().get(), - changeNumber)); + changeNumber, projectKey)); LOG.info("Getting current patch list for patchset {}", currentPatchset.getId()); final PatchList patchList = getPatchList(patchListCache, change, currentPatchset); @@ -156,8 +158,7 @@ } else if (patchsetReviewInfo.getReviewState() == COMMITTER_ATTENTION_NEEDED) { LOG.info("Patchset {} affects no configured components, committers attention needed", currentPatchset.getId()); - } - else { + } else { LOG.info( "Patchset {} does not have verifications from following components yet : {}", currentPatchset.getId(), patchsetReviewInfo.getMissingComponentReview());
diff --git a/src/main/java/io/fd/maintainer/plugin/service/MaintainersProvider.java b/src/main/java/io/fd/maintainer/plugin/service/MaintainersProvider.java index b2d940a..4e8acb1 100644 --- a/src/main/java/io/fd/maintainer/plugin/service/MaintainersProvider.java +++ b/src/main/java/io/fd/maintainer/plugin/service/MaintainersProvider.java
@@ -20,6 +20,7 @@ import static java.util.Objects.nonNull; import com.google.gerrit.reviewdb.client.Change; +import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.server.ReviewDb; import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gwtorm.server.OrmException; @@ -65,10 +66,12 @@ } @Nonnull - public List<ComponentInfo> getMaintainersInfo(@Nonnull final String branchName, final int changeNumber) { + public List<ComponentInfo> getMaintainersInfo(@Nonnull final String branchName, final int changeNumber, + @Nonnull final Project.NameKey projectKey) { // get configuration for branch of change - final PluginBranchSpecificSettings settings = settingsProvider.getBranchSpecificSettings(branchName); + final PluginBranchSpecificSettings settings = + settingsProvider.getBranchSpecificSettings(branchName, projectKey); try (final ReviewDb reviewDb = schemaFactory.open()) { final Change change = reviewDb.changes().get(new Change.Id(changeNumber));
diff --git a/src/main/java/io/fd/maintainer/plugin/service/SettingsProvider.java b/src/main/java/io/fd/maintainer/plugin/service/SettingsProvider.java index 9c3f6fd..8dc9ca0 100644 --- a/src/main/java/io/fd/maintainer/plugin/service/SettingsProvider.java +++ b/src/main/java/io/fd/maintainer/plugin/service/SettingsProvider.java
@@ -18,8 +18,10 @@ import static java.lang.String.format; +import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.RefNames; import com.google.gerrit.server.config.PluginConfigFactory; +import com.google.gerrit.server.project.NoSuchProjectException; import com.google.inject.Inject; import com.google.inject.Singleton; import io.fd.maintainer.plugin.service.dto.PluginBranchSpecificSettings; @@ -62,26 +64,28 @@ @Inject private PluginConfigFactory cfg; - public PluginBranchSpecificSettings getBranchSpecificSettings(@Nonnull final String branchName) { + public PluginBranchSpecificSettings getBranchSpecificSettings(@Nonnull final String branchName, + @Nonnull final Project.NameKey projectKey) { final String fullBranchName = branchName.startsWith(RefNames.REFS_HEADS) ? branchName : RefNames.REFS_HEADS.concat(branchName); LOG.info("Reading configuration for branch {}", fullBranchName); - return getSettingsForBranch(fullBranchName, closesBranchMatch(fullBranchName)); + return getSettingsForBranch(fullBranchName, closesBranchMatch(fullBranchName, projectKey), projectKey); } - private PluginBranchSpecificSettings getSettingsForBranch(final String branchName, final String closestBranch) { + private PluginBranchSpecificSettings getSettingsForBranch(final String branchName, final String closestBranch, + final Project.NameKey projectKey) { return new PluginBranchSpecificSettings.PluginSettingsBuilder() - .setPluginUserName(pluginUserOrThrow(branchName, closestBranch)) - .setLocalFilePath(fileNameRefOrDefault(branchName, closestBranch)) - .setFileRef(filePathRefOrDefault(branchName, closestBranch)) - .setAllowMaintainersSubmit(allowMaintainersSubmitOrDefault(branchName, closestBranch)) - .setAutoAddReviewers(autoAddReviewersOrDefault(branchName, closestBranch)) - .setAutoSubmit(autoSubmitOrDefault(branchName, closestBranch)) - .setDislikeWarnings(dislikeWarningsOrDefault(branchName, closestBranch)) - .setBranch(globalPluginConfig().getSubsections(BRANCH_SECTION) + .setPluginUserName(pluginUserOrThrow(branchName, closestBranch, projectKey)) + .setLocalFilePath(fileNameRefOrDefault(branchName, closestBranch, projectKey)) + .setFileRef(filePathRefOrDefault(branchName, closestBranch, projectKey)) + .setAllowMaintainersSubmit(allowMaintainersSubmitOrDefault(branchName, closestBranch, projectKey)) + .setAutoAddReviewers(autoAddReviewersOrDefault(branchName, closestBranch, projectKey)) + .setAutoSubmit(autoSubmitOrDefault(branchName, closestBranch, projectKey)) + .setDislikeWarnings(dislikeWarningsOrDefault(branchName, closestBranch, projectKey)) + .setBranch(projectSpecificPluginConfig(projectKey).getSubsections(BRANCH_SECTION) .stream() .filter(subSection -> subSection.equals(branchName)) .findAny() @@ -89,34 +93,43 @@ .createPluginSettings(); } - private Boolean autoAddReviewersOrDefault(final String branch, final String closesBranch) { - return getKey(branch, closesBranch, AUTO_ADD_REVIEWERS, DEFAULT_AUTO_ADD_REVIEWERS, Boolean::valueOf); + private Boolean autoAddReviewersOrDefault(final String branch, final String closesBranch, + final Project.NameKey projectKey) { + return getKey(projectKey, branch, closesBranch, AUTO_ADD_REVIEWERS, DEFAULT_AUTO_ADD_REVIEWERS, + Boolean::valueOf); } - private Boolean autoSubmitOrDefault(final String branch, final String closestBranch) { - return getKey(branch, closestBranch, AUTO_SUBMIT, DEFAULT_AUTO_SUBMIT, Boolean::valueOf); + private Boolean autoSubmitOrDefault(final String branch, final String closestBranch, + final Project.NameKey projectKey) { + return getKey(projectKey, branch, closestBranch, AUTO_SUBMIT, DEFAULT_AUTO_SUBMIT, Boolean::valueOf); } - private Boolean allowMaintainersSubmitOrDefault(final String branch, final String closesBranch) { - return getKey(branch, closesBranch, ALLOW_SUBMIT, DEFAULT_ALLOW_SUBMIT, Boolean::valueOf); + private Boolean allowMaintainersSubmitOrDefault(final String branch, final String closesBranch, + final Project.NameKey projectKey) { + return getKey(projectKey, branch, closesBranch, ALLOW_SUBMIT, DEFAULT_ALLOW_SUBMIT, Boolean::valueOf); } - private Boolean dislikeWarningsOrDefault(final String branch, final String closesBranch){ - return getKey(branch, closesBranch, DISLIKE_WARNINGS, DEFAULT_DISLIKE_WARNINGS, Boolean::valueOf); + private Boolean dislikeWarningsOrDefault(final String branch, final String closesBranch, + final Project.NameKey projectKey) { + return getKey(projectKey, branch, closesBranch, DISLIKE_WARNINGS, DEFAULT_DISLIKE_WARNINGS, Boolean::valueOf); } - private String fileNameRefOrDefault(final String branch, final String closesBranch) { - return getKey(branch, closesBranch, MAINTAINERS_FILE_REF, DEFAULT_MAINTAINERS_FILE_REF, String::valueOf); + private String fileNameRefOrDefault(final String branch, final String closesBranch, + final Project.NameKey projectKey) { + return getKey(projectKey, branch, closesBranch, MAINTAINERS_FILE_REF, DEFAULT_MAINTAINERS_FILE_REF, + String::valueOf); } - private String filePathRefOrDefault(final String branch, final String closesBranch) { - return getKey(branch, closesBranch, MAINTAINERS_FILE_PATH_REF, DEFAULT_MAINTAINERS_FILE_PATH_REF, + private String filePathRefOrDefault(final String branch, final String closesBranch, + final Project.NameKey projectKey) { + return getKey(projectKey, branch, closesBranch, MAINTAINERS_FILE_PATH_REF, DEFAULT_MAINTAINERS_FILE_PATH_REF, String::valueOf); } private String pluginUserOrThrow(final String branch, - final String alternativeBranch) { - final Config config = globalPluginConfig(); + final String alternativeBranch, + final Project.NameKey projectKey) { + final Config config = projectSpecificPluginConfig(projectKey); return Optional.ofNullable(config.getString(BRANCH_SECTION, branch, PLUGIN_USER)) .orElse(Optional.ofNullable(config.getString(BRANCH_SECTION, alternativeBranch, PLUGIN_USER)) .orElseThrow(() -> { @@ -125,27 +138,32 @@ })); } - private <T> T getKey(final String branch, + private <T> T getKey(final Project.NameKey projectKey, + final String branch, final String alternativeBranch, final String subKey, final T defaultValue, final Function<String, T> mapTo) { - return Optional.ofNullable(globalPluginConfig() + return Optional.ofNullable(projectSpecificPluginConfig(projectKey) .getString(BRANCH_SECTION, branch, subKey)) .map(mapTo) .orElse(Optional.ofNullable( - globalPluginConfig().getString(BRANCH_SECTION, alternativeBranch, subKey)) + projectSpecificPluginConfig(projectKey).getString(BRANCH_SECTION, alternativeBranch, subKey)) .map(mapTo) .orElse(defaultValue)); } - private Config globalPluginConfig() { - return cfg.getGlobalPluginConfig(MAINTAINER_PLUGIN); + private Config projectSpecificPluginConfig(final Project.NameKey projectKey) { + try { + return cfg.getProjectPluginConfig(projectKey, MAINTAINER_PLUGIN); + } catch (NoSuchProjectException e) { + throw new IllegalStateException(format("Project %s not found", projectKey)); + } } // match by the number of changes needed to change one String into another - private String closesBranchMatch(final String branchName) { - return globalPluginConfig().getSubsections(BRANCH_SECTION).stream() + private String closesBranchMatch(final String branchName, final Project.NameKey projectKey) { + return projectSpecificPluginConfig(projectKey).getSubsections(BRANCH_SECTION).stream() .reduce((branchOne, branchTwo) -> closestMatch(branchName, branchOne, branchTwo)) // if non use default .orElse(RefNames.REFS_HEADS);