Skip to content

Commit

Permalink
[FIXED JENKINS-45467] Fixes for GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Jul 12, 2017
1 parent 388545b commit d8234e3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -23,7 +23,7 @@
<properties>
<jenkins.version>1.625.3</jenkins.version>
<workflow.version>1.14.2</workflow.version>
<scm-api.version>2.2.0-20170711.135617-15</scm-api.version>
<scm-api.version>2.2.0-20170711.141026-16</scm-api.version>
</properties>

<scm>
Expand Down
Expand Up @@ -362,7 +362,9 @@ private Object readResolve() {
}
traits.add(new ForkPullRequestDiscoveryTrait(s, new ForkPullRequestDiscoveryTrait.TrustContributors()));
}
if (!DescriptorImpl.SAME.equals(checkoutCredentialsId)) {
if (checkoutCredentialsId != null
&& !DescriptorImpl.SAME.equals(checkoutCredentialsId)
&& !checkoutCredentialsId.equals(scanCredentialsId)) {
traits.add(new SSHCheckoutTrait(checkoutCredentialsId));
}
if ((includes != null && !"*".equals(includes)) || (excludes != null && !"".equals(excludes))) {
Expand Down
Expand Up @@ -451,7 +451,9 @@ private Object readResolve() {
if (!"*".equals(includes) || !"".equals(excludes)) {
traits.add(new WildcardSCMHeadFilterTrait(includes, excludes));
}
if (!DescriptorImpl.SAME.equals(checkoutCredentialsId)) {
if (checkoutCredentialsId != null
&& !DescriptorImpl.SAME.equals(checkoutCredentialsId)
&& !checkoutCredentialsId.equals(scanCredentialsId)) {
traits.add(new SSHCheckoutTrait(checkoutCredentialsId));
}
this.traits = traits;
Expand Down
Expand Up @@ -25,8 +25,10 @@

import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
Expand All @@ -38,6 +40,7 @@
import hudson.scm.SCM;
import hudson.scm.SCMDescriptor;
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import jenkins.plugins.git.GitSCMBuilder;
Expand All @@ -46,6 +49,7 @@
import jenkins.scm.api.trait.SCMSourceContext;
import jenkins.scm.api.trait.SCMSourceTrait;
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.AncestorInPath;
Expand Down Expand Up @@ -113,7 +117,7 @@ public static class DescriptorImpl extends SCMSourceTraitDescriptor {
@NonNull
@Override
public String getDisplayName() {
return "Checkout over SSH";
return Messages.SSHCheckoutTrait_displayName();
}

/**
Expand Down Expand Up @@ -167,7 +171,7 @@ public ListBoxModel doFillCredentialsIdItems(@CheckForNull @AncestorInPath Item
return new StandardListBoxModel().includeCurrentValue(credentialsId);
}
StandardListBoxModel result = new StandardListBoxModel();
result.add("- use build agent's key -", "");
result.add(Messages.SSHCheckoutTrait_useAgentKey(), "");
return result.includeMatchingAs(
context instanceof Queue.Task
? Tasks.getDefaultAuthenticationOf((Queue.Task) context)
Expand All @@ -178,5 +182,46 @@ public ListBoxModel doFillCredentialsIdItems(@CheckForNull @AncestorInPath Item
CredentialsMatchers.instanceOf(SSHUserPrivateKey.class)
);
}

/**
* Validation for checkout credentials.
*
* @param context the context.
* @param serverUrl the server url.
* @param value the current selection.
* @return the validation results
*/
@Restricted(NoExternalUse.class)
@SuppressWarnings("unused") // stapler form binding
public FormValidation doCheckCredentialsId(@CheckForNull @AncestorInPath Item context,
@QueryParameter String serverUrl,
@QueryParameter String value) {
if (context == null
? !Jenkins.getActiveInstance().hasPermission(Jenkins.ADMINISTER)
: !context.hasPermission(Item.EXTENDED_READ)) {
return FormValidation.ok();
}
if (StringUtils.isBlank(value)) {
// use agent key
return FormValidation.ok();
}
if (CredentialsMatchers.firstOrNull(
CredentialsProvider
.lookupCredentials(SSHUserPrivateKey.class, context, context instanceof Queue.Task
? Tasks.getDefaultAuthenticationOf((Queue.Task) context)
: ACL.SYSTEM, URIRequirementBuilder.fromUri(serverUrl).build()),
CredentialsMatchers.withId(value)) != null) {
return FormValidation.ok();
}
if (CredentialsMatchers.firstOrNull(CredentialsProvider
.lookupCredentials(StandardUsernameCredentials.class, context, context instanceof Queue.Task
? Tasks.getDefaultAuthenticationOf((Queue.Task) context)
: ACL.SYSTEM, URIRequirementBuilder.fromUri(serverUrl).build()),
CredentialsMatchers.withId(value)) != null) {
return FormValidation.error(Messages.SSHCheckoutTrait_incompatibleCredentials());
}
return FormValidation.warning(Messages.SSHCheckoutTrait_missingCredentials());
}

}
}
Expand Up @@ -46,3 +46,7 @@ BranchDiscoveryTrait.onlyPRs=Only branches that are also filed as PRs
BranchDiscoveryTrait.allBranches=All branches
BranchDiscoveryTrait.authorityDisplayName=Trust origin branches
BranchDiscoveryTrait.displayName=Discover branches
SSHCheckoutTrait.displayName=Checkout over SSH
SSHCheckoutTrait.incompatibleCredentials=The currently configured credentials are incompatible with this behaviour
SSHCheckoutTrait.missingCredentials=The currently configured credentials cannot be found
SSHCheckoutTrait.useAgentKey=- use build agent''s key -

0 comments on commit d8234e3

Please sign in to comment.