Skip to content

Commit

Permalink
git-client: teach submoduleUpdate how to pass credentials
Browse files Browse the repository at this point in the history
Teach the submoduleUpdate call to perform submodule URL lookup, and call
"submodule update" separately for each submodule. Lookup the
credentials, and call it using launchCommandWithCredentials. This
enables submodules to correctly get the SSH or HTTP setup necessary such
that the Jenkins credentials will be passed into each submodule.

We can't just call "git submodule update" since it may be possible
(however unlikely!) that each submodule wants to use a separate
credential. Thus, perform lookup using each URL and run through a
forloop to actually update each submodule instead of depending on the
git implementation to do this for us.

JENKINS-20941 - Credentials and Submodules

Signed-off-by: Jacob Keller <jacob.e.keller@intel.com>
  • Loading branch information
jacob-keller committed Oct 20, 2015
1 parent 6eded9c commit deaf403
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Expand Up @@ -51,6 +51,7 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.regex.Matcher;


/**
Expand Down Expand Up @@ -922,7 +923,47 @@ else if (!referencePath.isDirectory())
args.add("--reference", ref);
}

launchCommandIn(args, workspace, environment, timeout);

// We need to call submodule update for each configured
// submodule. Note that we can't reliably depend on the
// getSubmodules() since it is possible "HEAD" doesn't exist,
// and we don't really want to recursively find all possible
// submodules, just the ones for this super project. Thus,
// loop through the config output and parse it for configured
// modules.
String cfgOutput = null;
try {
// We might fail if we have no modules, so catch this
// exception and just return.
cfgOutput = launchCommand("config", "--get-regexp", "^submodule");
} catch (GitException e) {
listener.error("No submodules found.");
return;
}

// Use a matcher to find each configured submodule name, and
// then run the submodule update command with the provided
// path.
Pattern pattern = Pattern.compile("^submodule\\.(.*)\\.url", Pattern.MULTILINE);
Matcher matcher = pattern.matcher(cfgOutput);
while (matcher.find()) {
ArgumentListBuilder perModuleArgs = args.clone();
String sUrl = matcher.group(1);

URIish urIish = null;
try {
urIish = new URIish(getSubmoduleUrl(sUrl));
} catch (URISyntaxException e) {
listener.error("Invalid repository for " + sUrl);
throw new GitException("Invalid repository for " + sUrl);
}

StandardCredentials cred = credentials.get(urIish.toPrivateString());
if (cred == null) cred = defaultCredentials;

perModuleArgs.add(sUrl);
launchCommandWithCredentials(perModuleArgs, workspace, cred, urIish, timeout);
}
}
};
}
Expand Down

0 comments on commit deaf403

Please sign in to comment.