Skip to content

Commit

Permalink
Merge pull request #39 from Dohbedoh/JENKINS-34825
Browse files Browse the repository at this point in the history
Jenkins 34825
  • Loading branch information
p4paul committed Mar 6, 2017
2 parents 11b2394 + 1e4f953 commit 060fe50
Show file tree
Hide file tree
Showing 30 changed files with 547 additions and 84 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -70,6 +70,13 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>cloudbees-folder</artifactId>
<version>5.16</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-aggregator</artifactId>
Expand Down
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
43 changes: 30 additions & 13 deletions src/main/java/org/jenkinsci/plugins/p4/PerforceScm.java
Expand Up @@ -16,6 +16,7 @@
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Job;
import hudson.model.Item;
import hudson.model.Node;
import hudson.model.Run;
import hudson.model.TaskListener;
Expand All @@ -40,6 +41,7 @@
import org.jenkinsci.plugins.p4.changes.P4ChangeSet;
import org.jenkinsci.plugins.p4.changes.P4Revision;
import org.jenkinsci.plugins.p4.client.ConnectionHelper;
import org.jenkinsci.plugins.p4.credentials.P4BaseCredentials;
import org.jenkinsci.plugins.p4.credentials.P4CredentialsImpl;
import org.jenkinsci.plugins.p4.filters.Filter;
import org.jenkinsci.plugins.p4.filters.FilterPerChangeImpl;
Expand All @@ -56,8 +58,10 @@
import org.jenkinsci.plugins.p4.workspace.StreamWorkspaceImpl;
import org.jenkinsci.plugins.p4.workspace.TemplateWorkspaceImpl;
import org.jenkinsci.plugins.p4.workspace.Workspace;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;

import java.io.File;
Expand Down Expand Up @@ -220,8 +224,22 @@ public RepositoryBrowser<?> guessBrowser() {
logger.fine("No credential for perforce");
return null;
}

// Retrieve item from request
StaplerRequest req = Stapler.getCurrentRequest();
Job job = req == null ? null : req.findAncestorObject(Job.class);

// If cannot retrieve item, check from root
P4BaseCredentials credentials = job == null
? ConnectionHelper.findCredential(scmCredential, Jenkins.getActiveInstance())
: ConnectionHelper.findCredential(scmCredential, job);

if(credentials == null) {
logger.fine("Could not retrieve credentials from id: '${scmCredential}");
return null;
}
try {
ConnectionHelper connection = new ConnectionHelper(scmCredential, null);
ConnectionHelper connection = new ConnectionHelper(credentials, null);
String swarm = connection.getSwarm();
URL url = new URL(swarm);
return new SwarmBrowser(url);
Expand Down Expand Up @@ -352,7 +370,7 @@ private PollingResult pollWorkspace(EnvVars envVars, TaskListener listener, File

// Create task
PollTask task = new PollTask(filter, last);
task.setCredential(credential);
task.setCredential(credential, lastRun);
task.setWorkspace(ws);
task.setListener(listener);
task.setLimit(pin);
Expand Down Expand Up @@ -382,7 +400,7 @@ public void checkout(Run<?, ?> run, Launcher launcher, FilePath buildWorkspace,
// Create task
CheckoutTask task = new CheckoutTask(populate);
task.setListener(listener);
task.setCredential(credential);
task.setCredential(credential, run);

// Get workspace used for the Task
Workspace ws = task.setEnvironment(run, workspace, buildWorkspace);
Expand Down Expand Up @@ -513,7 +531,7 @@ public void buildEnvVars(AbstractBuild<?, ?> build, Map<String, String> env) {
TagAction tagAction = build.getAction(TagAction.class);
if (tagAction != null) {
// Set P4_CHANGELIST value
String change = getChangeNumber(tagAction);
String change = getChangeNumber(tagAction, build);
if (change != null) {
env.put("P4_CHANGELIST", change);
}
Expand Down Expand Up @@ -554,7 +572,7 @@ public void buildEnvVars(AbstractBuild<?, ?> build, Map<String, String> env) {
}
}

private String getChangeNumber(TagAction tagAction) {
private String getChangeNumber(TagAction tagAction, Run<?, ?> run) {
P4Revision buildChange = tagAction.getBuildChange();

if (!buildChange.isLabel()) {
Expand All @@ -570,7 +588,7 @@ private String getChangeNumber(TagAction tagAction) {
// not a change number
}

ConnectionHelper p4 = new ConnectionHelper(getCredential(), null);
ConnectionHelper p4 = new ConnectionHelper(run, credential, null);
String name = buildChange.toString();
try {
Label label = p4.getLabel(name);
Expand Down Expand Up @@ -629,7 +647,6 @@ public boolean processWorkspaceBeforeDeletion(Job<?, ?> job, FilePath buildWorks

logger.info("processWorkspaceBeforeDeletion");

String scmCredential = getCredential();
Run<?, ?> run = job.getLastBuild();

if (run == null) {
Expand All @@ -647,7 +664,7 @@ public boolean processWorkspaceBeforeDeletion(Job<?, ?> job, FilePath buildWorks
}

// exit early if client workspace does not exist
ConnectionHelper connection = new ConnectionHelper(scmCredential, null);
ConnectionHelper connection = new ConnectionHelper(run, credential, null);
try {
if (!connection.isClient(client)) {
logger.warning("P4: client not found:" + client);
Expand All @@ -661,7 +678,7 @@ public boolean processWorkspaceBeforeDeletion(Job<?, ?> job, FilePath buildWorks
// Setup Cleanup Task
RemoveClientTask task = new RemoveClientTask(client);
task.setListener(listener);
task.setCredential(credential);
task.setCredential(credential, job);

// Set workspace used for the Task
Workspace ws = task.setEnvironment(run, workspace, buildWorkspace);
Expand Down Expand Up @@ -823,12 +840,12 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
* @return A list of Perforce credential items to populate the jelly
* Select list.
*/
public ListBoxModel doFillCredentialItems() {
return P4CredentialsImpl.doFillCredentialItems();
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);
}
}

Expand Down
Expand Up @@ -173,7 +173,7 @@ public void endElement(String uri, String localName, String qName) throws SAXExc
String credential = scm.getCredential();

// Log in to Perforce and find change-list
ConnectionHelper p4 = new ConnectionHelper(credential, null);
ConnectionHelper p4 = new ConnectionHelper(run, credential, null);

// Add changelist to entry
if (qName.equalsIgnoreCase("changenumber")) {
Expand Down
Expand Up @@ -65,7 +65,7 @@ public void perform(Run<?, ?> run, FilePath buildWorkspace, Launcher launcher, T
// Setup Unshelve Task
RemoveClientTask task = new RemoveClientTask(client);
task.setListener(listener);
task.setCredential(credential);
task.setCredential(credential, run);

// Set workspace used for the Task
Workspace ws = task.setEnvironment(run, workspace, buildWorkspace);
Expand Down
15 changes: 14 additions & 1 deletion 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,8 +73,19 @@ public class ClientHelper extends ConnectionHelper {

private IClient iclient;

@Deprecated
public ClientHelper(String credential, TaskListener listener, String client, String charset) {
super(credential, listener);
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);
}

Expand Down
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.p4.client;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.perforce.p4java.admin.IProperty;
Expand Down Expand Up @@ -29,6 +30,9 @@
import com.perforce.p4java.server.IOptionsServer;
import com.perforce.p4java.server.callback.ICommandCallback;
import com.perforce.p4java.server.callback.IProgressCallback;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.security.ACL;
import hudson.util.LogTaskListener;
Expand All @@ -39,6 +43,7 @@
import org.jenkinsci.plugins.p4.credentials.P4BaseCredentials;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
Expand All @@ -59,6 +64,7 @@ public class ConnectionHelper implements AutoCloseable {
protected final P4BaseCredentials p4credential;
protected final Validate validate;

@Deprecated
public ConnectionHelper(String credentialID, TaskListener listener) {
this.listener = listener;
P4BaseCredentials credential = findCredential(credentialID);
Expand All @@ -69,6 +75,18 @@ public ConnectionHelper(String credentialID, TaskListener listener) {
validate = new Validate(listener);
}

public ConnectionHelper(ItemGroup context, String credentialID, TaskListener listener) {
this(findCredential(credentialID, context), listener);
}

public ConnectionHelper(Item job, String credentialID, TaskListener listener) {
this(findCredential(credentialID, job), listener);
}

public ConnectionHelper(Run run, String credentialID, TaskListener listener) {
this(findCredential(credentialID, run), listener);
}

public ConnectionHelper(P4BaseCredentials credential, TaskListener listener) {
this.listener = listener;
this.p4credential = credential;
Expand Down Expand Up @@ -567,9 +585,11 @@ public void disconnect() {
/**
* Finds a Perforce Credential based on the String id.
*
* @deprecated Use {@link #findCredential(String, ItemGroup)} or {@link #findCredential(String, Item)}
* @param id Credential ID
* @return a P4StandardCredentials credential or null if not found.
*/
@Deprecated
public static P4BaseCredentials findCredential(String id) {
Class<P4BaseCredentials> type = P4BaseCredentials.class;
Jenkins scope = Jenkins.getInstance();
Expand All @@ -587,6 +607,66 @@ public static P4BaseCredentials findCredential(String id) {
return null;
}

/**
* Finds a Perforce Credential based on the String id.
*
* @param credentialsId Credential ID
* @param context The context
* @return a P4StandardCredentials credential or null if not found.
*/
public static P4BaseCredentials findCredential(String credentialsId, ItemGroup context) {
if (credentialsId == null) {
return null;
}
P4BaseCredentials credentials = CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(P4BaseCredentials.class, context,
ACL.SYSTEM, Collections.<DomainRequirement>emptyList()),
CredentialsMatchers.allOf(
CredentialsMatchers.withId(credentialsId),
CredentialsMatchers.instanceOf(P4BaseCredentials.class)));
return credentials;
}

/**
* Finds a Perforce Credential based on credentials ID and {@link Item}.
* This also tracks usage of the credentials.
*
* @param credentialsId Credential ID
* @param item The {@link Item}
* @return a P4StandardCredentials credential or null if not found.
*/
public static P4BaseCredentials findCredential(String credentialsId, Item item) {
if (credentialsId == null) {
return null;
}
P4BaseCredentials credentials = CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(P4BaseCredentials.class, item,
ACL.SYSTEM, Collections.<DomainRequirement>emptyList()),
CredentialsMatchers.allOf(
CredentialsMatchers.withId(credentialsId),
CredentialsMatchers.instanceOf(P4BaseCredentials.class)));
CredentialsProvider.track(item, credentials);
return credentials;
}

/**
* Finds a Perforce Credential based on the String id and {@link Run}.
* This also tracks usage of the credentials.
*
* @param credentialsId Credential ID
* @param run The {@link Run}
* @return a P4StandardCredentials credential or null if not found.
*/
public static P4BaseCredentials findCredential(String credentialsId, Run run) {
if (credentialsId == null) {
return null;
}
P4BaseCredentials credentials = CredentialsProvider.findCredentialById(credentialsId,
P4BaseCredentials.class, run, Collections.<DomainRequirement>emptyList());
CredentialsProvider.track(run, credentials);
return credentials;
}

public void log(String msg) {
if (listener == null) {
return;
Expand Down

0 comments on commit 060fe50

Please sign in to comment.