public static List<UserRemoteConfig> createRepoList(String url, String credentialsId) { List<UserRemoteConfig> repoList = new ArrayList<UserRemoteConfig>(); repoList.add(new UserRemoteConfig(url, null, null, credentialsId)); return repoList; }
public String toString() { return getRefspec() + " => " + getUrl() + " (" + getName() + ")"; }
/** * Returns a List of Pair<key, value> which contains the git credentials to use. * Key - url to repository * Value - StandardCredentials, containing username and password */ private List<Pair<String, StandardCredentials>> getGitClientCredentials() { List<Pair<String, StandardCredentials>> credentialsList = new ArrayList<>(); GitSCM gitScm = getJenkinsScm(); for (UserRemoteConfig uc : gitScm.getUserRemoteConfigs()) { String url = uc.getUrl(); // In case overriding credentials are defined, we will use it for this URL if (this.credentials != null) { credentialsList.add(Pair.of(url, this.credentials)); continue; } // Get credentials from jenkins credentials plugin if (uc.getCredentialsId() != null) { StandardUsernameCredentials credentials = CredentialsMatchers.firstOrNull( CredentialsProvider.lookupCredentials(StandardUsernameCredentials.class, build.getProject(), ACL.SYSTEM, URIRequirementBuilder.fromUri(url).build()), CredentialsMatchers.allOf(CredentialsMatchers.withId(uc.getCredentialsId()), GitClient.CREDENTIALS_MATCHER)); if (credentials != null) { credentialsList.add(Pair.of(url, (StandardCredentials)credentials)); } } } return credentialsList; }
UserRemoteConfig config = gitSCM.getUserRemoteConfigs().get(0); BranchSpec branchSpec = gitSCM.getBranches().get(0); String remote = config.getUrl(); String cacheEntry = AbstractGitSCMSource.getCacheEntry(remote); Lock cacheLock = AbstractGitSCMSource.getCacheLock(cacheEntry); String credentialsId = config.getCredentialsId(); if (credentialsId != null) { StandardCredentials credential = CredentialsMatchers.firstOrNull( client.init(); String remoteName = StringUtils.defaultIfBlank(config.getName(), Constants.DEFAULT_REMOTE_NAME); listener.getLogger().println("Setting " + remoteName + " to " + remote); client.setRemoteUrl(remoteName, remote);
public BuildData(String scmName, Collection<UserRemoteConfig> remoteConfigs) { this.scmName = scmName; for(UserRemoteConfig c : remoteConfigs) { remoteUrls.add(c.getUrl()); } }
remoteName = remote.getName(); if (remoteName == null || remoteName.isEmpty()) { remoteName = "origin";
@NonNull /*package*/ GitClient createClient(TaskListener listener, EnvVars environment, Job project, Node n, FilePath ws) throws IOException, InterruptedException { String gitExe = getGitExe(n, listener); Git git = Git.with(listener, environment).in(ws).using(gitExe); GitClient c = git.getClient(); for (GitSCMExtension ext : extensions) { c = ext.decorate(this,c); } for (UserRemoteConfig uc : getUserRemoteConfigs()) { String ucCredentialsId = uc.getCredentialsId(); if (ucCredentialsId != null) { String url = getParameterString(uc.getUrl(), environment); List<StandardUsernameCredentials> urlCredentials = CredentialsProvider.lookupCredentials(StandardUsernameCredentials.class, project, ACL.SYSTEM, URIRequirementBuilder.fromUri(url).build()); CredentialsMatcher ucMatcher = CredentialsMatchers.withId(ucCredentialsId); CredentialsMatcher idMatcher = CredentialsMatchers.allOf(ucMatcher, GitClient.CREDENTIALS_MATCHER); StandardUsernameCredentials credentials = CredentialsMatchers.firstOrNull(urlCredentials, idMatcher); if (credentials != null) { c.addCredentials(url, credentials); if (project != null && project.getLastBuild() != null) { CredentialsProvider.track(project.getLastBuild(), credentials); } } } } // TODO add default credentials return c; }
private boolean isRelevantBuildData(BuildData bd) { for(UserRemoteConfig c : getUserRemoteConfigs()) { if(bd.hasBeenReferenced(c.getUrl())) { return true; } } return false; }
@Nonnull @Override List<UserRemoteConfig> getRemotes(@Nonnull GitLabSCMSource source) throws GitLabAPIException { List<UserRemoteConfig> remotes = new ArrayList<>(2); remotes.add(new UserRemoteConfig( getProject(getProjectId(), source).getRemote(source), "merge-request", "", source.getCredentialsId())); if (merge) { remotes.addAll(targetBranch.getRemotes(source)); } return remotes; }
private void updateFromUserData() throws GitException { // do what newInstance used to do directly from the request data if (userRemoteConfigs == null) { return; /* Prevent NPE when no remote config defined */ } try { String[] pUrls = new String[userRemoteConfigs.size()]; String[] repoNames = new String[userRemoteConfigs.size()]; String[] refSpecs = new String[userRemoteConfigs.size()]; for (int i = 0; i < userRemoteConfigs.size(); ++i) { pUrls[i] = userRemoteConfigs.get(i).getUrl(); repoNames[i] = userRemoteConfigs.get(i).getName(); refSpecs[i] = userRemoteConfigs.get(i).getRefspec(); } this.remoteRepositories = DescriptorImpl.createRepositoryConfigurations(pUrls, repoNames, refSpecs); // TODO: replace with new repositories } catch (IOException e1) { throw new GitException("Error creating repositories", e1); } }
private Boolean repoMatches(final GitCodePushedEventArgs gitCodePushedEventArgs, final Job job) { if (job instanceof WorkflowJob) { final FlowDefinition jobDef = ((WorkflowJob) job).getDefinition(); if (jobDef instanceof CpsScmFlowDefinition) { final SCM jobSCM = ((CpsScmFlowDefinition) jobDef).getScm(); if (jobSCM instanceof GitSCM) { final GitSCM gitJobSCM = (GitSCM) jobSCM; final URIish uri = gitCodePushedEventArgs.getRepoURIish(); for (final UserRemoteConfig remoteConfig : gitJobSCM.getUserRemoteConfigs()) { final String jobRepoUrl = remoteConfig.getUrl(); if (StringUtils.equalsIgnoreCase(jobRepoUrl, uri.toString())) { return true; } } } } } return false; }
/** * Converts the {@link #asRefSpecs()} into {@link UserRemoteConfig} instances. * * @return the list of {@link UserRemoteConfig} instances. */ @NonNull public final List<UserRemoteConfig> asRemoteConfigs() { List<UserRemoteConfig> result = new ArrayList<>(1 + additionalRemotes.size()); result.add(new UserRemoteConfig(remote(), remoteName(), joinRefSpecs(asRefSpecs()), credentialsId())); for (AdditionalRemote r : additionalRemotes.values()) { result.add(new UserRemoteConfig(r.remote(), r.remoteName(), joinRefSpecs(r.asRefSpecs()), credentialsId())); } return result; }
@Nonnull List<UserRemoteConfig> getRemotes(@Nonnull GitLabSCMSource source) throws GitLabAPIException { return singletonList( new UserRemoteConfig( getProject(projectId, source).getRemote(source), "origin", getRefSpec().delegate().toString(), source.getCredentialsId())); }
/** * @return the {@link UserRemoteConfig} instances. * @deprecated use {@link GitSCMBuilder#asRemoteConfigs()} */ @Deprecated @Restricted(DoNotUse.class) @RestrictedSince("3.4.0") protected List<UserRemoteConfig> getRemoteConfigs() { List<RefSpec> refSpecs = getRefSpecs(); List<UserRemoteConfig> result = new ArrayList<>(refSpecs.size()); String remote = getRemote(); for (RefSpec refSpec : refSpecs) { result.add(new UserRemoteConfig(remote, getRemoteName(), refSpec.toString(), getCredentialsId())); } return result; }
refspec = cfg.getFetchRefSpecs().get(0).toString(); userRemoteConfigs.add(new UserRemoteConfig(url, cfg.getName(), refspec, null));
Collections.singletonList(new UserRemoteConfig(gitSource.getUri(), null, null, null)), branchSpecs, false,
@Issue("JENKINS-42836") @Test public void usingParameterInScriptPath() throws Exception { sampleRepo.init(); sampleRepo.write("flow.groovy", "echo 'version one'"); sampleRepo.git("add", "flow.groovy"); sampleRepo.write("otherFlow.groovy", "echo 'version two'"); sampleRepo.git("add", "otherFlow.groovy"); sampleRepo.git("commit", "--all", "--message=commits"); WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); CpsScmFlowDefinition def = new CpsScmFlowDefinition(new GitSCM(Collections.singletonList(new UserRemoteConfig(sampleRepo.fileUrl(), null, null, null)), Collections.singletonList(new BranchSpec("master")), false, Collections.<SubmoduleConfig>emptyList(), null, null, Collections.<GitSCMExtension>emptyList()), "${SCRIPT_PATH}"); p.setDefinition(def); p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("SCRIPT_PATH", "flow.groovy"))); r.assertLogContains("version one", r.assertBuildStatusSuccess(p.scheduleBuild2(0))); r.assertLogContains("version two", r.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction(new StringParameterValue("SCRIPT_PATH", "otherFlow.groovy"))))); } }
@Issue("JENKINS-28447") @Test public void usingParameter() throws Exception { sampleRepo.init(); sampleRepo.write("flow.groovy", "echo 'version one'"); sampleRepo.git("add", "flow.groovy"); sampleRepo.git("commit", "--message=one"); sampleRepo.git("tag", "one"); sampleRepo.write("flow.groovy", "echo 'version two'"); sampleRepo.git("commit", "--all", "--message=two"); WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p"); CpsScmFlowDefinition def = new CpsScmFlowDefinition(new GitSCM(Collections.singletonList(new UserRemoteConfig(sampleRepo.fileUrl(), null, null, null)), Collections.singletonList(new BranchSpec("${VERSION}")), false, Collections.<SubmoduleConfig>emptyList(), null, null, Collections.<GitSCMExtension>emptyList()), "flow.groovy"); def.setLightweight(false); // TODO SCMFileSystem.of cannot pick up build parameters p.setDefinition(def); p.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("VERSION", "master"))); r.assertLogContains("version two", r.assertBuildStatusSuccess(p.scheduleBuild2(0))); r.assertLogContains("version one", r.assertBuildStatusSuccess(p.scheduleBuild2(0, new ParametersAction(new StringParameterValue("VERSION", "one"))))); }