Line data Source code
1 : // Copyright (C) 2015 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.config; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.common.flogger.FluentLogger; 19 : import com.google.gerrit.extensions.registration.DynamicItem; 20 : import com.google.gerrit.server.change.MergeabilityComputationBehavior; 21 : import com.google.gerrit.server.config.ScheduleConfig.Schedule; 22 : import com.google.inject.Inject; 23 : import com.google.inject.Singleton; 24 : import java.util.Optional; 25 : import java.util.concurrent.TimeUnit; 26 : import org.eclipse.jgit.lib.Config; 27 : 28 : @Singleton 29 : public class ChangeCleanupConfig { 30 138 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 31 : 32 : private static final String SECTION = "changeCleanup"; 33 : private static final String KEY_ABANDON_AFTER = "abandonAfter"; 34 : private static final String KEY_ABANDON_IF_MERGEABLE = "abandonIfMergeable"; 35 : private static final String KEY_ABANDON_MESSAGE = "abandonMessage"; 36 : private static final String KEY_CLEANUP_ACCOUNT_PATCH_REVIEW = "cleanupAccountPatchReview"; 37 : private static final String DEFAULT_ABANDON_MESSAGE = 38 : "Auto-Abandoned due to inactivity, see " 39 : + "${URL}\n" 40 : + "\n" 41 : + "If this change is still wanted it should be restored."; 42 : 43 : private final DynamicItem<UrlFormatter> urlFormatter; 44 : private final Optional<Schedule> schedule; 45 : private final long abandonAfter; 46 : private final boolean abandonIfMergeable; 47 : private final boolean cleanupAccountPatchReview; 48 : private final String abandonMessage; 49 : 50 : @Inject 51 138 : ChangeCleanupConfig(@GerritServerConfig Config cfg, DynamicItem<UrlFormatter> urlFormatter) { 52 138 : this.urlFormatter = urlFormatter; 53 138 : schedule = ScheduleConfig.createSchedule(cfg, SECTION); 54 138 : abandonAfter = readAbandonAfter(cfg); 55 138 : boolean indexMergeable = MergeabilityComputationBehavior.fromConfig(cfg).includeInIndex(); 56 138 : if (!indexMergeable) { 57 138 : if (!readAbandonIfMergeable(cfg)) { 58 1 : logger.atWarning().log( 59 : "index.change.indexMergeable is disabled; %s.%s=false will be ineffective", 60 : SECTION, KEY_ABANDON_IF_MERGEABLE); 61 : } 62 138 : abandonIfMergeable = true; 63 : } else { 64 10 : abandonIfMergeable = readAbandonIfMergeable(cfg); 65 : } 66 138 : cleanupAccountPatchReview = 67 138 : cfg.getBoolean(SECTION, null, KEY_CLEANUP_ACCOUNT_PATCH_REVIEW, false); 68 138 : abandonMessage = readAbandonMessage(cfg); 69 138 : } 70 : 71 : private boolean readAbandonIfMergeable(Config cfg) { 72 138 : return cfg.getBoolean(SECTION, null, KEY_ABANDON_IF_MERGEABLE, true); 73 : } 74 : 75 : private long readAbandonAfter(Config cfg) { 76 138 : long abandonAfter = 77 138 : ConfigUtil.getTimeUnit(cfg, SECTION, null, KEY_ABANDON_AFTER, 0, TimeUnit.MILLISECONDS); 78 138 : return abandonAfter >= 0 ? abandonAfter : 0; 79 : } 80 : 81 : private String readAbandonMessage(Config cfg) { 82 138 : String abandonMessage = cfg.getString(SECTION, null, KEY_ABANDON_MESSAGE); 83 138 : return Strings.isNullOrEmpty(abandonMessage) ? DEFAULT_ABANDON_MESSAGE : abandonMessage; 84 : } 85 : 86 : public Optional<Schedule> getSchedule() { 87 138 : return schedule; 88 : } 89 : 90 : public long getAbandonAfter() { 91 1 : return abandonAfter; 92 : } 93 : 94 : public boolean getAbandonIfMergeable() { 95 1 : return abandonIfMergeable; 96 : } 97 : 98 : public boolean getCleanupAccountPatchReview() { 99 1 : return cleanupAccountPatchReview; 100 : } 101 : 102 : public String getAbandonMessage() { 103 1 : String docUrl = 104 1 : urlFormatter.get().getDocUrl("user-change-cleanup.html", "auto-abandon").orElse(""); 105 1 : return docUrl.isEmpty() ? abandonMessage : abandonMessage.replace("${URL}", docUrl); 106 : } 107 : }