Skip to content

Commit

Permalink
Merge pull request #74 from Siriah/master
Browse files Browse the repository at this point in the history
[JENKINS-30295] Implement getCommitId and getTimestamp methods of ChangeLogSet.Entry
  • Loading branch information
jglick committed Oct 11, 2016
2 parents ef1b836 + 060365c commit 8063cc0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
21 changes: 17 additions & 4 deletions src/main/java/hudson/plugins/mercurial/MercurialChangeSet.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
11 changes: 8 additions & 3 deletions src/test/java/hudson/plugins/mercurial/MercurialRule.java
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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]);
}
}
25 changes: 24 additions & 1 deletion src/test/java/hudson/plugins/mercurial/SCMTestBase.java
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 8063cc0

Please sign in to comment.