Skip to content

Commit

Permalink
[FIXED JENKINS-31991] Where possible, in the Parameter page of a buil…
Browse files Browse the repository at this point in the history
…d, the credentials values should be a link to the credential
  • Loading branch information
stephenc committed May 23, 2016
1 parent 6aa38ce commit 30ce5b3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
Expand Up @@ -2,13 +2,16 @@

import com.cloudbees.plugins.credentials.common.IdCredentials;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.EnvVars;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.Executor;
import hudson.model.ParameterValue;
import hudson.model.Run;
import hudson.model.User;
import hudson.model.queue.WorkUnit;
import hudson.security.ACL;
import hudson.util.VariableResolver;
Expand All @@ -18,6 +21,8 @@
import java.util.List;
import jenkins.model.Jenkins;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.Stapler;
Expand Down Expand Up @@ -134,6 +139,70 @@ public String describe() {
return Messages.CredentialsParameterValue_NotAvailableToCurrentUser();
}

public String iconClassName() {
if (StringUtils.isBlank(value)) {
return "";
}
final Run run = Stapler.getCurrentRequest().findAncestorObject(Run.class);
if (run == null) {
throw new IllegalStateException("Should only be called from value.jelly");
}
StandardCredentials c = CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(StandardCredentials.class, run.getParent(), ACL.SYSTEM,
Collections.<DomainRequirement>emptyList()), CredentialsMatchers.withId(value));
if (c != null) {
return c.getDescriptor().getIconClassName();
}
c = CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(StandardCredentials.class, run.getParent(),
Jenkins.getAuthentication(),
Collections.<DomainRequirement>emptyList()), CredentialsMatchers.withId(value));
if (c != null) {
return c.getDescriptor().getIconClassName();
}
return "icon-credentials-credential";
}

public String url() {
if (StringUtils.isBlank(value)) {
return null;
}
final Run run = Stapler.getCurrentRequest().findAncestorObject(Run.class);
if (run == null) {
throw new IllegalStateException("Should only be called from value.jelly");
}
SecurityContext oldContext = ACL.impersonate(ACL.SYSTEM);
try {
for (CredentialsStore store : CredentialsProvider.lookupStores(run.getParent())) {
String url = url(store);
if (url != null) {
return url;
}
}
} finally {
SecurityContextHolder.setContext(oldContext);
}
for (CredentialsStore store: CredentialsProvider.lookupStores(User.current())) {
String url = url(store);
if (url != null) {
return url;
}
}
return null;
}

private String url(CredentialsStore store) {
for (Domain d: store.getDomains()) {
for (Credentials c: store.getCredentials(d)) {
if (c instanceof IdCredentials && value.equals(((IdCredentials) c).getId())) {
String link = store.getRelativeLinkToAction();
return link == null ? null : link + d.getUrl() + "credential/"+ Util.rawEncode(value);
}
}
}
return null;
}

public boolean isDefaultValue() {
return isDefaultValue;
}
Expand Down
Expand Up @@ -451,6 +451,10 @@ public String getRelativeLinkToAction() {
if (relativeLink == null) {
return null;
}
CredentialsStoreAction a = getStoreAction();
if (a != null) {
return relativeLink + "credentials/store/" + a.getUrlName() + "/";
}
List<CredentialsStoreAction> actions;
if (context instanceof Actionable) {
actions = ((Actionable) context).getActions(CredentialsStoreAction.class);
Expand Down
Expand Up @@ -45,6 +45,7 @@
import hudson.util.CopyOnWriteMap;
import java.io.IOException;
import java.io.ObjectStreamException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -731,6 +732,12 @@ public boolean updateCredentials(@NonNull Domain domain, @NonNull Credentials cu
@NonNull Credentials replacement) throws IOException {
return getInstance().updateCredentials(domain, current, replacement);
}

@Override
public String getRelativeLinkToContext() {
StaplerRequest request = Stapler.getCurrentRequest();
return URI.create(request.getContextPath() + "/" + user.getUrl() + "/").normalize().toString() ;
}
}

}
Expand Up @@ -24,8 +24,20 @@
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:c="/lib/credentials">
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form" xmlns:c="/lib/credentials" xmlns:l="/lib/layout">
<f:entry title="${it.name}" description="${it.description}">
${it.describe()}
<j:set var="url" value="${it.url()}"/>
<j:choose>
<j:when test="${url!=null and !url.isEmpty()}">
<a href="${it.url()}">
<l:icon class="${it.iconClassName()} icon-md"/>
${it.describe()}
</a>
</j:when>
<j:otherwise>
<l:icon class="${it.iconClassName()} icon-md"/>
${it.describe()}
</j:otherwise>
</j:choose>
</f:entry>
</j:jelly>
</j:jelly>

0 comments on commit 30ce5b3

Please sign in to comment.