Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
566eedc
withCredentials support -- initial implementation and tests
sahilm02 Apr 22, 2025
4626f86
Sort the imports on new test
MarkEWaite Apr 25, 2025
a6fd562
Simplify implementation with single binding
MarkEWaite Apr 25, 2025
4bb4317
Update src/test/java/org/jenkinsci/plugin/gitea/credentials/PersonalA…
sahilm02 Apr 25, 2025
33b0b22
Simplify test script
MarkEWaite Apr 25, 2025
38e517e
Merge branch 'feature/withCredentials-support' of github.com:sahilm02…
sahilm02 Apr 25, 2025
c214a56
Add giteaPersonalAccessToken documentation and example
MarkEWaite Apr 25, 2025
9667755
Merge remote-tracking branch 'markwaite/feature/withCredentials-suppo…
sahilm02 Apr 25, 2025
47258b9
Use echo instead of println
MarkEWaite Apr 25, 2025
f9e1723
Improve comment in test
MarkEWaite Apr 25, 2025
c89581d
Merge remote-tracking branch 'markwaite/feature/withCredentials-suppo…
sahilm02 Apr 25, 2025
b4388d9
Use StringCredentials
MarkEWaite Apr 26, 2025
ba4aa66
Use StringCredentials
MarkEWaite Apr 29, 2025
6ae6216
Choose 'Secret text' credential from snippet generator
MarkEWaite Apr 29, 2025
9bd7c77
Merge branch 'jenkinsci:master' into feature/withCredentials-support-…
MarkEWaite May 19, 2025
8401187
Merge branch 'master' into feature/withCredentials-support-using-Stri…
MarkEWaite Nov 26, 2025
c71a294
Include sh and bat in example and test
MarkEWaite Nov 26, 2025
8978f4a
Merge branch 'master' into feature/withCredentials-support
sahilm02 Dec 2, 2025
df93222
Merge remote-tracking branch 'markwaite/feature/withCredentials-suppo…
sahilm02 Dec 2, 2025
80946b7
Merge branch 'feature/withCredentials-support' of github.com:sahilm02…
sahilm02 Dec 2, 2025
b291ead
Update src/test/java/org/jenkinsci/plugin/gitea/credentials/PersonalA…
sahilm02 Dec 9, 2025
748d476
Remove unnecessary files
sahilm02 Dec 9, 2025
86c9133
Merge branch 'feature/withCredentials-support' of github.com:sahilm02…
sahilm02 Dec 9, 2025
8d7beaf
Merge branch 'master' into feature/withCredentials-support
sahilm02 Dec 9, 2025
fc1afca
Fix checkstyle
sahilm02 Dec 12, 2025
8147fe3
Merge branch 'feature/withCredentials-support' of github.com:sahilm02…
sahilm02 Dec 12, 2025
529d3fa
Fix checkstyle
sahilm02 Dec 12, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
<artifactId>workflow-multibranch</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-basic-steps</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.jenkinsci.plugin.gitea.credentials;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.Secret;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.credentialsbinding.BindingDescriptor;
import org.jenkinsci.plugins.credentialsbinding.MultiBinding;
import org.kohsuke.stapler.DataBoundConstructor;

public class PersonalAccessTokenBinding extends MultiBinding<PersonalAccessToken> {

// Environment variable name to be used in the binding
private final String variable;

@DataBoundConstructor
public PersonalAccessTokenBinding(String credentialsId, String variable) {
super(credentialsId);
this.variable = variable;
}

@Override
protected Class<PersonalAccessToken> type() {
return PersonalAccessToken.class;
}

@Override
public Set<String> variables() {
// Return a set containing the environment variable name
return Collections.singleton(variable);

Check warning on line 40 in src/main/java/org/jenkinsci/plugin/gitea/credentials/PersonalAccessTokenBinding.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 40 is not covered by tests
}

@Override
public MultiEnvironment bind(
@NonNull Run<?, ?> build,
@Nullable FilePath workspace,
@Nullable Launcher launcher,
@NonNull TaskListener listener)
throws IOException, InterruptedException {
// Retrieve the PersonalAccessToken credentials
PersonalAccessToken credentials = getCredentials(build);

Map<String, String> values = new LinkedHashMap<>();
values.put(variable, Secret.toString(credentials.getToken()));
return new MultiEnvironment(values);
}

@Symbol("giteaPersonalAccessToken") // Symbol annotation for use in Jenkins UI
@Extension
public static class DescriptorImpl extends BindingDescriptor<PersonalAccessTokenImpl> {

@Override
protected Class<PersonalAccessTokenImpl> type() {
return PersonalAccessTokenImpl.class;

Check warning on line 64 in src/main/java/org/jenkinsci/plugin/gitea/credentials/PersonalAccessTokenBinding.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 64 is not covered by tests
}

@NonNull
@Override
public String getDisplayName() {
return Messages.PersonalAccessTokenImpl_displayName(); // Localized display name
}

@Override
public boolean requiresWorkspace() {
return false; // This binding does not require a workspace
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.jenkinsci.plugin.gitea.credentials;

import java.nio.charset.StandardCharsets;
import java.util.List;

import org.apache.commons.io.IOUtils;

Check warning on line 6 in src/test/java/org/jenkinsci/plugin/gitea/credentials/PersonalAccessTokenBindingTest.java

View check run for this annotation

ci.jenkins.io / Java Compiler

checkstyle:check

ERROR: (imports) ImportOrder: Extra separation in import group before 'org.apache.commons.io.IOUtils'

Check warning on line 6 in src/test/java/org/jenkinsci/plugin/gitea/credentials/PersonalAccessTokenBindingTest.java

View check run for this annotation

ci.jenkins.io / CheckStyle

ImportOrderCheck

ERROR: Extra separation in import group before 'org.apache.commons.io.IOUtils'
Raw output
<p>Since Checkstyle 3.2</p><p>Checks the ordering/grouping of imports. Features are:</p><ul><li>groups type/static imports: ensures that groups of imports come in a specific order (e.g., java. comes first, javax. comes second, then everything else)</li><li>adds a separation between type import groups : ensures that a blank line sit between each group</li><li>type/static import groups aren't separated internally: ensures that each group aren't separated internally by blank line or comment</li><li>sorts type/static imports inside each group: ensures that imports within each group are in lexicographic order</li><li>sorts according to case: ensures that the comparison between imports is case sensitive, in <a href="https://en.wikipedia.org/wiki/ASCII#Order">ASCII sort order</a></li><li>arrange static imports: ensures the relative order between type imports and static imports (see <a href="property_types.html#importOrder">import orders</a>)</li></ul><p><a href="#ImportOrder_Examples">Examples section</a> contains examples that work with default formatter configurations of Eclipse, IntelliJ IDEA and NetBeans </p>
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import com.cloudbees.plugins.credentials.CredentialsProvider;

Check warning on line 14 in src/test/java/org/jenkinsci/plugin/gitea/credentials/PersonalAccessTokenBindingTest.java

View check run for this annotation

ci.jenkins.io / Java Compiler

checkstyle:check

ERROR: (imports) ImportOrder: Extra separation in import group before 'com.cloudbees.plugins.credentials.CredentialsProvider'

Check warning on line 14 in src/test/java/org/jenkinsci/plugin/gitea/credentials/PersonalAccessTokenBindingTest.java

View check run for this annotation

ci.jenkins.io / Java Compiler

checkstyle:check

ERROR: (imports) ImportOrder: Wrong order for 'com.cloudbees.plugins.credentials.CredentialsProvider' import.

Check warning on line 14 in src/test/java/org/jenkinsci/plugin/gitea/credentials/PersonalAccessTokenBindingTest.java

View check run for this annotation

ci.jenkins.io / CheckStyle

ImportOrderCheck

ERROR: Extra separation in import group before 'com.cloudbees.plugins.credentials.CredentialsProvider'
Raw output
<p>Since Checkstyle 3.2</p><p>Checks the ordering/grouping of imports. Features are:</p><ul><li>groups type/static imports: ensures that groups of imports come in a specific order (e.g., java. comes first, javax. comes second, then everything else)</li><li>adds a separation between type import groups : ensures that a blank line sit between each group</li><li>type/static import groups aren't separated internally: ensures that each group aren't separated internally by blank line or comment</li><li>sorts type/static imports inside each group: ensures that imports within each group are in lexicographic order</li><li>sorts according to case: ensures that the comparison between imports is case sensitive, in <a href="https://en.wikipedia.org/wiki/ASCII#Order">ASCII sort order</a></li><li>arrange static imports: ensures the relative order between type imports and static imports (see <a href="property_types.html#importOrder">import orders</a>)</li></ul><p><a href="#ImportOrder_Examples">Examples section</a> contains examples that work with default formatter configurations of Eclipse, IntelliJ IDEA and NetBeans </p>

Check warning on line 14 in src/test/java/org/jenkinsci/plugin/gitea/credentials/PersonalAccessTokenBindingTest.java

View check run for this annotation

ci.jenkins.io / CheckStyle

ImportOrderCheck

ERROR: Wrong order for 'com.cloudbees.plugins.credentials.CredentialsProvider' import.
Raw output
<p>Since Checkstyle 3.2</p><p>Checks the ordering/grouping of imports. Features are:</p><ul><li>groups type/static imports: ensures that groups of imports come in a specific order (e.g., java. comes first, javax. comes second, then everything else)</li><li>adds a separation between type import groups : ensures that a blank line sit between each group</li><li>type/static import groups aren't separated internally: ensures that each group aren't separated internally by blank line or comment</li><li>sorts type/static imports inside each group: ensures that imports within each group are in lexicographic order</li><li>sorts according to case: ensures that the comparison between imports is case sensitive, in <a href="https://en.wikipedia.org/wiki/ASCII#Order">ASCII sort order</a></li><li>arrange static imports: ensures the relative order between type imports and static imports (see <a href="property_types.html#importOrder">import orders</a>)</li></ul><p><a href="#ImportOrder_Examples">Examples section</a> contains examples that work with default formatter configurations of Eclipse, IntelliJ IDEA and NetBeans </p>
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.CredentialsStore;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.domains.Domain;

import hudson.model.Run;

Check warning on line 20 in src/test/java/org/jenkinsci/plugin/gitea/credentials/PersonalAccessTokenBindingTest.java

View check run for this annotation

ci.jenkins.io / Java Compiler

checkstyle:check

ERROR: (imports) ImportOrder: Extra separation in import group before 'hudson.model.Run'

Check warning on line 20 in src/test/java/org/jenkinsci/plugin/gitea/credentials/PersonalAccessTokenBindingTest.java

View check run for this annotation

ci.jenkins.io / CheckStyle

ImportOrderCheck

ERROR: Extra separation in import group before 'hudson.model.Run'
Raw output
<p>Since Checkstyle 3.2</p><p>Checks the ordering/grouping of imports. Features are:</p><ul><li>groups type/static imports: ensures that groups of imports come in a specific order (e.g., java. comes first, javax. comes second, then everything else)</li><li>adds a separation between type import groups : ensures that a blank line sit between each group</li><li>type/static import groups aren't separated internally: ensures that each group aren't separated internally by blank line or comment</li><li>sorts type/static imports inside each group: ensures that imports within each group are in lexicographic order</li><li>sorts according to case: ensures that the comparison between imports is case sensitive, in <a href="https://en.wikipedia.org/wiki/ASCII#Order">ASCII sort order</a></li><li>arrange static imports: ensures the relative order between type imports and static imports (see <a href="property_types.html#importOrder">import orders</a>)</li></ul><p><a href="#ImportOrder_Examples">Examples section</a> contains examples that work with default formatter configurations of Eclipse, IntelliJ IDEA and NetBeans </p>

@WithJenkins
class PersonalAccessTokenBindingTest {

private static final String API_TOKEN = "secret";
private static final String API_TOKEN_ID = "personalAccessTokenId";

private JenkinsRule jenkins;

@BeforeEach
void setUp(JenkinsRule rule) throws Exception {
jenkins = rule;
for (CredentialsStore credentialsStore : CredentialsProvider.lookupStores(jenkins.jenkins)) {
if (credentialsStore instanceof SystemCredentialsProvider.StoreImpl) {
List<Domain> domains = credentialsStore.getDomains();
credentialsStore.addCredentials(
domains.get(0),
new PersonalAccessTokenImpl(
CredentialsScope.GLOBAL,
API_TOKEN_ID,
"Gitea Personal Access Token",
API_TOKEN));
}
}
}

@Test
void withCredentials_success() throws Exception {
WorkflowJob project = jenkins.createProject(WorkflowJob.class);
String pipelineText = IOUtils.toString(
getClass().getResourceAsStream("pipeline/withCredentials-pipeline.groovy"), StandardCharsets.UTF_8);
project.setDefinition(new CpsFlowDefinition(pipelineText, false));
Run<?, ?> build = jenkins.buildAndAssertSuccess(project);
// assert false to know we run it in tests
jenkins.assertLogContains("Token1 is ecret", build);
jenkins.assertLogContains("Token2 is ecret", build);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.jenkinsci.plugin.gitea.credentials.pipeline

node {
withCredentials([[
$class: 'org.jenkinsci.plugin.gitea.credentials.PersonalAccessTokenBinding',
credentialsId: "personalAccessTokenId",
variable: "API_TOKEN1"
]]) {
println "Token1 is ${API_TOKEN1.substring(1)}"
}
withCredentials([giteaPersonalAccessToken(
credentialsId: "personalAccessTokenId",
variable: "API_TOKEN2"
)]) {
println "Token2 is ${API_TOKEN2.substring(1)}"
}
}
Loading