Skip to content

Commit

Permalink
- settings env variables now
Browse files Browse the repository at this point in the history
- introduced "naming strategies" to make that part extendable separate from the parsers
-
  • Loading branch information
rmalchow committed May 25, 2015
1 parent 053b764 commit 73977ac
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 9 deletions.
27 changes: 25 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.554</version>
<version>1.614</version>
</parent>

<artifactId>semantic-versioning-plugin</artifactId>
Expand All @@ -16,6 +16,29 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.0</version>
<configuration>
<goals>deploy</goals>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-gitexe</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<version>1.106</version>
<configuration>
<pluginFirstClassLoader>true</pluginFirstClassLoader>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down Expand Up @@ -45,7 +68,7 @@
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>maven-plugin</artifactId>
<version>1.509.2</version>
<version>2.9</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public class ColumnDisplayStrategies {
public static final String LAST_SUCCESSFUL_BUILD_VERSION_STRATEGY = "Show Last Successful Semantic Version";
public static final String NA_STRATEGY = "Show 'N/A'";
}

public class NamingStrategies {
public static final String NOOP_NAMING = "none";
public static final String MAVEN_RELEASE_DEVELOPMENT = "Maven, release and development version";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,51 @@

package org.jenkinsci.plugins.SemanticVersioning;

import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.EnvironmentContributingAction;
import hudson.model.TaskListener;
import hudson.model.AbstractBuild;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Builder;
import hudson.util.ListBoxModel;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import jenkins.model.Jenkins;
import net.sf.json.JSONObject;

import org.jenkinsci.plugins.SemanticVersioning.naming.NamingStrategy;
import org.jenkinsci.plugins.SemanticVersioning.parsing.BuildDefinitionParser;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import java.io.IOException;

public class SemanticVersioningBuilder extends Builder {

private BuildDefinitionParser parser;
private NamingStrategy namingStrategy;
private String envVariable = "BUILD_VERSION";
private boolean useJenkinsBuildNumber;

@DataBoundConstructor
public SemanticVersioningBuilder(String parser, boolean useJenkinsBuildNumber) {
public SemanticVersioningBuilder(String parser, String namingStrategy, boolean useJenkinsBuildNumber, String envVariable) {
this.useJenkinsBuildNumber = useJenkinsBuildNumber;
this.envVariable = envVariable;
try {
this.parser = (BuildDefinitionParser) Jenkins.getInstance().getExtensionList(parser).iterator().next();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
this.namingStrategy = (NamingStrategy) Jenkins.getInstance().getExtensionList(namingStrategy).iterator().next();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

@Extension
Expand All @@ -76,18 +92,44 @@ public String getParser() {
return this.parser.getClass().getCanonicalName();
}

public String getNamingStrategy() {
return this.namingStrategy.getClass().getCanonicalName();
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
SemanticVersioningProcesser semanticVersioningApp = new SemanticVersioningProcesser(
build,
this.parser,
this.useJenkinsBuildNumber,
Messages.SEMANTIC_VERSION_FILENAME);
semanticVersioningApp.determineSemanticVersion();
AppVersion appVersion = semanticVersioningApp.determineSemanticVersion();
int buildNumber = -1;
try {
buildNumber = Integer.parseInt(build.getEnvironment(TaskListener.NULL).get("BUILD_NUMBER"));
} catch (Exception e) {
}
try {
Map<String,String> vars = new HashMap<String,String>();
namingStrategy.exportNames(appVersion,vars,useJenkinsBuildNumber,buildNumber);

build.addAction(new InjectVersionVarsAction(vars));
} catch (Exception e) {
e.printStackTrace();
throw new IOException(e);
}
return true;
}

@Extension(ordinal = 9999)
public String getEnvVariable() {
return envVariable;
}

public void setEnvVariable(String envVariable) {
this.envVariable = envVariable;
}

@Extension(ordinal = 9999)
public static final class SemanticVersioningBuilderDescriptor extends BuildStepDescriptor<Builder> {
/**
* In order to load the persisted global configuration, you have to call
Expand All @@ -110,10 +152,18 @@ public ListBoxModel doFillParserItems() {
parsersModel.add(parser.getDescriptor().getDisplayName(), parser
.getClass().getCanonicalName());
}

return parsersModel;
}

public ListBoxModel doFillNamingStrategyItems() {
ListBoxModel namingStrategiesModel = new ListBoxModel();
for (NamingStrategy namingStrategy : Jenkins.getInstance()
.getExtensionList(NamingStrategy.class)) {
namingStrategiesModel.add(namingStrategy.getDescriptor().getDisplayName(), namingStrategy.getClass().getCanonicalName());
}
return namingStrategiesModel;
}

public boolean getDefaultUseJenkinsBuildNumber() {
return true;
}
Expand All @@ -133,4 +183,49 @@ public Builder newInstance(StaplerRequest req, JSONObject formData) throws FormE
return super.newInstance(req, formData);
}
}


private class InjectVersionVarsAction implements EnvironmentContributingAction {

private Map<String,String> versions;

public InjectVersionVarsAction(Map<String, String> versions) {
super();
this.versions = versions;
}


public String getIconFileName() {
return null;
}

public String getDisplayName() {
return Messages.DISPLAY_NAME;
}

public String getUrlName() {
return null;
}

public void buildEnvVars(AbstractBuild<?, ?> build, EnvVars env) {
if (env == null) {
return;
}

if (versions == null) {
return;
}

for (Map.Entry<String, String> entry : versions.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key != null && value != null) {
env.put(key, value);
}
}

}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jenkinsci.plugins.SemanticVersioning.naming;

import java.util.Map;
import java.util.logging.Logger;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Descriptor;

import org.jenkinsci.plugins.SemanticVersioning.AppVersion;
import org.jenkinsci.plugins.SemanticVersioning.Messages;
import org.jenkinsci.plugins.SemanticVersioning.parsing.AbstractSemanticParserDescription;

@Extension
public class MavenReleaseNamingStrategy implements NamingStrategy {

private static Logger logger = Logger.getLogger(String.valueOf(AppVersion.class));

public Descriptor<NamingStrategy> getDescriptor() {
return new AbstractSemanticParserDescription() {

@Override
public String getDisplayName() {

return Messages.NamingStrategies.MAVEN_RELEASE_DEVELOPMENT;
}
};
}

public void exportNames(AppVersion current, Map<String,String> vars, boolean useBuildNumber, int buildNumber) {
logger.info("SemanticVersioningProcesser::getAppVersion -> maven naming: " +current.toJsonString());
String releaseVersion = current.getMajor()+"."+current.getMinor()+(useBuildNumber?"."+buildNumber:"");
String developmentVersion = current.getMajor()+"."+(useBuildNumber?current.getMinor()+"."+(buildNumber+1):""+(current.getMinor()+1))+"-SNAPSHOT";
logger.info("SemanticVersioningProcesser::getAppVersion -> setting release version: " +releaseVersion);
logger.info("SemanticVersioningProcesser::getAppVersion -> setting development version: " +developmentVersion);
vars.put("releaseVersion", releaseVersion);
vars.put("developmentVersion", developmentVersion);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jenkinsci.plugins.SemanticVersioning.naming;

import hudson.model.Describable;

import java.util.Map;

import org.jenkinsci.plugins.SemanticVersioning.AppVersion;

public interface NamingStrategy extends Describable<NamingStrategy> {

public void exportNames(AppVersion current, Map<String,String> vars, boolean useBuildNumber, int buildNumber);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jenkinsci.plugins.SemanticVersioning.naming;

import hudson.Extension;
import hudson.model.Descriptor;

import java.util.Map;
import java.util.logging.Logger;

import org.jenkinsci.plugins.SemanticVersioning.AppVersion;
import org.jenkinsci.plugins.SemanticVersioning.Messages;
import org.jenkinsci.plugins.SemanticVersioning.parsing.AbstractSemanticParserDescription;

@Extension
public class NoopNamingStrategy implements NamingStrategy {

private static Logger logger = Logger.getLogger(String.valueOf(AppVersion.class));

public Descriptor<NamingStrategy> getDescriptor() {
return new AbstractSemanticParserDescription() {

@Override
public String getDisplayName() {

return Messages.NamingStrategies.NOOP_NAMING;
}
};
}

public void exportNames(AppVersion current, Map<String,String> vars, boolean useBuildNumber, int buildNumber) {
// it's called "noop", d'oh!
logger.info("SemanticVersioningProcesser::getAppVersion -> not setting anything (NOOP): " + current.toJsonString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@
import hudson.model.AbstractBuild;
import hudson.model.Descriptor;
import hudson.remoting.VirtualChannel;

import org.jenkinsci.plugins.SemanticVersioning.AppVersion;
import org.jenkinsci.plugins.SemanticVersioning.InvalidBuildFileFormatException;
import org.jenkinsci.plugins.SemanticVersioning.Messages;
import org.jenkinsci.remoting.RoleChecker;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;

import java.io.File;
import java.io.IOException;

Expand Down Expand Up @@ -121,6 +124,9 @@ public Document invoke(File pom, VirtualChannel channel)
}
}

public void checkRoles(RoleChecker arg0) throws SecurityException {
}

This comment has been minimized.

Copy link
@jglick

jglick Aug 3, 2015

Member

Probably wrong. See jenkinsci/remoting#56.

});
} catch (InterruptedException e) {
throw new InvalidBuildFileFormatException(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
-->
<f:entry title="Version Number Parser" field="parser">
<f:select />
</f:entry>
<f:entry title="Naming Strategies" field="namingStrategy">
<f:select />
</f:entry>
<f:entry title="Environment Variable" field="envVariable">
<f:textbox />
</f:entry>
<f:entry
title="Build Number Source"
Expand Down

0 comments on commit 73977ac

Please sign in to comment.