Skip to content

Instantly share code, notes, and snippets.

@trnl
Created August 31, 2012 01:50
Show Gist options
  • Save trnl/3547642 to your computer and use it in GitHub Desktop.
Save trnl/3547642 to your computer and use it in GitHub Desktop.
Jenkins Training: Day 2 Part 2
package org.jenkinsci.plugins;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Notifier;
import hudson.tasks.Publisher;
import org.kohsuke.stapler.DataBoundConstructor;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TwitterNotifier extends Notifier {
private static final Logger LOGGER = Logger.getLogger(TwitterNotifier.class.getName());
@DataBoundConstructor
public TwitterNotifier() {
}
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.BUILD;
}
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
Configuration configuration = new ConfigurationBuilder()
.setOAuthConsumerKey(TwitterJobProperty.DESCRIPTOR.getConsumerKey())
.setOAuthConsumerSecret(TwitterJobProperty.DESCRIPTOR.getConsumerSecret())
.setOAuthAccessToken(TwitterJobProperty.DESCRIPTOR.getAccessToken())
.setOAuthAccessTokenSecret(TwitterJobProperty.DESCRIPTOR.getAccessTokenSecret())
.setUseSSL(true)
.build();
Twitter twitter = new TwitterFactory(configuration).getInstance();
try {
twitter.updateStatus("I'm done. Client: " + InetAddress.getLocalHost().getHostName() + ". Time: "+ new Date());
} catch (TwitterException e) {
LOGGER.log(Level.SEVERE, "Error updating status", e);
}
return true;
}
@Extension
public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
@Override
public String getDisplayName() {
return "Update Twitter Status";
}
}
}
package org.jenkinsci.plugins;
import hudson.model.AbstractBuild;
import hudson.model.Action;
import java.util.List;
public class TwitterBuildAction implements Action {
private final AbstractBuild<?, ?> build;
private List<TwitterMessage> tweets;
public TwitterBuildAction(AbstractBuild<?, ?> build, List<TwitterMessage> tweets) {
this.build = build;
this.tweets = tweets;
}
public String getIconFileName() {
// "cool" is the name given to maven during creation, name of the folder
return (tweets != null && tweets.size() > 0) ? "/plugin/cool/img/twitter.png" : null;
}
public String getDisplayName() {
return "Twitter"; //To change body of implemented methods use File | Settings | File Templates.
}
public String getUrlName() {
return "twitter"; //To change body of implemented methods use File | Settings | File Templates.
}
public AbstractBuild<?, ?> getBuild() {
return build;
}
public List<TwitterMessage> getTweets() {
return tweets;
}
public void setTweets(List<TwitterMessage> tweets) {
this.tweets = tweets;
}
}
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:l="/lib/layout">
<l:layout>
<st:include it="${it.build}" page="sidepanel.jelly"/>
<l:main-panel>
<style>
.tweet{background: rgb(245,245,245); padding:10px; margin-bottom: 5px; position:relative}
.tweet img{float:left; margin-right:10px}
.tweet h1{font-size:120%}
.tweet .tweet-time{position:absolute; right:10px; top: 10px;}
</style>
<h1>Twitter Public Timeline</h1>
<j:forEach var="tweet" items="${it.tweets}">
<div class="tweet">
<img src="${tweet.profileImage}"/>
<div><h1>@${tweet.username}</h1>${tweet.text}</div>
<div class="tweet-time">${tweet.dateCreated}</div>
</div>
</j:forEach>
</l:main-panel>
</l:layout>
</j:jelly>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment