From cd27b335cdcf5849c3d9ca444ad57376e7c8aeed Mon Sep 17 00:00:00 2001 From: Brett Ryan Date: Mon, 21 Mar 2016 12:17:18 +1100 Subject: [PATCH] Fix atlassian API deleting the current version. It turns out that AttachmentManager.getPreviousVersions can actually return the current version. I'm looking into finding out what conditions cause this but have for the moment put in protection to stop us deleting the current version. --- .../plugins/attachments/PurgeAttachmentsJob.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/drunkendev/confluence/plugins/attachments/PurgeAttachmentsJob.java b/src/main/java/com/drunkendev/confluence/plugins/attachments/PurgeAttachmentsJob.java index e978c70..94eb712 100644 --- a/src/main/java/com/drunkendev/confluence/plugins/attachments/PurgeAttachmentsJob.java +++ b/src/main/java/com/drunkendev/confluence/plugins/attachments/PurgeAttachmentsJob.java @@ -30,6 +30,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.quartz.JobExecutionContext; @@ -43,6 +44,7 @@ import static java.util.Comparator.nullsFirst; import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toMap; +import static java.util.stream.Collectors.toSet; import static org.apache.commons.lang.StringUtils.isNotBlank; import static org.apache.commons.lang3.StringUtils.repeat; @@ -163,7 +165,18 @@ public void doExecute(JobExecutionContext jec) throws JobExecutionException { counters[IDX_PRIOR_VERSIONS] += prior.size(); List toDelete = findDeletions(prior, settings); - if (!toDelete.isEmpty()) { + Set badVersions = toDelete.stream() + .filter(n -> n.getVersion() >= attachment.getVersion()) + .map(n -> n.getVersion()) + .collect(toSet()); + if (badVersions.size() > 0) { + LOG.error("Attachment bas invalid prior versions: {}:{} :- {} ({}) :: {}", + attachment.getSpaceKey(), + attachment.getSpace().getName(), + attachment.getDisplayTitle(), + attachment.getVersion(), + badVersions); + } else if (!toDelete.isEmpty()) { boolean canUpdate; if (!settings.isReportOnly() && !systemSettings.isReportOnly()) { canUpdate = systemSettings.getDeleteLimit() == 0 ||