Skip to content

Commit

Permalink
[JENKINS-34825] Added support for folder credentials to PerformSCMSource
Browse files Browse the repository at this point in the history
  • Loading branch information
Dohbedoh committed Mar 6, 2017
1 parent 460b252 commit 1e4f953
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 30 deletions.
Expand Up @@ -86,7 +86,7 @@ private ClientHelper getClientHelper(DescriptorImpl p4scm) throws Exception {
workspace.setExpand(new HashMap<String, String>());
workspace.setRootPath(rootPath);

ClientHelper p4 = new ClientHelper(credential, listener, clientName, "utf8");
ClientHelper p4 = new ClientHelper(Jenkins.getActiveInstance(), credential, listener, clientName, "utf8");
p4.setClient(workspace);

return p4;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jenkinsci/plugins/p4/PerforceScm.java
Expand Up @@ -588,7 +588,7 @@ private String getChangeNumber(TagAction tagAction, Run<?, ?> run) {
// not a change number
}

ConnectionHelper p4 = new ConnectionHelper(run.getParent(), credential, null);
ConnectionHelper p4 = new ConnectionHelper(run, credential, null);
String name = buildChange.toString();
try {
Label label = p4.getLabel(name);
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/jenkinsci/plugins/p4/client/ClientHelper.java
Expand Up @@ -29,6 +29,8 @@
import hudson.AbortException;
import hudson.model.Descriptor;
import hudson.model.TaskListener;
import hudson.model.Item;
import hudson.model.ItemGroup;
import jenkins.model.Jenkins;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -71,11 +73,22 @@ public class ClientHelper extends ConnectionHelper {

private IClient iclient;

@Deprecated
public ClientHelper(String credential, TaskListener listener, String client, String charset) {
super(Jenkins.getActiveInstance(), credential, listener);
clientLogin(client, charset);
}

public ClientHelper(ItemGroup context, String credential, TaskListener listener, String client, String charset) {
super(context, credential, listener);
clientLogin(client, charset);
}

public ClientHelper(Item context, String credential, TaskListener listener, String client, String charset) {
super(context, credential, listener);
clientLogin(client, charset);
}

public ClientHelper(P4BaseCredentials credential, TaskListener listener, String client, String charset) {
super(credential, listener);
clientLogin(client, charset);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jenkinsci/plugins/p4/groovy/P4Groovy.java
Expand Up @@ -3,6 +3,7 @@
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.server.IOptionsServer;
import hudson.model.TaskListener;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.p4.client.ClientHelper;
import org.jenkinsci.plugins.p4.workspace.Workspace;

Expand Down Expand Up @@ -84,7 +85,7 @@ private IOptionsServer getConnection() {
String client = workspace.getFullName();
String charset = workspace.getCharset();

ClientHelper p4 = new ClientHelper(credential, listener, client, charset);
ClientHelper p4 = new ClientHelper(Jenkins.getActiveInstance(), credential, listener, client, charset);
try {
p4.setClient(workspace);
} catch (Exception e) {
Expand Down
Expand Up @@ -104,7 +104,7 @@ protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHe
observer.observe(head, revision);
} else {
String base = head.getPath();
SCMSourceCriteria.Probe probe = new P4Probe(credential, listener, charset, base);
SCMSourceCriteria.Probe probe = new P4Probe(getOwner(), credential, listener, charset, base);
if (criteria.isHead(probe, listener)) {
// get revision and add observe
SCMRevision revision = getRevision(head, listener);
Expand All @@ -125,7 +125,8 @@ protected List<String> getIncludePaths() {
}

protected P4Revision getRevision(P4Head head, TaskListener listener) throws Exception {
try (ClientHelper p4 = new ClientHelper(credential, listener, scmSourceClient, charset)) {

try (ClientHelper p4 = new ClientHelper(getOwner(), credential, listener, scmSourceClient, charset)) {
long change = p4.getHead(head.getPath() + "/...");
P4Revision revision = new P4Revision(head, change);
return revision;
Expand Down
Expand Up @@ -29,7 +29,7 @@ public List<P4Head> getHeads(@NonNull TaskListener listener) throws Exception {
List<String> paths = getIncludePaths();
HashSet<P4Head> list = new HashSet<P4Head>();

ConnectionHelper p4 = new ConnectionHelper(credential, listener);
ConnectionHelper p4 = new ConnectionHelper(getOwner(), getCredential(), listener);

List<IFileSpec> specs = p4.getDirs(paths);
for (IFileSpec s : specs) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jenkinsci/plugins/p4/scm/P4Probe.java
Expand Up @@ -5,6 +5,7 @@
import jenkins.scm.api.SCMFile;
import jenkins.scm.api.SCMProbe;
import jenkins.scm.api.SCMProbeStat;
import jenkins.scm.api.SCMSourceOwner;
import org.jenkinsci.plugins.p4.client.ClientHelper;

import java.io.IOException;
Expand All @@ -22,9 +23,9 @@ public class P4Probe extends SCMProbe {

private transient ClientHelper p4;

public P4Probe(String credential, TaskListener listener, String charset, String base) {
public P4Probe(SCMSourceOwner owner, String credential, TaskListener listener, String charset, String base) {
this.base = base;
this.p4 = new ClientHelper(credential, listener, scmSourceClient, charset);
this.p4 = new ClientHelper(owner, credential, listener, scmSourceClient, charset);
}

@Override
Expand Down
@@ -1,6 +1,7 @@
package org.jenkinsci.plugins.p4.scm;

import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.scm.RepositoryBrowser;
import hudson.scm.RepositoryBrowsers;
import hudson.util.FormValidation;
Expand All @@ -9,6 +10,7 @@
import org.jenkinsci.plugins.p4.browsers.P4Browser;
import org.jenkinsci.plugins.p4.credentials.P4CredentialsImpl;
import org.jenkinsci.plugins.p4.workspace.WorkspaceDescriptor;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.QueryParameter;

import java.util.List;
Expand All @@ -22,12 +24,18 @@ public String getDisplayName() {
return "Perforce";
}

public ListBoxModel doFillCredentialItems() {
return P4CredentialsImpl.doFillCredentialItems();
/**
* Credentials list, a Jelly config method for a build job.
*
* @return A list of Perforce credential items to populate the jelly
* Select list.
*/
public ListBoxModel doFillCredentialItems(@AncestorInPath Item project, @QueryParameter String credential) {
return P4CredentialsImpl.doFillCredentialItems(project, credential);
}

public FormValidation doCheckCredential(@QueryParameter String value) {
return P4CredentialsImpl.doCheckCredential(value);
public FormValidation doCheckCredential(@AncestorInPath Item project, @QueryParameter String value) {
return P4CredentialsImpl.doCheckCredential(project, value);
}

public ListBoxModel doFillCharsetItems() {
Expand Down
Expand Up @@ -26,7 +26,7 @@ public List<P4Head> getHeads(@NonNull TaskListener listener) throws Exception {
List<String> paths = getIncludePaths();
HashSet<P4Head> list = new HashSet<P4Head>();

ConnectionHelper p4 = new ConnectionHelper(credential, listener);
ConnectionHelper p4 = new ConnectionHelper(getOwner(), credential, listener);
try {
List<IStreamSummary> specs = p4.getStreams(paths);
for (IStreamSummary s : specs) {
Expand Down
Expand Up @@ -181,7 +181,7 @@ public List<String> getTags() {
* @return Perforce Label object
*/
public Label getLabel(String tag) {
ClientHelper p4 = new ClientHelper(credential, null, client, charset);
ClientHelper p4 = new ClientHelper(ClientHelper.findCredential(credential, getRun()), null, client, charset);
try {
Label label = p4.getLabel(tag);
return label;
Expand Down
Expand Up @@ -3,12 +3,7 @@
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Cause;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.model.*;
import hudson.tasks.Builder;
import org.jenkinsci.plugins.p4.credentials.P4PasswordImpl;
import org.jenkinsci.plugins.p4.populate.AutoCleanImpl;
Expand Down
Expand Up @@ -63,10 +63,6 @@ public void testBasicJenkinsfile() throws Exception {
WorkspaceSpec spec = new WorkspaceSpec(false, false, false, false, false, false, stream, line, view);
ManualWorkspaceImpl workspace = new ManualWorkspaceImpl("none", true, client, spec);

// Get current change
ClientHelper p4 = new ClientHelper(CREDENTIAL, null, client, "none");
int head = Integer.parseInt(p4.getCounter("change"));

// SCM and Populate options
Populate populate = new AutoCleanImpl();
PerforceScm scm = new PerforceScm(CREDENTIAL, workspace, populate);
Expand All @@ -75,6 +71,10 @@ public void testBasicJenkinsfile() throws Exception {
WorkflowJob job = jenkins.jenkins.createProject(WorkflowJob.class, "basicJenkinsfile");
job.setDefinition(new CpsScmFlowDefinition(scm, "Jenkinsfile"));

// Get current change
ClientHelper p4 = new ClientHelper(job, CREDENTIAL, null, client, "none");
int head = Integer.parseInt(p4.getCounter("change"));

// Build 1
WorkflowRun run = job.scheduleBuild2(0).get();
jenkins.assertBuildStatusSuccess(run);
Expand Down Expand Up @@ -126,10 +126,6 @@ public void testDiffClients() throws Exception {
WorkspaceSpec spec = new WorkspaceSpec(false, false, false, false, false, false, stream, line, view);
ManualWorkspaceImpl workspace = new ManualWorkspaceImpl("none", true, client, spec);

// Get current change
ClientHelper p4 = new ClientHelper(CREDENTIAL, null, client, "none");
int head = Integer.parseInt(p4.getCounter("change"));

// SCM and Populate options
Populate populate = new AutoCleanImpl();
PerforceScm scm = new PerforceScm(CREDENTIAL, workspace, populate);
Expand All @@ -138,6 +134,10 @@ public void testDiffClients() throws Exception {
WorkflowJob job = jenkins.jenkins.createProject(WorkflowJob.class, "diffClientsJenkinsfile");
job.setDefinition(new CpsScmFlowDefinition(scm, "Jenkinsfile"));

// Get current change
ClientHelper p4 = new ClientHelper(job, CREDENTIAL, null, client, "none");
int head = Integer.parseInt(p4.getCounter("change"));

// Build 1
WorkflowRun run = job.scheduleBuild2(0).get();
jenkins.assertBuildStatusSuccess(run);
Expand Down Expand Up @@ -285,7 +285,7 @@ public void testMulitSyncPolling() throws Exception {
submitFile(jenkins, "//depot/Data/Jenkinsfile", content2);

// Get latest change
ClientHelper p4 = new ClientHelper(CREDENTIAL, null, client, "none");
ClientHelper p4 = new ClientHelper(job, CREDENTIAL, null, client, "none");
int head = Integer.parseInt(p4.getCounter("change"));

// Build 2
Expand Down
Expand Up @@ -77,7 +77,7 @@ public void testPublishWithPurge() throws Exception {
assertEquals(Result.SUCCESS, build.getResult());

// Stat file and check type
ClientHelper p4 = new ClientHelper(CREDENTIAL, null, client, "none");
ClientHelper p4 = new ClientHelper(project, CREDENTIAL, null, client, "none");
List<IFileSpec> fileSpec = FileSpecBuilder.makeFileSpecList("//depot/Data/artifact.1");
GetExtendedFilesOptions opts = new GetExtendedFilesOptions();
List<IExtendedFileSpec> eSpec = p4.connection.getExtendedFiles(fileSpec, opts);
Expand Down
@@ -1,9 +1,17 @@
package org.jenkinsci.plugins.p4.scm;

import com.cloudbees.hudson.plugins.folder.AbstractFolder;
import com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.domains.Domain;
import hudson.model.Result;
import jenkins.branch.BranchSource;
import jenkins.scm.api.SCMSource;
import org.jenkinsci.plugins.p4.DefaultEnvironment;
import org.jenkinsci.plugins.p4.SampleServerRule;
import org.jenkinsci.plugins.p4.credentials.P4BaseCredentials;
import org.jenkinsci.plugins.p4.credentials.P4PasswordImpl;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
Expand All @@ -18,6 +26,7 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class PerforceScmSourceTest extends DefaultEnvironment {
Expand Down Expand Up @@ -189,4 +198,61 @@ public void testRootPathStreams() throws Exception {

assertThat("We now have branches", multi.getItems(), not(containsInAnyOrder()));
}


@Test
public void testMultiBranchClassicWithCredentialsInFolder() throws Exception {

WorkflowMultiBranchProject multi = jenkins.jenkins.createProject(WorkflowMultiBranchProject.class, "multi-classic-creds-in-folder");

CredentialsStore folderStore = getFolderStore(multi);
P4BaseCredentials inFolderCredentials = new P4PasswordImpl(
CredentialsScope.GLOBAL, "idInFolder", "desc:passwd", p4d.getRshPort(),
null, "jenkins", "0", "0", null, "jenkins");
folderStore.addCredentials(Domain.global(), inFolderCredentials);

String format = "jenkins-${NODE_NAME}-${JOB_NAME}";
String includes = "//stream/...";
SCMSource source = new BranchesScmSource("classic", inFolderCredentials.getId(), includes, null, format, null);
multi.getSourcesList().add(new BranchSource(source));
multi.scheduleBuild2(0);
jenkins.waitUntilNoActivity();

assertEquals("Branch Indexing succeeded", Result.SUCCESS, multi.getComputation().getResult());
assertThat("We now have branches", multi.getItems(), not(containsInAnyOrder()));
}

@Test
public void testMultiBranchStreamWithCredentialsInFolder() throws Exception {

WorkflowMultiBranchProject multi = jenkins.jenkins.createProject(WorkflowMultiBranchProject.class, "multi-streams-creds-in-folder");

CredentialsStore folderStore = getFolderStore(multi);
P4BaseCredentials inFolderCredentials = new P4PasswordImpl(
CredentialsScope.GLOBAL, "idInFolder", "desc:passwd", p4d.getRshPort(),
null, "jenkins", "0", "0", null, "jenkins");
folderStore.addCredentials(Domain.global(), inFolderCredentials);

String format = "jenkins-${NODE_NAME}-${JOB_NAME}";
String includes = "//stream/...";
SCMSource source = new StreamsScmSource("streams", inFolderCredentials.getId(), includes, null, format, null);
multi.getSourcesList().add(new BranchSource(source));
multi.scheduleBuild2(0);
jenkins.waitUntilNoActivity();

assertEquals("Branch Indexing succeeded", Result.SUCCESS, multi.getComputation().getResult());
assertThat("We now have branches", multi.getItems(), not(containsInAnyOrder()));
}

private CredentialsStore getFolderStore(AbstractFolder f) {
Iterable<CredentialsStore> stores = CredentialsProvider.lookupStores(f);
CredentialsStore folderStore = null;
for (CredentialsStore s : stores) {
if (s.getProvider() instanceof FolderCredentialsProvider && s.getContext() == f) {
folderStore = s;
break;
}
}
return folderStore;
}
}

0 comments on commit 1e4f953

Please sign in to comment.