Skip to content

Commit

Permalink
Merge branch 'feature/credential-usage-event' of https://github.com/m…
Browse files Browse the repository at this point in the history
…eiswjn/credentials-plugin into feature/credential-usage-event
  • Loading branch information
meiswjn committed Mar 31, 2022
2 parents 342e742 + 5066407 commit d8c2e77
Show file tree
Hide file tree
Showing 38 changed files with 444 additions and 1,513 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
if: needs.validate.outputs.should_release == 'true'
steps:
- name: Check out
uses: actions/checkout@v2.4.0
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up JDK 8
Expand Down
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<properties>
<changelist>999999-SNAPSHOT</changelist>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
<jenkins.version>2.332.1</jenkins.version>
<jenkins.version>2.340</jenkins.version>
<java.level>8</java.level>
<antlr4.version>4.9.3</antlr4.version>
</properties>
Expand Down Expand Up @@ -128,13 +128,13 @@
<dependency>
<groupId>org.kohsuke.metainf-services</groupId>
<artifactId>metainf-services</artifactId>
<version>1.8</version>
<version>1.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-matchers</artifactId>
<version>2.8.4</version>
<version>2.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -200,10 +200,10 @@
<profile>
<id>pdfs</id>
<properties>
<asciidoctor.maven.plugin.version>2.2.1</asciidoctor.maven.plugin.version>
<asciidoctor.maven.plugin.version>2.2.2</asciidoctor.maven.plugin.version>
<asciidoctorj.pdf.version>1.6.2</asciidoctorj.pdf.version>
<asciidoctorj.version>2.5.2</asciidoctorj.version>
<jruby.version>9.3.2.0</jruby.version>
<asciidoctorj.version>2.5.3</asciidoctorj.version>
<jruby.version>9.3.4.0</jruby.version>
</properties>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,6 @@ public static String getMenuItemIconUrl(String url) {
*/
@CheckForNull
public static Icon getIconByClassSpec(String spec) {
try {
Method getIconByClassSpec = IconSet.class.getMethod("getIconByClassSpec", Object.class);
return (Icon) getIconByClassSpec.invoke(IconSet.icons, spec);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
// ignore
}
return IconSet.icons.getIconByClassSpec(spec);
}

Expand All @@ -196,35 +190,14 @@ public static Icon getIcon(@NonNull Action action) {
if (action.getIconFileName() == null) {
return null;
}
Icon icon;
try {
Method getIconByClassSpec = IconSet.class.getMethod("getIconByClassSpec", Object.class);
icon = action instanceof IconSpec
? (Icon) getIconByClassSpec.invoke(IconSet.icons, ((IconSpec) action).getIconClassName())
: null;
if (icon != null) {
return icon;
}
Method toNormalizedIconNameClass = IconSet.class.getMethod("toNormalizedIconNameClass", Object.class);
icon = (Icon) getIconByClassSpec
.invoke(IconSet.icons, toNormalizedIconNameClass.invoke(null, action.getIconFileName()));
if (icon != null) {
return icon;
}
Method getIconByUrl = IconSet.class.getMethod("getIconByUrl", Object.class);
return (Icon) getIconByUrl.invoke(IconSet.icons, action.getIconFileName());
} catch (NoSuchMethodException e) {
icon = action instanceof IconSpec
? IconSet.icons.getIconByClassSpec(((IconSpec) action).getIconClassName())
: null;
if (icon == null) {
icon = IconSet.icons.getIconByClassSpec(IconSet.toNormalizedIconNameClass(action.getIconFileName()));
}
if (icon == null) {
icon = IconSet.icons.getIconByUrl(action.getIconFileName());
}
} catch (InvocationTargetException | IllegalAccessException e) {
icon = null;
Icon icon = action instanceof IconSpec
? IconSet.icons.getIconByClassSpec(((IconSpec) action).getIconClassName())
: null;
if (icon == null) {
icon = IconSet.icons.getIconByClassSpec(IconSet.toNormalizedIconNameClass(action.getIconFileName()));
}
if (icon == null) {
icon = IconSet.icons.getIconByUrl(action.getIconFileName());
}
return icon;
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/cloudbees/plugins/credentials/Credentials.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.ExtensionPoint;
import hudson.model.Describable;
import hudson.model.Run;
import java.io.Serializable;

/**
Expand Down Expand Up @@ -54,4 +55,16 @@ public interface Credentials extends Describable<Credentials>, Serializable, Ext
@NonNull
@SuppressWarnings("unchecked")
CredentialsDescriptor getDescriptor();

/**
* Optionally produce a special value when used in the context of a particular build.
* @param context a build wishing to consume these credentials
* @return contextualized credentials, preferably implementing the same interfaces (if not of the same concrete type); by default, {@code this}
* @see CredentialsProvider#findCredentialById(java.lang.String, java.lang.Class, hudson.model.Run, com.cloudbees.plugins.credentials.domains.DomainRequirement...)
*/
@NonNull
default Credentials forRun(@NonNull Run<?, ?> context) {
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public String getCredentialsPage() {
* @since 1.25
*/
public String getIconClassName() {
return "icon-credentials-credential";
return "symbol-key";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public String iconClassName() {
if (c != null) {
return c.getDescriptor().getIconClassName();
}
return "icon-credentials-credential";
return "symbol-key";
}

public String url() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,8 @@ public static <C extends IdCredentials> C findCredentialById(@NonNull String id,
CredentialsProvider.lookupCredentials(type, run.getParent(), ACL.SYSTEM, domainRequirements)
);
}
return CredentialsMatchers.firstOrNull(candidates, CredentialsMatchers.withId(id));
// TODO should this be calling track?
return contextualize(type, CredentialsMatchers.firstOrNull(candidates, CredentialsMatchers.withId(id)), run);
}
// this is a parameter and not the default value, we need to determine who triggered the build
final Map.Entry<User, Run<?, ?>> triggeredBy = triggeredBy(run);
Expand Down Expand Up @@ -957,7 +958,23 @@ public static <C extends IdCredentials> C findCredentialById(@NonNull String id,
C result = CredentialsMatchers.firstOrNull(candidates, CredentialsMatchers.withId(id));
// if the run has not completed yet then we can safely assume that the credential is being used for this run
// so we will track it's usage. We use isLogUpdated() as it could be used during post production
return run.isLogUpdated() ? track(run, result) : result;
if (run.isLogUpdated()) {
track(run, result);
}
return contextualize(type, result, run);
}

@CheckForNull
private static <C extends Credentials> C contextualize(@NonNull Class<C> type, @CheckForNull C credentials, @NonNull Run<?, ?> run) {
if (credentials != null) {
Credentials contextualized = credentials.forRun(run);
if (type.isInstance(contextualized)) {
return type.cast(contextualized);
} else {
LOGGER.warning(() -> "Ignoring " + contextualized.getClass().getName() + " return value of " + credentials.getClass().getName() + ".forRun since it is not assignable to " + type.getName());
}
}
return credentials;
}

/**
Expand Down Expand Up @@ -1075,7 +1092,7 @@ public String getDisplayName() {
*/
@Override
public String getIconClassName() {
return "icon-credentials-credentials";
return "symbol-key";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import net.sf.json.JSONObject;
import org.acegisecurity.AccessDeniedException;
import org.apache.commons.lang.StringUtils;
import org.jenkins.ui.icon.IconSet;
import org.jenkins.ui.icon.IconSpec;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
Expand Down Expand Up @@ -406,7 +407,7 @@ public boolean isVisible() {
@Override
public String getIconClassName() {
return isVisible()
? "icon-credentials-credentials"
? "symbol-key"
: null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class GlobalCredentialsConfiguration extends ManagementLink
public String getIconFileName() {
return ExtensionList.lookup(CredentialsDescriptor.class).isEmpty()
? null
: "/plugin/credentials/images/credentials.svg";
: "symbol-key";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public String getCategoryName() {

@Override
public String getIconFileName() {
return "/plugin/credentials/images/credentials.svg";
return "symbol-key";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ public <C extends Credentials> List<C> getCredentials(@NonNull Class<C> type, @N

@Override
public String getIconClassName() {
return "icon-credentials-system-store";
return "symbol-jenkins";
}
}

Expand Down Expand Up @@ -629,7 +629,7 @@ public CredentialsStore getStore() {
@Override
public String getIconFileName() {
return isVisible()
? "/plugin/credentials/images/system-store.svg"
? "symbol-jenkins"
: null;
}

Expand All @@ -639,7 +639,7 @@ public String getIconFileName() {
@Override
public String getIconClassName() {
return isVisible()
? "icon-credentials-system-store"
? "symbol-jenkins"
: null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public <C extends Credentials> List<C> getCredentials(@NonNull Class<C> type,
*/
@Override
public String getIconClassName() {
return "icon-credentials-user-store";
return "symbol-person";
}

/**
Expand Down Expand Up @@ -569,7 +569,7 @@ public CredentialsStore getStore() {
@Override
public String getIconFileName() {
return isVisible()
? "/plugin/credentials/images/user-store.svg"
? "symbol-person"
: null;
}

Expand All @@ -579,7 +579,7 @@ public String getIconFileName() {
@Override
public String getIconClassName() {
return isVisible()
? "icon-credentials-user-store"
? "symbol-person"
: null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public ModelObject getContext() {
@Override
public String getIconFileName() {
return isVisible()
? "/plugin/credentials/images/credentials.svg"
? "symbol-key"
: null;
}

Expand Down Expand Up @@ -295,7 +295,7 @@ public List<TableEntry> getTableEntries() {
@Override
public String getIconClassName() {
return isVisible()
? "icon-credentials-credentials"
? "symbol-key"
: null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public String getDisplayName() {
*/
@Override
public String getIconClassName() {
return "icon-credentials-certificate";
return "icon-application-certificate";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public String getDisplayName() {
*/
@Override
public String getIconClassName() {
return "icon-credentials-userpass";
return "symbol-id-card";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,10 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:f="/lib/form" xmlns:l="/lib/layout">
<l:ajax>
<div class="hd">
<h2 tooltip="${it.description}">
<l:icon class="${it.iconClassName} icon-xlg"/> ${it.description}: ${it.displayName}
</h2>
<h2 style="margin-top: 1rem" tooltip="${it.description}">${it.description}: ${it.displayName}</h2>
</div>
<div class="bd">
<h3>
<l:icon class="icon-credentials-new-credential icon-md"/>
${%Add Credentials}
</h3>
<h3>${%Add Credentials}</h3>
<form action="${it.url}/addCredentials" method="POST" id="credentials-dialog-form">
<table width="100%">
<f:entry title="${%Domain}">
Expand All @@ -49,7 +44,7 @@
<st:include page="credential"/>
</f:block>
<f:block>
<input id="credentials-add-submit" class="yui-button" value="${%Add}"
<input id="credentials-add-submit" class="yui-button primary" value="${%Add}"
onclick="return window.credentials.addSubmit(this);"/>
<input id="credentials-add-abort" class="yui-button"
onclick="window.credentials.dialog.hide(); return false;" value="${%Cancel}"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt">
<l:layout title="${it.fullDisplayName}" norefresh="true" permission="${it.domain.parent.DELETE}">
<l:layout title="${%Delete credentials}" norefresh="true" permission="${it.domain.parent.DELETE}">
<st:include page="sidepanel.jelly" />
<l:main-panel>
<h1>${%Delete credentials}</h1>
<form method="post" action="doDelete">
${%confirm(it.displayName,it.typeName)}
<f:submit value="${%Yes}"/>
<p>${%confirm(it.displayName,it.typeName)}</p>
<div>
<f:submit value="${%Yes}"/>
</div>
</form>
</l:main-panel>
</l:layout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
<l:layout title="${it.fullDisplayName}" norefresh="true" permission="${it.domain.parent.VIEW}">
<st:include page="sidepanel.jelly"/>
<l:main-panel>
<h1><l:icon class="${it.credentials.descriptor.iconClassName} icon-xlg"
alt="${it.credentials.descriptor.displayName}" tooltip="${it.credentials.descriptor.displayName}"
title="${it.credentials.descriptor.displayName}"/> ${it.displayName}</h1>
<h1>${it.displayName}</h1>
<div>
<j:out value="${it.description!=null ? app.markupFormatter.translate(it.description) : ''}" />
</div>
Expand Down
Loading

0 comments on commit d8c2e77

Please sign in to comment.