diff options
author | Klaudio Sinani <klaudio.sinani@sonarsource.com> | 2022-10-26 15:16:57 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-10-28 20:03:23 +0000 |
commit | ce409098feab4f5f5ca8168cd995775e1fa32cbb (patch) | |
tree | a04c7bf89c2b7792dbed6895b6f3db0fd3171cb1 /sonar-scanner-engine/src/main/java | |
parent | 84dfd8b586f0c458d324e815f8b09ce3b395c044 (diff) | |
download | sonarqube-ce409098feab4f5f5ca8168cd995775e1fa32cbb.tar.gz sonarqube-ce409098feab4f5f5ca8168cd995775e1fa32cbb.zip |
[NO-JIRA] Fix reported code smells & bugs
Diffstat (limited to 'sonar-scanner-engine/src/main/java')
-rw-r--r-- | sonar-scanner-engine/src/main/java/org/sonar/scanner/ci/vendors/Jenkins.java | 10 | ||||
-rw-r--r-- | sonar-scanner-engine/src/main/java/org/sonar/scm/git/GitScmProvider.java | 11 |
2 files changed, 11 insertions, 10 deletions
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); } |