Skip to content

Commit

Permalink
Merge pull request #164 from William-Hill-Online/removingDeprecatedPr…
Browse files Browse the repository at this point in the history
…ojectBuilder

Using properties instead of depending on the deprecated . Fixes #162
  • Loading branch information
gabrie-allaigre authored Aug 26, 2018
2 parents 1aa1490 + 71448aa commit 0f99e94
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 166 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
public class CommitPublishPostJob implements PostJob {

private static final Logger LOG = Loggers.get(CommitPublishPostJob.class);
private static final String SONAR_PROJECT_BASE_DIR = "sonar.projectBaseDir";
private static final String SONAR_WORKING_DIRECTORY = "sonar.working.directory";

private final GitLabPluginConfiguration gitLabPluginConfiguration;
private final SonarFacade sonarFacade;
Expand All @@ -60,12 +62,23 @@ public CommitPublishPostJob(GitLabPluginConfiguration gitLabPluginConfiguration,
@Override
public void describe(PostJobDescriptor descriptor) {
descriptor.name("GitLab Commit Issue Publisher")
.requireProperty(GitLabPlugin.GITLAB_URL, GitLabPlugin.GITLAB_USER_TOKEN, GitLabPlugin.GITLAB_PROJECT_ID, GitLabPlugin.GITLAB_COMMIT_SHA, GitLabPlugin.GITLAB_REF_NAME);
.requireProperty(GitLabPlugin.GITLAB_URL, GitLabPlugin.GITLAB_USER_TOKEN, GitLabPlugin.GITLAB_PROJECT_ID,
GitLabPlugin.GITLAB_COMMIT_SHA, GitLabPlugin.GITLAB_REF_NAME, SONAR_PROJECT_BASE_DIR, SONAR_WORKING_DIRECTORY);
}

@Override
public void execute(PostJobContext context) {
try {
if (!gitLabPluginConfiguration.isEnabled()) {
return;
}
File baseDir = fileFromProperty(context, "sonar.projectBaseDir");
sonarFacade.init(baseDir, fileFromProperty(context, "sonar.working.directory"));
commitFacade.init(baseDir);

if (StatusNotificationsMode.COMMIT_STATUS.equals(gitLabPluginConfiguration.statusNotificationsMode())) {
commitFacade.createOrUpdateSonarQubeStatus(gitLabPluginConfiguration.buildInitState().getMeaning(), "SonarQube analysis in progress");
}
QualityGate qualityGate;
List<Issue> issues;
if (context.analysisMode().isPublish()) {
Expand Down Expand Up @@ -95,6 +108,11 @@ public void execute(PostJobContext context) {
}
}

@SuppressWarnings("OptionalGetWithoutIsPresent")
private File fileFromProperty(PostJobContext context, String property) {
return new File(context.config().get(property).get());
}

private List<Issue> toIssues(Iterable<PostJobIssue> postJobIssues) {
if (postJobIssues == null) {
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,6 @@ public static List<PropertyDefinition> definitions() {

@Override
public void define(Context context) {
context.addExtensions(ReporterBuilder.class, GitLabPluginConfiguration.class, CommitProjectBuilder.class, CommitFacade.class, SonarFacade.class, MarkDownUtils.class, CommitPublishPostJob.class).addExtensions(definitions());
context.addExtensions(ReporterBuilder.class, GitLabPluginConfiguration.class, CommitFacade.class, SonarFacade.class, MarkDownUtils.class, CommitPublishPostJob.class).addExtensions(definitions());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@
import org.sonar.api.batch.postjob.internal.DefaultPostJobDescriptor;
import org.sonar.api.batch.postjob.issue.PostJobIssue;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.config.Configuration;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.internal.ConfigurationBridge;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.System2;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
Expand All @@ -66,6 +69,8 @@ public void prepare() {
.category(CoreProperties.CATEGORY_GENERAL).defaultValue(CoreProperties.SERVER_BASE_URL_DEFAULT_VALUE).build()).addComponents(GitLabPlugin.definitions()));
settings.setProperty(CoreProperties.SERVER_BASE_URL, "http://myserver");
settings.setProperty(GitLabPlugin.GITLAB_COMMIT_SHA, "abc123");
settings.setProperty("sonar.projectBaseDir", "projectBaseDir");
settings.setProperty("sonar.working.directory", "workingDir");

sonarFacade = Mockito.mock(SonarFacade.class);

Expand All @@ -77,6 +82,7 @@ public void prepare() {
when(analysisMode.isPublish()).thenReturn(false);
context = Mockito.mock(PostJobContext.class);
when(context.analysisMode()).thenReturn(analysisMode);
when(context.config()).thenReturn(new ConfigurationBridge(settings));

GitLabPluginConfiguration config = new GitLabPluginConfiguration(settings.asConfig(), new System2());

Expand All @@ -90,7 +96,8 @@ public void testDescriptor() {

Assertions.assertThat(postJobDescriptor.name()).isEqualTo("GitLab Commit Issue Publisher");
Assertions.assertThat(postJobDescriptor.properties())
.containsExactly(GitLabPlugin.GITLAB_URL, GitLabPlugin.GITLAB_USER_TOKEN, GitLabPlugin.GITLAB_PROJECT_ID, GitLabPlugin.GITLAB_COMMIT_SHA, GitLabPlugin.GITLAB_REF_NAME);
.containsExactly(GitLabPlugin.GITLAB_URL, GitLabPlugin.GITLAB_USER_TOKEN, GitLabPlugin.GITLAB_PROJECT_ID,
GitLabPlugin.GITLAB_COMMIT_SHA, GitLabPlugin.GITLAB_REF_NAME, "sonar.projectBaseDir", "sonar.working.directory");
}

@Test
Expand Down

0 comments on commit 0f99e94

Please sign in to comment.