Skip to content

Commit

Permalink
Merge pull request #15 from MadsNielsen/JENKINS19657
Browse files Browse the repository at this point in the history
[JENKINS-19657]
  • Loading branch information
MadsNielsen committed Dec 20, 2013
2 parents 648e23e + 209a49a commit e5e71e9
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 33 deletions.
Expand Up @@ -10,7 +10,7 @@

public class ClearCaseVersionNumberTemplate extends Template {

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

@Override
public String parse( CCUCMBuildAction action, String args ) throws TemplateException {
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/net/praqma/hudson/nametemplates/EnvTemplate.java
Expand Up @@ -9,7 +9,7 @@

public class EnvTemplate extends Template {

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

@Override
public String parse( CCUCMBuildAction action, String args ) throws TemplateException {
Expand All @@ -21,18 +21,11 @@ public String parse( CCUCMBuildAction action, String args ) throws TemplateExcep
logger.warning( "I could not get env vars: " + e.getMessage() );
return "?";
}

logger.finest( "ENV VARS: " + vars );
logger.finest( "ENV VARS: " + System.getenv() );

if( vars.containsKey( args ) ) {
logger.finest( "EnvVars: " + args + "=" + vars.get( args ) );
if( vars.containsKey( args ) ) {
return vars.get( args );
} else if( action.getBuild().getBuildVariables().containsKey( args ) ) {
logger.finest( "BuildVars: " + args + "=" + action.getBuild().getBuildVariables().get( args ) );
} else if( action.getBuild().getBuildVariables().containsKey( args ) ) {
return action.getBuild().getBuildVariables().get( args );
} else if( System.getenv().containsKey( args ) ) {
logger.finest( "Vars: " + args + "=" + System.getenv( args ) );
return vars.get( args );
} else {
return "?";
Expand Down
Expand Up @@ -47,7 +47,7 @@ public String invoke(File f, VirtualChannel channel) throws IOException, Interru
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;
}
}
}

}
Expand Down
61 changes: 49 additions & 12 deletions src/main/java/net/praqma/hudson/nametemplates/NameTemplate.java
@@ -1,6 +1,7 @@
package net.praqma.hudson.nametemplates;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
Expand All @@ -12,10 +13,10 @@

public class NameTemplate {

private static Map<String, Template> templates = new HashMap<String, Template>();
private static Logger logger = Logger.getLogger( NameTemplate.class.getName() );
private static final Map<String, Template> templates = new HashMap<String, Template>();
private static final Logger logger = Logger.getLogger( NameTemplate.class.getName() );

static {
static {
templates.put( "date", new DateTemplate() );
templates.put( "time", new TimeTemplate() );
templates.put( "stream", new StreamTemplate() );
Expand All @@ -32,14 +33,18 @@ public class NameTemplate {
private static Pattern rx_ = Pattern.compile( "(\\[.*?\\])" );
private static Pattern rx_checkFinal = Pattern.compile( "^[\\w\\._-]*$" );

public static void validateTemplates( CCUCMBuildAction action ) {
/**
* This is the method we use to validate templates.
* @param action
*/
public static void validateTemplates( CCUCMBuildAction action) {
logger.finer( "Validating templates for " + action );
Set<String> keys = templates.keySet();
for( String key : keys ) {
String r;
//Only evaluate those that are actually chosen
Set<String> keys = getChosenTemplates(action.getNameTemplate());
for( String key : keys ) {
try {
logger.finer( "Validating " + key );
r = templates.get( key ).parse( action, "" );
templates.get( key ).parse( action, "" );
} catch (TemplateException e) {
logger.warning( "Could not validate " + key );
}
Expand All @@ -54,6 +59,34 @@ public static String trim( String template ) {
return template;
}

/**
* Method that extracts the names of the chose templates.
* @param templatestring
* @return a Set containing the name of the templates chosen.
*/
public static Set<String> getChosenTemplates(String templatestring) {
Set<String> chosenTemplates = new HashSet<String>();
Matcher m = rx_.matcher( templatestring );

while( m.find() ) {
String replace = m.group(1);
String templateName = replace.toLowerCase().substring( 1, replace.length()-1 );
if( templateName.contains( "=" ) ) {
String[] s = templateName.split( "=" );
templateName = s[0];

}
chosenTemplates.add(templateName);
}
return chosenTemplates;
}

/**
* Checks to see if the templates are valid, and that the template names are available.
* @param template
* @return
* @throws TemplateException
*/
public static boolean testTemplate( String template ) throws TemplateException {
Matcher m = rx_.matcher( template );
String result = template;
Expand Down Expand Up @@ -83,8 +116,14 @@ public static boolean testTemplate( String template ) throws TemplateException {
return true;
}

/**
* At this point. We do not need to filter chosen templates.
* @param template
* @param action
* @return
* @throws TemplateException
*/
public static String parseTemplate( String template, CCUCMBuildAction action ) throws TemplateException {
logger.finer( "Parsing template for " + action );
Matcher m = rx_.matcher( template );
String result = template;

Expand All @@ -103,9 +142,7 @@ public static String parseTemplate( String template, CCUCMBuildAction action ) t
templateName = s[0];
args = s[1];
}

logger.finer( "--->" + templateName + ": " + args );


if( !templates.containsKey( templateName ) ) {
throw new TemplateException( "The template " + templateName + " does not exist" );
} else {
Expand Down
Expand Up @@ -10,7 +10,7 @@ public String parse( CCUCMBuildAction action, String args ) {
try {
return action.getBaseline().getUser();
} catch ( Exception e ) {
return "unknownstream";
return "unknownuser";
}
}
}
13 changes: 5 additions & 8 deletions src/main/java/net/praqma/hudson/notifier/CCUCMNotifier.java
Expand Up @@ -42,7 +42,7 @@ public class CCUCMNotifier extends Notifier {
private PrintStream out;

private Status status;
private static Logger logger = Logger.getLogger( CCUCMNotifier.class.getName() );
private static final Logger logger = Logger.getLogger( CCUCMNotifier.class.getName() );
private String jobName = "";
private Integer jobNumber = 0;
public static String logShortPrefix = String.format("[%s]", Config.nameShort);
Expand Down Expand Up @@ -128,7 +128,6 @@ public boolean perform( AbstractBuild<?, ?> build, Launcher launcher, BuildListe
out.println( "NotifierException: " + ne.getMessage() );
} catch( IOException e ) {
out.println( String.format( "%s Couldn't set build description", logShortPrefix ) );
//out.println( "[" + Config.nameShort + "] Couldn't set build description." );
}
} else {
if( action.getResolveBaselineException() != null ) {
Expand Down Expand Up @@ -235,19 +234,17 @@ private void processBuild( AbstractBuild<?, ?> build, Launcher launcher, BuildLi

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

pstate.setWorkspace( workspace );

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

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


if( action != null ) {
action.setCreatedBaseline( targetbaseline );
}
//At this point action can only be non-null.
action.setCreatedBaseline( targetbaseline );

} catch( Exception e ) {
ExceptionUtils.print( e, out, false );
logger.warning( "Failed to create baseline on stream" );
Expand Down

0 comments on commit e5e71e9

Please sign in to comment.