Line data Source code
1 : // Copyright (C) 2020 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.pgm; 16 : 17 : import com.google.gerrit.metrics.DisabledMetricMaker; 18 : import com.google.gerrit.metrics.MetricMaker; 19 : import com.google.gerrit.pgm.init.api.ConsoleUI; 20 : import com.google.gerrit.pgm.util.SiteProgram; 21 : import com.google.gerrit.server.config.GerritServerConfigModule; 22 : import com.google.gerrit.server.config.SitePath; 23 : import com.google.gerrit.server.notedb.DeleteZombieCommentsRefs; 24 : import com.google.gerrit.server.schema.SchemaModule; 25 : import com.google.gerrit.server.securestore.SecureStoreClassName; 26 : import com.google.inject.AbstractModule; 27 : import com.google.inject.Guice; 28 : import com.google.inject.Injector; 29 : import com.google.inject.Module; 30 : import com.google.inject.assistedinject.FactoryModuleBuilder; 31 : import com.google.inject.util.Providers; 32 : import java.io.IOException; 33 : import java.nio.file.Path; 34 : import java.util.ArrayList; 35 : import java.util.List; 36 : import org.kohsuke.args4j.Option; 37 : 38 : /** 39 : * A pgm which can be used to clean zombie draft comments refs More context in 40 : * https://gerrit-review.googlesource.com/c/gerrit/+/246233 41 : * 42 : * <p>The implementation is in {@link DeleteZombieCommentsRefs} 43 : */ 44 0 : public class DeleteZombieDrafts extends SiteProgram { 45 0 : @Option( 46 : name = "--cleanup-percentage", 47 : aliases = {"-c"}, 48 : usage = "Clean a % of zombie drafts (default is 100%)") 49 0 : private Integer cleanupPercentage = 100; 50 : 51 : @Override 52 : public int run() throws IOException { 53 0 : mustHaveValidSite(); 54 0 : Injector sysInjector = getSysInjector(); 55 0 : DeleteZombieCommentsRefs cleanup = 56 0 : sysInjector.getInstance(DeleteZombieCommentsRefs.Factory.class).create(cleanupPercentage); 57 0 : cleanup.execute(); 58 0 : return 0; 59 : } 60 : 61 : private Injector getSysInjector() { 62 0 : List<Module> modules = new ArrayList<>(); 63 0 : modules.add( 64 0 : new AbstractModule() { 65 : @Override 66 : protected void configure() { 67 0 : bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath()); 68 0 : bind(ConsoleUI.class).toInstance(ConsoleUI.getInstance(false)); 69 0 : bind(String.class) 70 0 : .annotatedWith(SecureStoreClassName.class) 71 0 : .toProvider(Providers.of(getConfiguredSecureStoreClass())); 72 0 : bind(MetricMaker.class).to(DisabledMetricMaker.class); 73 0 : install(new FactoryModuleBuilder().build(DeleteZombieCommentsRefs.Factory.class)); 74 0 : } 75 : }); 76 0 : modules.add(new GerritServerConfigModule()); 77 0 : modules.add(new SchemaModule()); 78 0 : return Guice.createInjector(modules); 79 : } 80 : }