Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Null Safety in GitLabConnectionProperty.java #1619

Merged
merged 10 commits into from
Jan 15, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import java.util.Objects;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.WebApplicationException;
import jenkins.model.Jenkins;
Expand Down Expand Up @@ -109,57 +110,73 @@

@Override
public JobProperty<?> newInstance(StaplerRequest req, JSONObject formData) throws FormException {
return req.bindJSON(GitLabConnectionProperty.class, formData);
if (req != null) {
return req.bindJSON(GitLabConnectionProperty.class, formData);
} else {
throw new IllegalArgumentException("StaplerRequest 'req' cannot be null.");
}
}

public ListBoxModel doFillGitLabConnectionItems() {
ListBoxModel options = new ListBoxModel();
GitLabConnectionConfig descriptor =
(GitLabConnectionConfig) Jenkins.getInstance().getDescriptor(GitLabConnectionConfig.class);
for (GitLabConnection connection : descriptor.getConnections()) {
options.add(connection.getName(), connection.getName());
GitLabConnectionConfig descriptor = (GitLabConnectionConfig)
Objects.requireNonNull(Jenkins.getInstance()).getDescriptor(GitLabConnectionConfig.class);

if (descriptor != null) {
for (GitLabConnection connection : descriptor.getConnections()) {
options.add(connection.getName(), connection.getName());
}
} else {
throw new IllegalStateException("GitLabConnectionConfig descriptor cannot be null.");
}

return options;
}

public ListBoxModel doFillJobCredentialIdItems(
@AncestorInPath Item item, @QueryParameter String url, @QueryParameter String jobCredentialId) {
StandardListBoxModel result = new StandardListBoxModel();
if (item == null) {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return result.includeCurrentValue(jobCredentialId);
}
} else {
if (!item.hasPermission(Item.EXTENDED_READ) && !item.hasPermission(CredentialsProvider.USE_ITEM)) {
return result.includeCurrentValue(jobCredentialId);
}
}
return result.includeEmptyValue()
.includeMatchingAs(
ACL.SYSTEM,
item,
StandardCredentials.class,
URIRequirementBuilder.fromUri(url).build(),
new GitLabCredentialMatcher())
.includeCurrentValue(jobCredentialId);
}

@RequirePOST
@Restricted(DoNotUse.class)
public FormValidation doTestConnection(
@QueryParameter String jobCredentialId,
@QueryParameter String gitLabConnection,
@AncestorInPath Item item) {
item.checkPermission(Item.CONFIGURE);
try {
GitLabConnection gitLabConnectionTested = null;
GitLabConnectionConfig descriptor =
(GitLabConnectionConfig) Jenkins.getInstance().getDescriptor(GitLabConnectionConfig.class);
for (GitLabConnection connection : descriptor.getConnections()) {
if (gitLabConnection.equals(connection.getName())) {
gitLabConnectionTested = connection;
GitLabConnectionConfig descriptor = (GitLabConnectionConfig)
Objects.requireNonNull(Jenkins.getInstance()).getDescriptor(GitLabConnectionConfig.class);

if (descriptor != null) {
for (GitLabConnection connection : descriptor.getConnections()) {
if (gitLabConnection.equals(connection.getName())) {
gitLabConnectionTested = connection;
}
}
} else {
throw new IllegalStateException("GitLabConnectionConfig descriptor cannot be null.");

Check warning on line 177 in src/main/java/com/dabsquared/gitlabjenkins/connection/GitLabConnectionProperty.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 113-177 are not covered by tests
}

if (gitLabConnectionTested == null) {
return FormValidation.error(Messages.connection_error("The GitLab Connection does not exist"));
}
Expand Down