use of com.google.gerrit.server.notedb.ChangeNoteUtil.AttentionStatusInNoteDb in project gerrit by GerritCodeReview.
the class CommitRewriter method fixedCommitMessage.
/**
* Fixes commit body case by case, so it does not contain user data. Returns fixed commit message,
* or {@link Optional#empty} if no fixes were applied.
*/
private Optional<String> fixedCommitMessage(RevCommit revCommit, ChangeFixProgress fixProgress) throws ConfigInvalidException {
byte[] raw = revCommit.getRawBuffer();
Charset enc = RawParseUtils.parseEncoding(raw);
Optional<CommitMessageRange> commitMessageRange = ChangeNoteUtil.parseCommitMessageRange(revCommit);
if (!commitMessageRange.isPresent()) {
throw new ConfigInvalidException("Failed to parse commit message " + revCommit.getName());
}
String changeSubject = RawParseUtils.decode(enc, raw, commitMessageRange.get().subjectStart(), commitMessageRange.get().subjectEnd());
Optional<String> fixedChangeMessage = Optional.empty();
String originalChangeMessage = null;
if (commitMessageRange.isPresent() && commitMessageRange.get().hasChangeMessage()) {
originalChangeMessage = RawParseUtils.decode(enc, raw, commitMessageRange.get().changeMessageStart(), commitMessageRange.get().changeMessageEnd() + 1).trim();
}
List<FooterLine> footerLines = revCommit.getFooterLines();
StringBuilder footerLinesBuilder = new StringBuilder();
boolean anyFootersFixed = false;
for (FooterLine fl : footerLines) {
String footerKey = fl.getKey();
String footerValue = fl.getValue();
if (footerKey.equalsIgnoreCase(FOOTER_TAG.getName())) {
fixProgress.tag = footerValue;
} else if (footerKey.equalsIgnoreCase(FOOTER_ASSIGNEE.getName())) {
Account.Id oldAssignee = fixProgress.assigneeId;
FixIdentResult fixedAssignee = null;
if (footerValue.equals("")) {
fixProgress.assigneeId = null;
} else {
fixedAssignee = getFixedIdentString(fixProgress, footerValue);
fixProgress.assigneeId = fixedAssignee.accountId;
}
if (!fixedChangeMessage.isPresent()) {
fixedChangeMessage = fixAssigneeChangeMessage(fixProgress, Optional.ofNullable(oldAssignee), Optional.ofNullable(fixProgress.assigneeId), originalChangeMessage);
}
if (fixedAssignee != null && fixedAssignee.fixedIdentString.isPresent()) {
addFooter(footerLinesBuilder, footerKey, fixedAssignee.fixedIdentString.get());
anyFootersFixed = true;
continue;
}
} else if (Arrays.stream(ReviewerStateInternal.values()).anyMatch(state -> footerKey.equalsIgnoreCase(state.getFooterKey().getName()))) {
if (!fixedChangeMessage.isPresent()) {
fixedChangeMessage = fixReviewerChangeMessage(originalChangeMessage);
}
FixIdentResult fixedReviewer = getFixedIdentString(fixProgress, footerValue);
if (fixedReviewer.fixedIdentString.isPresent()) {
addFooter(footerLinesBuilder, footerKey, fixedReviewer.fixedIdentString.get());
anyFootersFixed = true;
continue;
}
} else if (footerKey.equalsIgnoreCase(FOOTER_REAL_USER.getName())) {
FixIdentResult fixedRealUser = getFixedIdentString(fixProgress, footerValue);
if (fixedRealUser.fixedIdentString.isPresent()) {
addFooter(footerLinesBuilder, footerKey, fixedRealUser.fixedIdentString.get());
anyFootersFixed = true;
continue;
}
} else if (footerKey.equalsIgnoreCase(FOOTER_LABEL.getName())) {
int uuidStart = footerValue.indexOf(", ");
int voterIdentStart = footerValue.indexOf(' ', uuidStart != -1 ? uuidStart + 2 : 0);
FixIdentResult fixedVoter = null;
if (voterIdentStart > 0) {
String originalIdentString = footerValue.substring(voterIdentStart + 1);
fixedVoter = getFixedIdentString(fixProgress, originalIdentString);
}
if (!fixedChangeMessage.isPresent()) {
fixedChangeMessage = fixRemoveVoteChangeMessage(fixProgress, fixedVoter == null ? fixProgress.updateAuthorId : Optional.of(fixedVoter.accountId), originalChangeMessage);
}
if (fixedVoter != null && fixedVoter.fixedIdentString.isPresent()) {
String fixedLabelVote = footerValue.substring(0, voterIdentStart) + " " + fixedVoter.fixedIdentString.get();
addFooter(footerLinesBuilder, footerKey, fixedLabelVote);
anyFootersFixed = true;
continue;
}
} else if (footerKey.equalsIgnoreCase(FOOTER_SUBMITTED_WITH.getName())) {
// Record format:
// Submitted-with: OK
// Submitted-with: OK: Code-Review: User Name <accountId@serverId>
int voterIdentStart = StringUtils.ordinalIndexOf(footerValue, ": ", 2);
if (voterIdentStart >= 0) {
String originalIdentString = footerValue.substring(voterIdentStart + 2);
FixIdentResult fixedVoter = getFixedIdentString(fixProgress, originalIdentString);
if (fixedVoter.fixedIdentString.isPresent()) {
String fixedLabelVote = footerValue.substring(0, voterIdentStart) + ": " + fixedVoter.fixedIdentString.get();
addFooter(footerLinesBuilder, footerKey, fixedLabelVote);
anyFootersFixed = true;
continue;
}
}
} else if (footerKey.equalsIgnoreCase(FOOTER_ATTENTION.getName())) {
AttentionStatusInNoteDb originalAttentionSetUpdate = gson.fromJson(footerValue, AttentionStatusInNoteDb.class);
FixIdentResult fixedAttentionAccount = getFixedIdentString(fixProgress, originalAttentionSetUpdate.personIdent);
Optional<String> fixedReason = fixAttentionSetReason(originalAttentionSetUpdate.reason);
if (fixedAttentionAccount.fixedIdentString.isPresent() || fixedReason.isPresent()) {
AttentionStatusInNoteDb fixedAttentionSetUpdate = new AttentionStatusInNoteDb(fixedAttentionAccount.fixedIdentString.isPresent() ? fixedAttentionAccount.fixedIdentString.get() : originalAttentionSetUpdate.personIdent, originalAttentionSetUpdate.operation, fixedReason.isPresent() ? fixedReason.get() : originalAttentionSetUpdate.reason);
addFooter(footerLinesBuilder, footerKey, gson.toJson(fixedAttentionSetUpdate));
anyFootersFixed = true;
continue;
}
}
addFooter(footerLinesBuilder, footerKey, footerValue);
}
// getPossibleAccountReplacement)
if (!fixedChangeMessage.isPresent()) {
fixedChangeMessage = fixReviewerChangeMessage(originalChangeMessage);
}
if (!fixedChangeMessage.isPresent()) {
fixedChangeMessage = fixRemoveVotesChangeMessage(fixProgress, originalChangeMessage);
}
if (!fixedChangeMessage.isPresent()) {
fixedChangeMessage = fixRemoveVoteChangeMessage(fixProgress, Optional.empty(), originalChangeMessage);
}
if (!fixedChangeMessage.isPresent()) {
fixedChangeMessage = fixAssigneeChangeMessage(fixProgress, Optional.empty(), Optional.empty(), originalChangeMessage);
}
if (!fixedChangeMessage.isPresent()) {
fixedChangeMessage = fixSubmitChangeMessage(originalChangeMessage);
}
if (!fixedChangeMessage.isPresent()) {
fixedChangeMessage = fixDeleteChangeMessageCommitMessage(originalChangeMessage);
}
if (!fixedChangeMessage.isPresent()) {
fixedChangeMessage = fixCodeOwnersOnReviewChangeMessage(fixProgress.updateAuthorId, originalChangeMessage);
}
if (!fixedChangeMessage.isPresent() && Objects.equals(fixProgress.tag, CODE_OWNER_ADD_REVIEWER_TAG)) {
fixedChangeMessage = fixCodeOwnersOnAddReviewerChangeMessage(fixProgress, originalChangeMessage);
}
if (!anyFootersFixed && !fixedChangeMessage.isPresent()) {
return Optional.empty();
}
StringBuilder fixedCommitBuilder = new StringBuilder();
fixedCommitBuilder.append(changeSubject);
fixedCommitBuilder.append("\n\n");
if (commitMessageRange.get().hasChangeMessage()) {
fixedCommitBuilder.append(fixedChangeMessage.orElse(originalChangeMessage));
fixedCommitBuilder.append("\n\n");
}
fixedCommitBuilder.append(footerLinesBuilder);
return Optional.of(fixedCommitBuilder.toString());
}
use of com.google.gerrit.server.notedb.ChangeNoteUtil.AttentionStatusInNoteDb in project gerrit by GerritCodeReview.
the class CommitRewriterTest method fixAttentionFooter.
// TODO(issue-15517): Fix the JdkObsolete issue with Date once JGit's PersonIdent class supports
// Instants
@SuppressWarnings("JdkObsolete")
@Test
public void fixAttentionFooter() throws Exception {
Change c = newChange();
ImmutableList.Builder<ObjectId> commitsToFix = new ImmutableList.Builder<>();
// Only 'reason' fix is required
ChangeUpdate invalidAttentionSetUpdate = newUpdate(c, changeOwner);
invalidAttentionSetUpdate.putReviewer(otherUserId, REVIEWER);
invalidAttentionSetUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(otherUserId, Operation.ADD, String.format("Added by %s using the hovercard menu", otherUser.getName())));
commitsToFix.add(invalidAttentionSetUpdate.commit());
ChangeUpdate invalidMultipleAttentionSetUpdate = newUpdate(c, changeOwner);
invalidMultipleAttentionSetUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(changeOwner.getAccountId(), Operation.ADD, String.format("%s replied on the change", otherUser.getName())));
invalidMultipleAttentionSetUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(otherUserId, Operation.REMOVE, String.format("Removed by %s using the hovercard menu", otherUser.getName())));
commitsToFix.add(invalidMultipleAttentionSetUpdate.commit());
String otherUserIdentToFix = getAccountIdentToFix(otherUser.getAccount());
String changeOwnerIdentToFix = getAccountIdentToFix(changeOwner.getAccount());
commitsToFix.add(writeUpdate(RefNames.changeMetaRef(c.getId()), getChangeUpdateBody(c, /*changeMessage=*/
null, // Only 'person_ident' fix is required
"Attention: " + gson.toJson(new AttentionStatusInNoteDb(otherUserIdentToFix, Operation.ADD, "Added by someone using the hovercard menu")), // Both 'reason' and 'person_ident' fix is required
"Attention: " + gson.toJson(new AttentionStatusInNoteDb(changeOwnerIdentToFix, Operation.REMOVE, String.format("%s replied on the change", otherUser.getName())))), getAuthorIdent(changeOwner.getAccount())));
ChangeUpdate validAttentionSetUpdate = newUpdate(c, changeOwner);
validAttentionSetUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(otherUserId, Operation.REMOVE, "Removed by someone"));
validAttentionSetUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(changeOwner.getAccountId(), Operation.ADD, "Added by someone"));
validAttentionSetUpdate.commit();
ChangeUpdate invalidRemovedByClickUpdate = newUpdate(c, changeOwner);
invalidRemovedByClickUpdate.addToPlannedAttentionSetUpdates(AttentionSetUpdate.createForWrite(changeOwner.getAccountId(), Operation.REMOVE, String.format("Removed by %s by clicking the attention icon", otherUser.getName())));
commitsToFix.add(invalidRemovedByClickUpdate.commit());
Ref metaRefBeforeRewrite = repo.exactRef(RefNames.changeMetaRef(c.getId()));
ImmutableList<RevCommit> commitsBeforeRewrite = logMetaRef(repo, metaRefBeforeRewrite);
ImmutableList<Integer> invalidCommits = commitsToFix.build().stream().map(commit -> commitsBeforeRewrite.indexOf(commit)).collect(toImmutableList());
ChangeNotes notesBeforeRewrite = newNotes(c);
RunOptions options = new RunOptions();
options.dryRun = false;
BackfillResult result = rewriter.backfillProject(project, repo, options);
assertThat(result.fixedRefDiff.keySet()).containsExactly(RefNames.changeMetaRef(c.getId()));
notesBeforeRewrite.getAttentionSetUpdates();
Instant updateTimestamp = serverIdent.getWhen().toInstant();
ImmutableList<AttentionSetUpdate> attentionSetUpdatesBeforeRewrite = ImmutableList.of(AttentionSetUpdate.createFromRead(invalidRemovedByClickUpdate.getWhen(), changeOwner.getAccountId(), Operation.REMOVE, String.format("Removed by %s by clicking the attention icon", otherUser.getName())), AttentionSetUpdate.createFromRead(validAttentionSetUpdate.getWhen(), changeOwner.getAccountId(), Operation.ADD, "Added by someone"), AttentionSetUpdate.createFromRead(validAttentionSetUpdate.getWhen(), otherUserId, Operation.REMOVE, "Removed by someone"), AttentionSetUpdate.createFromRead(updateTimestamp, changeOwner.getAccountId(), Operation.REMOVE, String.format("%s replied on the change", otherUser.getName())), AttentionSetUpdate.createFromRead(updateTimestamp, otherUserId, Operation.ADD, "Added by someone using the hovercard menu"), AttentionSetUpdate.createFromRead(invalidMultipleAttentionSetUpdate.getWhen(), otherUserId, Operation.REMOVE, String.format("Removed by %s using the hovercard menu", otherUser.getName())), AttentionSetUpdate.createFromRead(invalidMultipleAttentionSetUpdate.getWhen(), changeOwner.getAccountId(), Operation.ADD, String.format("%s replied on the change", otherUser.getName())), AttentionSetUpdate.createFromRead(invalidAttentionSetUpdate.getWhen(), otherUserId, Operation.ADD, String.format("Added by %s using the hovercard menu", otherUser.getName())));
ImmutableList<AttentionSetUpdate> attentionSetUpdatesAfterRewrite = ImmutableList.of(AttentionSetUpdate.createFromRead(invalidRemovedByClickUpdate.getWhen(), changeOwner.getAccountId(), Operation.REMOVE, "Removed by someone by clicking the attention icon"), AttentionSetUpdate.createFromRead(validAttentionSetUpdate.getWhen(), changeOwner.getAccountId(), Operation.ADD, "Added by someone"), AttentionSetUpdate.createFromRead(validAttentionSetUpdate.getWhen(), otherUserId, Operation.REMOVE, "Removed by someone"), AttentionSetUpdate.createFromRead(updateTimestamp, changeOwner.getAccountId(), Operation.REMOVE, "Someone replied on the change"), AttentionSetUpdate.createFromRead(updateTimestamp, otherUserId, Operation.ADD, "Added by someone using the hovercard menu"), AttentionSetUpdate.createFromRead(invalidMultipleAttentionSetUpdate.getWhen(), otherUserId, Operation.REMOVE, "Removed by someone using the hovercard menu"), AttentionSetUpdate.createFromRead(invalidMultipleAttentionSetUpdate.getWhen(), changeOwner.getAccountId(), Operation.ADD, "Someone replied on the change"), AttentionSetUpdate.createFromRead(invalidAttentionSetUpdate.getWhen(), otherUserId, Operation.ADD, "Added by someone using the hovercard menu"));
ChangeNotes notesAfterRewrite = newNotes(c);
assertThat(notesBeforeRewrite.getAttentionSetUpdates()).containsExactlyElementsIn(attentionSetUpdatesBeforeRewrite);
assertThat(notesAfterRewrite.getAttentionSetUpdates()).containsExactlyElementsIn(attentionSetUpdatesAfterRewrite);
Ref metaRefAfterRewrite = repo.exactRef(RefNames.changeMetaRef(c.getId()));
assertThat(metaRefAfterRewrite.getObjectId()).isNotEqualTo(metaRefBeforeRewrite.getObjectId());
ImmutableList<RevCommit> commitsAfterRewrite = logMetaRef(repo, metaRefAfterRewrite);
assertValidCommits(commitsBeforeRewrite, commitsAfterRewrite, invalidCommits);
assertFixedCommits(commitsToFix.build(), result, c.getId());
List<String> commitHistoryDiff = commitHistoryDiff(result, c.getId());
assertThat(commitHistoryDiff).hasSize(4);
assertThat(commitHistoryDiff.get(0)).isEqualTo("@@ -8 +8 @@\n" + "-Attention: {\"person_ident\":\"Gerrit User 2 \\u003c2@gerrit\\u003e\",\"operation\":\"ADD\",\"reason\":\"Added by Other Account using the hovercard menu\"}\n" + "+Attention: {\"person_ident\":\"Gerrit User 2 \\u003c2@gerrit\\u003e\",\"operation\":\"ADD\",\"reason\":\"Added by someone using the hovercard menu\"}\n");
assertThat(Arrays.asList(commitHistoryDiff.get(1).split("\n"))).containsExactly("@@ -7,2 +7,2 @@", "-Attention: {\"person_ident\":\"Gerrit User 1 \\u003c1@gerrit\\u003e\",\"operation\":\"ADD\",\"reason\":\"Other Account replied on the change\"}", "-Attention: {\"person_ident\":\"Gerrit User 2 \\u003c2@gerrit\\u003e\",\"operation\":\"REMOVE\",\"reason\":\"Removed by Other Account using the hovercard menu\"}", "+Attention: {\"person_ident\":\"Gerrit User 1 \\u003c1@gerrit\\u003e\",\"operation\":\"ADD\",\"reason\":\"Someone replied on the change\"}", "+Attention: {\"person_ident\":\"Gerrit User 2 \\u003c2@gerrit\\u003e\",\"operation\":\"REMOVE\",\"reason\":\"Removed by someone using the hovercard menu\"}");
assertThat(Arrays.asList(commitHistoryDiff.get(2).split("\n"))).containsExactly("@@ -7,2 +7,2 @@", "-Attention: {\"person_ident\":\"Other Account \\u003c2@gerrit\\u003e\",\"operation\":\"ADD\",\"reason\":\"Added by someone using the hovercard menu\"}", "-Attention: {\"person_ident\":\"Change Owner \\u003c1@gerrit\\u003e\",\"operation\":\"REMOVE\",\"reason\":\"Other Account replied on the change\"}", "+Attention: {\"person_ident\":\"Gerrit User 2 \\u003c2@gerrit\\u003e\",\"operation\":\"ADD\",\"reason\":\"Added by someone using the hovercard menu\"}", "+Attention: {\"person_ident\":\"Gerrit User 1 \\u003c1@gerrit\\u003e\",\"operation\":\"REMOVE\",\"reason\":\"Someone replied on the change\"}");
assertThat(commitHistoryDiff.get(3)).isEqualTo("@@ -7 +7 @@\n" + "-Attention: {\"person_ident\":\"Gerrit User 1 \\u003c1@gerrit\\u003e\",\"operation\":\"REMOVE\",\"reason\":\"Removed by Other Account by clicking the attention icon\"}\n" + "+Attention: {\"person_ident\":\"Gerrit User 1 \\u003c1@gerrit\\u003e\",\"operation\":\"REMOVE\",\"reason\":\"Removed by someone by clicking the attention icon\"}\n");
BackfillResult secondRunResult = rewriter.backfillProject(project, repo, options);
assertThat(secondRunResult.fixedRefDiff.keySet()).isEmpty();
assertThat(secondRunResult.refsFailedToFix).isEmpty();
}
Aggregations