Skip to content

Instantly share code, notes, and snippets.

@djangofan
Last active December 14, 2015 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save djangofan/5069075 to your computer and use it in GitHub Desktop.
Save djangofan/5069075 to your computer and use it in GitHub Desktop.
A Bamboo build checker for Jenkins
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.List;
import java.util.Scanner;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
//http://bamboo:8055/rss/createAllBuildsRssFeed.action?feedType=rssAll&buildKey=RELEASE
public class BambooNotifier extends NotifierUtils {
URL thisURL;
File tmpDir = new File( System.getProperty( "java.io.tmpdir" ) );
BambooNotifier( URL thisUrl ) {
log.println( "Loaded BambooNotifier class with URL: " + thisUrl );
this.thisURL = thisUrl;
}
public int getLatestBuildNumber() {
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = null;
int buildNum = 0;
try {
feed = input.build(new XmlReader( thisURL ) );
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (FeedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//log.println( "getTitles: \n" );
@SuppressWarnings("unchecked")
List<SyndEntry> entries = feed.getEntries();
//for ( SyndEntry s : entries ) {
// log.println( s.getTitleEx().getValue() );
//}
//log.println("\n");
String buildResult = entries.get(0).getTitleEx().getValue();
buildNum = Integer.parseInt( buildResult.substring(11, buildResult.indexOf(" ") ) );
return buildNum;
}
public boolean greaterThanLastBuild( int version ) {
boolean greaterThan = false;
File versionFile = new File( tmpDir.getAbsolutePath() + System.getProperty( "file.separator" ) + "buildVerBamboo.txt" );
// if buildVer.txt exists read the first line into an integer value
if ( versionFile.exists() ) {
Scanner fileScanner = null;
try {
fileScanner = new Scanner( versionFile );
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
int lastVer = Integer.parseInt( fileScanner.nextLine() );
// is new version greater than last?
if ( version > lastVer ) {
greaterThan = true;
// store new version number in buildVer.txt
writeIntToFile( version, versionFile );
}
} else {
// create new file and add latest version number to it
try {
versionFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
writeIntToFile( version, versionFile );
greaterThan = true;
log.println( "No buildVer.txt file was found." );
}
return greaterThan;
}
public void writeIntToFile( int val, File fileName ) {
log.println( "Writing \"" + val + "\" to tmp file \"" + fileName.getAbsolutePath() );
try {
FileWriter outFile = new FileWriter( fileName );
PrintWriter out = new PrintWriter( outFile );
out.println( Integer.toString( val ) );
out.close();
} catch (IOException e){
e.printStackTrace();
}
}
}
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Assert;
import org.junit.Test;
import tr.qa.BambooNotifier;
// run this test with -info arg to see log file output
public class BambooStatus extends NotifierUtils {
BambooNotifier notifier;
URL thisURL;
@Test
public void getBambooStatus() {
try {
thisURL = new URL("http://bamboo:8085/rss/createAllBuildsRssFeed.action?feedType=rssAll&buildKey=RELEASE");
} catch ( MalformedURLException e ) {
e.printStackTrace();
}
notifier = new BambooNotifier( thisURL );
int buildNum;
buildNum = notifier.getLatestBuildNumber();
classlogger.info( "Latest build seems to be: " + buildNum );
log.println( "Latest build seems to be: " + buildNum );
Assert.assertTrue( "No new build was found yet.", notifier.greaterThanLastBuild( buildNum ) );
log.println("New build number found was: " + buildNum );
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
group = 'what.ever'
ext {
projTitle = 'Bamboo Build Status'
projVersion = '1.0'
}
repositories {
mavenCentral()
}
dependencies {
compile group: 'junit', name: 'junit', version: '4.+'
compile group: 'rome', name: 'rome', version: '1.+'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.+'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.+'
}
task show << {
println ""
println "-----------------------------------------------"
println "Project Name: " + projTitle
println "Version: " + projVersion
println "Build output: " + relativePath(compileJava.destinationDir)
println "Resources output: " + relativePath(processResources.destinationDir)
println "-----------------------------------------------"
}
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>build/logs/junit.log</file>
<append>false</append>
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%-4r %-5level %logger{35}: %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
<!-- We want error logging from this logger to go to an extra appender
It still inherits CONSOLE STDOUT from the root logger -->
<logger name="junit" level="INFO">
<appender-ref ref="STDOUT" />
</logger>
</configuration>
import java.io.PrintStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class NotifierUtils {
protected Logger classlogger = LoggerFactory.getLogger( getClass() );
protected static Logger staticlogger = LoggerFactory.getLogger( NotifierUtils.class );
protected static PrintStream log = System.out;
NotifierUtils() {
// does nothing
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment