Format times nicely
1 files changed, 38 insertions(+), 0 deletions(-)

M googlechat.groovy
M googlechat.groovy +38 -0
@@ 26,12 26,21 @@ import com.atlassian.jira.event.issue.*
 
 import org.ofbiz.core.entity.GenericValue
 
+import org.joda.time.Seconds;
+import org.joda.time.DateTime;
+import org.joda.time.Duration;
+import org.joda.time.format.PeriodFormatter;
+import org.joda.time.format.PeriodFormatterBuilder;
+import org.joda.time.Interval;
+import org.joda.time.Period;
+
 Long WEBHOOK_CHAT_CUSTOMFIELD_ID = 16520;
 
 def log(msg) {
 	System.out.println(msg);
 }
 
+
 /* Return the URL webhook this issue wants to update, or null if unset. */
 def String getWebhookUrl(Issue issue, Long chatWebhookCFId) {
 	return "https://chat.googleapis.com/v1/spaces/AAAAzndUbkU/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=tXwRXybHGSWBoY5WqzPjlcWDTDdegfAZUAISlH_tVyM%3D";

          
@@ 172,6 181,31 @@ class SimpleChatMessage implements ChatM
 			replaceAll(~'[A-Z]+-[0-9]+', "<${baseUrl}/browse/"+'$0|$0>');
 	}
 
+	/* Format seconds as days, hours and minutes. */
+	public String formatSecs(int secs) {
+		PeriodFormatter formatter = new PeriodFormatterBuilder()
+		     .appendDays()
+		     .appendSuffix("d ")
+		     .appendHours()
+		     .appendSuffix("h ")
+		     .appendMinutes()
+		     .appendSuffix("m ")
+		     .toFormatter();
+		// https://stackoverflow.com/questions/22420335/convert-joda-time-seconds-to-formatted-time-string
+		String formatted = formatter.print(Period.seconds(secs).normalizedStandard());
+		return formatted;
+	}
+
+	/* Given a string allegedly representing seconds, returns a nice representation in days/hours/minutes. */
+	String timeFormat(String secsStr) {
+		//https://stackoverflow.com/questions/1713481/groovy-string-to-int
+		if (secsStr?.isInteger()) {
+			int secs = secsStr as Integer;
+			//log "Parsed ${secsStr} into ${secs} seconds, formatted to ${formatSecs(secs)}";
+			return formatSecs(secs);
+		} else return secsStr;
+	}
+
 	String changeLog() {
 		if (event?.changeLog) {
 			// "changeitems: ${event.changeLog.getRelated('ChildChangeItem').getClass()}";

          
@@ 186,6 220,10 @@ class SimpleChatMessage implements ChatM
 					keyValue(field, oldval);
 				} else if (field == "description") { 
 					keyValue(field, reformat(oldval)); // Don't print the old description, it's too confusing
+				} else if (field == "timespent" || field == "timeestimate" || field == "timeoriginalestimate") { 
+					log "We're timeformatting ${newval}"
+					log "We're timeformatting ${oldval}"
+					keyValue(field, timeFormat(newval) + (oldval ? " (was: ${timeFormat(oldval)})" : ""));
 				} else {
 					keyValue(field, newval + (oldval ? " (was: ${oldval})" : ""));
 				}