From ce409098feab4f5f5ca8168cd995775e1fa32cbb Mon Sep 17 00:00:00 2001 From: Klaudio Sinani Date: Wed, 26 Oct 2022 15:16:57 +0200 Subject: [NO-JIRA] Fix reported code smells & bugs --- .../src/main/java/org/sonar/scanner/ci/vendors/Jenkins.java | 10 ++++++---- .../src/main/java/org/sonar/scm/git/GitScmProvider.java | 11 +++++------ 2 files changed, 11 insertions(+), 10 deletions(-) (limited to 'sonar-scanner-engine/src') diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/ci/vendors/Jenkins.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/ci/vendors/Jenkins.java index 50a89f72740..7721daadd6d 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/ci/vendors/Jenkins.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/ci/vendors/Jenkins.java @@ -20,7 +20,9 @@ package org.sonar.scanner.ci.vendors; import java.nio.file.Path; +import java.util.Optional; import org.apache.commons.lang.StringUtils; +import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryBuilder; @@ -99,10 +101,10 @@ public class Jenkins implements CiVendor { String refName = "refs/remotes/origin/" + gitBranch; try (Repository repo = builder.build()) { - Ref ref = repo.exactRef(refName); - if (ref != null) { - return ref.getObjectId().getName(); - } + return Optional.ofNullable(repo.exactRef(refName)) + .map(Ref::getObjectId) + .map(ObjectId::getName) + .orElse(null); } catch (Exception e) { LOG.debug("Couldn't find git sha1 in '{}': {}", refName, e.getMessage()); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java index bfcbe5f3726..68d1049aeae 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java @@ -48,6 +48,7 @@ import org.eclipse.jgit.diff.RenameDetector; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.NullProgressMonitor; +import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectReader; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; @@ -369,12 +370,10 @@ public class GitScmProvider extends ScmProvider { public String revisionId(Path path) { RepositoryBuilder builder = getVerifiedRepositoryBuilder(path); try { - Ref head = getHead(builder.build()); - if (head == null || head.getObjectId() == null) { - // can happen on fresh, empty repos - return null; - } - return head.getObjectId().getName(); + return Optional.ofNullable(getHead(builder.build())) + .map(Ref::getObjectId) + .map(ObjectId::getName) + .orElse(null); } catch (IOException e) { throw new IllegalStateException("I/O error while getting revision ID for path: " + path, e); } -- cgit v1.2.3