Skip to content

Commit

Permalink
Pull Date parsing into ConfigInfo class, use HistoryDao.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriedenhagen committed Sep 5, 2013
1 parent 5187bab commit a36f492
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 52 deletions.
50 changes: 36 additions & 14 deletions src/main/java/hudson/plugins/jobConfigHistory/ConfigInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
Expand Down Expand Up @@ -35,7 +38,7 @@ public class ConfigInfo {
/** One of created, changed, renamed or deleted. */
private final String operation;

/** true if this information is for a Hudson job,
/** true if this information is for a Hudson job,
* as opposed to information for a system configuration file.
*/
private boolean isJob;
Expand All @@ -50,21 +53,19 @@ public class ConfigInfo {
* @param histDescr
* metadata of the change
* @return a new ConfigInfo object.
*
* @throws UnsupportedEncodingException
* if UTF-8 is not available (probably a serious error).
*/
public static ConfigInfo create(final AbstractItem item, final File file, final HistoryDescr histDescr)
throws UnsupportedEncodingException {
public static ConfigInfo create(final AbstractItem item, final File file, final HistoryDescr histDescr) {
final String encodedURL = getEncodedUrl(file);
return new ConfigInfo(
item.getFullName(),
URLEncoder.encode(file.getAbsolutePath(), "utf-8"),
encodedURL,
histDescr.getTimestamp(),
histDescr.getUser(),
histDescr.getOperation(),
histDescr.getUserID(),
true);
}

/**
* Returns a new ConfigInfo object for a system configuration file.
* @param name
Expand All @@ -74,16 +75,14 @@ public static ConfigInfo create(final AbstractItem item, final File file, final
* @param histDescr
* metadata of the change.
* @param isJob
* whether it is a job's config info or not.
* whether it is a job's config info or not.
* @return a new ConfigInfo object.
* @throws UnsupportedEncodingException
* if UTF-8 is not available
*/
public static ConfigInfo create(final String name, final File file, final HistoryDescr histDescr, final boolean isJob)
throws UnsupportedEncodingException {
public static ConfigInfo create(final String name, final File file, final HistoryDescr histDescr, final boolean isJob) {
final String encodedURL = getEncodedUrl(file);
return new ConfigInfo(
name,
URLEncoder.encode(file.getAbsolutePath(), "utf-8"),
encodedURL,
histDescr.getTimestamp(),
histDescr.getUser(),
histDescr.getOperation(),
Expand Down Expand Up @@ -180,8 +179,31 @@ public boolean getIsJob() {
return isJob;
}

@Override public String toString() {
@Override
public String toString() {
return operation + " on " + file + " @" + date;
}

private static String getEncodedUrl(final File file) throws RuntimeException {
final String encodedURL;
try {
encodedURL = URLEncoder.encode(file.getAbsolutePath(), "utf-8");
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException("Could not encode " + file.getAbsolutePath(), ex);
}
return encodedURL;
}

/**
* Returns a {@link Date}.
*
* @return The parsed date as a java.util.Date.
*/
public Date parsedDate() {
try {
return new SimpleDateFormat(JobConfigHistoryConsts.ID_FORMATTER).parse(getDate());
} catch (ParseException ex) {
throw new RuntimeException("Could not parse Date" + getDate(), ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.listeners.RunListener;
import java.util.Map;
import java.util.SortedMap;
import jenkins.model.RunAction2;

/**
Expand Down Expand Up @@ -83,25 +85,18 @@ public void onStarted(AbstractBuild build, TaskListener listener) {

//get timestamp of config-change
final ArrayList<ConfigInfo> configs = new ArrayList<ConfigInfo>();
final File historyRootDir = Hudson.getInstance()
.getPlugin(JobConfigHistory.class).getHistoryDir(project.getConfigFile());
if (historyRootDir.exists()) {
try {
for (final File historyDir : historyRootDir.listFiles(JobConfigHistory.HISTORY_FILTER)) {
final XmlFile historyXml = new XmlFile(new File(historyDir, JobConfigHistoryConsts.HISTORY_FILE));
final HistoryDescr histDescr = (HistoryDescr) historyXml.read();
final ConfigInfo config = ConfigInfo.create(project, historyDir, histDescr);
configs.add(config);
}
} catch (IOException ex) {
LOG.finest("Could not parse history files: " + ex);
}
final HistoryDao historyDao = PluginUtils.getHistoryDao();
final SortedMap<String, HistoryDescr> revisions = historyDao.getRevisions(project);
for (Map.Entry<String, HistoryDescr> revision : revisions.entrySet()) {
final XmlFile historyXml = historyDao.getOldRevision(project, revision.getKey());
final File historyDir = historyXml.getFile();
final ConfigInfo config = ConfigInfo.create(project, historyDir, revision.getValue());
configs.add(config);
}

if (configs.size() > 1) {
Collections.sort(configs, ConfigInfoComparator.INSTANCE);
final ConfigInfo lastChange = Collections.min(configs, ConfigInfoComparator.INSTANCE);
final Date lastConfigChange = parseDate(lastChange);
final Date lastConfigChange = lastChange.parsedDate();

if (lastBuildDate != null && lastConfigChange.after(lastBuildDate)) {
final String[] dates = {lastChange.getDate(), findLastRelevantConfigChangeDate(configs, lastBuildDate)};
Expand All @@ -124,30 +119,14 @@ public void onStarted(AbstractBuild build, TaskListener listener) {
private String findLastRelevantConfigChangeDate(ArrayList<ConfigInfo> configs, Date lastBuildDate) {
for (int i = 1; i < configs.size(); i++) {
final ConfigInfo oldConfigChange = configs.get(i);
final Date changeDate = parseDate(oldConfigChange);
final Date changeDate = oldConfigChange.parsedDate();
if (changeDate != null && changeDate.before(lastBuildDate)) {
return oldConfigChange.getDate();
}
}
return configs.get(1).getDate();
}

/**
* Parses the date from a config info into a java.util.Date.
*
* @param config A ConfigInfo.
* @return The parsed date as a java.util.Date.
*/
private Date parseDate(ConfigInfo config) {
Date date = null;
try {
date = new SimpleDateFormat(JobConfigHistoryConsts.ID_FORMATTER).parse(config.getDate());
} catch (ParseException ex) {
LOG.finest("Could not parse Date: " + ex);
}
return date;
}

} // end Listener

/**
Expand Down
29 changes: 23 additions & 6 deletions src/test/java/hudson/plugins/jobConfigHistory/ConfigInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hudson.model.ItemGroup;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.*;
import org.junit.Test;
Expand All @@ -21,8 +22,12 @@ public class ConfigInfoTest {
private final AbstractItem itemMock = mock(AbstractItem.class);

private final File file = new File("");

private final HistoryDescr historyDescr = new HistoryDescr("Firstname Lastname", "userId", "operation", "123");

private static final String DATE = "2012-11-21_11-29-12";

private final HistoryDescr historyDescr = new HistoryDescr(
"Firstname Lastname", "userId", "operation", DATE);


public ConfigInfoTest() {
when(itemGroupMock.getFullName()).thenReturn("does not matter parent");
Expand All @@ -34,21 +39,21 @@ public ConfigInfoTest() {
* Test of create method, of class ConfigInfo.
*/
@Test
public void testCreate_3args() throws UnsupportedEncodingException {
public void testCreate_3args() {
ConfigInfo sut = ConfigInfo.create(itemMock, file, historyDescr);
assertNotNull(sut);
assertEquals("Firstname Lastname", sut.getUser());
assertEquals("userId", sut.getUserID());
assertEquals("operation", sut.getOperation());
assertEquals("123", sut.getDate());
assertEquals(DATE, sut.getDate());
assertEquals(true, sut.getIsJob());
}

/**
* Test of create method, of class ConfigInfo.
*/
@Test
public void testCreate_4args() throws UnsupportedEncodingException {
public void testCreate_4args() {
ConfigInfo sut = ConfigInfo.create("jobName", file, historyDescr, false);
assertNotNull(sut);
assertEquals(false, sut.getIsJob());
Expand All @@ -58,10 +63,22 @@ public void testCreate_4args() throws UnsupportedEncodingException {
* Test of toString method, of class ConfigInfo.
*/
@Test
public void testToString() throws UnsupportedEncodingException {
public void testToString() {
ConfigInfo sut = ConfigInfo.create("jobName", file, historyDescr, false);
String result = sut.toString();
assertThat(result, startsWith("operation on "));
}

/**
* Test of parsedDate method, of class ConfigInfo.
*/
@Test
public void testParsedDate() {
//"2012-11-21_11-29-12"
ConfigInfo sut = ConfigInfo.create("jobName", file, historyDescr, false);
Date expResult = new Date(112, 10, 21, 11, 29, 12);
Date result = sut.parsedDate();
assertEquals(expResult, result);
}

}

0 comments on commit a36f492

Please sign in to comment.