Skip to content

Instantly share code, notes, and snippets.

View andres-rama-hs's full-sized avatar

Andres Rama andres-rama-hs

View GitHub Profile
@andres-rama-hs
andres-rama-hs / HWNTestAssertions.java
Created April 26, 2016 00:00
A quick example of how once you have your test build, JUnit works as you'd expect
String s = FileUtils.readFileToString(build.getLogFile());
assertThat(s, contains("aaa1"));
assertThat(s, contains("aaa2"));
assertThat(s, contains("aaa3"));
@andres-rama-hs
andres-rama-hs / HWNTestProjectCode.java
Created April 25, 2016 23:59
These 3 lines create a project, schedule build number 0 on it, and immediatley get the build so that it can be used/inspected
FreeStyleProject project = j.createFreeStyleProject();
project.getBuildersList().add(new HelloWorldNotifier(“aaa”));
FreeStyleBuild build = project.scheduleBuild2(0).get();
@andres-rama-hs
andres-rama-hs / HWNJenkinsRule.java
Created April 25, 2016 23:58
This singular line allows you to trivially spin up a test Jenkins for testing your plugin
@Rule public JenkinsRule j = new JenkinsRule();
@andres-rama-hs
andres-rama-hs / HWNBuildListener.java
Created April 25, 2016 23:57
The BuildListener class used by HelloWorldNotifier
@Extension
public class BuildListener extends RunListener<AbstractBuild> {
@Override
public void onStarted(AbstractBuild abstractBuild, TaskListener listener) {
// Do nothing.
}
@Override
public void onCompleted(AbstractBuild abstractBuild, TaskListener listener) {
@andres-rama-hs
andres-rama-hs / HWNConfigure.java
Created April 25, 2016 23:54
The function that will allow HelloWorldNotifier to load a value from the global config of the plugin
@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException{
global = formData.getString("global");
save();
return super.configure(req, formData);
}
@andres-rama-hs
andres-rama-hs / HWNGlobalConfMethods.java
Created April 25, 2016 23:53
The two methods that must be implemented by HelloWorldNotifier in order to have a descriptor
@Override
public String getDisplayName() {
return "HelloWorldNotifier"; // What people will see as the plugin name in the configs
}
@Override
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
return true; // We are always OK with someone adding this as a build step for their job
}
@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> { // Publisher because Notifiers are a type of publisher
Private Integer global;
}
@andres-rama-hs
andres-rama-hs / HWNConstructor.java
Created April 25, 2016 23:48
the constructor code for HWN
private final String local;
@DataBoundConstructor
public HelloWorldNotifier(String local){
this.local = local;
}
package testGroup.HelloWorldNotifier;
public class HelloWorldNotifier extends Notifier {
}
<settings>
<pluginGroups>
<pluginGroup>org.jenkins-ci.tools</pluginGroup>
</pluginGroups>
<profiles>
<!-- Give access to Jenkins plugins -->
<profile>
<id>jenkins</id>
<activation>