Skip to content

Commit 4f72825

Browse files
Support withCredentials on GitLabApiToken(Impl) (#1754)
* support withCredentials -- initial implementation and tests * support withCredentials -- move tests + final touches * support withCredentials -- de-hardcore display name string * support withCredentials -- use interface instead of implementation class * support withCredentials -- groovy file from tabs to spaces * revert test port back to 80 Co-authored-by: Kris Stern <[email protected]> --------- Co-authored-by: Kris Stern <[email protected]>
1 parent f5659c7 commit 4f72825

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.dabsquared.gitlabjenkins.connection;
2+
3+
import edu.umd.cs.findbugs.annotations.NonNull;
4+
import edu.umd.cs.findbugs.annotations.Nullable;
5+
import hudson.Extension;
6+
import hudson.FilePath;
7+
import hudson.Launcher;
8+
import hudson.model.Run;
9+
import hudson.model.TaskListener;
10+
import hudson.util.Secret;
11+
import java.io.IOException;
12+
import java.util.Collections;
13+
import java.util.LinkedHashMap;
14+
import java.util.Map;
15+
import java.util.Set;
16+
import org.jenkinsci.Symbol;
17+
import org.jenkinsci.plugins.credentialsbinding.BindingDescriptor;
18+
import org.jenkinsci.plugins.credentialsbinding.MultiBinding;
19+
import org.kohsuke.stapler.DataBoundConstructor;
20+
21+
public class GitLabApiTokenBinding extends MultiBinding<GitLabApiToken> {
22+
23+
private final String variable;
24+
25+
@DataBoundConstructor
26+
public GitLabApiTokenBinding(String credentialsId, String variable) {
27+
super(credentialsId);
28+
this.variable = variable;
29+
}
30+
31+
@Override
32+
protected Class<GitLabApiToken> type() {
33+
return GitLabApiToken.class;
34+
}
35+
36+
@Override
37+
public Set<String> variables() {
38+
return Collections.singleton(variable);
39+
}
40+
41+
@Override
42+
public MultiEnvironment bind(
43+
@NonNull Run<?, ?> build,
44+
@Nullable FilePath workspace,
45+
@Nullable Launcher launcher,
46+
@NonNull TaskListener listener)
47+
throws IOException, InterruptedException {
48+
GitLabApiToken credentials = getCredentials(build);
49+
Map<String, String> values = new LinkedHashMap<>();
50+
values.put(variable, Secret.toString(credentials.getApiToken()));
51+
return new MultiEnvironment(values);
52+
}
53+
54+
@Symbol("gitlabApiToken")
55+
@Extension
56+
public static class DescriptorImpl extends BindingDescriptor<GitLabApiTokenImpl> {
57+
58+
@Override
59+
protected Class<GitLabApiTokenImpl> type() {
60+
return GitLabApiTokenImpl.class;
61+
}
62+
63+
@NonNull
64+
@Override
65+
public String getDisplayName() {
66+
return Messages.GitLabApiToken_name();
67+
}
68+
69+
@Override
70+
public boolean requiresWorkspace() {
71+
return false;
72+
}
73+
}
74+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.dabsquared.gitlabjenkins.connection;
2+
3+
import com.cloudbees.plugins.credentials.CredentialsProvider;
4+
import com.cloudbees.plugins.credentials.CredentialsScope;
5+
import com.cloudbees.plugins.credentials.CredentialsStore;
6+
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
7+
import com.cloudbees.plugins.credentials.domains.Domain;
8+
import hudson.model.Run;
9+
import hudson.util.Secret;
10+
import java.io.IOException;
11+
import java.util.List;
12+
import org.apache.commons.io.IOUtils;
13+
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
14+
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
15+
import org.junit.Before;
16+
import org.junit.Rule;
17+
import org.junit.Test;
18+
import org.jvnet.hudson.test.JenkinsRule;
19+
20+
public class GitLabApiTokenBindingTest {
21+
22+
private static final String API_TOKEN = "secret";
23+
private static final String API_TOKEN_ID = "apiTokenId";
24+
25+
@Rule
26+
public JenkinsRule jenkins = new JenkinsRule();
27+
28+
@Before
29+
public void setup() throws IOException {
30+
for (CredentialsStore credentialsStore : CredentialsProvider.lookupStores(jenkins.jenkins)) {
31+
if (credentialsStore instanceof SystemCredentialsProvider.StoreImpl) {
32+
List<Domain> domains = credentialsStore.getDomains();
33+
credentialsStore.addCredentials(
34+
domains.get(0),
35+
new GitLabApiTokenImpl(
36+
CredentialsScope.GLOBAL,
37+
API_TOKEN_ID,
38+
"GitLab API Token",
39+
Secret.fromString(API_TOKEN)));
40+
}
41+
}
42+
}
43+
44+
@Test
45+
public void withCredentials_success() throws Exception {
46+
WorkflowJob project = jenkins.createProject(WorkflowJob.class);
47+
String pipelineText =
48+
IOUtils.toString(getClass().getResourceAsStream("pipeline/withCredentials-pipeline.groovy"));
49+
project.setDefinition(new CpsFlowDefinition(pipelineText, false));
50+
Run<?, ?> build = jenkins.buildAndAssertSuccess(project);
51+
// assert false to know we run it in tests
52+
jenkins.assertLogContains("Token1 is ecret", build);
53+
jenkins.assertLogContains("Token2 is ecret", build);
54+
}
55+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.dabsquared.gitlabjenkins.connection.pipeline
2+
3+
node {
4+
withCredentials([[
5+
$class: 'com.dabsquared.gitlabjenkins.connection.GitLabApiTokenBinding',
6+
credentialsId: "apiTokenId",
7+
variable: "API_TOKEN1"
8+
]]) {
9+
println "Token1 is ${API_TOKEN1.substring(1)}"
10+
}
11+
12+
withCredentials([gitlabApiToken(
13+
credentialsId: "apiTokenId",
14+
variable: "API_TOKEN2"
15+
)]) {
16+
println "Token2 is ${API_TOKEN2.substring(1)}"
17+
}
18+
}

0 commit comments

Comments
 (0)