]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11077 display branch info as CE DEBUG log
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 3 Aug 2018 08:53:07 +0000 (10:53 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 5 Aug 2018 21:17:30 +0000 (23:17 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/component/BranchLoader.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/component/BranchLoaderTest.java

index 0c2d80f4a05c74ecf0d2fa50ebb62f3f9fadaf19..57ac865e523ffecd5c906aecec54b7177b97c969 100644 (file)
@@ -21,12 +21,17 @@ package org.sonar.server.computation.task.projectanalysis.component;
 
 import javax.annotation.Nullable;
 import org.sonar.api.utils.MessageException;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
 import org.sonar.scanner.protocol.output.ScannerReport;
+import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
 import org.sonar.server.computation.task.projectanalysis.analysis.MutableAnalysisMetadataHolder;
 
 import static org.apache.commons.lang.StringUtils.trimToNull;
 
 public class BranchLoader {
+  private static final Logger LOGGER = Loggers.get(BranchLoader.class);
+
   private final MutableAnalysisMetadataHolder metadataHolder;
   private final BranchLoaderDelegate delegate;
 
@@ -52,5 +57,14 @@ public class BranchLoader {
     } else {
       metadataHolder.setBranch(new DefaultBranchImpl(deprecatedBranch));
     }
+
+    Branch branch = metadataHolder.getBranch();
+    if (branch.isLegacyFeature() ) {
+      LOGGER.debug("On deprecated branch {}", branch.getName());
+    } else if (branch.isMain()) {
+      LOGGER.debug("On main branch");
+    } else {
+      LOGGER.debug("On branch {} [{}]", branch.getName(), branch.getType());
+    }
   }
 }
index 84ac5dd0d255646a3f823300faee8dc06c5d46ca..ead886922ae764d6fb8821d5275ee6b44b13425a 100644 (file)
  */
 package org.sonar.server.computation.task.projectanalysis.component;
 
+import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonar.api.utils.MessageException;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
 import org.sonar.db.component.BranchDto;
+import org.sonar.db.component.BranchType;
 import org.sonar.scanner.protocol.output.ScannerReport;
 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public class BranchLoaderTest {
   @Rule
@@ -38,6 +43,14 @@ public class BranchLoaderTest {
   @Rule
   public AnalysisMetadataHolderRule metadataHolder = new AnalysisMetadataHolderRule();
 
+  @Rule
+  public LogTester logTester = new LogTester();
+
+  @Before
+  public void setUp() {
+    logTester.setLevel(LoggerLevel.DEBUG);
+  }
+
   @Test
   public void throw_ME_if_both_branch_properties_are_set() {
     ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder()
@@ -63,10 +76,11 @@ public class BranchLoaderTest {
     Branch branch = metadataHolder.getBranch();
     assertThat(branch.isMain()).isTrue();
     assertThat(branch.getName()).isEqualTo(BranchDto.DEFAULT_MAIN_BRANCH_NAME);
+    assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("On main branch");
   }
 
   @Test
-  public void default_support_of_branches_is_enabled_if_delegate_is_absent() {
+  public void support_of_deprecated_branches_is_enabled_if_delegate_is_absent() {
     ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder()
       .setDeprecatedBranch("foo")
       .build();
@@ -78,10 +92,11 @@ public class BranchLoaderTest {
     Branch branch = metadataHolder.getBranch();
     assertThat(branch.isMain()).isTrue();
     assertThat(branch.getName()).isEqualTo("foo");
+    assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("On deprecated branch foo");
   }
 
   @Test
-  public void default_support_of_branches_is_enabled_if_delegate_is_present() {
+  public void support_of_deprecated_branches_is_enabled_if_delegate_is_present() {
     ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder()
       .setDeprecatedBranch("foo")
       .build();
@@ -94,6 +109,26 @@ public class BranchLoaderTest {
     Branch branch = metadataHolder.getBranch();
     assertThat(branch.isMain()).isTrue();
     assertThat(branch.getName()).isEqualTo("foo");
+    assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("On deprecated branch foo");
+  }
+
+  @Test
+  public void support_of_branches_is_enabled_if_delegate_is_present() {
+    ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder()
+      .setBranchName("foo")
+      .setBranchType(ScannerReport.Metadata.BranchType.SHORT)
+      .build();
+
+    FakeDelegate delegate = new FakeDelegate();
+    new BranchLoader(metadataHolder, delegate).load(metadata);
+
+    assertThat(metadataHolder.getBranch()).isNotNull();
+
+    Branch branch = metadataHolder.getBranch();
+    assertThat(branch.isMain()).isFalse();
+    assertThat(branch.getName()).isEqualTo("foo");
+    assertThat(branch.getType()).isEqualTo(BranchType.SHORT);
+    assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("On branch foo [SHORT]");
   }
 
   private class FakeDelegate implements BranchLoaderDelegate {
@@ -101,6 +136,9 @@ public class BranchLoaderTest {
 
     @Override
     public void load(ScannerReport.Metadata metadata) {
+      when(branch.isMain()).thenReturn(false);
+      when(branch.getName()).thenReturn(metadata.getBranchName());
+      when(branch.getType()).thenReturn(BranchType.valueOf(metadata.getBranchType().name()));
       metadataHolder.setBranch(branch);
     }
   }