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.common.collect.ImmutableList; 19 : import com.google.gerrit.common.Nullable; 20 : import com.google.gerrit.entities.Change; 21 : import com.google.gerrit.entities.PatchSet; 22 : import com.google.gerrit.extensions.api.changes.NotifyHandling; 23 : import com.google.gerrit.extensions.common.InputWithMessage; 24 : import com.google.gerrit.server.ChangeMessagesUtil; 25 : import com.google.gerrit.server.PatchSetUtil; 26 : import com.google.gerrit.server.extensions.events.WorkInProgressStateChanged; 27 : import com.google.gerrit.server.notedb.ChangeUpdate; 28 : import com.google.gerrit.server.update.BatchUpdateOp; 29 : import com.google.gerrit.server.update.ChangeContext; 30 : import com.google.gerrit.server.update.PostUpdateContext; 31 : import com.google.inject.Inject; 32 : import com.google.inject.assistedinject.Assisted; 33 : import org.eclipse.jgit.lib.ObjectId; 34 : 35 : /* Set work in progress or ready for review state on a change */ 36 : public class WorkInProgressOp implements BatchUpdateOp { 37 : public static class Input extends InputWithMessage { 38 : @Nullable public NotifyHandling notify; 39 : 40 : public Input() { 41 4 : this(null); 42 4 : } 43 : 44 : public Input(@Nullable String message) { 45 11 : super(message); 46 11 : } 47 : } 48 : 49 : public interface Factory { 50 : WorkInProgressOp create(boolean workInProgress, Input in); 51 : } 52 : 53 : private final ChangeMessagesUtil cmUtil; 54 : private final EmailReviewComments.Factory email; 55 : private final PatchSetUtil psUtil; 56 : private final boolean workInProgress; 57 : private final Input in; 58 : private final WorkInProgressStateChanged stateChanged; 59 : 60 11 : private boolean sendEmail = true; 61 : private ObjectId preUpdateMetaId; 62 : private Change change; 63 : private PatchSet ps; 64 : private String mailMessage; 65 : 66 : @Inject 67 : WorkInProgressOp( 68 : ChangeMessagesUtil cmUtil, 69 : EmailReviewComments.Factory email, 70 : PatchSetUtil psUtil, 71 : WorkInProgressStateChanged stateChanged, 72 : @Assisted boolean workInProgress, 73 11 : @Assisted Input in) { 74 11 : this.cmUtil = cmUtil; 75 11 : this.email = email; 76 11 : this.psUtil = psUtil; 77 11 : this.stateChanged = stateChanged; 78 11 : this.workInProgress = workInProgress; 79 11 : this.in = in; 80 11 : } 81 : 82 : public void suppressEmail() { 83 3 : this.sendEmail = false; 84 3 : } 85 : 86 : @Override 87 : public boolean updateChange(ChangeContext ctx) { 88 11 : preUpdateMetaId = ctx.getNotes().getMetaId(); 89 11 : change = ctx.getChange(); 90 11 : ps = psUtil.get(ctx.getNotes(), change.currentPatchSetId()); 91 11 : ChangeUpdate update = ctx.getUpdate(change.currentPatchSetId()); 92 11 : change.setWorkInProgress(workInProgress); 93 11 : if (!change.hasReviewStarted() && !workInProgress) { 94 6 : change.setReviewStarted(true); 95 : } 96 11 : change.setLastUpdatedOn(ctx.getWhen()); 97 11 : update.setWorkInProgress(workInProgress); 98 11 : addMessage(ctx); 99 11 : return true; 100 : } 101 : 102 : private void addMessage(ChangeContext ctx) { 103 11 : Change c = ctx.getChange(); 104 11 : StringBuilder buf = 105 11 : new StringBuilder(c.isWorkInProgress() ? "Set Work In Progress" : "Set Ready For Review"); 106 : 107 11 : String m = Strings.nullToEmpty(in == null ? null : in.message).trim(); 108 11 : if (!m.isEmpty()) { 109 1 : buf.append("\n\n"); 110 1 : buf.append(m); 111 : } 112 : 113 11 : mailMessage = 114 11 : cmUtil.setChangeMessage( 115 : ctx, 116 11 : buf.toString(), 117 11 : c.isWorkInProgress() 118 11 : ? ChangeMessagesUtil.TAG_SET_WIP 119 8 : : ChangeMessagesUtil.TAG_SET_READY); 120 11 : } 121 : 122 : @Override 123 : public void postUpdate(PostUpdateContext ctx) { 124 11 : stateChanged.fire(ctx.getChangeData(change), ps, ctx.getAccount(), ctx.getWhen()); 125 11 : NotifyResolver.Result notify = ctx.getNotify(change.getId()); 126 11 : if (workInProgress 127 8 : || notify.handling().compareTo(NotifyHandling.OWNER_REVIEWERS) < 0 128 : || !sendEmail) { 129 11 : return; 130 : } 131 8 : email 132 8 : .create( 133 : ctx, 134 : ps, 135 : preUpdateMetaId, 136 : mailMessage, 137 8 : ImmutableList.of(), 138 : mailMessage, 139 8 : ImmutableList.of()) 140 8 : .sendAsync(); 141 8 : } 142 : }