Skip to content

Commit

Permalink
Added UTC TimeZone and Duration Comparison to DateVersionCommand and …
Browse files Browse the repository at this point in the history
…updated the Format of the printed DateTime

Also added the Build Time to the ChangelogCommand and refactored the code a bit
  • Loading branch information
DManstrator committed Jul 2, 2019
1 parent 1691cd4 commit 6099bc7
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.almightyalpaca.discord.jdabutler.commands.commands;

import com.almightyalpaca.discord.jdabutler.Bot;
import com.almightyalpaca.discord.jdabutler.commands.Command;
import com.almightyalpaca.discord.jdabutler.util.DateUtils;
import com.almightyalpaca.discord.jdabutler.util.EmbedUtil;
import com.kantenkugel.discordbot.jenkinsutil.JenkinsApi;
import com.kantenkugel.discordbot.jenkinsutil.JenkinsBuild;
import com.kantenkugel.discordbot.versioncheck.VersionCheckerRegistry;
import com.kantenkugel.discordbot.versioncheck.changelog.ChangelogProvider;
import com.kantenkugel.discordbot.versioncheck.changelog.JenkinsChangelogProvider;
import com.kantenkugel.discordbot.versioncheck.items.VersionedItem;
import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.EmbedBuilder;
Expand All @@ -13,12 +18,17 @@
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;

import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public class ChangelogCommand extends Command
{

private static final String[] ALIASES = { "changes" };
private static final JenkinsApi JENKINS = DateUtils.JENKINS;
private static final DateTimeFormatter FORMATTER = DateUtils.getDateTimeFormatter();

@Override
public void dispatch(final User sender, final TextChannel channel, final Message message, final String content, final GuildMessageReceivedEvent event)
Expand Down Expand Up @@ -67,11 +77,33 @@ public void dispatch(final User sender, final TextChannel channel, final Message
reply(event, "The specified version does not exist");
return;
}

// Get time of build
String publishedTime;
try
{
final JenkinsBuild build = args == null
? JENKINS.getLastSuccessfulBuild()
: JENKINS.getBuild(Integer.parseInt(args[0]));

final OffsetDateTime buildTime = build.buildTime;
publishedTime = FORMATTER.format(buildTime);
}
catch (NumberFormatException | IOException ex)
{
Bot.LOG.error("Exception in ChangelogCommand occured!", ex);
publishedTime = "Unable to get Release Time";
}

String title;
if(changelog.getChangelogUrl() == null)
eb.appendDescription("**").appendDescription(changelog.getTitle()).appendDescription("**:\n");
title = String.format("**%s**", changelog.getTitle());
else
eb.appendDescription("[").appendDescription(changelog.getTitle()).appendDescription("](")
.appendDescription(changelog.getChangelogUrl()).appendDescription("):\n");
title = String.format("[%s](%s)", changelog.getTitle(), changelog.getChangelogUrl());

final String versionTitle = String.format("%s *(%s)*:\n", title, publishedTime);
eb.appendDescription(versionTitle);

if(changelog.getChangeset().isEmpty())
eb.appendDescription("No changes available for this version");
else
Expand All @@ -80,13 +112,19 @@ public void dispatch(final User sender, final TextChannel channel, final Message
//more than 1 version given
else
{
List<ChangelogProvider.Changelog> changelogs = clProvider.getChangelogs(args[versionStart], args[versionStart + 1]);
final String startVersion = args[versionStart];
final String endVersion = args[versionStart + 1];

// get and increment build number instead of fetching it from every changelog to be more reliable
final int start = JenkinsChangelogProvider.extractBuild(startVersion);
List<ChangelogProvider.Changelog> changelogs = clProvider.getChangelogs(startVersion, endVersion);
if(changelogs.size() == 0)
{
reply(event, "No Changelogs found in given range");
return;
}
int fields = 0;
int buildNr = start;
for(ChangelogProvider.Changelog changelog : changelogs) {
String body = String.join("\n", changelog.getChangeset());
if(body.length() > MessageEmbed.VALUE_MAX_LENGTH)
Expand All @@ -97,13 +135,27 @@ public void dispatch(final User sender, final TextChannel channel, final Message
body = "[Link]("+changelog.getChangelogUrl()+") Too large to show.";
}

eb.addField(changelog.getTitle(), body, false);
// Get time of build
String publishedTime;
try
{
final JenkinsBuild build = JENKINS.getBuild(buildNr);
final OffsetDateTime buildTime = build.buildTime;
publishedTime = FORMATTER.format(buildTime);
}
catch (IOException ex)
{
Bot.LOG.error("Exception in ChangelogCommand occured!", ex);
publishedTime = "Unable to get Release Time";
}
eb.addField(String.format("%s (%s)", changelog.getTitle(), publishedTime), body, false);

if(++fields == 19 && changelogs.size() > 20)
{
eb.addField("...", "Embed limit reached. See [Online changelog]("
+ clProvider.getChangelogUrl() + ')', false);
}
buildNr++;
}
}

Expand Down Expand Up @@ -135,4 +187,5 @@ public String getName()
{
return "changelog";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.almightyalpaca.discord.jdabutler.Bot;
import com.almightyalpaca.discord.jdabutler.commands.Command;
import com.almightyalpaca.discord.jdabutler.util.DateUtils;
import com.almightyalpaca.discord.jdabutler.util.DurationUtils;
import com.almightyalpaca.discord.jdabutler.util.EmbedUtil;
import com.kantenkugel.discordbot.jenkinsutil.JenkinsApi;
import com.kantenkugel.discordbot.jenkinsutil.JenkinsBuild;
Expand All @@ -15,30 +17,15 @@

import java.awt.Color;
import java.io.IOException;
import java.time.Duration;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

public class DateVersionCommand extends Command
{
private static final String DATE_FORMAT = "yyyy-MM-dd; HH:mm:ss";
private static final String[] ALIASES = { "published" };
private static final JenkinsApi JENKINS = JenkinsApi.JDA_JENKINS;
private static final DateTimeFormatter FORMATTER = getDateTimeFormatter();

private static DateTimeFormatter getDateTimeFormatter() {
DateTimeFormatter formatter;
try
{
formatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
}
catch (NullPointerException | IllegalArgumentException ex)
{
final String defaultFormat = "dd.MM.yyyy; HH:mm:ss";
Bot.LOG.warn("Given format for DateVersionCommand was not valid, using: " + defaultFormat);
formatter = DateTimeFormatter.ofPattern(defaultFormat);
}
return formatter;
}
private static final JenkinsApi JENKINS = DateUtils.JENKINS;
private static final DateTimeFormatter FORMATTER = DateUtils.getDateTimeFormatter();

@Override
public void dispatch(final User sender, final TextChannel channel, final Message message, final String content, final GuildMessageReceivedEvent event)
Expand Down Expand Up @@ -81,9 +68,15 @@ else if (ex instanceof NumberFormatException)
}

// Get time of build
OffsetDateTime buildTime = build.buildTime;
final OffsetDateTime buildTime = build.buildTime;
final String publishedTime = FORMATTER.format(buildTime);

final Duration dur = DurationUtils.toDuration(System.currentTimeMillis() - buildTime.toInstant().toEpochMilli());
final String difference = DurationUtils.formatDuration(dur, true);

final int lastSpace = difference.lastIndexOf(' ');
final String differenceWithoutMs = lastSpace < 0 ? difference : difference.substring(0, lastSpace);

// Get correct version (copied from JenkinsChangelogProvider#getChangelogs(String, String))
final String buildVersion = build.status == JenkinsBuild.Status.SUCCESS
? build.artifacts.values().iterator().next().fileNameParts.get(1)
Expand All @@ -94,7 +87,7 @@ else if (ex instanceof NumberFormatException)
EmbedUtil.setColor(eb);

final MessageEmbed successEmbed = eb.setAuthor("Release Time of Version " + buildVersion, build.getUrl(), EmbedUtil.getJDAIconUrl())
.setTitle(publishedTime, null)
.setTitle(publishedTime).setDescription(String.format("That was approximately %s ago.", differenceWithoutMs))
.build();

reply(event, successEmbed);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.almightyalpaca.discord.jdabutler.util;

import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

import com.almightyalpaca.discord.jdabutler.Bot;
import com.kantenkugel.discordbot.jenkinsutil.JenkinsApi;

public final class DateUtils
{
private static final String DATE_FORMAT = "dd/MM/yyyy 'at' HH:mm:ss";
public static final JenkinsApi JENKINS = JenkinsApi.JDA_JENKINS;
public static final DateTimeFormatter FORMATTER = getDateTimeFormatter();

public static DateTimeFormatter getDateTimeFormatter() {
DateTimeFormatter formatter;
try
{
formatter = DateTimeFormatter.ofPattern(DATE_FORMAT + " (z)");
}
catch (NullPointerException | IllegalArgumentException ex)
{
final String defaultFormat = "dd.MM.yyyy 'at' HH:mm:ss (z)";
Bot.LOG.warn("Given format for DateVersionCommand was not valid, using: " + defaultFormat);
formatter = DateTimeFormatter.ofPattern(defaultFormat);
}

return formatter.withZone(ZoneId.of("UTC"));
}

// prevent instantiation
private DateUtils() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ private List<String> getChangelog(List<JenkinsChange> changeSet)
return fields;
}

private static int extractBuild(String version)
// public since also accessed in the ChangelogCommand class
public static int extractBuild(String version)
{
int i = version.lastIndexOf('_');
try
Expand Down

0 comments on commit 6099bc7

Please sign in to comment.