Created
August 30, 2012 14:36
-
-
Save trnl/3529769 to your computer and use it in GitHub Desktop.
Jenkins Training: Day 2 Part 1
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
<settings> | |
<pluginGroups> | |
<pluginGroup>org.jenkins-ci.tools</pluginGroup> | |
</pluginGroups> | |
<profiles> | |
<!-- Give access to Jenkins plugins --> | |
<profile> | |
<id>jenkins</id> | |
<activation> | |
<activeByDefault>true</activeByDefault> | |
<!-- change this to false, if you don't like to have it on per default --> | |
</activation> | |
<repositories> | |
<repository> | |
<id>repo.jenkins-ci.org</id> | |
<url>http://repo.jenkins-ci.org/public/</url> | |
</repository> | |
</repositories> | |
<pluginRepositories> | |
<pluginRepository> | |
<id>repo.jenkins-ci.org</id> | |
<url>http://repo.jenkins-ci.org/public/</url> | |
</pluginRepository> | |
</pluginRepositories> | |
</profile> | |
</profiles> | |
</settings> |
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
<?xml version="1.0"?> | |
<dependencies> | |
<dependency> | |
<groupId>com.google.guava</groupId> | |
<artifactId>guava</artifactId> | |
<version>11.0.1</version> | |
<scope>provided</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.twitter4j</groupId> | |
<artifactId>twitter4j-core</artifactId> | |
<version>2.2.5</version> | |
</dependency> | |
</dependencies> |
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
<?jelly escape-by-default='true'?> | |
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"> | |
<f:entry title="${%Schedule}" help="/descriptor/hudson.triggers.TimerTrigger/help/spec"> | |
<f:textarea name="cronTabSpec" checkUrl="'${rootURL}/trigger/TimerTrigger/check?value='+encodeURIComponent(this.value)" value="${instance.spec}"/> | |
</f:entry> | |
</j:jelly> |
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 org.jenkinsci.plugins; | |
import antlr.ANTLRException; | |
import hudson.Extension; | |
import hudson.model.Cause; | |
import hudson.model.Item; | |
import hudson.model.Project; | |
import hudson.triggers.Trigger; | |
import hudson.triggers.TriggerDescriptor; | |
import org.kohsuke.stapler.DataBoundConstructor; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class TwitterTrigger extends Trigger<Project> { | |
private static final Logger LOGGER = Logger.getLogger(TwitterTrigger.class.getName()); | |
@Extension | |
public static class DescriptorImpl extends TriggerDescriptor { | |
@Override | |
public boolean isApplicable(Item item) { | |
return true; | |
} | |
@Override | |
public String getDisplayName() { | |
return "Poll Twitter"; | |
} | |
} | |
@DataBoundConstructor | |
public TwitterTrigger(String cronTabSpec) throws ANTLRException { | |
super(cronTabSpec); | |
} | |
@Override | |
public void run() { | |
LOGGER.info("TwitterTrigger executing"); | |
} | |
} |
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
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form"> | |
<f:section title="Twitter"> | |
<f:entry title="Consumer key" field="consumerKey"> | |
<f:textbox/> | |
</f:entry> | |
<f:entry title="Consumer secret" field="consumerSecret"> | |
<f:password/> | |
</f:entry> | |
<f:entry title="Access token" field="accessToken"> | |
<f:textbox/> | |
</f:entry> | |
<f:entry title="Access token secret" field="accessTokenSecret"> | |
<f:password/> | |
</f:entry> | |
</f:section> | |
</j:jelly> |
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 org.jenkinsci.plugins; | |
import hudson.Extension; | |
import hudson.model.AbstractProject; | |
import hudson.model.JobProperty; | |
import hudson.model.JobPropertyDescriptor; | |
import net.sf.json.JSONObject; | |
import org.kohsuke.stapler.StaplerRequest; | |
public class TwitterJobProperty extends JobProperty<AbstractProject<?, ?>> { | |
@Extension | |
public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); | |
public static final class DescriptorImpl extends JobPropertyDescriptor { | |
public String getConsumerKey() { | |
return consumerKey; | |
} | |
public void setConsumerKey(String consumerKey) { | |
this.consumerKey = consumerKey; | |
} | |
public String getConsumerSecret() { | |
return consumerSecret; | |
} | |
public void setConsumerSecret(String consumerSecret) { | |
this.consumerSecret = consumerSecret; | |
} | |
public String getAccessToken() { | |
return accessToken; | |
} | |
public void setAccessToken(String accessToken) { | |
this.accessToken = accessToken; | |
} | |
public String getAccessTokenSecret() { | |
return accessTokenSecret; | |
} | |
public void setAccessTokenSecret(String accessTokenSecret) { | |
this.accessTokenSecret = accessTokenSecret; | |
} | |
public boolean isFilled() { | |
return consumerKey != null && consumerSecret != null && accessToken != null && accessTokenSecret != null; | |
} | |
private String consumerSecret; | |
private String consumerKey; | |
private String accessToken; | |
private String accessTokenSecret; | |
public DescriptorImpl() { | |
super(TwitterJobProperty.class); | |
load(); | |
} | |
@Override | |
public String getDisplayName() { | |
return "Twitter"; | |
} | |
@Override | |
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException { | |
req.bindJSON(this, formData); | |
save(); | |
return true; | |
} | |
} | |
} |
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 org.jenkinsci.plugins; | |
import antlr.ANTLRException; | |
import hudson.Extension; | |
import hudson.model.Cause; | |
import hudson.model.Item; | |
import hudson.model.Project; | |
import hudson.triggers.Trigger; | |
import hudson.triggers.TriggerDescriptor; | |
import org.kohsuke.stapler.DataBoundConstructor; | |
import twitter4j.*; | |
import twitter4j.conf.Configuration; | |
import twitter4j.conf.ConfigurationBuilder; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class TwitterTrigger extends Trigger<Project> { | |
private static final Logger LOGGER = Logger.getLogger(TwitterTrigger.class.getName()); | |
@Extension | |
public static class DescriptorImpl extends TriggerDescriptor { | |
@Override | |
public boolean isApplicable(Item item) { | |
return true; | |
} | |
@Override | |
public String getDisplayName() { | |
return "Poll Twitter"; | |
} | |
} | |
@DataBoundConstructor | |
public TwitterTrigger(String cronTabSpec) throws ANTLRException { | |
super(cronTabSpec); | |
} | |
private long latestMention = -1; | |
@Override | |
public void run() { | |
if (TwitterJobProperty.DESCRIPTOR.isFilled()) { | |
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 { | |
Status s = twitter.getMentions(new Paging(1, 1)).get(0); | |
if (latestMention != s.getId()) { | |
latestMention = s.getId(); | |
job.scheduleBuild(0, new TwitterCause("Found a tweet:" + s.getText())); | |
} | |
} catch (TwitterException e) { | |
LOGGER.log(Level.SEVERE, "Error getting timeline", e); | |
} | |
} | |
} | |
private static class TwitterCause extends Cause { | |
private final String description; | |
public TwitterCause(String description) { | |
this.description = description; | |
} | |
public String getShortDescription() { | |
return description; | |
} | |
} | |
} |
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 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.Builder; | |
import twitter4j.Status; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterException; | |
import twitter4j.TwitterFactory; | |
import twitter4j.conf.Configuration; | |
import twitter4j.conf.ConfigurationBuilder; | |
import java.util.List; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class TwitterJobBuilder extends Builder { | |
private static final Logger LOGGER = Logger.getLogger(TwitterJobBuilder.class.getName()); | |
@Override | |
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) { | |
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 { | |
List<Status> statuses = twitter.getPublicTimeline(); | |
for (Status s : statuses) { | |
LOGGER.info(s.getText()); | |
} | |
} catch (TwitterException e) { | |
LOGGER.log(Level.SEVERE, "Error getting public timeline", e); | |
} | |
return true; | |
} | |
@Override | |
public DescriptorImpl getDescriptor() { | |
return (DescriptorImpl) super.getDescriptor(); | |
} | |
@Extension | |
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> { | |
public boolean isApplicable(Class<? extends AbstractProject> aClass) { | |
return true; | |
} | |
public String getDisplayName() { | |
return "Grab Twitter Public Timeline"; | |
} | |
} | |
} |
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 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.Builder; | |
import org.apache.commons.collections.CollectionUtils; | |
import org.apache.commons.collections.Transformer; | |
import org.kohsuke.stapler.DataBoundConstructor; | |
import twitter4j.Status; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterException; | |
import twitter4j.TwitterFactory; | |
import twitter4j.conf.Configuration; | |
import twitter4j.conf.ConfigurationBuilder; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class TwitterJobBuilder extends Builder { | |
@DataBoundConstructor | |
public TwitterJobBuilder() { | |
super(); | |
} | |
private static final Logger LOGGER = Logger.getLogger(TwitterJobBuilder.class.getName()); | |
@Override | |
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) { | |
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 { | |
List<Status> statuses = twitter.getPublicTimeline(); | |
List<TwitterMessage> messages = new ArrayList<TwitterMessage>(); | |
CollectionUtils.collect(statuses, new Transformer() { | |
public Object transform(Object input) { | |
Status s = (Status) input; | |
return new TwitterMessage( | |
s.getText(), | |
s.getUser().getName(), | |
s.getUser().getProfileImageURL().toString(), | |
s.getCreatedAt() | |
); | |
} | |
}, | |
messages); | |
} catch (TwitterException e) { | |
LOGGER.log(Level.SEVERE, "Error getting public timeline", e); | |
} | |
return true; | |
} | |
@Override | |
public DescriptorImpl getDescriptor() { | |
return (DescriptorImpl) super.getDescriptor(); | |
} | |
@Extension | |
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> { | |
public boolean isApplicable(Class<? extends AbstractProject> aClass) { | |
return true; | |
} | |
public String getDisplayName() { | |
return "Grab Twitter Public Timeline"; | |
} | |
} | |
} |
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 org.jenkinsci.plugins; | |
import java.io.Serializable; | |
import java.util.Date; | |
public class TwitterMessage implements Serializable { | |
private String text; | |
private String username; | |
private String profileImage; | |
private Date dateCreated; | |
public TwitterMessage(String text, String username, String profileImage, Date dateCreated) { | |
this.text = text; | |
this.username = username; | |
this.profileImage = profileImage; | |
this.dateCreated = dateCreated; | |
} | |
public Date getDateCreated() { | |
return dateCreated; | |
} | |
public void setDateCreated(Date dateCreated) { | |
this.dateCreated = dateCreated; | |
} | |
public String getText() { | |
return text; | |
} | |
public void setText(String text) { | |
this.text = text; | |
} | |
public String getUsername() { | |
return username; | |
} | |
public void setUsername(String username) { | |
this.username = username; | |
} | |
public String getProfileImage() { | |
return profileImage; | |
} | |
public void setProfileImage(String profileImage) { | |
this.profileImage = profileImage; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment