Skip to content
This repository was archived by the owner on Apr 2, 2019. It is now read-only.

Commit 4e5a41e

Browse files
committed
SONARGITUB-14 Fail if plugin is used in publish mode
1 parent 9d527ff commit 4e5a41e

2 files changed

Lines changed: 98 additions & 1 deletion

File tree

src/main/java/org/sonar/plugins/github/PullRequestProjectBuilder.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
package org.sonar.plugins.github;
2121

2222
import org.kohsuke.github.GHCommitState;
23+
import org.sonar.api.CoreProperties;
2324
import org.sonar.api.batch.bootstrap.ProjectBuilder;
25+
import org.sonar.api.config.Settings;
26+
import org.sonar.api.utils.MessageException;
2427

2528
/**
2629
* Trigger load of pull request metadata at the very beginning of SQ analysis. Also
@@ -31,21 +34,31 @@ public class PullRequestProjectBuilder extends ProjectBuilder {
3134

3235
private final GitHubPluginConfiguration gitHubPluginConfiguration;
3336
private final PullRequestFacade pullRequestFacade;
37+
private final Settings settings;
3438

35-
public PullRequestProjectBuilder(GitHubPluginConfiguration gitHubPluginConfiguration, PullRequestFacade pullRequestFacade) {
39+
public PullRequestProjectBuilder(GitHubPluginConfiguration gitHubPluginConfiguration, PullRequestFacade pullRequestFacade, Settings settings) {
3640
this.gitHubPluginConfiguration = gitHubPluginConfiguration;
3741
this.pullRequestFacade = pullRequestFacade;
42+
this.settings = settings;
3843
}
3944

4045
@Override
4146
public void build(Context context) {
4247
if (!gitHubPluginConfiguration.isEnabled()) {
4348
return;
4449
}
50+
checkMode();
4551
int pullRequestNumber = gitHubPluginConfiguration.pullRequestNumber();
4652
pullRequestFacade.init(pullRequestNumber, context.projectReactor().getRoot().getBaseDir());
4753

4854
pullRequestFacade.createOrUpdateSonarQubeStatus(GHCommitState.PENDING, "SonarQube analysis in progress");
4955
}
5056

57+
private void checkMode() {
58+
if (!settings.getBoolean(CoreProperties.DRY_RUN)) {
59+
throw MessageException.of("The GitHub plugin is only intended to be used in preview mode. Please set '" + CoreProperties.ANALYSIS_MODE + "'.");
60+
}
61+
62+
}
63+
5164
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* SonarQube :: GitHub Plugin
3+
* Copyright (C) 2015 SonarSource
4+
* sonarqube@googlegroups.com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
19+
*/
20+
package org.sonar.plugins.github;
21+
22+
import java.io.File;
23+
import org.junit.Before;
24+
import org.junit.Rule;
25+
import org.junit.Test;
26+
import org.junit.rules.ExpectedException;
27+
import org.sonar.api.CoreProperties;
28+
import org.sonar.api.batch.bootstrap.ProjectBuilder;
29+
import org.sonar.api.config.PropertyDefinitions;
30+
import org.sonar.api.config.Settings;
31+
import org.sonar.api.utils.MessageException;
32+
33+
import static org.mockito.Matchers.any;
34+
import static org.mockito.Matchers.eq;
35+
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
36+
import static org.mockito.Mockito.mock;
37+
import static org.mockito.Mockito.verify;
38+
import static org.mockito.Mockito.verifyZeroInteractions;
39+
import static org.mockito.Mockito.withSettings;
40+
41+
public class PullRequestProjectBuilderTest {
42+
43+
@Rule
44+
public ExpectedException thrown = ExpectedException.none();
45+
46+
private PullRequestProjectBuilder pullRequestProjectBuilder;
47+
private PullRequestFacade facade;
48+
private Settings settings;
49+
50+
@Before
51+
public void prepare() {
52+
settings = new Settings(new PropertyDefinitions(GitHubPlugin.class));
53+
facade = mock(PullRequestFacade.class);
54+
pullRequestProjectBuilder = new PullRequestProjectBuilder(new GitHubPluginConfiguration(settings), facade, settings);
55+
56+
}
57+
58+
@Test
59+
public void shouldDoNothing() {
60+
pullRequestProjectBuilder.build(null);
61+
verifyZeroInteractions(facade);
62+
}
63+
64+
@Test
65+
public void shouldFailIfNotPreview() {
66+
settings.setProperty(GitHubPlugin.GITHUB_PULL_REQUEST, "1");
67+
68+
thrown.expect(MessageException.class);
69+
thrown.expectMessage("The GitHub plugin is only intended to be used in preview mode. Please set 'sonar.analysis.mode'.");
70+
71+
pullRequestProjectBuilder.build(null);
72+
}
73+
74+
@Test
75+
public void shouldNotFailIfPreview() {
76+
settings.setProperty(GitHubPlugin.GITHUB_PULL_REQUEST, "1");
77+
settings.setProperty(CoreProperties.DRY_RUN, "true");
78+
79+
pullRequestProjectBuilder.build(mock(ProjectBuilder.Context.class, withSettings().defaultAnswer(RETURNS_DEEP_STUBS)));
80+
81+
verify(facade).init(eq(1), any(File.class));
82+
83+
}
84+
}

0 commit comments

Comments
 (0)