@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(); }
@VisibleForTesting void logSuccess(@Nullable String taskId) { if (taskId == null) { LOG.info("ANALYSIS SUCCESSFUL"); } else { Map<String, String> metadata = new LinkedHashMap<>(); String effectiveKey = moduleHierarchy.root().getKeyWithBranch(); properties.organizationKey().ifPresent(org -> metadata.put("organization", org)); metadata.put("projectKey", effectiveKey); metadata.put("serverUrl", server.getPublicRootUrl()); metadata.put("serverVersion", server.getVersion()); properties.branch().ifPresent(branch -> metadata.put("branch", branch)); URL dashboardUrl = buildDashboardUrl(server.getPublicRootUrl(), effectiveKey); metadata.put("dashboardUrl", dashboardUrl.toExternalForm()); URL taskUrl = HttpUrl.parse(server.getPublicRootUrl()).newBuilder() .addPathSegment("api").addPathSegment("ce").addPathSegment("task") .addQueryParameter(ID, taskId) .build() .url(); metadata.put("ceTaskId", taskId); metadata.put("ceTaskUrl", taskUrl.toExternalForm()); LOG.info("ANALYSIS SUCCESSFUL, you can browse {}", dashboardUrl); LOG.info("Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report"); LOG.info("More about the report processing at {}", taskUrl); dumpMetadata(metadata); } }
@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"); }
private List<QualityProfile> loadAndOverrideIfNeeded(@Nullable String profileName, StringBuilder url) throws IOException { properties.organizationKey().ifPresent(k -> url.append("&organization=").append(encodeForUrl(k))); Map<String, QualityProfile> result = call(url.toString()); if (profileName != null) { StringBuilder urlForName = new StringBuilder(WS_URL + "?profileName="); urlForName.append(encodeForUrl(profileName)); properties.organizationKey().ifPresent(k -> urlForName.append("&organization=").append(encodeForUrl(k))); result.putAll(call(urlForName.toString())); } if (result.isEmpty()) { throw MessageException.of("No quality profiles have been found, you probably don't have any language plugin installed."); } return new ArrayList<>(result.values()); }
/** * This should be called in the beginning of the analysis to fail fast */ public void validate() { metadataFilePath(); } }
InputModuleHierarchy tree = getComponentByType(InputModuleHierarchy.class); ScanProperties properties = getComponentByType(ScanProperties.class); properties.validate(); properties.organizationKey().ifPresent(k -> LOG.info("Organization key: {}", k));
@Override public void stop() { if (!properties.shouldKeepReport()) { deleteQuietly(reportDir); } }
@Test public void should_define_preload_file_metadata() { settings.setProperty("sonar.preloadFileMetadata", "true"); assertThat(underTest.preloadFileMetadata()).isTrue(); }
@Test public void should_define_branch_name() { settings.setProperty("sonar.branch.name", "name"); assertThat(underTest.branch()).isEqualTo(Optional.of("name")); }
@Test public void validate_fails_if_metadata_file_location_is_not_absolute() { settings.setProperty("sonar.scanner.metadataFilePath", "relative"); exception.expect(MessageException.class); exception.expectMessage("Property 'sonar.scanner.metadataFilePath' must point to an absolute path: relative"); underTest.validate(); } }
@Test public void should_define_organization_key() { settings.setProperty("sonar.organization", "org"); assertThat(underTest.organizationKey()).isEqualTo(Optional.of("org")); }
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); } } }
@VisibleForTesting void logSuccess(@Nullable String taskId) { if (taskId == null) { LOG.info("ANALYSIS SUCCESSFUL"); } else { Map<String, String> metadata = new LinkedHashMap<>(); String effectiveKey = moduleHierarchy.root().getKeyWithBranch(); properties.organizationKey().ifPresent(org -> metadata.put("organization", org)); metadata.put("projectKey", effectiveKey); metadata.put("serverUrl", server.getPublicRootUrl()); metadata.put("serverVersion", server.getVersion()); properties.branch().ifPresent(branch -> metadata.put("branch", branch)); URL dashboardUrl = buildDashboardUrl(server.getPublicRootUrl(), effectiveKey); metadata.put("dashboardUrl", dashboardUrl.toExternalForm()); URL taskUrl = HttpUrl.parse(server.getPublicRootUrl()).newBuilder() .addPathSegment("api").addPathSegment("ce").addPathSegment("task") .addQueryParameter(ID, taskId) .build() .url(); metadata.put("ceTaskId", taskId); metadata.put("ceTaskUrl", taskUrl.toExternalForm()); LOG.info("ANALYSIS SUCCESSFUL, you can browse {}", dashboardUrl); LOG.info("Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report"); LOG.info("More about the report processing at {}", taskUrl); dumpMetadata(metadata); } }
InputModuleHierarchy tree = getComponentByType(InputModuleHierarchy.class); ScanProperties properties = getComponentByType(ScanProperties.class); properties.validate(); properties.organizationKey().ifPresent(k -> LOG.info("Organization key: {}", k));
@Test public void should_define_keep_report() { settings.setProperty("sonar.scanner.keepReport", "true"); assertThat(underTest.shouldKeepReport()).isTrue(); }
evaluateCoverageExclusions(moduleCoverageAndDuplicationExclusions, inputFile); evaluateDuplicationExclusions(moduleCoverageAndDuplicationExclusions, inputFile); if (properties.preloadFileMetadata()) { inputFile.checkMetadata();
@Test public void load_sets_organization_parameter_if_defined_in_settings() throws IOException { when(properties.organizationKey()).thenReturn(Optional.of("my-org")); prepareCallWithResults(); underTest.load("foo", null); verifyCalledPath("/api/qualityprofiles/search.protobuf?projectKey=foo&organization=my-org"); }
@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_not_delete_report_if_property_is_set() throws IOException { when(properties.shouldKeepReport()).thenReturn(true); Path reportDir = temp.getRoot().toPath().resolve("scanner-report"); Files.createDirectory(reportDir); underTest.start(); underTest.stop(); assertThat(reportDir).isDirectory(); }
evaluateCoverageExclusions(moduleCoverageAndDuplicationExclusions, inputFile); evaluateDuplicationExclusions(moduleCoverageAndDuplicationExclusions, inputFile); if (properties.preloadFileMetadata()) { inputFile.checkMetadata();