Line data Source code
1 : // Copyright (C) 2012 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.extensions.common.TestSubmitRuleInput; 18 : import com.google.gerrit.extensions.common.TestSubmitRuleInput.Filters; 19 : import com.google.gerrit.extensions.restapi.IdString; 20 : import com.google.gerrit.extensions.restapi.RestModifyView; 21 : import com.google.gerrit.extensions.restapi.TopLevelResource; 22 : import com.google.gerrit.json.OutputFormat; 23 : import com.google.gerrit.server.change.RevisionResource; 24 : import com.google.gerrit.server.restapi.change.ChangesCollection; 25 : import com.google.gerrit.server.restapi.change.Revisions; 26 : import com.google.gerrit.sshd.SshCommand; 27 : import com.google.inject.Inject; 28 : import java.nio.ByteBuffer; 29 : import org.eclipse.jgit.util.IO; 30 : import org.eclipse.jgit.util.RawParseUtils; 31 : import org.kohsuke.args4j.Argument; 32 : import org.kohsuke.args4j.Option; 33 : 34 1 : abstract class BaseTestPrologCommand extends SshCommand { 35 1 : private TestSubmitRuleInput input = new TestSubmitRuleInput(); 36 : 37 : @Inject private ChangesCollection changes; 38 : 39 : @Inject private Revisions revisions; 40 : 41 : @Argument(index = 0, required = true, usage = "ChangeId to load in prolog environment") 42 : protected String changeId; 43 : 44 : @Option( 45 : name = "-s", 46 : usage = 47 : "Read prolog script from stdin instead of reading rules.pl from the refs/meta/config branch") 48 : protected boolean useStdin; 49 : 50 : @Option( 51 : name = "--no-filters", 52 : aliases = {"-n"}, 53 : usage = "Don't run the submit_filter/2 from the parent projects") 54 : void setNoFilters(boolean no) { 55 0 : input.filters = no ? Filters.SKIP : Filters.RUN; 56 0 : } 57 : 58 : protected abstract RestModifyView<RevisionResource, TestSubmitRuleInput> createView(); 59 : 60 : @Override 61 : protected final void run() throws UnloggedFailure { 62 0 : enableGracefulStop(); 63 : try { 64 0 : RevisionResource revision = 65 0 : revisions.parse( 66 0 : changes.parse(TopLevelResource.INSTANCE, IdString.fromUrl(changeId)), 67 0 : IdString.fromUrl("current")); 68 0 : if (useStdin) { 69 0 : ByteBuffer buf = IO.readWholeStream(in, 4096); 70 0 : input.rule = RawParseUtils.decode(buf.array(), buf.arrayOffset(), buf.limit()); 71 : } 72 0 : Object result = createView().apply(revision, input); 73 0 : OutputFormat.JSON.newGson().toJson(result, stdout); 74 0 : stdout.print('\n'); 75 0 : } catch (Exception e) { 76 0 : throw die("Processing of prolog script failed: " + e); 77 0 : } 78 0 : } 79 : }