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.common.base.Strings; 18 : import com.google.gerrit.extensions.api.projects.ConfigInput; 19 : import com.google.gerrit.extensions.client.InheritableBoolean; 20 : import com.google.gerrit.extensions.client.SubmitType; 21 : import com.google.gerrit.extensions.restapi.RestApiException; 22 : import com.google.gerrit.server.permissions.PermissionBackendException; 23 : import com.google.gerrit.server.project.ProjectResource; 24 : import com.google.gerrit.server.project.ProjectState; 25 : import com.google.gerrit.server.restapi.project.PutConfig; 26 : import com.google.gerrit.sshd.CommandMetaData; 27 : import com.google.gerrit.sshd.SshCommand; 28 : import com.google.inject.Inject; 29 : import org.kohsuke.args4j.Argument; 30 : import org.kohsuke.args4j.Option; 31 : 32 : @CommandMetaData(name = "set-project", description = "Change a project's settings") 33 1 : final class SetProjectCommand extends SshCommand { 34 : @Argument(index = 0, required = true, metaVar = "NAME", usage = "name of the project") 35 : private ProjectState projectState; 36 : 37 : @Option( 38 : name = "--description", 39 : aliases = {"-d"}, 40 : metaVar = "DESCRIPTION", 41 : usage = "description of project") 42 : private String projectDescription; 43 : 44 : @Option( 45 : name = "--submit-type", 46 : aliases = {"-t"}, 47 : usage = "project submit type\n(default: MERGE_IF_NECESSARY)") 48 : private SubmitType submitType; 49 : 50 : @Option(name = "--contributor-agreements", usage = "if contributor agreement is required") 51 : private InheritableBoolean contributorAgreements; 52 : 53 : @Option(name = "--signed-off-by", usage = "if signed-off-by is required") 54 : private InheritableBoolean signedOffBy; 55 : 56 : @Option(name = "--content-merge", usage = "allow automatic conflict resolving within files") 57 : private InheritableBoolean contentMerge; 58 : 59 : @Option(name = "--change-id", usage = "if change-id is required") 60 : private InheritableBoolean requireChangeID; 61 : 62 : @Option( 63 : name = "--use-contributor-agreements", 64 : aliases = {"--ca"}, 65 : usage = "if contributor agreement is required") 66 : void setUseContributorArgreements(@SuppressWarnings("unused") boolean on) { 67 0 : contributorAgreements = InheritableBoolean.TRUE; 68 0 : } 69 : 70 : @Option( 71 : name = "--no-contributor-agreements", 72 : aliases = {"--nca"}, 73 : usage = "if contributor agreement is not required") 74 : void setNoContributorArgreements(@SuppressWarnings("unused") boolean on) { 75 0 : contributorAgreements = InheritableBoolean.FALSE; 76 0 : } 77 : 78 : @Option( 79 : name = "--use-signed-off-by", 80 : aliases = {"--so"}, 81 : usage = "if signed-off-by is required") 82 : void setUseSignedOffBy(@SuppressWarnings("unused") boolean on) { 83 0 : signedOffBy = InheritableBoolean.TRUE; 84 0 : } 85 : 86 : @Option( 87 : name = "--no-signed-off-by", 88 : aliases = {"--nso"}, 89 : usage = "if signed-off-by is not required") 90 : void setNoSignedOffBy(@SuppressWarnings("unused") boolean on) { 91 0 : signedOffBy = InheritableBoolean.FALSE; 92 0 : } 93 : 94 : @Option(name = "--use-content-merge", usage = "allow automatic conflict resolving within files") 95 : void setUseContentMerge(@SuppressWarnings("unused") boolean on) { 96 0 : contentMerge = InheritableBoolean.TRUE; 97 0 : } 98 : 99 : @Option( 100 : name = "--no-content-merge", 101 : usage = "don't allow automatic conflict resolving within files") 102 : void setNoContentMerge(@SuppressWarnings("unused") boolean on) { 103 0 : contentMerge = InheritableBoolean.FALSE; 104 0 : } 105 : 106 : @Option( 107 : name = "--require-change-id", 108 : aliases = {"--id"}, 109 : usage = "if change-id is required") 110 : void setRequireChangeId(@SuppressWarnings("unused") boolean on) { 111 0 : requireChangeID = InheritableBoolean.TRUE; 112 0 : } 113 : 114 : @Option( 115 : name = "--no-change-id", 116 : aliases = {"--nid"}, 117 : usage = "if change-id is not required") 118 : void setNoChangeId(@SuppressWarnings("unused") boolean on) { 119 0 : requireChangeID = InheritableBoolean.FALSE; 120 0 : } 121 : 122 : @Option( 123 : name = "--project-state", 124 : aliases = {"--ps"}, 125 : usage = "project's visibility state") 126 : private com.google.gerrit.extensions.client.ProjectState state; 127 : 128 : @Option(name = "--max-object-size-limit", usage = "max Git object size for this project") 129 : private String maxObjectSizeLimit; 130 : 131 : @Inject private PutConfig putConfig; 132 : 133 : @Override 134 : protected void run() throws Failure { 135 0 : enableGracefulStop(); 136 0 : ConfigInput configInput = new ConfigInput(); 137 0 : configInput.requireChangeId = requireChangeID; 138 0 : configInput.submitType = submitType; 139 0 : configInput.useContentMerge = contentMerge; 140 0 : configInput.useContributorAgreements = contributorAgreements; 141 0 : configInput.useSignedOffBy = signedOffBy; 142 0 : configInput.state = state; 143 0 : configInput.maxObjectSizeLimit = maxObjectSizeLimit; 144 : // Description is different to other parameters, null won't result in 145 : // keeping the existing description, it would delete it. 146 0 : if (Strings.emptyToNull(projectDescription) != null) { 147 0 : configInput.description = projectDescription; 148 : } else { 149 0 : configInput.description = projectState.getProject().getDescription(); 150 : } 151 : 152 : try { 153 0 : putConfig.apply(new ProjectResource(projectState, user), configInput); 154 0 : } catch (RestApiException | PermissionBackendException e) { 155 0 : throw die(e); 156 0 : } 157 0 : } 158 : }