Add concept of auto generated messages to Gerrit (backend)
This change adds tags with the prefix 'autogenerated:gerrit:xxx' to all non-review messages. Feature: Issue 4595 Change-Id: Icafe4b44be33291883ab0e5e5f452e0d4a92cd0e
This commit is contained in:
		@@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
 | 
			
		||||
import static com.google.common.base.Preconditions.checkState;
 | 
			
		||||
 | 
			
		||||
import com.google.common.annotations.VisibleForTesting;
 | 
			
		||||
import com.google.gerrit.common.Nullable;
 | 
			
		||||
import com.google.gerrit.reviewdb.client.Account;
 | 
			
		||||
import com.google.gerrit.reviewdb.client.ChangeMessage;
 | 
			
		||||
import com.google.gerrit.reviewdb.client.PatchSet;
 | 
			
		||||
@@ -43,22 +44,50 @@ import java.util.Objects;
 | 
			
		||||
 */
 | 
			
		||||
@Singleton
 | 
			
		||||
public class ChangeMessagesUtil {
 | 
			
		||||
  public static ChangeMessage newMessage(BatchUpdate.ChangeContext ctx,
 | 
			
		||||
      String body) throws OrmException {
 | 
			
		||||
  public final static String TAG_ABANDON =
 | 
			
		||||
      "autogenerated:gerrit:abandon";
 | 
			
		||||
  public final static String TAG_CHERRY_PICK_CHANGE =
 | 
			
		||||
      "autogenerated:gerrit:cherryPickChange";
 | 
			
		||||
  public final static String TAG_DELETE_ASSIGNEE =
 | 
			
		||||
      "autogenerated:gerrit:deleteAssignee";
 | 
			
		||||
  public final static String TAG_DELETE_REVIEWER =
 | 
			
		||||
      "autogenerated:gerrit:deleteReviewer";
 | 
			
		||||
  public final static String TAG_DELETE_VOTE =
 | 
			
		||||
      "autogenerated:gerrit:deleteVote";
 | 
			
		||||
  public final static String TAG_MERGED =
 | 
			
		||||
      "autogenerated:gerrit:merged";
 | 
			
		||||
  public final static String TAG_MOVE =
 | 
			
		||||
      "autogenerated:gerrit:move";
 | 
			
		||||
  public final static String TAG_RESTORE =
 | 
			
		||||
      "autogenerated:gerrit:restore";
 | 
			
		||||
  public final static String TAG_REVERT =
 | 
			
		||||
      "autogenerated:gerrit:revert";
 | 
			
		||||
  public final static String TAG_SET_ASSIGNEE =
 | 
			
		||||
      "autogenerated:gerrit:setAssignee";
 | 
			
		||||
  public final static String TAG_SET_HASHTAGS =
 | 
			
		||||
      "autogenerated:gerrit:setHashtag";
 | 
			
		||||
  public final static String TAG_SET_TOPIC =
 | 
			
		||||
      "autogenerated:gerrit:setTopic";
 | 
			
		||||
  public final static String TAG_UPLOADED_PATCH_SET =
 | 
			
		||||
      "autogenerated:gerrit:newPatchSet";
 | 
			
		||||
 | 
			
		||||
  public static ChangeMessage newMessage( BatchUpdate.ChangeContext ctx,
 | 
			
		||||
      String body, @Nullable String tag) throws OrmException {
 | 
			
		||||
    return newMessage(
 | 
			
		||||
        ctx.getDb(), ctx.getChange().currentPatchSetId(),
 | 
			
		||||
        ctx.getUser(), ctx.getWhen(), body);
 | 
			
		||||
        ctx.getUser(), ctx.getWhen(), body, tag);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public static ChangeMessage newMessage(
 | 
			
		||||
      ReviewDb db, PatchSet.Id psId, CurrentUser user, Timestamp when,
 | 
			
		||||
      String body) throws OrmException {
 | 
			
		||||
      String body, @Nullable String tag) throws OrmException {
 | 
			
		||||
    checkNotNull(psId);
 | 
			
		||||
    Account.Id accountId = user.isInternalUser() ? null : user.getAccountId();
 | 
			
		||||
    ChangeMessage m = new ChangeMessage(
 | 
			
		||||
        new ChangeMessage.Key(psId.getParentKey(), ChangeUtil.messageUUID(db)),
 | 
			
		||||
        accountId, when, psId);
 | 
			
		||||
    m.setMessage(body);
 | 
			
		||||
    m.setTag(tag);
 | 
			
		||||
    user.updateRealAccountId(m::setRealAuthor);
 | 
			
		||||
    return m;
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -202,7 +202,8 @@ public class Abandon implements RestModifyView<ChangeResource, AbandonInput>,
 | 
			
		||||
        msg.append(msgTxt.trim());
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      return ChangeMessagesUtil.newMessage(ctx, msg.toString());
 | 
			
		||||
      return ChangeMessagesUtil.newMessage(
 | 
			
		||||
          ctx, msg.toString(), ChangeMessagesUtil.TAG_ABANDON);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 
 | 
			
		||||
@@ -365,7 +365,7 @@ public class ChangeInserter extends BatchUpdate.InsertChangeOp {
 | 
			
		||||
    if (message != null) {
 | 
			
		||||
      changeMessage = ChangeMessagesUtil.newMessage(
 | 
			
		||||
          db, patchSet.getId(), ctx.getUser(), patchSet.getCreatedOn(),
 | 
			
		||||
          message);
 | 
			
		||||
          message, ChangeMessagesUtil.TAG_UPLOADED_PATCH_SET);
 | 
			
		||||
      cmUtil.addChangeMessage(db, update, changeMessage);
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
 
 | 
			
		||||
@@ -290,7 +290,8 @@ public class CherryPickChange {
 | 
			
		||||
          .append(" as commit ")
 | 
			
		||||
          .append(cherryPickCommit.name());
 | 
			
		||||
      ChangeMessage changeMessage = ChangeMessagesUtil.newMessage(
 | 
			
		||||
          ctx.getDb(), psId, ctx.getUser(), ctx.getWhen(), sb.toString());
 | 
			
		||||
          ctx.getDb(), psId, ctx.getUser(), ctx.getWhen(), sb.toString(),
 | 
			
		||||
          ChangeMessagesUtil.TAG_CHERRY_PICK_CHANGE);
 | 
			
		||||
      cmUtil.addChangeMessage(ctx.getDb(), ctx.getUpdate(psId), changeMessage);
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -115,7 +115,8 @@ public class DeleteAssignee implements RestModifyView<ChangeResource, Input> {
 | 
			
		||||
    private void addMessage(BatchUpdate.ChangeContext ctx,
 | 
			
		||||
        ChangeUpdate update, Account deleted) throws OrmException {
 | 
			
		||||
      ChangeMessage cmsg = ChangeMessagesUtil.newMessage(
 | 
			
		||||
          ctx, "Assignee deleted: " + deleted.getName(anonymousCowardName));
 | 
			
		||||
          ctx, "Assignee deleted: " + deleted.getName(anonymousCowardName),
 | 
			
		||||
          ChangeMessagesUtil.TAG_DELETE_ASSIGNEE);
 | 
			
		||||
      cmUtil.addChangeMessage(ctx.getDb(), update, cmsg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -184,7 +184,8 @@ public class DeleteReviewer
 | 
			
		||||
      ChangeUpdate update = ctx.getUpdate(currPs.getId());
 | 
			
		||||
      update.removeReviewer(reviewerId);
 | 
			
		||||
 | 
			
		||||
      changeMessage = ChangeMessagesUtil.newMessage(ctx, msg.toString());
 | 
			
		||||
      changeMessage = ChangeMessagesUtil.newMessage(ctx, msg.toString(),
 | 
			
		||||
          ChangeMessagesUtil.TAG_DELETE_REVIEWER);
 | 
			
		||||
      cmUtil.addChangeMessage(ctx.getDb(), update, changeMessage);
 | 
			
		||||
 | 
			
		||||
      return true;
 | 
			
		||||
 
 | 
			
		||||
@@ -175,7 +175,8 @@ public class DeleteVote
 | 
			
		||||
      msg.append(" by ")
 | 
			
		||||
          .append(userFactory.create(accountId).getNameEmail())
 | 
			
		||||
          .append("\n");
 | 
			
		||||
      changeMessage = ChangeMessagesUtil.newMessage(ctx, msg.toString());
 | 
			
		||||
      changeMessage = ChangeMessagesUtil.newMessage(ctx, msg.toString(),
 | 
			
		||||
          ChangeMessagesUtil.TAG_DELETE_VOTE);
 | 
			
		||||
      cmUtil.addChangeMessage(ctx.getDb(), ctx.getUpdate(psId),
 | 
			
		||||
          changeMessage);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -176,7 +176,8 @@ public class Move implements RestModifyView<ChangeResource, MoveInput> {
 | 
			
		||||
        msgBuf.append(input.message);
 | 
			
		||||
      }
 | 
			
		||||
      ChangeMessage cmsg =
 | 
			
		||||
          ChangeMessagesUtil.newMessage(ctx, msgBuf.toString());
 | 
			
		||||
          ChangeMessagesUtil.newMessage(
 | 
			
		||||
              ctx, msgBuf.toString(), ChangeMessagesUtil.TAG_MOVE);
 | 
			
		||||
      cmUtil.addChangeMessage(ctx.getDb(), update, cmsg);
 | 
			
		||||
 | 
			
		||||
      return true;
 | 
			
		||||
 
 | 
			
		||||
@@ -225,7 +225,8 @@ public class PatchSetInserter extends BatchUpdate.Op {
 | 
			
		||||
 | 
			
		||||
    if (message != null) {
 | 
			
		||||
      changeMessage = ChangeMessagesUtil.newMessage(
 | 
			
		||||
          db, patchSet.getId(), ctx.getUser(), ctx.getWhen(), message);
 | 
			
		||||
          db, patchSet.getId(), ctx.getUser(), ctx.getWhen(), message,
 | 
			
		||||
          ChangeMessagesUtil.TAG_UPLOADED_PATCH_SET);
 | 
			
		||||
      changeMessage.setMessage(message);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -932,8 +932,7 @@ public class PostReview implements RestModifyView<RevisionResource, ReviewInput>
 | 
			
		||||
 | 
			
		||||
      message = ChangeMessagesUtil.newMessage(
 | 
			
		||||
          ctx.getDb(), psId, user, ctx.getWhen(),
 | 
			
		||||
          "Patch Set " + psId.get() + ":" + buf);
 | 
			
		||||
      message.setTag(in.tag);
 | 
			
		||||
          "Patch Set " + psId.get() + ":" + buf, in.tag);
 | 
			
		||||
      cmUtil.addChangeMessage(ctx.getDb(), ctx.getUpdate(psId), message);
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -254,7 +254,8 @@ public class PublishDraftPatchSet implements RestModifyView<RevisionResource, In
 | 
			
		||||
        throws EmailException, OrmException {
 | 
			
		||||
      ChangeMessage msg = ChangeMessagesUtil.newMessage(
 | 
			
		||||
          ctx.getDb(), psId, ctx.getUser(), ctx.getWhen(),
 | 
			
		||||
          "Uploaded patch set " + psId.get() + ".");
 | 
			
		||||
          "Uploaded patch set " + psId.get() + ".",
 | 
			
		||||
          ChangeMessagesUtil.TAG_UPLOADED_PATCH_SET);
 | 
			
		||||
      ReplacePatchSetSender cm =
 | 
			
		||||
          replacePatchSetFactory.create(ctx.getProject(), change.getId());
 | 
			
		||||
      cm.setFrom(ctx.getAccountId());
 | 
			
		||||
 
 | 
			
		||||
@@ -114,7 +114,8 @@ public class PutTopic implements RestModifyView<ChangeResource, Input>,
 | 
			
		||||
      change.setTopic(Strings.emptyToNull(newTopicName));
 | 
			
		||||
      update.setTopic(change.getTopic());
 | 
			
		||||
 | 
			
		||||
      ChangeMessage cmsg = ChangeMessagesUtil.newMessage(ctx, summary);
 | 
			
		||||
      ChangeMessage cmsg = ChangeMessagesUtil.newMessage(ctx, summary,
 | 
			
		||||
          ChangeMessagesUtil.TAG_SET_TOPIC);
 | 
			
		||||
      cmUtil.addChangeMessage(ctx.getDb(), update, cmsg);
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -130,7 +130,8 @@ public class Restore implements RestModifyView<ChangeResource, RestoreInput>,
 | 
			
		||||
        msg.append("\n\n");
 | 
			
		||||
        msg.append(input.message.trim());
 | 
			
		||||
      }
 | 
			
		||||
      return ChangeMessagesUtil.newMessage(ctx, msg.toString());
 | 
			
		||||
      return ChangeMessagesUtil.newMessage(ctx, msg.toString(),
 | 
			
		||||
          ChangeMessagesUtil.TAG_RESTORE);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 
 | 
			
		||||
@@ -274,7 +274,8 @@ public class Revert implements RestModifyView<ChangeResource, RevertInput>,
 | 
			
		||||
      Change change = ctx.getChange();
 | 
			
		||||
      PatchSet.Id patchSetId = change.currentPatchSetId();
 | 
			
		||||
      ChangeMessage changeMessage = ChangeMessagesUtil.newMessage(ctx,
 | 
			
		||||
          "Created a revert of this change as I" + computedChangeId.name());
 | 
			
		||||
          "Created a revert of this change as I" + computedChangeId.name(),
 | 
			
		||||
          ChangeMessagesUtil.TAG_REVERT);
 | 
			
		||||
      cmUtil.addChangeMessage(ctx.getDb(), ctx.getUpdate(patchSetId),
 | 
			
		||||
          changeMessage);
 | 
			
		||||
      return true;
 | 
			
		||||
 
 | 
			
		||||
@@ -131,7 +131,8 @@ public class SetAssigneeOp extends BatchUpdate.Op {
 | 
			
		||||
      msg.append(" to: ");
 | 
			
		||||
      msg.append(newAssignee.getName(anonymousCowardName));
 | 
			
		||||
    }
 | 
			
		||||
    ChangeMessage cmsg = ChangeMessagesUtil.newMessage(ctx, msg.toString());
 | 
			
		||||
    ChangeMessage cmsg = ChangeMessagesUtil.newMessage(ctx, msg.toString(),
 | 
			
		||||
        ChangeMessagesUtil.TAG_SET_ASSIGNEE);
 | 
			
		||||
    cmUtil.addChangeMessage(ctx.getDb(), update, cmsg);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -136,7 +136,8 @@ public class SetHashtagsOp extends BatchUpdate.Op {
 | 
			
		||||
    StringBuilder msg = new StringBuilder();
 | 
			
		||||
    appendHashtagMessage(msg, "added", toAdd);
 | 
			
		||||
    appendHashtagMessage(msg, "removed", toRemove);
 | 
			
		||||
    ChangeMessage cmsg = ChangeMessagesUtil.newMessage(ctx, msg.toString());
 | 
			
		||||
    ChangeMessage cmsg = ChangeMessagesUtil.newMessage(ctx, msg.toString(),
 | 
			
		||||
        ChangeMessagesUtil.TAG_SET_HASHTAGS);
 | 
			
		||||
    cmUtil.addChangeMessage(ctx.getDb(), update, cmsg);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -755,6 +755,7 @@ public class MergeOp implements AutoCloseable {
 | 
			
		||||
              ChangeMessage msg = ChangeMessagesUtil.newMessage(
 | 
			
		||||
                  ctx.getDb(), change.currentPatchSetId(),
 | 
			
		||||
                  internalUserFactory.create(), change.getLastUpdatedOn(),
 | 
			
		||||
                  ChangeMessagesUtil.TAG_MERGED,
 | 
			
		||||
                  "Project was deleted.");
 | 
			
		||||
              cmUtil.addChangeMessage(ctx.getDb(),
 | 
			
		||||
                  ctx.getUpdate(change.currentPatchSetId()), msg);
 | 
			
		||||
 
 | 
			
		||||
@@ -150,7 +150,8 @@ public class MergedByPushOp extends BatchUpdate.Op {
 | 
			
		||||
    }
 | 
			
		||||
    msgBuf.append(".");
 | 
			
		||||
    ChangeMessage msg = ChangeMessagesUtil.newMessage(
 | 
			
		||||
        ctx.getDb(), psId, ctx.getUser(), ctx.getWhen(), msgBuf.toString());
 | 
			
		||||
        ctx.getDb(), psId, ctx.getUser(), ctx.getWhen(), msgBuf.toString(),
 | 
			
		||||
        ChangeMessagesUtil.TAG_MERGED);
 | 
			
		||||
    cmUtil.addChangeMessage(ctx.getDb(), update, msg);
 | 
			
		||||
 | 
			
		||||
    PatchSetApproval submitter = ApprovalsUtil.newApproval(
 | 
			
		||||
 
 | 
			
		||||
@@ -285,7 +285,7 @@ public class ReplaceOp extends BatchUpdate.Op {
 | 
			
		||||
      message.append("\n").append(reviewMessage);
 | 
			
		||||
    }
 | 
			
		||||
    msg = ChangeMessagesUtil.newMessage(ctx.getDb(), patchSetId, ctx.getUser(),
 | 
			
		||||
        ctx.getWhen(), message.toString());
 | 
			
		||||
        ctx.getWhen(), message.toString(), ChangeMessagesUtil.TAG_UPLOADED_PATCH_SET);
 | 
			
		||||
    cmUtil.addChangeMessage(ctx.getDb(), update, msg);
 | 
			
		||||
 | 
			
		||||
    if (mergedByPushOp == null) {
 | 
			
		||||
 
 | 
			
		||||
@@ -449,7 +449,8 @@ abstract class SubmitStrategyOp extends BatchUpdate.Op {
 | 
			
		||||
  private ChangeMessage message(ChangeContext ctx, PatchSet.Id psId,
 | 
			
		||||
      String body) throws OrmException {
 | 
			
		||||
    return ChangeMessagesUtil.newMessage(
 | 
			
		||||
        ctx.getDb(), psId, ctx.getUser(), ctx.getWhen(), body);
 | 
			
		||||
        ctx.getDb(), psId, ctx.getUser(), ctx.getWhen(), body,
 | 
			
		||||
        ChangeMessagesUtil.TAG_MERGED);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private void setMerged(ChangeContext ctx, ChangeMessage msg)
 | 
			
		||||
 
 | 
			
		||||
@@ -104,12 +104,11 @@ class ChangeNotesParser {
 | 
			
		||||
    abstract PatchSet.Id psId();
 | 
			
		||||
    abstract Account.Id accountId();
 | 
			
		||||
    abstract String label();
 | 
			
		||||
    @Nullable abstract String tag();
 | 
			
		||||
 | 
			
		||||
    private static ApprovalKey create(PatchSet.Id psId, Account.Id accountId,
 | 
			
		||||
        String label, @Nullable String tag) {
 | 
			
		||||
        String label) {
 | 
			
		||||
      return new AutoValue_ChangeNotesParser_ApprovalKey(
 | 
			
		||||
          psId, accountId, label, tag);
 | 
			
		||||
          psId, accountId, label);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -740,7 +739,7 @@ class ChangeNotesParser {
 | 
			
		||||
      psa.setRealAccountId(realAccountId);
 | 
			
		||||
    }
 | 
			
		||||
    ApprovalKey k =
 | 
			
		||||
        ApprovalKey.create(psId, effectiveAccountId, l.label(), tag);
 | 
			
		||||
        ApprovalKey.create(psId, effectiveAccountId, l.label());
 | 
			
		||||
    if (!approvals.containsKey(k)) {
 | 
			
		||||
      approvals.put(k, psa);
 | 
			
		||||
    }
 | 
			
		||||
@@ -789,7 +788,7 @@ class ChangeNotesParser {
 | 
			
		||||
    if (!Objects.equals(realAccountId, committerId)) {
 | 
			
		||||
      remove.setRealAccountId(realAccountId);
 | 
			
		||||
    }
 | 
			
		||||
    ApprovalKey k = ApprovalKey.create(psId, effectiveAccountId, label, tag);
 | 
			
		||||
    ApprovalKey k = ApprovalKey.create(psId, effectiveAccountId, label);
 | 
			
		||||
    if (!approvals.containsKey(k)) {
 | 
			
		||||
      approvals.put(k, remove);
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -139,10 +139,8 @@ public class ChangeNotesTest extends AbstractChangeNotesTest {
 | 
			
		||||
 | 
			
		||||
    ImmutableListMultimap<PatchSet.Id, PatchSetApproval> approvals =
 | 
			
		||||
        notes.getApprovals();
 | 
			
		||||
    assertThat(approvals).hasSize(2);
 | 
			
		||||
    assertThat(approvals).hasSize(1);
 | 
			
		||||
    assertThat(approvals.entries().asList().get(0).getValue().getTag())
 | 
			
		||||
        .isEqualTo(tag1);
 | 
			
		||||
    assertThat(approvals.entries().asList().get(1).getValue().getTag())
 | 
			
		||||
        .isEqualTo(tag2);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -86,7 +86,7 @@ limitations under the License.
 | 
			
		||||
        assert.equal(gwtLink.href,
 | 
			
		||||
            'http://' + location.host + '/?polygerrit=0#/c/1/1/testfile.txt@2');
 | 
			
		||||
        done();
 | 
			
		||||
      })
 | 
			
		||||
      });
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    test('sets plugins count', function() {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user