Skip to content

Commit 8609b8c

Browse files
committed
chore: fix review findings
1 parent b500bdd commit 8609b8c

File tree

5 files changed

+37
-22
lines changed

5 files changed

+37
-22
lines changed

src/main/java/io/jenkins/plugins/gitlabserverconfig/credentials/GroupAccessTokenImpl.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials;
77
import edu.umd.cs.findbugs.annotations.CheckForNull;
88
import edu.umd.cs.findbugs.annotations.NonNull;
9+
import edu.umd.cs.findbugs.annotations.Nullable;
910
import hudson.Extension;
1011
import hudson.util.FormValidation;
1112
import hudson.util.Secret;
1213
import jenkins.model.Jenkins;
13-
import org.apache.commons.lang.StringUtils;
14+
import org.apache.commons.lang3.StringUtils;
1415
import org.jenkinsci.Symbol;
1516
import org.kohsuke.accmod.Restricted;
1617
import org.kohsuke.accmod.restrictions.NoExternalUse;
1718
import org.kohsuke.stapler.DataBoundConstructor;
19+
import org.kohsuke.stapler.DataBoundSetter;
1820
import org.kohsuke.stapler.QueryParameter;
21+
import org.kohsuke.stapler.interceptor.RequirePOST;
1922

2023
/**
2124
* Default implementation of {@link GroupAccessToken} for use by {@link Jenkins} {@link
@@ -26,25 +29,20 @@ public class GroupAccessTokenImpl extends BaseStandardCredentials implements Gro
2629
/**
2730
* Our token.
2831
*/
29-
@NonNull
30-
private final Secret token;
32+
@Nullable
33+
private Secret token;
3134

3235
/**
3336
* Constructor.
3437
*
3538
* @param scope the credentials scope.
3639
* @param id the credentials id.
3740
* @param description the description of the token.
38-
* @param token the token itself (will be passed through {@link Secret#fromString(String)})
3941
*/
4042
@DataBoundConstructor
4143
public GroupAccessTokenImpl(
42-
@CheckForNull CredentialsScope scope,
43-
@CheckForNull String id,
44-
@CheckForNull String description,
45-
@NonNull String token) {
44+
@CheckForNull CredentialsScope scope, @CheckForNull String id, @CheckForNull String description) {
4645
super(scope, id, description);
47-
this.token = Secret.fromString(token);
4846
}
4947

5048
/**
@@ -56,6 +54,11 @@ public Secret getToken() {
5654
return token;
5755
}
5856

57+
@DataBoundSetter
58+
public void setToken(String token) {
59+
this.token = Secret.fromString(token);
60+
}
61+
5962
@NonNull
6063
@Override
6164
public String getUsername() {
@@ -94,7 +97,10 @@ public String getDisplayName() {
9497
*/
9598
@Restricted(NoExternalUse.class) // stapler
9699
@SuppressWarnings("unused")
100+
@RequirePOST
97101
public FormValidation doCheckToken(@QueryParameter String value) {
102+
Jenkins.get().checkPermission(CredentialsProvider.USE_OWN);
103+
98104
Secret secret = Secret.fromString(value);
99105
if (StringUtils.equals(value, secret.getPlainText())) {
100106
if (value.length() < GITLAB_ACCESS_TOKEN_MINIMAL_LENGTH) {

src/main/java/io/jenkins/plugins/gitlabserverconfig/credentials/PersonalAccessTokenImpl.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.cloudbees.plugins.credentials.impl.BaseStandardCredentials;
77
import edu.umd.cs.findbugs.annotations.CheckForNull;
88
import edu.umd.cs.findbugs.annotations.NonNull;
9+
import edu.umd.cs.findbugs.annotations.Nullable;
910
import hudson.Extension;
1011
import hudson.util.FormValidation;
1112
import hudson.util.Secret;
@@ -15,7 +16,9 @@
1516
import org.kohsuke.accmod.Restricted;
1617
import org.kohsuke.accmod.restrictions.NoExternalUse;
1718
import org.kohsuke.stapler.DataBoundConstructor;
19+
import org.kohsuke.stapler.DataBoundSetter;
1820
import org.kohsuke.stapler.QueryParameter;
21+
import org.kohsuke.stapler.interceptor.RequirePOST;
1922

2023
/**
2124
* Default implementation of {@link PersonalAccessToken} for use by {@link Jenkins} {@link
@@ -26,25 +29,20 @@ public class PersonalAccessTokenImpl extends BaseStandardCredentials implements
2629
/**
2730
* Our token.
2831
*/
29-
@NonNull
30-
private final Secret token;
32+
@Nullable
33+
private Secret token;
3134

3235
/**
3336
* Constructor.
3437
*
3538
* @param scope the credentials scope.
3639
* @param id the credentials id.
3740
* @param description the description of the token.
38-
* @param token the token itself (will be passed through {@link Secret#fromString(String)})
3941
*/
4042
@DataBoundConstructor
4143
public PersonalAccessTokenImpl(
42-
@CheckForNull CredentialsScope scope,
43-
@CheckForNull String id,
44-
@CheckForNull String description,
45-
@NonNull String token) {
44+
@CheckForNull CredentialsScope scope, @CheckForNull String id, @CheckForNull String description) {
4645
super(scope, id, description);
47-
this.token = Secret.fromString(token);
4846
}
4947

5048
/**
@@ -56,6 +54,11 @@ public Secret getToken() {
5654
return token;
5755
}
5856

57+
@DataBoundSetter
58+
public void setToken(String token) {
59+
this.token = Secret.fromString(token);
60+
}
61+
5962
@NonNull
6063
@Override
6164
public String getUsername() {
@@ -94,7 +97,10 @@ public String getDisplayName() {
9497
*/
9598
@Restricted(NoExternalUse.class) // stapler
9699
@SuppressWarnings("unused")
100+
@RequirePOST
97101
public FormValidation doCheckToken(@QueryParameter String value) {
102+
Jenkins.get().checkPermission(CredentialsProvider.USE_OWN);
103+
98104
Secret secret = Secret.fromString(value);
99105
if (StringUtils.equals(value, secret.getPlainText())) {
100106
if (value.length() < GITLAB_ACCESS_TOKEN_MINIMAL_LENGTH) {

src/main/java/io/jenkins/plugins/gitlabserverconfig/servers/helpers/GitLabPersonalAccessTokenCreator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,9 @@ public FormValidation doCreateTokenByPassword(
188188
private void createCredentials(@Nullable String serverUrl, String token, String username, String tokenName) {
189189
String url = defaultIfBlank(serverUrl, GitLabServer.GITLAB_SERVER_URL);
190190
String description = String.format("Auto Generated by %s server for %s user", url, username);
191-
PersonalAccessToken credentials =
192-
new PersonalAccessTokenImpl(CredentialsScope.GLOBAL, tokenName, description, token);
191+
PersonalAccessTokenImpl credentials =
192+
new PersonalAccessTokenImpl(CredentialsScope.GLOBAL, tokenName, description);
193+
credentials.setToken(token);
193194
saveCredentials(url, credentials);
194195
}
195196

src/test/java/io/jenkins/plugins/gitlabserverconfig/credentials/GroupAccessTokenImplTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public class GroupAccessTokenImplTest {
1919
@Test
2020
public void configRoundtrip() throws Exception {
2121
GroupAccessTokenImpl expected =
22-
new GroupAccessTokenImpl(CredentialsScope.GLOBAL, "magic-id", "configRoundtrip", "sAf_Xasnou47yxoAsC");
22+
new GroupAccessTokenImpl(CredentialsScope.GLOBAL, "magic-id", "configRoundtrip");
23+
expected.setToken("sAf_Xasnou47yxoAsC");
2324
CredentialsBuilder builder = new CredentialsBuilder(expected);
2425
j.configRoundtrip(builder);
2526
j.assertEqualDataBoundBeans(expected, builder.credentials);

src/test/java/io/jenkins/plugins/gitlabserverconfig/credentials/PersonalAccessTokenImplTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ public class PersonalAccessTokenImplTest {
1818

1919
@Test
2020
public void configRoundtrip() throws Exception {
21-
PersonalAccessTokenImpl expected = new PersonalAccessTokenImpl(
22-
CredentialsScope.GLOBAL, "magic-id", "configRoundtrip", "sAf_Xasnou47yxoAsC");
21+
PersonalAccessTokenImpl expected =
22+
new PersonalAccessTokenImpl(CredentialsScope.GLOBAL, "magic-id", "configRoundtrip");
23+
expected.setToken("sAf_Xasnou47yxoAsC");
2324
CredentialsBuilder builder = new CredentialsBuilder(expected);
2425
j.configRoundtrip(builder);
2526
j.assertEqualDataBoundBeans(expected, builder.credentials);

0 commit comments

Comments
 (0)