Skip to content

Commit 1c53689

Browse files
authored
Merge pull request #658 from joseblas/JENKINS-30515-3.9-2
JENKINS-30515 Adding messages for credentials use: empty, found and not found
2 parents 33a9343 + 014d66c commit 1c53689

File tree

3 files changed

+153
-2
lines changed

3 files changed

+153
-2
lines changed

Jenkinsfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Test plugin compatibility to latest Jenkins LTS
44
// Allow failing tests to retry execution
5-
buildPlugin(jenkinsVersions: [null, '2.60.1'],
5+
buildPlugin(jenkinsVersions: [null, '2.60.3'],
66
findbugs: [run: true, archive: true, unstableTotalAll: '0'],
77
failFast: false)
88

src/main/java/hudson/plugins/git/GitSCM.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
import java.util.regex.Matcher;
114114
import java.util.regex.Pattern;
115115

116+
import static java.lang.String.format;
116117
import static org.apache.commons.collections.CollectionUtils.isEmpty;
117118
import static org.apache.commons.lang.StringUtils.isBlank;
118119

@@ -825,7 +826,9 @@ public GitClient createClient(TaskListener listener, EnvVars environment, Run<?,
825826

826827
for (UserRemoteConfig uc : getUserRemoteConfigs()) {
827828
String ucCredentialsId = uc.getCredentialsId();
828-
if (ucCredentialsId != null) {
829+
if (ucCredentialsId == null) {
830+
listener.getLogger().println("No credentials specified");
831+
} else {
829832
String url = getParameterString(uc.getUrl(), environment);
830833
List<StandardUsernameCredentials> urlCredentials = CredentialsProvider.lookupCredentials(
831834
StandardUsernameCredentials.class,
@@ -840,9 +843,12 @@ public GitClient createClient(TaskListener listener, EnvVars environment, Run<?,
840843
StandardUsernameCredentials credentials = CredentialsMatchers.firstOrNull(urlCredentials, idMatcher);
841844
if (credentials != null) {
842845
c.addCredentials(url, credentials);
846+
listener.getLogger().println(format("using credential %s", credentials.getId()));
843847
if (project != null && project.getLastBuild() != null) {
844848
CredentialsProvider.track(project.getLastBuild(), credentials);
845849
}
850+
} else {
851+
listener.getLogger().println(format("Warning: CredentialId \"%s\" could not be found.", ucCredentialsId));
846852
}
847853
}
848854
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package hudson.plugins.git;
2+
3+
import com.cloudbees.plugins.credentials.*;
4+
import com.cloudbees.plugins.credentials.common.StandardCredentials;
5+
import com.cloudbees.plugins.credentials.domains.Domain;
6+
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
7+
import jenkins.model.Jenkins;
8+
import jenkins.plugins.git.GitSampleRepoRule;
9+
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
10+
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
11+
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
12+
import org.junit.Before;
13+
import org.junit.Rule;
14+
import org.junit.Test;
15+
import org.jvnet.hudson.test.Issue;
16+
import org.jvnet.hudson.test.JenkinsRule;
17+
18+
import java.util.Collections;
19+
20+
import static org.hamcrest.Matchers.notNullValue;
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertThat;
23+
24+
public class CredentialsUserRemoteConfigTest {
25+
26+
@Rule
27+
public JenkinsRule r = new JenkinsRule();
28+
29+
@Rule
30+
public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();
31+
32+
private CredentialsStore store = null;
33+
34+
@Before
35+
public void enableSystemCredentialsProvider() {
36+
SystemCredentialsProvider.getInstance().setDomainCredentialsMap(
37+
Collections.singletonMap(Domain.global(), Collections.<Credentials>emptyList()));
38+
for (CredentialsStore s : CredentialsProvider.lookupStores(Jenkins.getInstance())) {
39+
if (s.getProvider() instanceof SystemCredentialsProvider.ProviderImpl) {
40+
store = s;
41+
break;
42+
}
43+
}
44+
assertThat("The system credentials provider is enabled", store, notNullValue());
45+
}
46+
47+
@Issue("JENKINS-30515")
48+
@Test
49+
public void checkoutWithValidCredentials() throws Exception {
50+
sampleRepo.init();
51+
store.addCredentials(Domain.global(), createCredential(CredentialsScope.GLOBAL, "github"));
52+
store.save();
53+
54+
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
55+
p.setDefinition(new CpsFlowDefinition(
56+
"node {\n"
57+
+ " checkout(\n"
58+
+ " [$class: 'GitSCM', \n"
59+
+ " userRemoteConfigs: [[credentialsId: 'github', url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n"
60+
+ " )"
61+
+ "}"));
62+
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
63+
r.assertLogContains("using credential github", b);
64+
}
65+
66+
@Issue("JENKINS-30515")
67+
@Test
68+
public void checkoutWithDifferentCredentials() throws Exception {
69+
sampleRepo.init();
70+
store.addCredentials(Domain.global(), createCredential(CredentialsScope.GLOBAL, "other"));
71+
store.save();
72+
73+
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
74+
p.setDefinition(new CpsFlowDefinition(
75+
"node {\n"
76+
+ " checkout(\n"
77+
+ " [$class: 'GitSCM', \n"
78+
+ " userRemoteConfigs: [[credentialsId: 'github', url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n"
79+
+ " )"
80+
+ "}"));
81+
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
82+
System.out.println(JenkinsRule.getLog(b));
83+
r.assertLogContains("Warning: CredentialId \"github\" could not be found", b);
84+
}
85+
86+
@Issue("JENKINS-30515")
87+
@Test
88+
public void checkoutWithInvalidCredentials() throws Exception {
89+
sampleRepo.init();
90+
store.addCredentials(Domain.global(), createCredential(CredentialsScope.SYSTEM, "github"));
91+
store.save();
92+
93+
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
94+
p.setDefinition(new CpsFlowDefinition(
95+
"node {\n"
96+
+ " checkout(\n"
97+
+ " [$class: 'GitSCM', \n"
98+
+ " userRemoteConfigs: [[credentialsId: 'github', url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n"
99+
+ " )"
100+
+ "}"));
101+
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
102+
r.assertLogContains("Warning: CredentialId \"github\" could not be found", b);
103+
}
104+
105+
@Issue("JENKINS-30515")
106+
@Test
107+
public void checkoutWithNoCredentialsStoredButUsed() throws Exception {
108+
sampleRepo.init();
109+
110+
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
111+
p.setDefinition(new CpsFlowDefinition(
112+
"node {\n"
113+
+ " checkout(\n"
114+
+ " [$class: 'GitSCM', \n"
115+
+ " userRemoteConfigs: [[credentialsId: 'github', url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n"
116+
+ " )"
117+
+ "}"));
118+
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
119+
System.out.println(JenkinsRule.getLog(b));
120+
r.assertLogContains("Warning: CredentialId \"github\" could not be found", b);
121+
}
122+
123+
@Issue("JENKINS-30515")
124+
@Test
125+
public void checkoutWithNoCredentialsSpecified() throws Exception {
126+
sampleRepo.init();
127+
128+
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
129+
p.setDefinition(new CpsFlowDefinition(
130+
"node {\n"
131+
+ " checkout(\n"
132+
+ " [$class: 'GitSCM', \n"
133+
+ " userRemoteConfigs: [[url: $/" + sampleRepo + "/$, branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false]]]\n"
134+
+ " )"
135+
+ "}"));
136+
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
137+
System.out.println(JenkinsRule.getLog(b));
138+
r.assertLogContains("No credentials specified", b);
139+
}
140+
141+
142+
private StandardCredentials createCredential(CredentialsScope scope, String id) {
143+
return new UsernamePasswordCredentialsImpl(scope, id, "desc: " + id, "username", "password");
144+
}
145+
}

0 commit comments

Comments
 (0)