-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation of a Command printing the release time of a build #47
Open
DManstrator
wants to merge
4
commits into
Almighty-Alpaca:master
Choose a base branch
from
DManstrator:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e9a5fba
Implementation of a Command printing the release time of a build
DManstrator de55d6e
Implemented changes requested by the Review
DManstrator 1691cd4
Correctly used the Logger class for logging an Exception
DManstrator 6099bc7
Added UTC TimeZone and Duration Comparison to DateVersionCommand and …
DManstrator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
.../main/java/com/almightyalpaca/discord/jdabutler/commands/commands/DateVersionCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
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.DurationUtils; | ||
import com.almightyalpaca.discord.jdabutler.util.EmbedUtil; | ||
import com.kantenkugel.discordbot.jenkinsutil.JenkinsApi; | ||
import com.kantenkugel.discordbot.jenkinsutil.JenkinsBuild; | ||
|
||
import net.dv8tion.jda.api.EmbedBuilder; | ||
import net.dv8tion.jda.api.entities.Message; | ||
import net.dv8tion.jda.api.entities.MessageEmbed; | ||
import net.dv8tion.jda.api.entities.TextChannel; | ||
import net.dv8tion.jda.api.entities.User; | ||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; | ||
|
||
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[] ALIASES = { "published" }; | ||
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) | ||
{ | ||
JenkinsBuild build; | ||
try | ||
{ | ||
if (content.trim().isEmpty()) | ||
build = JENKINS.getLastSuccessfulBuild(); | ||
else | ||
{ | ||
String buildNrStr = content; | ||
|
||
final int underscoreIndex = content.indexOf('_'); // in case somebody provided a full version (e. g. 3.8.3_463) | ||
if (underscoreIndex != -1) | ||
buildNrStr = content.substring(underscoreIndex + 1); | ||
|
||
final int buildNr = Integer.parseInt(buildNrStr.trim()); | ||
build = JENKINS.getBuild(buildNr); | ||
} | ||
} | ||
catch (IOException | NumberFormatException ex) | ||
{ | ||
Bot.LOG.error("Exception in DateVersionCommand occured!", ex); | ||
|
||
String title; | ||
if (ex instanceof IOException) | ||
title = "Connection to the Jenkins Server timed out!"; | ||
else if (ex instanceof NumberFormatException) | ||
title = "Given input was not a valid build number!"; | ||
else | ||
title = "Unknown Error occured!"; | ||
DManstrator marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
final MessageEmbed failureEmbed = new EmbedBuilder().setAuthor("Error occured!", null, EmbedUtil.getJDAIconUrl()) | ||
.setTitle(title, null) | ||
.setColor(Color.RED) | ||
.build(); | ||
reply(event, failureEmbed); | ||
return; | ||
} | ||
|
||
// Get time of build | ||
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) | ||
: build.buildNum + " (failed)"; | ||
|
||
// Return Info to User | ||
final EmbedBuilder eb = new EmbedBuilder(); | ||
EmbedUtil.setColor(eb); | ||
|
||
final MessageEmbed successEmbed = eb.setAuthor("Release Time of Version " + buildVersion, build.getUrl(), EmbedUtil.getJDAIconUrl()) | ||
.setTitle(publishedTime).setDescription(String.format("That was approximately %s ago.", differenceWithoutMs)) | ||
.build(); | ||
|
||
reply(event, successEmbed); | ||
} | ||
|
||
@Override | ||
public String[] getAliases() | ||
{ | ||
return DateVersionCommand.ALIASES; | ||
} | ||
|
||
@Override | ||
public String getHelp() | ||
{ | ||
return "Prints the datetime when the given build number or the latest build was published"; | ||
} | ||
|
||
@Override | ||
public String getName() | ||
{ | ||
return "date"; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
bot/src/main/java/com/almightyalpaca/discord/jdabutler/util/DateUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
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() {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe I made a tiny mistake here. Shouldn't
args[0]
not be the item name according to line 45?I mean
VersionedItem#getChangelogProvider
always returnsnull
at the moment so it will always stop executing with"No Changelogs set up for " + item.getName()
(line 57) but if this is gonna be changed, this will "crash" (theNumberFormatException
will be catched) when providing another Item sinceargs[0]
isn't a build number / an integer in that case.I would fix that by replacing
args[0]
withargs[args.length - 1]
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kantenkugel I talked already with Alpaca in the JDA Server about this today, he has problems with the general code base right now so it would be great if you can also look over it (and maybe you can give me some response on that one too). However, it "sounds plausible" for Alpaca and it's ready to be merged (after I updated the code based on my last review).