Skip to content

Commit

Permalink
reduce overall branches (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
MxEh-TT committed Dec 2, 2024
1 parent 5bb059b commit e2d7798
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,11 @@ class UploadReportsStep extends Step {

ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item item, @QueryParameter String credentialsId) {
StandardListBoxModel result = new StandardListBoxModel()
if (item == null) {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return result.includeCurrentValue(credentialsId)
}
} else {
if (!item.hasPermission(Item.EXTENDED_READ)
&& !item.hasPermission(CredentialsProvider.USE_ITEM)) {
return result.includeCurrentValue(credentialsId)
}
if (isItemNullAndUserLacksAdminPermission(item)){
return result.includeCurrentValue(credentialsId)
}
if (isItemNotNullAndUserLacksRequiredPermissions(item)) {
return result.includeCurrentValue(credentialsId)
}
return result
.includeEmptyValue()
Expand All @@ -280,27 +276,50 @@ class UploadReportsStep extends Step {
}

FormValidation doCheckCredentialsId(@AncestorInPath Item item, @QueryParameter String value) {
if (item == null) {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
return FormValidation.ok()
}
} else {
if (!item.hasPermission(Item.EXTENDED_READ)
&& !item.hasPermission(CredentialsProvider.USE_ITEM)) {
return FormValidation.ok()
}
if (isItemNullAndUserLacksAdminPermission(item)) {
return FormValidation.ok();
}

if (isItemNotNullAndUserLacksRequiredPermissions(item)) {
return FormValidation.ok();
}

if (StringUtils.isBlank(value)) {
return FormValidation.ok()
return FormValidation.ok();
}
if (value.startsWith('${') && value.endsWith('}')) {
return FormValidation.warning('Cannot validate expression based credentials')

if (isExpressionBasedCredentials(value)) {
return FormValidation.warning("Cannot validate expression-based credentials");
}
if (CredentialsProvider.listCredentials(StandardCredentials.class, (Item) item, ACL.SYSTEM,
Collections.emptyList(), CredentialsMatchers.withId(value)).isEmpty()) {
return FormValidation.error('Cannot find currently selected credentials')

if (areCredentialsNotFound(item, value)) {
return FormValidation.error("Cannot find currently selected credentials");
}
return FormValidation.ok()

return FormValidation.ok();
}

private boolean isItemNullAndUserLacksAdminPermission(Item item) {
return item == null && !Jenkins.get().hasPermission(Jenkins.ADMINISTER);
}

private boolean isItemNotNullAndUserLacksRequiredPermissions(Item item) {
return item != null && !item.hasPermission(Item.EXTENDED_READ) &&
!item.hasPermission(CredentialsProvider.USE_ITEM);
}

private boolean isExpressionBasedCredentials(String value) {
return value.startsWith('${') && value.endsWith('}');
}

private boolean areCredentialsNotFound(Item item, String value) {
return CredentialsProvider.listCredentials(
StandardCredentials.class,
item,
ACL.SYSTEM,
Collections.emptyList(),
CredentialsMatchers.withId(value)
).isEmpty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.cloudbees.plugins.credentials.CredentialsScope
import com.cloudbees.plugins.credentials.common.StandardListBoxModel
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl
import de.tracetronic.jenkins.plugins.ecutestexecution.clients.RestApiClientFactory
import de.tracetronic.jenkins.plugins.ecutestexecution.model.GenerationResult
import de.tracetronic.jenkins.plugins.ecutestexecution.model.UploadResult
import hudson.model.Item
import hudson.security.ACL
Expand Down

0 comments on commit e2d7798

Please sign in to comment.