/
BuildTimestampEnvironmentContributor.java
70 lines (58 loc) · 2.44 KB
/
BuildTimestampEnvironmentContributor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.orctom.jenkins.plugin.buildtimestamp;
import com.orctom.jenkins.plugin.buildtimestamp.BuildTimestampWrapper.DescriptorImpl;
import hudson.EnvVars;
import hudson.Extension;
import hudson.model.EnvironmentContributor;
import hudson.model.Run;
import hudson.model.TaskListener;
import jenkins.model.Jenkins;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import static com.orctom.jenkins.plugin.buildtimestamp.BuildTimestampPlugin.DEFAULT_PROPERTY;
/**
* BuildTimestamp Environment Contributor
* Created by hao on 12/16/15.
*/
@Extension
public class BuildTimestampEnvironmentContributor extends EnvironmentContributor {
@Override
public void buildEnvironmentFor(Run run, EnvVars envVars, TaskListener listener)
throws IOException, InterruptedException {
if (null == envVars.get(DEFAULT_PROPERTY)) {
try {
Map<String, String> timestampProperties = buildTimestamp(run.getTimestamp());
envVars.putAll(timestampProperties);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private Map<String, String> buildTimestamp(Calendar timestamp) {
Map<String, String> timestampProperties = new HashMap<String, String>();
DescriptorImpl descriptor = getDescriptorImpl();
if (descriptor.isEnableBuildTimestamp()) {
TimeZone timeZone = TimeZone.getTimeZone(descriptor.getTimezone());
String pattern = descriptor.getPattern();
setTimestamp(timestampProperties, DEFAULT_PROPERTY, format(timestamp, timeZone, pattern, ""));
Set<BuildTimestampExtraProperty> extraProperties = descriptor.getExtraProperties();
for (BuildTimestampExtraProperty property : extraProperties) {
setTimestamp(timestampProperties, property.getKey(), format(timestamp, timeZone, property.getValue(), property.getShiftExpression()));
}
}
return timestampProperties;
}
private void setTimestamp(Map<String, String> timestampProperties, String key, String value) {
timestampProperties.put(key, value);
System.setProperty(key, value);
}
private String format(Calendar timestamp, TimeZone timeZone, String pattern, String shiftExpression) {
Calendar timestamp2 = ShiftExpressionHelper.doShift(timestamp, shiftExpression);
SimpleDateFormat format = new SimpleDateFormat(pattern);
format.setTimeZone(timeZone);
return format.format(timestamp2.getTime());
}
public DescriptorImpl getDescriptorImpl() {
return (DescriptorImpl) Jenkins.getInstance().getDescriptorOrDie(BuildTimestampWrapper.class);
}
}