Add details from original tag The original tag was being used as the basis for a branch or commit, which would result in errors when parsing. This stores a reference to the commit the original tag points to, and, when backing up as a tag, it also stores the original tag message. Change-Id: Ie23739630f18e8af0aa7a76c1e4f9b362a5f2d6b
diff --git a/src/main/java/com/googlesource/gerrit/plugins/refprotection/BackupRef.java b/src/main/java/com/googlesource/gerrit/plugins/refprotection/BackupRef.java index 93f0227..af075c9 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/refprotection/BackupRef.java +++ b/src/main/java/com/googlesource/gerrit/plugins/refprotection/BackupRef.java
@@ -34,6 +34,8 @@ import com.google.gerrit.server.project.ProjectResource; import com.google.inject.Inject; +import org.eclipse.jgit.errors.IncorrectObjectTypeException; +import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectInserter; @@ -43,6 +45,7 @@ import org.eclipse.jgit.lib.RefUpdate.Result; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.TagBuilder; +import org.eclipse.jgit.revwalk.RevTag; import org.eclipse.jgit.revwalk.RevWalk; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -93,10 +96,28 @@ update = "Deleted"; } String type = "branch"; + String fullMessage = ""; if (event.refUpdate.refName.startsWith(R_TAGS)) { type = "tag"; + try { + RevTag origTag = + revWalk.parseTag(ObjectId.fromString(event.refUpdate.oldRev)); + SimpleDateFormat format = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy ZZZZ"); + PersonIdent taggerIdent = origTag.getTaggerIdent(); + String tagger = + String.format("Tagger: %s <%s>\nDate: %s", + taggerIdent.getName(), taggerIdent.getEmailAddress(), + format.format(taggerIdent.getWhen())); + fullMessage = "\n\nOriginal tag:\n" + tagger + "\n\n" + origTag.getFullMessage(); + } catch (MissingObjectException e) { + log.warn("Original tag does not exist", e); + } catch (IncorrectObjectTypeException e) { + log.warn("Original tag was not a tag", e); + } catch (IOException e) { + log.warn("Unable to read original tag details", e); + } } - tag.setMessage(update + " " + type + " " + event.refUpdate.refName); + tag.setMessage(update + " " + type + " " + event.refUpdate.refName + fullMessage); tag.setTag(backupRef); ObjectInserter inserter = git.newObjectInserter();