Line data Source code
1 : // Copyright (C) 2017 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.change; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.gerrit.common.Nullable; 19 : import com.google.gerrit.entities.Change; 20 : import com.google.gerrit.entities.PatchSet; 21 : import com.google.gerrit.extensions.common.InputWithMessage; 22 : import com.google.gerrit.extensions.restapi.BadRequestException; 23 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 24 : import com.google.gerrit.server.ChangeMessagesUtil; 25 : import com.google.gerrit.server.ChangeUtil; 26 : import com.google.gerrit.server.PatchSetUtil; 27 : import com.google.gerrit.server.extensions.events.PrivateStateChanged; 28 : import com.google.gerrit.server.notedb.ChangeNotes; 29 : import com.google.gerrit.server.notedb.ChangeUpdate; 30 : import com.google.gerrit.server.update.BatchUpdateOp; 31 : import com.google.gerrit.server.update.ChangeContext; 32 : import com.google.gerrit.server.update.PostUpdateContext; 33 : import com.google.inject.Inject; 34 : import com.google.inject.assistedinject.Assisted; 35 : 36 : public class SetPrivateOp implements BatchUpdateOp { 37 : public interface Factory { 38 : SetPrivateOp create(boolean isPrivate, @Nullable InputWithMessage input); 39 : } 40 : 41 : private final PrivateStateChanged privateStateChanged; 42 : private final PatchSetUtil psUtil; 43 : private final ChangeMessagesUtil cmUtil; 44 : private final boolean isPrivate; 45 : @Nullable private final InputWithMessage input; 46 : 47 : private Change change; 48 : private PatchSet ps; 49 : private boolean isNoOp; 50 : 51 : @Inject 52 : SetPrivateOp( 53 : PrivateStateChanged privateStateChanged, 54 : PatchSetUtil psUtil, 55 : ChangeMessagesUtil cmUtil, 56 : @Assisted boolean isPrivate, 57 59 : @Assisted @Nullable InputWithMessage input) { 58 59 : this.privateStateChanged = privateStateChanged; 59 59 : this.psUtil = psUtil; 60 59 : this.cmUtil = cmUtil; 61 59 : this.isPrivate = isPrivate; 62 59 : this.input = input; 63 59 : } 64 : 65 : @Override 66 : public boolean updateChange(ChangeContext ctx) 67 : throws ResourceConflictException, BadRequestException { 68 59 : change = ctx.getChange(); 69 59 : if (ctx.getChange().isPrivate() == isPrivate) { 70 : // No-op 71 55 : isNoOp = true; 72 55 : return false; 73 : } 74 : 75 16 : if (isPrivate && !change.isNew()) { 76 1 : throw new BadRequestException( 77 1 : String.format("cannot set %s change to private", ChangeUtil.status(change))); 78 : } 79 16 : ChangeNotes notes = ctx.getNotes(); 80 16 : ps = psUtil.get(notes, change.currentPatchSetId()); 81 16 : ChangeUpdate update = ctx.getUpdate(change.currentPatchSetId()); 82 16 : change.setPrivate(isPrivate); 83 16 : change.setLastUpdatedOn(ctx.getWhen()); 84 16 : update.setPrivate(isPrivate); 85 16 : addMessage(ctx); 86 16 : return true; 87 : } 88 : 89 : @Override 90 : public void postUpdate(PostUpdateContext ctx) { 91 59 : if (!isNoOp) { 92 16 : privateStateChanged.fire(ctx.getChangeData(change), ps, ctx.getAccount(), ctx.getWhen()); 93 : } 94 59 : } 95 : 96 : private void addMessage(ChangeContext ctx) { 97 16 : Change c = ctx.getChange(); 98 16 : StringBuilder buf = new StringBuilder(c.isPrivate() ? "Set private" : "Unset private"); 99 : 100 16 : String m = Strings.nullToEmpty(input == null ? null : input.message).trim(); 101 16 : if (!m.isEmpty()) { 102 9 : buf.append("\n\n"); 103 9 : buf.append(m); 104 : } 105 : 106 16 : cmUtil.setChangeMessage( 107 : ctx, 108 16 : buf.toString(), 109 16 : c.isPrivate() ? ChangeMessagesUtil.TAG_SET_PRIVATE : ChangeMessagesUtil.TAG_UNSET_PRIVATE); 110 16 : } 111 : }