Created
May 21, 2016 16:07
-
-
Save Willsr71/59f1d98c29220ffa99b0036433471c61 to your computer and use it in GitHub Desktop.
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
package net.willsr71.discordnotifier; | |
import hudson.Extension; | |
import hudson.FilePath; | |
import hudson.Launcher; | |
import hudson.model.AbstractProject; | |
import hudson.model.Run; | |
import hudson.model.TaskListener; | |
import hudson.tasks.*; | |
import hudson.util.FormValidation; | |
import jenkins.tasks.SimpleBuildStep; | |
import net.sf.json.JSONObject; | |
import org.kohsuke.stapler.DataBoundConstructor; | |
import org.kohsuke.stapler.QueryParameter; | |
import org.kohsuke.stapler.StaplerRequest; | |
/* | |
import sx.blah.discord.api.ClientBuilder; | |
import sx.blah.discord.api.IDiscordClient; | |
import sx.blah.discord.util.DiscordException; | |
import sx.blah.discord.util.MessageBuilder; | |
*/ | |
import javax.annotation.Nonnull; | |
import javax.servlet.ServletException; | |
import java.io.IOException; | |
public class DiscordNotifier extends Notifier implements SimpleBuildStep { | |
private final String server; | |
private final String channel; | |
private final String message; | |
@DataBoundConstructor | |
public DiscordNotifier(String server, String channel, String message) { | |
this.server = server; | |
this.channel = channel; | |
this.message = message; | |
} | |
public String getServer() { | |
return server; | |
} | |
public String getChannel() { | |
return channel; | |
} | |
public String getMessage() { | |
return message; | |
} | |
@Override | |
public DescriptorImpl getDescriptor() { | |
return (DescriptorImpl) super.getDescriptor(); | |
} | |
@Override | |
public BuildStepMonitor getRequiredMonitorService() { | |
return null; | |
} | |
@Override | |
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener taskListener) throws InterruptedException, IOException { | |
/*System.out.println("Running1..."); | |
listener.getLogger().println("Running2..."); | |
if (getDescriptor().getApiToken().length() == 0) { | |
System.out.println("No API token specified1."); | |
listener.getLogger().println("No API token specified2."); | |
throw new NullPointerException("No API token specified3."); | |
} else { | |
System.out.println("API token specified1."); | |
listener.getLogger().println("API token specified2."); | |
} | |
System.out.println("Notifying Discord..."); | |
listener.getLogger().println("Notifying Discord..."); | |
/*try { | |
IDiscordClient client = new ClientBuilder().withToken(getDescriptor().getApiToken()).login(); | |
System.out.println("Logged in."); | |
new MessageBuilder(client).withChannel(getChannel()).withContent(getMessage()); | |
} catch (DiscordException e) { | |
e.printStackTrace(); | |
}*/ | |
} | |
@Extension | |
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> { | |
private String apiToken; | |
public DescriptorImpl() { | |
load(); | |
} | |
public FormValidation doCheckServer(@QueryParameter String server) throws IOException, ServletException { | |
if (server.length() == 0) { | |
return FormValidation.error("You must specify a server"); | |
} | |
if (getApiToken().length() == 0) { | |
return FormValidation.warning("You have not specified an API token in global settings."); | |
} | |
return FormValidation.ok(); | |
} | |
public FormValidation doCheckChannel(@QueryParameter String channel) throws IOException, ServletException { | |
if (channel.length() == 0) { | |
return FormValidation.error("You must specify a channel"); | |
} | |
return FormValidation.ok(); | |
} | |
public FormValidation doCheckMessage(@QueryParameter String message) throws IOException, ServletException { | |
if (message.length() == 0) { | |
return FormValidation.error("You must specify a message"); | |
} | |
return FormValidation.ok(); | |
} | |
public FormValidation doCheckApiToken(@QueryParameter String apiToken) throws IOException, ServletException { | |
if (apiToken.length() == 0) { | |
return FormValidation.warning("No API token specified"); | |
} | |
return FormValidation.ok(); | |
} | |
public boolean isApplicable(Class<? extends AbstractProject> aClass) { | |
return true; | |
} | |
public String getDisplayName() { | |
return "Notify Discord Server"; | |
} | |
@Override | |
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException { | |
apiToken = formData.getString("apiToken"); | |
save(); | |
return super.configure(req, formData); | |
} | |
public String getApiToken() { | |
return apiToken; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment