diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMNavigator.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMNavigator.java index 398af196..59fd2906 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMNavigator.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMNavigator.java @@ -15,6 +15,7 @@ import com.cloudbees.plugins.credentials.common.StandardCredentials; 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; @@ -526,6 +527,40 @@ public ListBoxModel doFillServerNameItems( return GitLabServers.get().getServerItems(); } + public FormValidation doCheckCredentialsId( + @AncestorInPath SCMSourceOwner context, @QueryParameter String serverName, @QueryParameter String value) + throws IOException, InterruptedException { + if (context == null) { + if (Jenkins.get().hasPermission(Jenkins.ADMINISTER)) { + return FormValidation.ok(); + } + } else { + if (!context.hasPermission(Item.EXTENDED_READ) + && !context.hasPermission(CredentialsProvider.USE_ITEM)) { + return FormValidation.ok(); + } + } + GitLabServer server = GitLabServers.get().findServer(serverName); + if (server == null) { + return FormValidation.ok(); + } + if (StringUtils.isBlank(value)) { + return FormValidation.warning("Credentials are recommended"); + } + if (CredentialsProvider.listCredentialsInItem( + StandardCredentials.class, + context, + context instanceof Queue.Task + ? ((Queue.Task) context).getDefaultAuthentication2() + : ACL.SYSTEM2, + URIRequirementBuilder.fromUri(serverName).build(), + GitLabServer.CREDENTIALS_MATCHER) + .isEmpty()) { + return FormValidation.error(Messages.GitLabSCMNavigator_selectedCredentialsMissing()); + } + return FormValidation.ok(); + } + public ListBoxModel doFillCredentialsIdItems( @AncestorInPath SCMSourceOwner context, @QueryParameter String serverName, diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java index 5dff095e..7c28b445 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java @@ -13,8 +13,10 @@ import com.cloudbees.plugins.credentials.CredentialsMatchers; import com.cloudbees.plugins.credentials.CredentialsProvider; +import com.cloudbees.plugins.credentials.common.StandardCredentials; 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.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -28,6 +30,7 @@ import hudson.model.TaskListener; import hudson.scm.SCM; import hudson.security.ACL; +import hudson.util.FormValidation; import hudson.util.ListBoxModel; import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabAvatar; import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabLink; @@ -836,6 +839,40 @@ public ListBoxModel doFillServerNameItems( return GitLabServers.get().getServerItems(); } + public FormValidation doCheckCredentialsId( + @AncestorInPath SCMSourceOwner context, @QueryParameter String serverName, @QueryParameter String value) + throws IOException, InterruptedException { + if (context == null) { + if (Jenkins.get().hasPermission(Jenkins.ADMINISTER)) { + return FormValidation.ok(); + } + } else { + if (!context.hasPermission(Item.EXTENDED_READ) + && !context.hasPermission(CredentialsProvider.USE_ITEM)) { + return FormValidation.ok(); + } + } + GitLabServer server = GitLabServers.get().findServer(serverName); + if (server == null) { + return FormValidation.ok(); + } + if (StringUtils.isBlank(value)) { + return FormValidation.warning("Credentials are recommended"); + } + if (CredentialsProvider.listCredentialsInItem( + StandardCredentials.class, + context, + context instanceof Queue.Task + ? ((Queue.Task) context).getDefaultAuthentication2() + : ACL.SYSTEM2, + URIRequirementBuilder.fromUri(serverName).build(), + GitLabServer.CREDENTIALS_MATCHER) + .isEmpty()) { + return FormValidation.error(Messages.GitLabSCMNavigator_selectedCredentialsMissing()); + } + return FormValidation.ok(); + } + public ListBoxModel doFillCredentialsIdItems( @AncestorInPath SCMSourceOwner context, @QueryParameter String serverName,