Skip to content
Permalink
Browse files

Merge pull request #74 from Siriah/master

[JENKINS-30295] Implement getCommitId and getTimestamp methods of ChangeLogSet.Entry
  • Loading branch information...
jglick committed Oct 11, 2016
2 parents ef1b836 + 060365c commit 8063cc0c601e1864528186b582bbd28da0dffd75
@@ -2,8 +2,8 @@

import hudson.model.User;
import hudson.scm.ChangeLogSet;
import hudson.scm.EditType;
import hudson.scm.ChangeLogSet.AffectedFile;
import hudson.scm.EditType;

import java.util.ArrayList;
import java.util.Arrays;
@@ -83,15 +83,28 @@ public long getRev() {
}

/**
* Gets the globally unique changeset ID. For general purpose use, use {@link #getNode()}. This method is intended
* for use via reflection by the email-ext plugin.
* Gets the globally unique changeset ID. For general purpose use, use {@link #getNode()}. This method was intended
* for use via reflection by the email-ext plugin, but versions 1.40 and later no longer need it.
*/
@Deprecated
public String getRevision() {
return node;
}

@Override
public String getCommitId() {
return node;
}

@Override
public long getTimestamp() {
//By default, the String 'date' is in the format '[TIMESTAMP].0[TIMEZONE_OFFSET]' where TIMESTAMP is the number
//of seconds (not milliseconds) since the epoch.
return Long.parseLong(date.split("\\.")[0]) * 1000;
}

/**
* Returns the date of the changeset. Also used via reflection by the email-ext plugin.
* Returns the timestamp of the changeset as a string.
*/
@Exported
public String getDate() {
@@ -5,8 +5,8 @@
import hudson.Launcher;
import hudson.model.Action;
import hudson.model.FreeStyleBuild;
import hudson.model.TaskListener;
import hudson.model.FreeStyleProject;
import hudson.model.TaskListener;
import hudson.scm.PollingResult;
import hudson.util.ArgumentListBuilder;
import hudson.util.StreamTaskListener;
@@ -18,13 +18,13 @@
import java.util.Set;
import java.util.TreeSet;

import static org.junit.Assert.*;
import org.junit.Assume;
import org.junit.internal.AssumptionViolatedException;
import org.junit.rules.ExternalResource;

import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.*;

public final class MercurialRule extends ExternalResource {

private TaskListener listener;
@@ -123,4 +123,9 @@ public String getLastChangesetId(File repo) throws Exception {
return hgExe().popen(new FilePath(repo), listener, false, new ArgumentListBuilder("log", "-l1", "--template", "{node}"));
}

public long getLastChangesetUnixTimestamp(File repo) throws Exception {
//hgdate returns the date as a pair of numbers: "1157407993 25200" (Unix timestamp, timezone offset).
String date = hgExe().popen(new FilePath(repo), listener, false, new ArgumentListBuilder("log", "-l1", "--template", "{date|hgdate}"));
return Long.valueOf(date.split(" ")[0]);
}
}
@@ -17,6 +17,7 @@
import hudson.scm.ChangeLogSet.Entry;
import hudson.scm.PollingResult;
import hudson.scm.SCM;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
@@ -29,16 +30,19 @@
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.jenkinsci.plugins.multiplescms.MultiSCM;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.FakeLauncher;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.*;

public abstract class SCMTestBase {

@Rule public JenkinsRule j = new JenkinsRule();
@@ -814,6 +818,25 @@ private void initRepoWithTag() throws Exception {
assertEquals(null, b.getEnvironment().get("MERCURIAL_REVISION_BRANCH"));
}

@Issue("JENKINS-30295")
@Test public void testChangeSetApiVersion1407Methods() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
p.setScm(new MercurialSCM(hgInstallation(), repo.getPath(), null, null, null, null, false));
m.hg(repo, "init");
m.touchAndCommit(repo, "f1");
p.scheduleBuild2(0).get();

m.touchAndCommit(repo, "f2");
MercurialChangeSet changeSet = (MercurialChangeSet) p.scheduleBuild2(0).get().getChangeSet().iterator().next();

String commitId = m.getLastChangesetId(repo);
long timestampInSeconds = m.getLastChangesetUnixTimestamp(repo);

assertEquals(commitId, changeSet.getCommitId());
assertEquals(timestampInSeconds * 1000, changeSet.getTimestamp());

}

/* TODO the following will pass, but canUpdate is not going to work without further changes:
public void testParameterizedBuildsSource() throws Exception {
p = createFreeStyleProject();

0 comments on commit 8063cc0

Please sign in to comment.
You can’t perform that action at this time.