From 5b96965518212a36fb451bad1740154ce98649a4 Mon Sep 17 00:00:00 2001 From: upagge Date: Tue, 3 Mar 2020 00:20:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B1=D0=BE=D1=87=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D1=8F=20=D1=81=20=D0=B4=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D0=B9=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B8=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 10 +++---- .../tsc/bitbucketbot/config/AppConfig.java | 6 ++++ .../domain/entity/PullRequest.java | 7 +++++ .../dto/bitbucket/PullRequestJson.java | 10 +++++++ .../bitbucket/sheet/PullRequestSheetJson.java | 1 + .../scheduler/SchedulerPushMessageSend.java | 11 +++++-- .../com/tsc/bitbucketbot/service/Utils.java | 19 ++++++++++-- .../converter/PullRequestJsonConverter.java | 2 ++ .../utils/LocalDateFromEpochDeserializer.java | 29 +++++++++++++++++++ .../com/tsc/bitbucketbot/utils/Message.java | 6 ++-- .../resources/liquibase/change-set/v1.2.0.xml | 10 +++++++ 11 files changed, 98 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/tsc/bitbucketbot/utils/LocalDateFromEpochDeserializer.java diff --git a/pom.xml b/pom.xml index 3672a76..9056521 100644 --- a/pom.xml +++ b/pom.xml @@ -30,11 +30,11 @@ spring-boot-starter - - com.google.code.gson - gson - 2.8.5 - + + + + + org.springframework.boot diff --git a/src/main/java/com/tsc/bitbucketbot/config/AppConfig.java b/src/main/java/com/tsc/bitbucketbot/config/AppConfig.java index 1dd0ae6..12ed871 100644 --- a/src/main/java/com/tsc/bitbucketbot/config/AppConfig.java +++ b/src/main/java/com/tsc/bitbucketbot/config/AppConfig.java @@ -17,4 +17,10 @@ public class AppConfig { return taskScheduler; } +// @Bean +// public ObjectMapper objectMapper(ObjectMapper objectMapper) { +// objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); +// return objectMapper; +// } + } diff --git a/src/main/java/com/tsc/bitbucketbot/domain/entity/PullRequest.java b/src/main/java/com/tsc/bitbucketbot/domain/entity/PullRequest.java index 613f01d..4462b52 100644 --- a/src/main/java/com/tsc/bitbucketbot/domain/entity/PullRequest.java +++ b/src/main/java/com/tsc/bitbucketbot/domain/entity/PullRequest.java @@ -21,6 +21,7 @@ import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; +import java.time.LocalDate; import java.util.List; /** @@ -70,4 +71,10 @@ public class PullRequest { @Column(name = "status") private PullRequestStatus status; + @Column(name = "create_date") + private LocalDate createDate; + + @Column(name = "update_date") + private LocalDate updateDate; + } diff --git a/src/main/java/com/tsc/bitbucketbot/dto/bitbucket/PullRequestJson.java b/src/main/java/com/tsc/bitbucketbot/dto/bitbucket/PullRequestJson.java index d91e773..91b23cf 100644 --- a/src/main/java/com/tsc/bitbucketbot/dto/bitbucket/PullRequestJson.java +++ b/src/main/java/com/tsc/bitbucketbot/dto/bitbucket/PullRequestJson.java @@ -1,7 +1,10 @@ package com.tsc.bitbucketbot.dto.bitbucket; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.tsc.bitbucketbot.utils.LocalDateFromEpochDeserializer; import lombok.Data; +import java.time.LocalDate; import java.util.List; /** @@ -15,6 +18,13 @@ public class PullRequestJson { private Long id; private Integer version; private PullRequestState state; + + @JsonDeserialize(using = LocalDateFromEpochDeserializer.class) + private LocalDate createdDate; + + @JsonDeserialize(using = LocalDateFromEpochDeserializer.class) + private LocalDate updatedDate; + private String title; private String description; private LinkJson links; diff --git a/src/main/java/com/tsc/bitbucketbot/dto/bitbucket/sheet/PullRequestSheetJson.java b/src/main/java/com/tsc/bitbucketbot/dto/bitbucket/sheet/PullRequestSheetJson.java index 81b1f96..1aba698 100644 --- a/src/main/java/com/tsc/bitbucketbot/dto/bitbucket/sheet/PullRequestSheetJson.java +++ b/src/main/java/com/tsc/bitbucketbot/dto/bitbucket/sheet/PullRequestSheetJson.java @@ -9,4 +9,5 @@ import com.tsc.bitbucketbot.dto.bitbucket.Sheet; * @author upagge [02.02.2020] */ public class PullRequestSheetJson extends Sheet { + } diff --git a/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPushMessageSend.java b/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPushMessageSend.java index 88704bd..ccde063 100644 --- a/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPushMessageSend.java +++ b/src/main/java/com/tsc/bitbucketbot/scheduler/SchedulerPushMessageSend.java @@ -1,6 +1,7 @@ package com.tsc.bitbucketbot.scheduler; -import com.google.gson.Gson; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.tsc.bitbucketbot.config.PushMessageConfig; import com.tsc.bitbucketbot.domain.MessageSend; import com.tsc.bitbucketbot.service.MessageSendService; @@ -30,7 +31,7 @@ public class SchedulerPushMessageSend { private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); private final MessageSendService messageSendService; - private final Gson gson = new Gson(); + private final ObjectMapper objectMapper = new ObjectMapper(); private final PushMessageConfig pushMessageConfig; private OkHttpClient client; @@ -61,7 +62,11 @@ public class SchedulerPushMessageSend { public void sendNewMessage() { List pushMessage = messageSendService.getPushMessage(); if (!pushMessage.isEmpty()) { - sendMessage(gson.toJson(pushMessage)); + try { + sendMessage(objectMapper.writeValueAsString(pushMessage)); + } catch (JsonProcessingException e) { + log.error(e.getMessage()); + } } } diff --git a/src/main/java/com/tsc/bitbucketbot/service/Utils.java b/src/main/java/com/tsc/bitbucketbot/service/Utils.java index 5a89540..24029c9 100644 --- a/src/main/java/com/tsc/bitbucketbot/service/Utils.java +++ b/src/main/java/com/tsc/bitbucketbot/service/Utils.java @@ -1,7 +1,10 @@ package com.tsc.bitbucketbot.service; -import com.google.gson.Gson; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; import lombok.NonNull; +import lombok.extern.slf4j.Slf4j; import java.io.BufferedReader; import java.io.IOException; @@ -16,9 +19,15 @@ import java.util.zip.GZIPInputStream; * * @author upagge [30.01.2020] */ +@Slf4j public class Utils { - private static Gson gson = new Gson(); + private static ObjectMapper objectMapper; + + static { + objectMapper = new ObjectMapper(); + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + } private Utils() { throw new IllegalStateException("Утилитарный класс"); @@ -54,7 +63,11 @@ public class Utils { } if (sb != null) { - return Optional.of(gson.fromJson(sb.toString(), classOfT)); + try { + return Optional.of(objectMapper.readValue(sb.toString(), classOfT)); + } catch (JsonProcessingException e) { + log.error(e.getMessage()); + } } return Optional.empty(); } diff --git a/src/main/java/com/tsc/bitbucketbot/service/converter/PullRequestJsonConverter.java b/src/main/java/com/tsc/bitbucketbot/service/converter/PullRequestJsonConverter.java index 19f9bea..eebc845 100644 --- a/src/main/java/com/tsc/bitbucketbot/service/converter/PullRequestJsonConverter.java +++ b/src/main/java/com/tsc/bitbucketbot/service/converter/PullRequestJsonConverter.java @@ -35,6 +35,8 @@ public class PullRequestJsonConverter implements Converter { + + @Override + public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt) { + try { + Long time = jp.readValueAs(Long.class); + Instant instant = Instant.ofEpochMilli(time); + LocalDate localDate = instant.atZone(ZoneId.systemDefault()).toLocalDate(); + return localDate; + } catch (IOException e) { + log.error(e.getMessage()); + } + return LocalDate.now(); + } + +} diff --git a/src/main/java/com/tsc/bitbucketbot/utils/Message.java b/src/main/java/com/tsc/bitbucketbot/utils/Message.java index 12bbf39..09b3c1b 100644 --- a/src/main/java/com/tsc/bitbucketbot/utils/Message.java +++ b/src/main/java/com/tsc/bitbucketbot/utils/Message.java @@ -24,17 +24,19 @@ public class Message { private static final String TWO_BREAK = "\n\n"; private static final String SMILE_AUTHOR = "\uD83D\uDC68\u200D\uD83D\uDCBB️"; private static final String SMILE_PEN = "✏️"; + private static final String SMILE_NEW_PR = "\uD83C\uDF89"; private static final String SMILE_UPDATE = "\uD83D\uDD04"; private static final String HR = "\n -- -- -- -- --\n"; + private Message() { throw new IllegalStateException("Утилитарный класс"); } @NonNull public static String newPullRequest(PullRequest pullRequest) { - return "\uD83C\uDF89 *Новый Pull Request*\n" + - "[" + pullRequest.getName() + "](" + pullRequest.getUrl() + ")\n" + + return SMILE_NEW_PR + " *Новый Pull Request*" + BREAK + + "[" + pullRequest.getName() + "](" + pullRequest.getUrl() + ")" + HR + SMILE_AUTHOR + ": " + pullRequest.getAuthor().getLogin() + TWO_BREAK; diff --git a/src/main/resources/liquibase/change-set/v1.2.0.xml b/src/main/resources/liquibase/change-set/v1.2.0.xml index 3cba5c3..dc8c7ec 100644 --- a/src/main/resources/liquibase/change-set/v1.2.0.xml +++ b/src/main/resources/liquibase/change-set/v1.2.0.xml @@ -15,4 +15,14 @@ + + + + + + + + + + \ No newline at end of file