Commits

Robert Massaioli [Atlassian] committed 6ee984f

Notifications now work as expected.

The plugin is officially a MVP. I am really really happy with this. I actually completed my ShipIt
project!

Woo hoo!

Comments (0)

Files changed (4)

src/main/java/com/atlassian/jira/extension/scheduled/PingNotificationJob.java

 package com.atlassian.jira.extension.scheduled;
 
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import com.atlassian.jira.issue.Issue;
-import com.atlassian.jira.issue.IssueManager;
-import com.atlassian.sal.api.ApplicationProperties;
-import org.joda.time.DateTime;
-import org.joda.time.format.ISODateTimeFormat;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import com.atlassian.crowd.embedded.api.User;
 import com.atlassian.jira.extension.PingService;
 import com.atlassian.jira.extension.persistence.PingEntity;
+import com.atlassian.jira.issue.Issue;
+import com.atlassian.jira.issue.IssueManager;
 import com.atlassian.jira.mail.MailService;
 import com.atlassian.jira.notification.NotificationRecipient;
 import com.atlassian.jira.user.util.UserManager;
+import com.atlassian.sal.api.ApplicationProperties;
 import com.atlassian.sal.api.scheduling.PluginJob;
+import org.joda.time.DateTime;
+import org.joda.time.format.DateTimeFormat;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static com.google.common.base.Preconditions.checkNotNull;
 
 public class PingNotificationJob implements PluginJob {
 	private static final Logger log = LoggerFactory.getLogger(PingNotificationJob.class);
 	
 	public void execute(Map<String, Object> jobObjects) {
 		Date currentTime = new Date();
-		final PingService pingService = (PingService) jobObjects.get(PingSchedulerImpl.PING_SERVICE);
-		final List<PingEntity> duePings = pingService.getPingsDueBefore(currentTime);
-		
-		final MailService mailService = (MailService) jobObjects.get(PingSchedulerImpl.MAIL_SERVICE);
-		final UserManager userManager = (UserManager) jobObjects.get(PingSchedulerImpl.USER_MANAGER);
-        final IssueManager issueManager = (IssueManager) jobObjects.get(PingSchedulerImpl.ISSUE_MANAGER);
-        final ApplicationProperties applicationProperties = (ApplicationProperties) jobObjects.get(PingSchedulerImpl.APPLICATION_PROPERTIES);
+		final PingService pingService = checkNotNull((PingService) jobObjects.get(PingSchedulerImpl.PING_SERVICE));
+		final MailService mailService = checkNotNull((MailService) jobObjects.get(PingSchedulerImpl.MAIL_SERVICE));
+		final UserManager userManager = checkNotNull((UserManager) jobObjects.get(PingSchedulerImpl.USER_MANAGER));
+        final IssueManager issueManager = checkNotNull((IssueManager) jobObjects.get(PingSchedulerImpl.ISSUE_MANAGER));
+        final ApplicationProperties applicationProperties = checkNotNull((ApplicationProperties) jobObjects.get(PingSchedulerImpl.APPLICATION_PROPERTIES));
+
 		log.info("Running the Ping Notification Job...");
+        final List<PingEntity> duePings = pingService.getPingsDueBefore(currentTime);
 		for(PingEntity ping : duePings) {
 			log.info("Sending notification for: " + ping.getIssueId());
             final Map<String, Object> velocityContext = new HashMap<String, Object>();
             final Issue issue = issueManager.getIssueObject(Long.valueOf(ping.getIssueId()));
 			final NotificationRecipient recipient = new NotificationRecipient(toAndFromUser);
 
-            final DateTime created = new DateTime(ping.getCreated());
-
+            // Create the context for all of the users.
             velocityContext.put("user", toAndFromUser);
             velocityContext.put("issue", issue);
             velocityContext.put("ping", ping);
             velocityContext.put("baseURL", applicationProperties.getBaseUrl());
-            velocityContext.put("prettyCreatedDate", created.toString(ISODateTimeFormat.dateTime()));
+            velocityContext.put("prettyCreatedDate", new DateTime(ping.getCreated()).toString(DateTimeFormat.fullDate()));
 			
 			final String subjectTemplate = "templates/email/subject/ping-notification.vm";
 			final String bodyTemplate = "templates/email/" + recipient.getFormat() + "/ping-notification.vm";
-			// TODO finish of the last of this...
+
             if(toAndFromUser != null && issue != null) {
 			    mailService.sendRenderedMail(toAndFromUser, recipient, subjectTemplate, bodyTemplate, velocityContext);
+            } else {
+                log.warn("Could not find the user {} or the issue {} so we did not mail the ping {}. But the ping was removed.",
+                        new Object[] {
+                                ping.getUsername(),
+                                ping.getIssueId(),
+                                ping.getID()
+                        });
             }
-			// TODO send an email notification here
 			pingService.removePing(ping.getID());
 		}
 		log.info("Finished the Ping Notification Job...");

src/main/java/com/atlassian/jira/extension/scheduled/PingSchedulerImpl.java

 package com.atlassian.jira.extension.scheduled;
 
-import java.util.Date;
-import java.util.HashMap;
-import java.util.concurrent.TimeUnit;
-
-import com.atlassian.jira.issue.IssueManager;
-import com.atlassian.sal.api.ApplicationProperties;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 import com.atlassian.jira.extension.PingService;
+import com.atlassian.jira.issue.IssueManager;
 import com.atlassian.jira.mail.MailService;
 import com.atlassian.jira.user.util.UserManager;
+import com.atlassian.sal.api.ApplicationProperties;
 import com.atlassian.sal.api.lifecycle.LifecycleAware;
 import com.atlassian.sal.api.message.I18nResolver;
 import com.atlassian.sal.api.scheduling.PluginScheduler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.concurrent.TimeUnit;
 
 public class PingSchedulerImpl implements PingScheduler, LifecycleAware {
 	private static final Logger log = LoggerFactory.getLogger(PingSchedulerImpl.class);

src/main/resources/templates/email/html/ping-notification.vm

 Hi ${user.displayName},
 
 <p>
-	On the ${prettyCreatedDate} you set a ping on <strong><a href="${baseURL}/browse/${issue.key}">${issue.key}</a></strong>.
+	On ${prettyCreatedDate} you set a ping on <strong><a href="${baseURL}/browse/${issue.key}">${issue.key}</a></strong>.
 </p>
 
 <ul>
 </p>
 
 <p>
-Good luck,
+Good luck,<br />
 Your friendly PingBot
 </p>
 

src/main/resources/templates/email/text/ping-notification.vm

 Hi ${user.displayName},
 
-On the ${prettyCreatedDate} you set a ping on ${issue.key}.
+On ${prettyCreatedDate} you set a ping on ${issue.key}.
 
 Issue Summary: ${issue.summary}
 Issue Link: ${baseURL}/browse/${issue.key}