Skip to content

Commit

Permalink
Merge pull request #466 from jbq/git_source_advanced_options
Browse files Browse the repository at this point in the history
[JENKINS-40908] GitSCMSource: support custom remote and refspec
  • Loading branch information
jbq committed Feb 15, 2017
2 parents d2c36cf + 2101155 commit c7809b5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java
Expand Up @@ -116,6 +116,9 @@ public AbstractGitSCMSource(String id) {
@CheckForNull
public abstract String getCredentialsId();

/**
* @return Git remote URL
*/
public abstract String getRemote();

public abstract String getIncludes();
Expand Down
44 changes: 38 additions & 6 deletions src/main/java/jenkins/plugins/git/GitSCMSource.java
Expand Up @@ -90,16 +90,16 @@
* @author Stephen Connolly
*/
public class GitSCMSource extends AbstractGitSCMSource {
private static final String DEFAULT_INCLUDES = "*";

private static final String DEFAULT_EXCLUDES = "";

public static final Logger LOGGER = Logger.getLogger(GitSCMSource.class.getName());

private final String remote;

private final String credentialsId;

private final String remoteName;

private final String rawRefSpecs;

private final String includes;

private final String excludes;
Expand All @@ -115,15 +115,32 @@ public class GitSCMSource extends AbstractGitSCMSource {
private List<GitSCMExtension> extensions;

@DataBoundConstructor
public GitSCMSource(String id, String remote, String credentialsId, String includes, String excludes, boolean ignoreOnPushNotifications) {
public GitSCMSource(String id, String remote, String credentialsId, String remoteName, String rawRefSpecs, String includes, String excludes, boolean ignoreOnPushNotifications) {
super(id);
this.remote = remote;
this.credentialsId = credentialsId;

if (remoteName == null)
// backwards compatibility
this.remoteName = "origin";
else
this.remoteName = remoteName;

if (rawRefSpecs == null)
// backwards compatibility
this.rawRefSpecs = String.format("+refs/heads/*:refs/remotes/%s/*", this.remoteName);
else
this.rawRefSpecs = rawRefSpecs;

this.includes = includes;
this.excludes = excludes;
this.ignoreOnPushNotifications = ignoreOnPushNotifications;
}

public GitSCMSource(String id, String remote, String credentialsId, String includes, String excludes, boolean ignoreOnPushNotifications) {
this(id, remote, credentialsId, null, null, includes, excludes, ignoreOnPushNotifications);
}

public boolean isIgnoreOnPushNotifications() {
return ignoreOnPushNotifications;
}
Expand Down Expand Up @@ -176,6 +193,15 @@ public String getRemote() {
return remote;
}

@Override
public String getRemoteName() {
return remoteName;
}

public String getRawRefSpecs() {
return rawRefSpecs;
}

@Override
public String getIncludes() {
return includes;
Expand All @@ -188,7 +214,13 @@ public String getExcludes() {

@Override
protected List<RefSpec> getRefSpecs() {
return Arrays.asList(new RefSpec("+refs/heads/*:refs/remotes/" + getRemoteName() + "/*"));
List<RefSpec> refSpecs = new ArrayList<>();

for (String rawRefSpec : rawRefSpecs.split(" ")) {
refSpecs.add(new RefSpec(rawRefSpec));
}

return refSpecs;
}

@Extension
Expand Down
Expand Up @@ -48,6 +48,12 @@
</f:entry>

<f:advanced>
<f:entry title="${%Remote Name}" field="remoteName">
<f:textbox default="origin"/>
</f:entry>
<f:entry title="${%RefSpecs}" field="rawRefSpecs">
<f:textbox default="+refs/heads/*:refs/remotes/origin/*"/>
</f:entry>
<f:entry title="${%Include branches}" field="includes">
<f:textbox default="*"/>
</f:entry>
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java
Expand Up @@ -4,6 +4,7 @@
import hudson.Launcher;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.git.UserRemoteConfig;
import hudson.scm.SCMRevisionState;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.extensions.GitSCMExtension;
Expand Down Expand Up @@ -171,4 +172,39 @@ public void testSpecificRevisionBuildChooser() throws Exception {
assertTrue(scmRevision.getExtensions().get(1) instanceof BuildChooserSetting);
assertEquals(2, scmRevision.getExtensions().size());
}


@Test
public void testCustomRemoteName() throws Exception {
sampleRepo.init();

GitSCMSource source = new GitSCMSource(null, sampleRepo.toString(), "", "upstream", null, "*", "", true);
SCMHead head = new SCMHead("master");
GitSCM scm = (GitSCM) source.build(head);
List<UserRemoteConfig> configs = scm.getUserRemoteConfigs();
assertEquals(1, configs.size());
UserRemoteConfig config = configs.get(0);
assertEquals("upstream", config.getName());
assertEquals("+refs/heads/*:refs/remotes/upstream/*", config.getRefspec());
}

@Test
public void testCustomRefSpecs() throws Exception {
sampleRepo.init();

GitSCMSource source = new GitSCMSource(null, sampleRepo.toString(), "", null, "+refs/heads/*:refs/remotes/origin/* +refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*", "*", "", true);
SCMHead head = new SCMHead("master");
GitSCM scm = (GitSCM) source.build(head);
List<UserRemoteConfig> configs = scm.getUserRemoteConfigs();

assertEquals(2, configs.size());

UserRemoteConfig config = configs.get(0);
assertEquals("origin", config.getName());
assertEquals("+refs/heads/*:refs/remotes/origin/*", config.getRefspec());

config = configs.get(1);
assertEquals("origin", config.getName());
assertEquals("+refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*", config.getRefspec());
}
}

0 comments on commit c7809b5

Please sign in to comment.