Line data Source code
1 : // Copyright (C) 2016 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.sshd.commands; 16 : 17 : import com.google.gerrit.entities.Change; 18 : import com.google.gerrit.exceptions.StorageException; 19 : import com.google.gerrit.extensions.common.Input; 20 : import com.google.gerrit.server.change.ChangeResource; 21 : import com.google.gerrit.server.permissions.PermissionBackendException; 22 : import com.google.gerrit.server.restapi.change.Index; 23 : import com.google.gerrit.sshd.ChangeArgumentParser; 24 : import com.google.gerrit.sshd.CommandMetaData; 25 : import com.google.gerrit.sshd.SshCommand; 26 : import com.google.inject.Inject; 27 : import java.util.LinkedHashMap; 28 : import java.util.Map; 29 : import org.kohsuke.args4j.Argument; 30 : 31 : @CommandMetaData(name = "changes", description = "Index changes") 32 1 : final class IndexChangesCommand extends SshCommand { 33 : @Inject private Index index; 34 : 35 : @Inject private ChangeArgumentParser changeArgumentParser; 36 : 37 : @Argument( 38 : index = 0, 39 : required = true, 40 : multiValued = true, 41 : metaVar = "CHANGE", 42 : usage = "changes to index") 43 : void addChange(String token) { 44 : try { 45 1 : changeArgumentParser.addChange(token, changes, null, false); 46 0 : } catch (UnloggedFailure | StorageException | PermissionBackendException e) { 47 0 : writeError("warning", e.getMessage()); 48 1 : } 49 1 : } 50 : 51 1 : private Map<Change.Id, ChangeResource> changes = new LinkedHashMap<>(); 52 : 53 : @Override 54 : protected void run() throws UnloggedFailure { 55 1 : enableGracefulStop(); 56 1 : boolean ok = true; 57 1 : for (ChangeResource rsrc : changes.values()) { 58 : try { 59 1 : index.apply(rsrc, new Input()); 60 0 : } catch (Exception e) { 61 0 : ok = false; 62 0 : writeError( 63 0 : "error", String.format("failed to index change %s: %s", rsrc.getId(), e.getMessage())); 64 1 : } 65 1 : } 66 1 : if (!ok) { 67 0 : throw die("failed to index one or more changes"); 68 : } 69 1 : } 70 : }