Skip to content

Commit

Permalink
Merge pull request #86 from fbelzunc/JENKINS-33147
Browse files Browse the repository at this point in the history
[FIXED JENKINS-33147] Customize Date format string and timezone
  • Loading branch information
oleg-nenashev committed Mar 14, 2016
2 parents 1ddf9ad + 126d292 commit be96e51
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
@@ -0,0 +1,61 @@
package hudson.plugins.promoted_builds;

import hudson.Extension;
import jenkins.model.GlobalConfiguration;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;

/**
* Global Jenkins configuration for Promoted Builds
*
* @since 2.26
*/

@Extension
public class GlobalBuildPromotedBuilds extends GlobalConfiguration {

/**
* By default ISO 8601 like 2016-10-12T09:30Z to be used with
* environmental variable $PROMOTED_TIMESTAMP.
*/
private String dateFormat;

/**
* By default Java timezone setting to be used with environmental
* variable $PROMOTED_TIMESTAMP.
*
* Other time zones can be selected if field is filled.
*/
private String timeZone;

public GlobalBuildPromotedBuilds() {
load();
}

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
req.bindJSON(this, json);
save();
return true;
}

public String getTimeZone() {
return timeZone;
}

public void setTimeZone(String timeZone) {
this.timeZone = timeZone;
}

public String getDateFormat() {
return dateFormat;
}

public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}

public static GlobalBuildPromotedBuilds get() {
return GlobalBuildPromotedBuilds.all().get(GlobalBuildPromotedBuilds.class);
}
}
40 changes: 39 additions & 1 deletion src/main/java/hudson/plugins/promoted_builds/Promotion.java
Expand Up @@ -31,15 +31,21 @@

import jenkins.model.Jenkins;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.export.Exported;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;

/**
Expand Down Expand Up @@ -104,7 +110,37 @@ public EnvVars getEnvironment(TaskListener listener) throws IOException, Interru
e.put("PROMOTED_JOB_FULL_NAME", target.getParent().getFullName());
e.put("PROMOTED_NUMBER", Integer.toString(target.getNumber()));
e.put("PROMOTED_ID", target.getId());
e.put("PROMOTED_TIMESTAMP", target.getTimestampString2());
GlobalBuildPromotedBuilds globalBuildPromotedBuilds = GlobalBuildPromotedBuilds.get();
String dateFormat = globalBuildPromotedBuilds.getDateFormat();
String timeZone = globalBuildPromotedBuilds.getTimeZone();
SimpleDateFormat format = null;
TimeZone tz = null;

if (dateFormat != null && !StringUtils.isBlank(dateFormat)) {
try {
format = new SimpleDateFormat(dateFormat);
} catch (IllegalArgumentException e1) {
LOGGER.log(Level.WARNING, String.format("An illegal date format was introduced: %s. Default ISO 8601 yyyy-MM-dd'T'HH:mmZ will be used", dateFormat), e1);
format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
}
} else {
// Per ISO 8601
format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mmZ");
}

if (timeZone !=null && !StringUtils.isBlank(timeZone)) {
try {
tz = TimeZone.getTimeZone(timeZone);
} catch (IllegalArgumentException e2) {
LOGGER.log(Level.WARNING, String.format("An illegal time zone was introduced: %s. Default GMT time zone will be used", timeZone), e2);
tz = TimeZone.getTimeZone("GMT");
}
} else {
tz = TimeZone.getTimeZone("GMT");
}

format.setTimeZone(tz);
e.put("PROMOTED_TIMESTAMP", format.format(new Date()));
e.put("PROMOTED_DISPLAY_NAME", target.getDisplayName());
e.put("PROMOTED_USER_NAME", getUserName());
e.put("PROMOTED_USER_ID", getUserId());
Expand Down Expand Up @@ -432,4 +468,6 @@ public static void buildParametersAction(List<Action> actions, AbstractBuild<?,
// Create list of actions to pass to scheduled build
actions.add(new ParametersAction(params));
}

private static final Logger LOGGER = Logger.getLogger(Promotion.class.getName());
}
@@ -0,0 +1,11 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:section title="Build Pipeline Plugin">
<f:entry field="dateFormat" title="Date format">
<f:textbox />
</f:entry>
<f:entry field="timeZone" title="Time zone">
<f:textbox />
</f:entry>
</f:section>
</j:jelly>
@@ -0,0 +1,5 @@
<div>
Used to specify the date format for $PROMOTED_TIMESTAMP.
<br/>
By default, ISO 8601 formatted presentation of current moment like: 2016-10-12T09:30Z.
</div>
@@ -0,0 +1,5 @@
<div>
Used to determinate the TZ for $PROMOTED_TIMESTAMP.
<br/>
By default, Java time zone.
</div>

0 comments on commit be96e51

Please sign in to comment.