/** * This should be called in the beginning of the analysis to fail fast */ public void validate() { metadataFilePath(); } }
private void dumpMetadata(Map<String, String> metadata) { Path file = properties.metadataFilePath(); try { Files.createDirectories(file.getParent()); try (Writer output = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) { for (Map.Entry<String, String> entry : metadata.entrySet()) { output.write(entry.getKey()); output.write("="); output.write(entry.getValue()); output.write("\n"); } } LOG.debug("Report metadata written to {}", file); } catch (IOException e) { throw new IllegalStateException("Unable to dump " + file, e); } } }
@Test public void log_and_dump_information_to_custom_path() throws IOException { underTest.logSuccess("TASK-123"); assertThat(properties.metadataFilePath()).exists(); assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Report metadata written to " + properties.metadataFilePath()); }
@Test public void should_define_metadata_file_path() throws IOException { Path path = temp.newFolder().toPath().resolve("report"); settings.setProperty("sonar.scanner.metadataFilePath", path.toString()); assertThat(underTest.metadataFilePath()).isEqualTo(path); }
@Test public void log_and_dump_information_about_report_uploading() throws IOException { when(properties.organizationKey()).thenReturn(Optional.of("MyOrg")); underTest.logSuccess("TASK-123"); assertThat(logTester.logs(LoggerLevel.INFO)) .contains("ANALYSIS SUCCESSFUL, you can browse https://localhost/dashboard?id=org.sonarsource.sonarqube%3Asonarqube") .contains("Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report") .contains("More about the report processing at https://localhost/api/ce/task?id=TASK-123"); assertThat(readFileToString(properties.metadataFilePath().toFile(), StandardCharsets.UTF_8)).isEqualTo( "organization=MyOrg\n" + "projectKey=org.sonarsource.sonarqube:sonarqube\n" + "serverUrl=https://localhost\n" + "serverVersion=6.4\n" + "dashboardUrl=https://localhost/dashboard?id=org.sonarsource.sonarqube%3Asonarqube\n" + "ceTaskId=TASK-123\n" + "ceTaskUrl=https://localhost/api/ce/task?id=TASK-123\n"); }
@Test public void log_public_url_if_defined_for_main_branch() throws IOException { when(server.getPublicRootUrl()).thenReturn("https://publicserver/sonarqube"); underTest.logSuccess("TASK-123"); assertThat(logTester.logs(LoggerLevel.INFO)) .contains("ANALYSIS SUCCESSFUL, you can browse https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube") .contains("More about the report processing at https://publicserver/sonarqube/api/ce/task?id=TASK-123"); assertThat(readFileToString(properties.metadataFilePath().toFile(), StandardCharsets.UTF_8)).isEqualTo( "projectKey=org.sonarsource.sonarqube:sonarqube\n" + "serverUrl=https://publicserver/sonarqube\n" + "serverVersion=6.4\n" + "dashboardUrl=https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube\n" + "ceTaskId=TASK-123\n" + "ceTaskUrl=https://publicserver/sonarqube/api/ce/task?id=TASK-123\n"); }
@Test public void defaults_if_no_setting_defined() { assertThat(underTest.branch()).isEmpty(); assertThat(underTest.organizationKey()).isEmpty(); assertThat(underTest.preloadFileMetadata()).isFalse(); assertThat(underTest.shouldKeepReport()).isFalse(); assertThat(underTest.metadataFilePath()).isEqualTo(project.getWorkDir().resolve("report-task.txt")); underTest.validate(); }
@Before public void setUp() throws IOException { root = new DefaultInputModule(ProjectDefinition.create().setKey("org.sonarsource.sonarqube:sonarqube").setBaseDir(temp.newFolder()).setWorkDir(temp.getRoot())); when(moduleHierarchy.root()).thenReturn(root); when(server.getPublicRootUrl()).thenReturn("https://localhost"); when(server.getVersion()).thenReturn("6.4"); when(properties.metadataFilePath()).thenReturn(temp.newFolder().toPath() .resolve("folder") .resolve("report-task.txt")); }
@Test public void log_public_url_if_defined_for_short_living_branches() throws IOException { when(server.getPublicRootUrl()).thenReturn("https://publicserver/sonarqube"); when(branchConfiguration.branchType()).thenReturn(SHORT); when(branchConfiguration.branchName()).thenReturn("branch-6.7"); ReportPublisher underTest = new ReportPublisher(properties, wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class), new ReportPublisherStep[0], branchConfiguration); underTest.logSuccess("TASK-123"); assertThat(logTester.logs(LoggerLevel.INFO)) .contains("ANALYSIS SUCCESSFUL, you can browse https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube&branch=branch-6.7&resolved=false") .contains("More about the report processing at https://publicserver/sonarqube/api/ce/task?id=TASK-123"); assertThat(readFileToString(properties.metadataFilePath().toFile(), StandardCharsets.UTF_8)).isEqualTo( "projectKey=org.sonarsource.sonarqube:sonarqube\n" + "serverUrl=https://publicserver/sonarqube\n" + "serverVersion=6.4\n" + "dashboardUrl=https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube&branch=branch-6.7&resolved=false\n" + "ceTaskId=TASK-123\n" + "ceTaskUrl=https://publicserver/sonarqube/api/ce/task?id=TASK-123\n"); }
@Test public void log_public_url_if_defined_for_pull_request() throws IOException { when(server.getPublicRootUrl()).thenReturn("https://publicserver/sonarqube"); when(branchConfiguration.branchName()).thenReturn("Bitbucket cloud Widget"); when(branchConfiguration.branchType()).thenReturn(PULL_REQUEST); when(branchConfiguration.pullRequestKey()).thenReturn("105"); ReportPublisher underTest = new ReportPublisher(properties, wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class), new ReportPublisherStep[0], branchConfiguration); underTest.logSuccess("TASK-123"); assertThat(logTester.logs(LoggerLevel.INFO)) .contains("ANALYSIS SUCCESSFUL, you can browse https://publicserver/sonarqube/project/issues?id=org.sonarsource.sonarqube%3Asonarqube&pullRequest=105&resolved=false") .contains("More about the report processing at https://publicserver/sonarqube/api/ce/task?id=TASK-123"); assertThat(readFileToString(properties.metadataFilePath().toFile(), StandardCharsets.UTF_8)).isEqualTo( "projectKey=org.sonarsource.sonarqube:sonarqube\n" + "serverUrl=https://publicserver/sonarqube\n" + "serverVersion=6.4\n" + "dashboardUrl=https://publicserver/sonarqube/project/issues?id=org.sonarsource.sonarqube%3Asonarqube&pullRequest=105&resolved=false\n" + "ceTaskId=TASK-123\n" + "ceTaskUrl=https://publicserver/sonarqube/api/ce/task?id=TASK-123\n"); }
@Test public void log_public_url_if_defined_for_long_living_branches() throws IOException { when(server.getPublicRootUrl()).thenReturn("https://publicserver/sonarqube"); when(branchConfiguration.branchType()).thenReturn(LONG); when(branchConfiguration.branchName()).thenReturn("branch-6.7"); ReportPublisher underTest = new ReportPublisher(properties, wsClient, server, contextPublisher, moduleHierarchy, mode, mock(TempFolder.class), new ReportPublisherStep[0], branchConfiguration); underTest.logSuccess("TASK-123"); assertThat(logTester.logs(LoggerLevel.INFO)) .contains("ANALYSIS SUCCESSFUL, you can browse https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube&branch=branch-6.7") .contains("More about the report processing at https://publicserver/sonarqube/api/ce/task?id=TASK-123"); assertThat(readFileToString(properties.metadataFilePath().toFile(), StandardCharsets.UTF_8)).isEqualTo( "projectKey=org.sonarsource.sonarqube:sonarqube\n" + "serverUrl=https://publicserver/sonarqube\n" + "serverVersion=6.4\n" + "dashboardUrl=https://publicserver/sonarqube/dashboard?id=org.sonarsource.sonarqube%3Asonarqube&branch=branch-6.7\n" + "ceTaskId=TASK-123\n" + "ceTaskUrl=https://publicserver/sonarqube/api/ce/task?id=TASK-123\n"); }
@Test public void log_but_not_dump_information_when_report_is_not_uploaded() throws IOException { underTest.logSuccess(/* report not uploaded, no server task */null); assertThat(logTester.logs(LoggerLevel.INFO)) .contains("ANALYSIS SUCCESSFUL") .doesNotContain("dashboard/index"); assertThat(properties.metadataFilePath()).doesNotExist(); }
/** * This should be called in the beginning of the analysis to fail fast */ public void validate() { metadataFilePath(); } }
private void dumpMetadata(Map<String, String> metadata) { Path file = properties.metadataFilePath(); try { Files.createDirectories(file.getParent()); try (Writer output = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) { for (Map.Entry<String, String> entry : metadata.entrySet()) { output.write(entry.getKey()); output.write("="); output.write(entry.getValue()); output.write("\n"); } } LOG.debug("Report metadata written to {}", file); } catch (IOException e) { throw new IllegalStateException("Unable to dump " + file, e); } } }