Skip to content

Commit

Permalink
add pipeline support (#176)
Browse files Browse the repository at this point in the history
* pom.xml: update plugin dependency to 2.29

Signed-off-by: Alejandro del Castillo <alejandro.delcastillo@ni.com>

* Testing.md: add COMPUTERNAME env variable dependency

Signed-off-by: Alejandro del Castillo <alejandro.delcastillo@ni.com>

* tfs: add pipeline support

Follow the guide on https://github.com/jenkinsci/pipeline-plugin/blob/master/DEVGUIDE.md.

Signed-off-by: Alejandro del Castillo <alejandro.delcastillo@ni.com>
  • Loading branch information
adelcast authored and jpricket committed Nov 2, 2017
1 parent 004f2ca commit 21632ef
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 153 deletions.
1 change: 1 addition & 0 deletions Testing.md
Expand Up @@ -42,6 +42,7 @@ You should also provide the following properties:
2. tfs_collection_url - set this to the full url of the collection (ex. http://tfs.corp.example.com:8081/tfs/jenkins-tfs-plugin)
3. tfs_user_name - set this to the test user you gave permissions to above (the default value if not provided is **jenkins-tfs-plugin**)
4. tfs_user_password - set this to the password of the test user (the default value if not provided is **for-test-only**)
5. Set the COMPUTERNAME environment variable as the end-to-end tests rely on its presence

Example:

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.19</version>
<version>2.29</version>
</parent>

<artifactId>tfs-parent</artifactId>
Expand Down
19 changes: 10 additions & 9 deletions tfs/src/main/java/hudson/plugins/tfs/ChangeSetReader.java
@@ -1,4 +1,3 @@
//CHECKSTYLE:OFF
package hudson.plugins.tfs;

import java.io.File;
Expand All @@ -10,30 +9,32 @@
import java.util.ArrayList;
import java.util.List;

import hudson.model.Run;
import hudson.scm.RepositoryBrowser;
import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;

import hudson.model.AbstractBuild;
import hudson.plugins.tfs.model.ChangeLogSet;
import hudson.plugins.tfs.model.ChangeSet;
import hudson.scm.ChangeLogParser;
import hudson.util.Digester2;

/**
* TeamFoundation change log reader.
*
*
* @author Erik Ramfelt
*/
*/
public class ChangeSetReader extends ChangeLogParser {

@Override
public ChangeLogSet parse(AbstractBuild build, File changelogFile) throws IOException, SAXException {
public ChangeLogSet parse(final Run build, final RepositoryBrowser<?> browser, final File changelogFile) throws IOException, SAXException {
try (FileInputStream stream = new FileInputStream(changelogFile); Reader reader = new InputStreamReader(stream, Charset.defaultCharset())) {
return parse(build, reader);
return parse(build, browser, reader);
}
}

public ChangeLogSet parse(AbstractBuild<?,?> build, Reader reader) throws IOException, SAXException {
/** Performs the actual parsing. */
public ChangeLogSet parse(final Run build, final RepositoryBrowser<?> browser, final Reader reader) throws IOException, SAXException {
List<ChangeSet> changesetList = new ArrayList<ChangeSet>();
Digester digester = new Digester2();
digester.push(changesetList);
Expand All @@ -50,9 +51,9 @@ public ChangeLogSet parse(AbstractBuild<?,?> build, Reader reader) throws IOExce
digester.addSetProperties("*/changeset/items/item");
digester.addBeanPropertySetter("*/changeset/items/item", "path");
digester.addSetNext("*/changeset/items/item", "add");

digester.parse(reader);

return new ChangeLogSet(build, changesetList);
return new ChangeLogSet(build, browser, changesetList);
}
}
272 changes: 169 additions & 103 deletions tfs/src/main/java/hudson/plugins/tfs/TeamFoundationServerScm.java

Large diffs are not rendered by default.

22 changes: 15 additions & 7 deletions tfs/src/main/java/hudson/plugins/tfs/model/ChangeLogSet.java
@@ -1,8 +1,9 @@
//CHECKSTYLE:OFF
package hudson.plugins.tfs.model;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.scm.RepositoryBrowser;

import java.util.ArrayList;
import java.util.Iterator;
Expand All @@ -11,24 +12,30 @@
/**
* ChangeLogSet for the Team Foundation Server SCM
* The log set will set the parent of the log entries in the constructor.
*
*
* @author Erik Ramfelt
*/
@SuppressFBWarnings(value = "NM_SAME_SIMPLE_NAME_AS_SUPERCLASS", justification = "Public so shouldn't be changed")
public class ChangeLogSet extends hudson.scm.ChangeLogSet<ChangeSet> {

private final List<ChangeSet> changesets;

public ChangeLogSet(AbstractBuild build, List<ChangeSet> changesets) {
super(build);
public ChangeLogSet(final Run<?, ?> build, final RepositoryBrowser<?> browser, final List<ChangeSet> changesets) {
super(build, browser);
this.changesets = changesets;
for (ChangeSet changeset : changesets) {
changeset.setParent(this);
}
}

public ChangeLogSet(AbstractBuild build, ChangeSet[] changesetArray) {
super(build);

@Deprecated
/* TODO: Used by TeamSystemWebAccessBrowserTest, should update to use non-deprecated method instead */
public ChangeLogSet(final AbstractBuild build, final ChangeSet[] changesetArray) {
this(build, build.getProject().getScm().getEffectiveBrowser(), changesetArray);
}

public ChangeLogSet(final Run<?, ?> build, final RepositoryBrowser<?> browser, final ChangeSet[] changesetArray) {
super(build, browser);
changesets = new ArrayList<ChangeSet>();
for (ChangeSet changeset : changesetArray) {
changeset.setParent(this);
Expand All @@ -41,6 +48,7 @@ public boolean isEmptySet() {
return changesets.isEmpty();
}

/** Returns a ChangeSet iterator. */
public Iterator<ChangeSet> iterator() {
return changesets.iterator();
}
Expand Down
@@ -1,4 +1,3 @@
//CHECKSTYLE:OFF
package hudson.plugins.tfs.util;

import java.io.IOException;
Expand All @@ -8,10 +7,8 @@
import java.util.Map;
import java.util.logging.Logger;

import hudson.Launcher;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Computer;
import hudson.model.Job;
import hudson.model.TaskListener;
Expand All @@ -27,54 +24,54 @@
* is being executed on (slave or master)</li>
* <li> NODE_NAME - The name of the node that the Launcher is being executed on</li>
* <li> Any environment variable that is set on the Node that the Launcher is
* being executed on (slave or master)</li>
* </ul>
*
* being executed on (slave or master)</li>
* </ul>
*
* @author Erik Ramfelt
*/
public class BuildVariableResolver implements VariableResolver<String> {
private Map<String,LazyResolver> lazyResolvers = new HashMap<String, LazyResolver>();

private Map<String, LazyResolver> lazyResolvers = new HashMap<String, LazyResolver>();

private List<VariableResolver<String>> otherResolvers = new ArrayList<VariableResolver<String>>();

private final Computer computer;

private static final Logger LOGGER = Logger.getLogger(BuildVariableResolver.class.getName());

public BuildVariableResolver(final Job<?, ?> job) {
computer = null;
lazyResolvers.put("JOB_NAME", new LazyResolver() {
public String getValue() {
return job.getName();
}
}
});
}
public BuildVariableResolver(final AbstractProject<?, ?> project, final Computer computer) {

public BuildVariableResolver(final Job<?, ?> project, final Computer computer) {
this.computer = computer;
lazyResolvers.put("JOB_NAME", new LazyResolver() {
public String getValue() {
return project.getName();
}
}
});
lazyResolvers.put("NODE_NAME", new LazyComputerResolver() {
public String getValue(Computer computer) {
public String getValue(final Computer computer) {
return (Util.fixEmpty(computer.getName()) == null ? "MASTER" : computer.getName());
}
}
});
lazyResolvers.put("USER_NAME", new LazyComputerResolver() {
public String getValue(Computer computer) throws IOException, InterruptedException {
public String getValue(final Computer computer) throws IOException, InterruptedException {
return (String) computer.getSystemProperties().get("user.name");
}
}
});
}

/**
* Constructor that can be used with a {@linkplain AbstractBuild} instance.
* Constructor that can be used with a {@linkplain AbstractBuild} instance.
* <p>
* This constructor should not be called in a method that may be called by
* {@link AbstractBuild#getEnvVars()}.
* {@link AbstractBuild#getEnvVars()}.
* @param build used to get the project and the build env vars
* @param computer used to retrieve the name of the remote computer or of the account under which Jenkins runs
*
Expand All @@ -85,14 +82,17 @@ public String getValue(Computer computer) throws IOException, InterruptedExcepti
public BuildVariableResolver(final AbstractBuild<?, ?> build, final Computer computer)
throws IOException, InterruptedException {
this(build.getProject(), computer);

final Map<String, String> envVars = build.getEnvironment(TaskListener.NULL);
if (envVars != null) {
otherResolvers.add(new VariableResolver.ByMap<String>(envVars));
}
}

public String resolve(String variable) {

/**
* @return resolved variable or null if the lookup failed
*/
public String resolve(final String variable) {
try {
if (lazyResolvers.containsKey(variable)) {
return lazyResolvers.get(variable).getValue();
Expand All @@ -109,12 +109,12 @@ public String resolve(String variable) {
}

/**
* Simple lazy variable resolver
* Simple lazy variable resolver.
*/
private interface LazyResolver {
String getValue() throws IOException, InterruptedException;
}

/**
* Class to handle cases when a Launcher was not created from a computer.
* @see Launcher#getComputer()
Expand Down
8 changes: 4 additions & 4 deletions tfs/src/test/java/hudson/plugins/tfs/ChangeSetReaderTest.java
Expand Up @@ -28,7 +28,7 @@ public void assertParsingTwoXmlChangeSets() throws Exception {
"</changelog>");

ChangeSetReader changesetReader = new ChangeSetReader();
ChangeLogSet logset = changesetReader.parse(null, reader);
ChangeLogSet logset = changesetReader.parse(null, null, reader);

ChangeSet changeset = logset.iterator().next();
assertEquals("User is incorrect", "user", changeset.getUser());
Expand Down Expand Up @@ -62,7 +62,7 @@ public void assertItemHasParent() throws Exception {
"</changelog>");

ChangeSetReader changesetReader = new ChangeSetReader();
ChangeLogSet logset = changesetReader.parse(null, reader);
ChangeLogSet logset = changesetReader.parse(null, null, reader);

ChangeSet changeset = logset.iterator().next();
Item item = changeset.getItems().get(0);
Expand All @@ -84,7 +84,7 @@ public void assertXmlWithEscapedCharsIsReadCorrectly() throws Exception {
"</changelog>");

ChangeSetReader changesetReader = new ChangeSetReader();
ChangeLogSet logset = changesetReader.parse(null, reader);
ChangeLogSet logset = changesetReader.parse(null, null, reader);

ChangeSet changeset = logset.iterator().next();
assertEquals("The chage set's comment is incorrect", "Just <testing> \"what\" happens when I use the & character...Hudson does not seem to like it!", changeset.getComment());
Expand All @@ -106,7 +106,7 @@ public void assertParsingOfKeywordCheckedInByIsParsed() throws Exception {
"</changelog>");

ChangeSetReader changesetReader = new ChangeSetReader();
ChangeLogSet logset = changesetReader.parse(null, reader);
ChangeLogSet logset = changesetReader.parse(null, null, reader);

ChangeSet changeset = logset.iterator().next();
assertEquals("Checked in by user is incorrect", "other_user", changeset.getCheckedInBy());
Expand Down
Expand Up @@ -14,23 +14,23 @@ public class ChangeLogSetTest {
public void assertChangeSetsHaveLogSetParent() throws Exception {
List<ChangeSet> changesets = new ArrayList<ChangeSet>();
changesets.add(new ChangeSet("version", null, "user", "comment"));
ChangeLogSet logset = new ChangeLogSet(null, changesets);
ChangeLogSet logset = new ChangeLogSet(null, null, changesets);
ChangeSet changeset = logset.iterator().next();
assertNotNull("Log set parent was null change set", changeset.getParent());
}

@Test
public void assertIsEmptyReturnsFalseWhenNoChangesets() throws Exception {
List<ChangeSet> changesets = new ArrayList<ChangeSet>();
ChangeLogSet logset = new ChangeLogSet(null, changesets);
ChangeLogSet logset = new ChangeLogSet(null, null, changesets);
assertTrue("The isEmpty did not return true with an empty log set", logset.isEmptySet());
}

@Test
public void assertIsEmptyReturnsTrueWithChangesets() throws Exception {
List<ChangeSet> changesets = new ArrayList<ChangeSet>();
changesets.add(new ChangeSet("version", null, "user", "comment"));
ChangeLogSet logset = new ChangeLogSet(null, changesets);
ChangeLogSet logset = new ChangeLogSet(null, null, changesets);
assertFalse("The isEmpty did not return false with a log set with change sets", logset.isEmptySet());
}
}

0 comments on commit 21632ef

Please sign in to comment.