From 469bc22936c4cc6b0bab492449140dfa894c3c08 Mon Sep 17 00:00:00 2001 From: Dimitris Stafylarakis Date: Fri, 8 Aug 2014 15:04:01 +0200 Subject: [PATCH 1/2] support multiple date formats --- .../gitlabjenkins/GitLabMergeRequest.java | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/dabsquared/gitlabjenkins/GitLabMergeRequest.java b/src/main/java/com/dabsquared/gitlabjenkins/GitLabMergeRequest.java index 90532f5..857dbac 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/GitLabMergeRequest.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/GitLabMergeRequest.java @@ -1,7 +1,12 @@ package com.dabsquared.gitlabjenkins; import java.io.IOException; +import java.lang.reflect.Type; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; +import java.util.Locale; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; @@ -10,6 +15,10 @@ import org.gitlab.api.models.GitlabProject; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; /** * Represents for WebHook payload @@ -23,13 +32,36 @@ public class GitLabMergeRequest { throw new IllegalArgumentException("payload should not be null"); } - Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setDateFormat("yyyy-MM-dd HH:mm:ss Z").create(); + Gson gson = new GsonBuilder() + .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) + .registerTypeAdapter(Date.class, new GitLabMergeRequest.DateSerializer()) + .create(); GitLabMergeRequest mergeRequest = gson.fromJson(payload, GitLabMergeRequest.class); return mergeRequest; } + + private static final String[] DATE_FORMATS = new String[] { + "yyyy-MM-dd HH:mm:ss Z", + "yyyy-MM-dd'T'HH:mm:ss.SSSZ" + }; + private static class DateSerializer implements JsonDeserializer { + public Date deserialize(JsonElement jsonElement, Type typeOF, + JsonDeserializationContext context) throws JsonParseException { + for (String format : DATE_FORMATS) { + try { + return new SimpleDateFormat(format, Locale.US).parse(jsonElement.getAsString()); + } catch (ParseException e) { + } + } + throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString() + + "\". Supported formats: " + Arrays.toString(DATE_FORMATS)); + } + } + + public GitLabMergeRequest() { } From f488686c4b709449e7ebed8ca5e9f353bc69e8a9 Mon Sep 17 00:00:00 2001 From: Dimitris Stafylarakis Date: Fri, 8 Aug 2014 15:16:34 +0200 Subject: [PATCH 2/2] fix issue #9 --- .../java/com/dabsquared/gitlabjenkins/GitLabMergeRequest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/dabsquared/gitlabjenkins/GitLabMergeRequest.java b/src/main/java/com/dabsquared/gitlabjenkins/GitLabMergeRequest.java index 857dbac..f63a51f 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/GitLabMergeRequest.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/GitLabMergeRequest.java @@ -43,7 +43,7 @@ public class GitLabMergeRequest { private static final String[] DATE_FORMATS = new String[] { "yyyy-MM-dd HH:mm:ss Z", - "yyyy-MM-dd'T'HH:mm:ss.SSSZ" + "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" };