Line data Source code
1 : // Copyright (C) 2021 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.restapi.project; 16 : 17 : import com.google.gerrit.extensions.restapi.AuthException; 18 : import com.google.gerrit.extensions.restapi.BadRequestException; 19 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 20 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 21 : import com.google.gerrit.extensions.restapi.Response; 22 : import com.google.gerrit.extensions.restapi.RestReadView; 23 : import com.google.gerrit.server.change.IncludedInRefs; 24 : import com.google.gerrit.server.permissions.PermissionBackendException; 25 : import com.google.gerrit.server.project.ProjectResource; 26 : import com.google.inject.Inject; 27 : import java.io.IOException; 28 : import java.util.Collection; 29 : import java.util.HashSet; 30 : import java.util.Map; 31 : import java.util.Set; 32 : import org.kohsuke.args4j.Option; 33 : 34 : public class CommitsIncludedInRefs implements RestReadView<ProjectResource> { 35 : protected final IncludedInRefs includedInRefs; 36 : 37 143 : protected Set<String> commits = new HashSet<>(); 38 143 : protected Set<String> refs = new HashSet<>(); 39 : 40 : @Option( 41 : name = "--commit", 42 : aliases = {"-c"}, 43 : required = true, 44 : metaVar = "COMMIT", 45 : usage = "commit sha1") 46 : protected void addCommit(String commit) { 47 0 : commits.add(commit); 48 0 : } 49 : 50 : @Option( 51 : name = "--ref", 52 : aliases = {"-r"}, 53 : required = true, 54 : metaVar = "REF", 55 : usage = "full ref name") 56 : protected void addRef(String ref) { 57 0 : refs.add(ref); 58 0 : } 59 : 60 : public void addCommits(Collection<String> commits) { 61 1 : this.commits.addAll(commits); 62 1 : } 63 : 64 : public void addRefs(Collection<String> refs) { 65 1 : this.refs.addAll(refs); 66 1 : } 67 : 68 : @Inject 69 143 : public CommitsIncludedInRefs(IncludedInRefs includedInRefs) { 70 143 : this.includedInRefs = includedInRefs; 71 143 : } 72 : 73 : @Override 74 : public Response<Map<String, Set<String>>> apply(ProjectResource resource) 75 : throws ResourceConflictException, BadRequestException, IOException, 76 : PermissionBackendException, ResourceNotFoundException, AuthException { 77 1 : if (commits.isEmpty()) { 78 1 : throw new BadRequestException("commit is required"); 79 : } 80 1 : if (refs.isEmpty()) { 81 1 : throw new BadRequestException("ref is required"); 82 : } 83 1 : return Response.ok(includedInRefs.apply(resource.getNameKey(), commits, refs)); 84 : } 85 : }