From 0f7df6d8a8be251dd337993431efbc3900e1cd84 Mon Sep 17 00:00:00 2001 From: Konstantin Schaper Date: Sun, 29 Nov 2020 20:09:59 +0100 Subject: [PATCH] fix sonarqube finding --- .../sonia/scm/repository/spi/GitTagCommand.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitTagCommand.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitTagCommand.java index ca3bd78c6e..63515d2c2e 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitTagCommand.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/GitTagCommand.java @@ -128,11 +128,17 @@ public class GitTagCommand extends AbstractGitCommand implements TagCommand { try (Git git = new Git(context.open())) { String name = request.getName(); final Repository repository = git.getRepository(); - Ref tagRef = findTagRef(git, name); + Optional tagRef = findTagRef(git, name); Tag tag; + // Deleting a non-existent tag is a valid action and simply succeeds without + // anything happening. + if (!tagRef.isPresent()) { + return; + } + try (RevWalk walk = new RevWalk(repository)) { - final RevCommit commit = GitUtil.getCommit(repository, walk, tagRef); + final RevCommit commit = GitUtil.getCommit(repository, walk, tagRef.get()); Long tagTime = GitUtil.getTagTime(walk, commit.toObjectId()); tag = new Tag(name, commit.name(), tagTime); } @@ -146,9 +152,9 @@ public class GitTagCommand extends AbstractGitCommand implements TagCommand { } } - private Ref findTagRef(Git git, String name) throws GitAPIException { + private Optional findTagRef(Git git, String name) throws GitAPIException { final String tagRef = "refs/tags/" + name; - return git.tagList().call().stream().filter(it -> it.getName().equals(tagRef)).findAny().get(); + return git.tagList().call().stream().filter(it -> it.getName().equals(tagRef)).findAny(); } private RepositoryHookEvent createTagHookEvent(TagHookContextProvider hookEvent) {