From 4621e7abe869caa475adcd4c16e81ea4eb4fb249 Mon Sep 17 00:00:00 2001 From: Janos Gyerik Date: Thu, 30 Nov 2017 16:23:24 +0100 Subject: [PATCH] SONAR-10153 Add ScmProvider methods to get last revision and relative paths from SCM root --- .../org/sonar/api/batch/scm/ScmProvider.java | 21 +++++++++++++- .../sonar/api/batch/scm/ScmProviderTest.java | 28 +++++++++++++------ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/scm/ScmProvider.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/scm/ScmProvider.java index e73d05a06cd..052a9a766e3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/scm/ScmProvider.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/scm/ScmProvider.java @@ -53,7 +53,7 @@ public abstract class ScmProvider { } public BlameCommand blameCommand() { - throw new UnsupportedOperationException("Blame command is not supported by " + key() + " provider"); + throw new UnsupportedOperationException(formatUnsupportedMessage("Blame command")); } /** @@ -64,4 +64,23 @@ public abstract class ScmProvider { public Set branchChangedFiles(String targetBranchName, Path rootBaseDir) { return null; } + + /** + * The relative path from SCM root + */ + public Path relativePathFromScmRoot(Path path) { + throw new UnsupportedOperationException(formatUnsupportedMessage("Getting relative path from SCM root")); + } + + /** + * The current revision id of the analyzed code, + * for example the SHA1 of the current HEAD in a Git branch. + */ + public String revisionId(Path path) { + throw new UnsupportedOperationException(formatUnsupportedMessage("Getting revision id")); + } + + private String formatUnsupportedMessage(String prefix) { + return prefix + " is not supported by " + key() + " provider"; + } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/ScmProviderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/ScmProviderTest.java index 8e2b2dd04c1..31e65ebe356 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/ScmProviderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/scm/ScmProviderTest.java @@ -19,6 +19,7 @@ */ package org.sonar.api.batch.scm; +import java.nio.file.Paths; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -30,19 +31,30 @@ public class ScmProviderTest { @Rule public ExpectedException thrown = ExpectedException.none(); - @Test - public void testDefaultImplementation() { - ScmProvider provider = new ScmProvider() { + private final ScmProvider provider = new ScmProvider() { - @Override - public String key() { - return "foo"; - } - }; + @Override + public String key() { + return "foo"; + } + }; + @Test + public void default_implementation_does_not_support_blame() { assertThat(provider.supports(null)).isFalse(); thrown.expect(UnsupportedOperationException.class); provider.blameCommand(); } + @Test + public void default_implementation_does_not_support_relativePathFromScmRoot() { + thrown.expect(UnsupportedOperationException.class); + provider.relativePathFromScmRoot(Paths.get("foo")); + } + + @Test + public void default_implementation_does_not_support_revisionId() { + thrown.expect(UnsupportedOperationException.class); + provider.revisionId(Paths.get("foo")); + } } -- 2.39.5