Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #131 from recena/JENKINS-29340
[JENKINS-29340] ISVNAuthentication provider did not provide credentials (part. II)
  • Loading branch information
recena committed Aug 19, 2015
2 parents 2e95528 + d89f530 commit b029f40
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 24 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -143,6 +143,12 @@ THE SOFTWARE.
<version>1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>envinject</artifactId>
<version>1.92</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.main</groupId>
Expand Down
51 changes: 27 additions & 24 deletions src/main/java/hudson/scm/SubversionSCM.java
Expand Up @@ -78,6 +78,7 @@
import java.util.LinkedHashSet;
import java.util.WeakHashMap;

import hudson.remoting.LocalChannel;
import hudson.security.ACL;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
Expand Down Expand Up @@ -1332,11 +1333,9 @@ public PollingResult compareRemoteRevisionWith(Job<?,?> project, Launcher launch
final SVNRevisionState baseline;
if (_baseline instanceof SVNRevisionState) {
baseline = (SVNRevisionState)_baseline;
}
else if (project.getLastBuild()!=null) {
} else if (project.getLastBuild()!=null) {
baseline = (SVNRevisionState)calcRevisionsFromBuild(project.getLastBuild(), launcher != null ? workspace : null, launcher, listener);
}
else {
} else {
baseline = new SVNRevisionState(null);
}

Expand All @@ -1354,9 +1353,8 @@ else if (project.getLastBuild()!=null) {
}
if (project instanceof AbstractProject && repositoryLocationsNoLongerExist(lastCompletedBuild, listener, env)) {
// Disable this project, see HUDSON-763
listener.getLogger().println(
Messages.SubversionSCM_pollChanges_locationsNoLongerExist(project));
disableProject((AbstractProject) project, listener);
listener.getLogger().println(Messages.SubversionSCM_pollChanges_locationsNoLongerExist(project));
disableProject((AbstractProject) project, listener);
return NO_CHANGES;
}

Expand All @@ -1371,34 +1369,40 @@ else if (project.getLastBuild()!=null) {
return BUILD_NOW;
}
if (!baseline.revisions.containsKey(url)) {
listener.getLogger().println(
Messages.SubversionSCM_pollChanges_locationNotInWorkspace(url));
listener.getLogger().println(Messages.SubversionSCM_pollChanges_locationNotInWorkspace(url));
return BUILD_NOW;
}
}
}

// determine where to perform polling. prefer the node where the build happened,
// in case a cluster is non-uniform. see http://www.nabble.com/svn-connection-from-slave-only-td24970587.html
VirtualChannel ch=null;
String nodeName = "master";
VirtualChannel channel = null;
if (workspace != null && !isPollFromMaster()) {
ch = workspace.getChannel();
channel = workspace.getChannel();
if (channel != null && channel instanceof Channel) {
nodeName = ((Channel) channel).getName();
}
}
if (ch==null) ch= MasterComputer.localChannel;

final String nodeName = ch instanceof Channel ? ((Channel) ch).getName() : "master";
if (channel == null) {
channel = FilePath.localChannel;
}

final SVNLogHandler logHandler = new SVNLogHandler(createSVNLogFilter(), listener);

final Map<String,ISVNAuthenticationProvider> authProviders = new LinkedHashMap<String,
ISVNAuthenticationProvider>();
final Map<String,ISVNAuthenticationProvider> authProviders = new LinkedHashMap<String, ISVNAuthenticationProvider>();

EnvVars env = null;
Run<?,?> run = project.getLastCompletedBuild();
if (run != null) {
env = run.getEnvironment(listener);
Node node;
if (nodeName.equals("master")) {
node = Jenkins.getInstance();
} else {
node = Jenkins.getInstance().getNode(nodeName);
}

// Reference: https://github.com/jenkinsci/subversion-plugin/pull/131
// Right way to get the environment variables when we do polling. http://tinyurl.com/o2o2kg9
EnvVars env = project.getEnvironment(node, listener);

for (ModuleLocation loc: getLocations(env, null)) {
String url;
try {
Expand All @@ -1412,7 +1416,7 @@ else if (project.getLastBuild()!=null) {
final ISVNAuthenticationProvider defaultAuthProvider = createAuthenticationProvider(project, null);

// figure out the remote revisions
return ch.call(new CompareAgainstBaselineCallable(baseline, logHandler, project.getName(), listener, defaultAuthProvider, authProviders, nodeName));
return channel.call(new CompareAgainstBaselineCallable(baseline, logHandler, project.getName(), listener, defaultAuthProvider, authProviders, nodeName));
}

public SVNLogFilter createSVNLogFilter() {
Expand Down Expand Up @@ -2986,8 +2990,7 @@ public ModuleLocation getExpandedLocation(Job<?, ?> project) {
}
}

return new ModuleLocation(returnURL, credentialsId, getLocalDir(), getDepthOption(),
isIgnoreExternalsOption());
return new ModuleLocation(returnURL, credentialsId, getLocalDir(), getDepthOption(), isIgnoreExternalsOption());
}

@Extension
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/hudson/scm/SubversionEnvInjectTest.java
@@ -0,0 +1,44 @@
package hudson.scm;

import hudson.model.FreeStyleProject;
import hudson.model.TaskListener;
import org.jenkinsci.plugins.envinject.EnvInjectJobProperty;
import org.jenkinsci.plugins.envinject.EnvInjectJobPropertyInfo;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

import static org.junit.Assert.assertTrue;

public class SubversionEnvInjectTest {

public static String REPO_URL = "https://svn.jenkins-ci.org/trunk/hudson/test-projects/${REPO}";

@Rule
public JenkinsRule jenkins = new JenkinsRule();

/**
* This test aims to verify that the variables defined in the "Properties Content" field, are availables in SCM Polling.
*/
@Issue("JENKINS-29340")
@Test
public void pollingWithEnvInject() throws Exception {
FreeStyleProject project = jenkins.createFreeStyleProject();

EnvInjectJobPropertyInfo jobPropertyInfo = new EnvInjectJobPropertyInfo(null, "REPO=trivial-maven", null, null, null, false);
EnvInjectJobProperty envInjectJobProperty = new EnvInjectJobProperty();
envInjectJobProperty.setOn(true);
envInjectJobProperty.setInfo(jobPropertyInfo);
project.addProperty(envInjectJobProperty);

project.setScm(new SubversionSCM(REPO_URL));

TaskListener listener = jenkins.createTaskListener();
PollingResult poll = project.poll(listener);
// If true means that parameters have been replaced correctly and we have a valid repository URL.
assertTrue(poll.hasChanges());

jenkins.assertBuildStatusSuccess(project.scheduleBuild2(0).get());
}
}

0 comments on commit b029f40

Please sign in to comment.