Skip to content

Commit

Permalink
Merge pull request #21 from MadsNielsen/JENKINS-26985
Browse files Browse the repository at this point in the history
JENKINS-26985
  • Loading branch information
MadsNielsen committed Apr 14, 2015
2 parents f53a6c3 + d3ab52a commit 1a4fafd
Show file tree
Hide file tree
Showing 27 changed files with 1,079 additions and 153 deletions.
18 changes: 9 additions & 9 deletions pom.xml
@@ -1,9 +1,9 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.554.3</version>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.554.3</version>
</parent>

<properties>
Expand All @@ -20,12 +20,12 @@

<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</repository>
<repository>
<id>praqma-repo</id>
<url>http://code.praqma.net/repo/maven/</url>
<id>praqma-repo</id>
<url>http://code.praqma.net/repo/maven/</url>
</repository>
</repositories>

Expand Down Expand Up @@ -430,14 +430,14 @@
<dependency>
<groupId>net.praqma</groupId>
<artifactId>cool</artifactId>
<version>0.6.38</version>
<version>0.6.39</version>
<type>jar</type>
</dependency>

<dependency>
<groupId>net.praqma</groupId>
<artifactId>cool</artifactId>
<version>0.6.38</version>
<version>0.6.39</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/net/praqma/hudson/CCUCMBuildAction.java
Expand Up @@ -7,6 +7,7 @@
import hudson.model.TaskListener;
import net.praqma.clearcase.ucm.entities.*;
import hudson.model.Action;
import java.util.ArrayList;
import net.praqma.clearcase.ucm.view.SnapshotView;
import net.praqma.hudson.scm.Polling;
import net.praqma.hudson.scm.Unstable;
Expand All @@ -20,6 +21,8 @@ public class CCUCMBuildAction implements Action {
private Component component;
private Project.PromotionLevel promotionLevel;
private String loadModule;
private List<Baseline> rebaseTargets = new ArrayList<Baseline>();
private List<Baseline> newFoundationStructure = new ArrayList<Baseline>();

/**
* Create a {@link Baseline} when deliver
Expand Down Expand Up @@ -364,4 +367,26 @@ public String stringify() {
return sb.toString();
}

/**
* @return the rebaseTargets
*/
public List<Baseline> getRebaseTargets() {
return rebaseTargets;
}

/**
* @param rebaseTargets the rebaseTargets to set
*/
public void setRebaseTargets(List<Baseline> rebaseTargets) {
this.rebaseTargets = rebaseTargets;
}

public List<Baseline> getNewFoundationStructure() {
return this.newFoundationStructure;
}

public void setNewFoundationStructure(List<Baseline> newFoundationStructure) {
this.newFoundationStructure = newFoundationStructure;
}

}
52 changes: 52 additions & 0 deletions src/main/java/net/praqma/hudson/nametemplates/FileFoundable.java
@@ -0,0 +1,52 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package net.praqma.hudson.nametemplates;

import hudson.FilePath;
import hudson.remoting.VirtualChannel;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;

/**
*
* @author Mads
*/
public class FileFoundable implements FilePath.FileCallable<String> {

public final String filename;

private static final Logger logger = Logger.getLogger( FileFoundable.class.getName() );


public FileFoundable(final String filename) {
logger.fine("[FileTemplate] FileFoundable created");
this.filename = filename;
}

@Override
public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
FilePath path = null;
logger.fine(String.format("In invoke. Operating on slave with workspace path: %s", f.getAbsolutePath()));
try {
File selectedFile = new File(filename);
if(selectedFile.isAbsolute()) {
path = new FilePath(selectedFile);
} else {
path = new FilePath(new FilePath(f), filename);
}
String readFile = path.readToString().trim();
logger.fine(String.format("[FileTemplate] This file was read on the slave: %s", readFile));

return readFile;
} catch (IOException ex) {
logger.fine(String.format("[FileTemplate] Using this file on remote: %s", path.absolutize().getRemote()));
logger.fine(String.format("[FileTemplate] Invoke caught exception with message %s", ex.getMessage()));
throw ex;
}
}

}
26 changes: 0 additions & 26 deletions src/main/java/net/praqma/hudson/nametemplates/FileTemplate.java
Expand Up @@ -25,30 +25,4 @@ public String parse( CCUCMBuildAction action, String filename, FilePath ws ) {
return "?";
}
}

public class FileFoundable implements FilePath.FileCallable<String> {
public final String filename;

public FileFoundable(final String filename) {
logger.fine("[FileTemplate] FileFoundable created");
this.filename = filename;
}

@Override
public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
FilePath path = null;
logger.fine(String.format("In invoke. Operating on slave with workspace path: %s",f.getAbsolutePath()));
try {
path = new FilePath(new FilePath(f), filename);
String readFile = path.readToString().trim();
logger.fine(String.format("[FileTemplate] This file was read on the slave: %s", readFile));
return readFile;
} catch (IOException ex) {
logger.fine(String.format("[FileTemplate] Using this file on remote: %s",path.absolutize().getRemote()));
logger.fine(String.format("[FileTemplate] Invoke caught exception with message %s", ex.getMessage()));
throw ex;
}
}

}
}
82 changes: 61 additions & 21 deletions src/main/java/net/praqma/hudson/notifier/CCUCMNotifier.java
Expand Up @@ -26,12 +26,16 @@
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.remoting.VirtualChannel;
import hudson.scm.SCM;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Notifier;
import hudson.tasks.Publisher;
import net.praqma.clearcase.Rebase;
import net.praqma.clearcase.ucm.entities.Component;
import net.praqma.hudson.exception.TemplateException;
import net.praqma.hudson.remoting.RebaseCancelTask;
import net.praqma.hudson.remoting.RebaseCompleteTask;
import org.apache.commons.lang.StringUtils;

public class CCUCMNotifier extends Notifier {
Expand All @@ -48,6 +52,7 @@ public CCUCMNotifier() { }
/**
* This indicates whether to let CCUCM run after(true) the job is done or
* before(false)
* @return
*/
@Override
public boolean needsToRunAfterFinalized() {
Expand Down Expand Up @@ -89,7 +94,7 @@ public boolean perform( AbstractBuild<?, ?> build, Launcher launcher, BuildListe

/* There's a valid baseline, lets process it */
if( baseline != null ) {
out.println( "Processing baseline" );
out.println( String.format ( "%s Processing baseline", "["+Config.nameShort + "]"));
status.setErrorMessage( action.getError() );

try {
Expand Down Expand Up @@ -150,14 +155,8 @@ public boolean perform( AbstractBuild<?, ?> build, Launcher launcher, BuildListe
*/
private void processBuild( AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener, CCUCMBuildAction pstate ) throws NotifierException {
Result buildResult = build.getResult();

VirtualChannel ch = launcher.getChannel();

if( ch == null ) {
logger.fine( "The channel was null" );
}

String workspace = null;

String workspace = null;
FilePath currentWorkspace = build.getExecutor().getCurrentWorkspace();
try {
workspace = build.getExecutor().getCurrentWorkspace().absolutize().getRemote();
Expand Down Expand Up @@ -209,15 +208,7 @@ private void processBuild( AbstractBuild<?, ?> build, Launcher launcher, BuildLi
if( treatSuccessful && pstate.doCreateBaseline() ) {

try {
out.println( String.format( "%s Creating baseline on Integration stream.", logShortPrefix ) );
out.println( String.format( "%s Absolute path of remoteWorkspace: %s", logShortPrefix, workspace ) );

pstate.setWorkspace( workspace );

NameTemplate.validateTemplates( pstate, build.getWorkspace() );
String name = NameTemplate.parseTemplate( pstate.getNameTemplate(), pstate, build.getWorkspace() );

targetbaseline = RemoteUtil.createRemoteBaseline( currentWorkspace, listener, name, pstate.getBaseline().getComponent(), action.getViewPath(), pstate.getBaseline().getUser() );
targetbaseline = createBaselineOnSuccess(workspace, pstate, build, currentWorkspace, pstate.getBaseline().getComponent());
action.setCreatedBaseline( targetbaseline );

} catch( Exception e ) {
Expand Down Expand Up @@ -255,6 +246,7 @@ private void processBuild( AbstractBuild<?, ?> build, Launcher launcher, BuildLi
try {
out.println( String.format("%s Trying to cancel the deliver.", logShortPrefix) );
//out.print( "[" + Config.nameShort + "] Trying to cancel the deliver. " );

RemoteUtil.completeRemoteDeliver( currentWorkspace, listener, pstate.getBaseline(), pstate.getStream(), action.getViewTag(), action.getViewPath(), false );
out.println( "Success." );
} catch( Exception e1 ) {
Expand All @@ -268,15 +260,52 @@ private void processBuild( AbstractBuild<?, ?> build, Launcher launcher, BuildLi
}
}
}
if( pstate.getPolling().isPollingOther() ) {
if( pstate.getPolling().isPollingOther() || pstate.getPolling().isPollingRebase() ) {
targetstream = pstate.getStream();
}

//Complete the rebase
try {
if( treatSuccessful && pstate.getPolling().isPollingRebase() ) {
out.println(String.format("%s The build was a succes, completing rebase", logShortPrefix));
build.getWorkspace().act(new RebaseCompleteTask(pstate.getStream()));

if(pstate.doCreateBaseline()) {
out.println( String.format( "%s Creating baseline on Integration stream.", logShortPrefix ) );
out.println( String.format( "%s Absolute path of remoteWorkspace: %s", logShortPrefix, workspace ) );
pstate.setWorkspace( workspace );
NameTemplate.validateTemplates( pstate, build.getWorkspace() );
String name = NameTemplate.parseTemplate( pstate.getNameTemplate(), pstate, build.getWorkspace() );
targetbaseline = RemoteUtil.createRemoteBaseline( currentWorkspace, name, pstate.getStream(), pstate.getViewPath() );
action.setCreatedBaseline( targetbaseline );
}

} else if ( !treatSuccessful && pstate.getPolling().isPollingRebase()) {
out.println(String.format( "%s The build failed, cancelling rebase", logShortPrefix));
build.getWorkspace().act(new RebaseCancelTask(pstate.getStream()));
}
} catch (TemplateException templex) {
out.println( String.format("%s %s", logShortPrefix, templex.getMessage() ) );
logger.warning( "Failing build because baseline could not be created in poll rebase" );
pstate.setRecommend(false);
build.setResult( Result.FAILURE );
} catch (Exception ex) {
Throwable cause = net.praqma.util.ExceptionUtils.unpackFrom( IOException.class, ex );
out.println( String.format( "%s Rebase operation failed", logShortPrefix ) );
ExceptionUtils.print( cause, out, true );
pstate.setRecommend(false);
logger.warning( "Failing build because baseline could not be created in poll rebase" );
build.setResult( Result.FAILURE );
}

/* Remote post build step, common to all types */
try {
logger.fine( String.format( "Remote post build step" ) );
out.println( String.format( "%s Performing common post build steps",logShortPrefix ) );
status = currentWorkspace.act( new RemotePostBuild( buildResult, status, listener, pstate.doMakeTag(), pstate.doRecommend(), pstate.getUnstable(), ( pstate.getPromotionLevel() == null ? true : false ), sourcebaseline, targetbaseline, sourcestream, targetstream, build.getParent().getDisplayName(), Integer.toString( build.getNumber() ) ) );

//Do not promote source when polling rebase.
boolean skipPromote = pstate.getPromotionLevel() == null || pstate.getPolling().isPollingRebase();
status = currentWorkspace.act( new RemotePostBuild( buildResult, status, listener, pstate.doMakeTag(), pstate.doRecommend(), pstate.getUnstable(), skipPromote, sourcebaseline, targetbaseline, sourcestream, targetstream, build.getParent().getDisplayName(), Integer.toString( build.getNumber() ), pstate.getRebaseTargets() ) );
} catch( Exception e ) {
status.setStable( false );
logger.log( Level.WARNING, "", e );
Expand All @@ -301,6 +330,17 @@ private void processBuild( AbstractBuild<?, ?> build, Launcher launcher, BuildLi
}
}

private Baseline createBaselineOnSuccess(String workspace, CCUCMBuildAction pstate, AbstractBuild<?, ?> build, FilePath currentWorkspace, Component component) throws IOException, TemplateException, InterruptedException {
Baseline targetbaseline;
out.println( String.format( "%s Creating baseline on Integration stream.", logShortPrefix ) );
out.println( String.format( "%s Absolute path of remoteWorkspace: %s", logShortPrefix, workspace ) );
pstate.setWorkspace( workspace );
NameTemplate.validateTemplates( pstate, build.getWorkspace() );
String name = NameTemplate.parseTemplate( pstate.getNameTemplate(), pstate, build.getWorkspace() );
targetbaseline = RemoteUtil.createRemoteBaseline( currentWorkspace, name, component, pstate.getViewPath() );
return targetbaseline;
}

/**
* This class is used by Hudson to define the plugin.
*
Expand Down

0 comments on commit 1a4fafd

Please sign in to comment.