Commits

Robert Massaioli [Atlassian] committed d442a4b

CONNECT-105 - Started refactoring the UpgradeTask

I just wanted to make the upgrade task clearer and to make it obvious what each part does. There was
a big chunk concerned with getting the correct UUID custom field that I wanted to make sure was
setup correctly.

  • Participants
  • Parent commits cdfde46
  • Branches CONNECT-105-allow-the-remote-user-to-be-customised

Comments (0)

Files changed (2)

File src/main/java/com/atlassian/jconnect/jira/upgrade/task/UpgradeTask001.java

 import com.atlassian.jconnect.jira.UserHelper;
 import com.atlassian.jconnect.jira.customfields.BuiltInField;
 import com.atlassian.jconnect.jira.customfields.CustomFieldHelper;
+import com.atlassian.jconnect.util.Either;
 import com.atlassian.jira.bc.issue.search.SearchService;
 import com.atlassian.jira.event.type.EventDispatchOption;
 import com.atlassian.jira.i18n.DefaultMessage;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.Serializable;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedList;
+import java.util.Set;
 
 /**
  * In CONNECT-105 we made it so that you can configure which user is the JMC Remote User. Before this time it was always the
         log.info("Found default remote user to use as default CF value. User Key: " + defaultRemoteUser.getKey());
 
         // Get the list of issues that are created by JMC (maybe all of the issues that have UUID's attatched)
-        final Collection<CustomField> potentialUUIDCFs = customFieldManager.getCustomFieldObjectsByName(UUID_CF_NAME);
-        CustomField uuidCF = null;
-        if(potentialUUIDCFs.size() < 1) {
-            final Message tooFew = new DefaultMessage("upgradetask.001.too-few-uuid-custom-fields");
-            return Collections.singleton(tooFew);
-        } else if (potentialUUIDCFs.size() > 1) {
-            final String uuidSpecification = System.getProperty(SPECIFICATION_UUID_SYSPROP);
-            if(uuidSpecification != null) {
-                boolean correctCFFound = false;
-                for(CustomField potentialField : potentialUUIDCFs) {
-                    if(potentialField.getId().equals(uuidSpecification)) {
-                        uuidCF = potentialField;
-                        correctCFFound = true;
-                        break;
-                    } else {
-                        log.debug("Custom field id {} does not match the value {} in the sysprop {}", new Object[] {potentialField.getId(), uuidSpecification, SPECIFICATION_UUID_SYSPROP} );
-                    }
-                }
-                if(!correctCFFound) {
-                    final Message incorrectSysprop = new DefaultMessage("upgradetask.001.sysprop-did-not-match-uuid");
-                    return Collections.singleton(incorrectSysprop);
-                }
-            } else {
-                final Message tooFew = new DefaultMessage("upgradetask.001.too-many-uuid-custom-fields");
-                return Collections.singleton(tooFew);
-            }
-        } else {
-           uuidCF = potentialUUIDCFs.iterator().next();
+        final Either<Collection<Message>, CustomField> potentialUuidCustomField = getUUIDCustomField();
+        if(potentialUuidCustomField.isLeft()) {
+            return potentialUuidCustomField.getLeft();
         }
-
-        if(uuidCF == null) {
-            // Since we seemed to have failed to get the UUID cf just show the too few error message again.
-            final Message tooFew = new DefaultMessage("upgradetask.001.too-few-uuid-custom-fields");
-            return Collections.singleton(tooFew);
-        }
-
-        log.info("Found UUID custom field and preparing for update.");
+        final CustomField uuidCF = potentialUuidCustomField.getRight();
+        log.info("Found UUID custom field and preparing for update: " + uuidCF.getId());
         final CustomField remoteUserCF = customFieldHelper.initBuiltInCustomField(BuiltInField.JMC_REMOTE_USER);
         log.info("Found (or created) JMC Remote User Custom Field and ready for update.");
         // JQL: uuid is not EMPTY
         final Query uuidIsPresentQuery = JqlQueryBuilder.newBuilder().where().customField(uuidCF.getIdAsLong()).isNot().empty().buildQuery();
 
-        // BATCH the requests in an attempt to not load too many issues in memory at any one time.
+        // BATCH the requests in an attempt to not load too many issues in memory at any one time. Just in case.
         boolean finishedConverting = false;
         int convertedIssues = 0;
         log.info("Converting issues in batches. Issues per Batch: " + BATCH_UPDATE_SIZE);
 
             for(Issue issue : searchResults.getIssues()) {
                 // TODO I do not like this code, I would much prefer that it happened in bulk rather than one issue at a time.
+                // however, if I did it in bulk I would have to search for all of the "JMC Remote User" fields that were not null
+                // but, since JMC Remote User is a new Custom Field that would mean that I would have to do a re-index and that is a
+                // big no-no in an upgrade task.
                 log.debug("Updating issue: " + issue.getKey());
                 final Object remoteUserValue = remoteUserCF.getValue(issue);
                 if(remoteUserValue == null) {
 
         return new LinkedList<Message>();
     }
+
+    private Either<Collection<Message>, CustomField> getUUIDCustomField() {
+        final Collection<CustomField> potentialUUIDCFs = customFieldManager.getCustomFieldObjectsByName(UUID_CF_NAME);
+        CustomField uuidCF = null;
+        final int uuidsFound = potentialUUIDCFs.size();
+        if(uuidsFound < 1) {
+            // We did not even find a UUID custom field, return an error
+            return Either.left(generateSimpleMessage("upgradetask.001.too-few-uuid-custom-fields"));
+        } else if (uuidsFound > 1) {
+            // This is a bit of a rare situation but one that would be horrible to get out of, so I have written in a workaround
+            // that involves running this upgrade task but with a sysprop to specify which of the multiple uuid cf's is the right one.
+            final String uuidSpecification = System.getProperty(SPECIFICATION_UUID_SYSPROP);
+            if(uuidSpecification != null) {
+                for(CustomField potentialField : potentialUUIDCFs) {
+                    if(potentialField.getId().equals(uuidSpecification)) {
+                        // Woo hoo! We found the one that the instance administrator specified
+                        return Either.right(potentialField);
+                    } else {
+                        log.debug("Custom field id {} does not match the value {} in the sysprop {}", new Object[] {potentialField.getId(), uuidSpecification, SPECIFICATION_UUID_SYSPROP} );
+                    }
+                }
+                return Either.left(generateSimpleMessage("upgradetask.001.sysprop-did-not-match-uuid"));
+            } else {
+                log.debug("There were a number of duplicate CustomFields that had the name {}. They are here:");
+                for(CustomField customField : potentialUUIDCFs) {
+                    log.debug(" - {} ( {} )", customField.getId(), customField.getFieldName());
+                }
+                return Either.left(generateSimpleMessage("upgradetask.001.too-many-uuid-custom-fields"));
+            }
+        } else {
+            return Either.right(potentialUUIDCFs.iterator().next());
+        }
+    }
+
+    private Collection<Message> generateSimpleMessage(String i18nKey, Serializable... arguments) {
+        final Message message = new DefaultMessage(i18nKey, arguments);
+        final Collection<Message> messages = Collections.singleton(message);
+        return messages;
+    }
 }

File src/main/resources/i18n.properties

 upgradetask.001.default-remote-user-missing=We could not find the default JIRA Connect Remote User and therefore, could not upgrade everybody to the next stage. Try creating the jiraconnectuser and trying again.
 upgradetask.001.too-few-uuid-custom-fields=Could not find the UUID custom field. Therefore we could not infer which issues were created by JIRA Connect. Try creating the uuid custom field manually and trying again.
 upgradetask.001.too-many-uuid-custom-fields=There was more than one custom field with the name UUID. We did not anticipate this and we cannot use UUID to ascertain which issues are the JIRA Connect issues. Specify the correct custom field, by id, in the sysprop: jmc.upgradetask001.uuid-custom-field-id
-upgradetask.001.sysprop-did-not-match-uuid=You provided a UUID to specify which uuid field to use for the JMC upgrade task but I could find no custom field that matched that id. Turn on debug logging to see what I did find and use the correct one from there.
+upgradetask.001.sysprop-did-not-match-uuid=You provided a UUID to specify which uuid field to use for the JMC upgrade task but we could find no custom field that matched that id. Turn on debug logging to see what we did find and use the correct one from there.