Skip to content

Commit

Permalink
Fix serverAuthenticationToken being required for configuration via CasC
Browse files Browse the repository at this point in the history
  • Loading branch information
Smasherr committed Aug 27, 2019
1 parent 8190c17 commit eb7ba40
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 23 deletions.
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,19 @@
<version>1.2.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>1.29</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<version>1.29</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/hudson/plugins/sonar/SonarInstallation.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.plaincredentials.StringCredentials;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

public class SonarInstallation implements Serializable {

Expand Down Expand Up @@ -76,24 +77,23 @@ public SonarInstallation(String name,
String serverUrl, String serverAuthenticationToken,
String mojoVersion, String additionalProperties, TriggersConfig triggers,
String additionalAnalysisProperties) {
this(name, serverUrl, null, Secret.fromString(StringUtils.trimToNull(serverAuthenticationToken)),
this(name, serverUrl, null,
mojoVersion, additionalProperties, additionalAnalysisProperties, triggers);
this.setServerAuthenticationToken(Secret.fromString(StringUtils.trimToNull(serverAuthenticationToken)));
}

@DataBoundConstructor
public SonarInstallation(
String name,
String serverUrl,
@Nullable String credentialsId,
@Nullable Secret serverAuthenticationToken,
String mojoVersion,
String additionalProperties,
String additionalAnalysisProperties,
TriggersConfig triggers) {
this.name = name;
this.serverUrl = serverUrl;
this.credentialsId = credentialsId;
this.serverAuthenticationToken = serverAuthenticationToken;
this.additionalAnalysisProperties = additionalAnalysisProperties;
this.mojoVersion = mojoVersion;
this.additionalProperties = additionalProperties;
Expand Down Expand Up @@ -157,6 +157,11 @@ public static final SonarInstallation get(String name) {
return null;
}

@DataBoundSetter
public void setServerAuthenticationToken(@Nullable Secret serverAuthenticationToken) {
this.serverAuthenticationToken = serverAuthenticationToken;
}

public String getName() {
return name;
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/hudson/plugins/sonar/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import hudson.model.Project;
import hudson.model.Result;
import hudson.model.Run;
import hudson.plugins.sonar.model.TriggersConfig;
import org.junit.Test;
import org.jvnet.hudson.test.MockBuilder;

Expand Down Expand Up @@ -123,7 +124,7 @@ private void configureSecuredSonar() {
SonarInstallation installation = spy(new SonarInstallation(
SONAR_INSTALLATION_NAME,
SONAR_HOST,
"token", null, null, null, null, null));
"token", null, null, null, (TriggersConfig)null));
addCredential("token", "secret");
configureSonar(installation);
}
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/hudson/plugins/sonar/CasCTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* SonarQube Scanner for Jenkins
* Copyright (C) 2007-2019 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package hudson.plugins.sonar;

import io.jenkins.plugins.casc.misc.RoundTripAbstractTest;
import jenkins.model.GlobalConfiguration;
import org.jvnet.hudson.test.RestartableJenkinsRule;

import static org.assertj.core.api.Assertions.assertThat;

public class CasCTest extends RoundTripAbstractTest {

@Override
protected String configResource() {
return "CasCTest/config.yaml";
}

@Override
protected void assertConfiguredAsExpected(RestartableJenkinsRule restartableJenkinsRule, String s) {
SonarInstallation installation = GlobalConfiguration.all()
.get(SonarGlobalConfiguration.class)
.getInstallations()[0];
assertThat(installation).isNotNull();
}

@Override
protected String stringInLogExpected() {
return "credentialsId = test-id";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import hudson.model.Result;
import hudson.model.Run;
import hudson.plugins.sonar.AbstractMsBuildSQRunner.SonarQubeScannerMsBuildParams;
import hudson.plugins.sonar.model.TriggersConfig;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
Expand Down Expand Up @@ -77,7 +78,7 @@ public void failExe() throws Exception {
@Test
public void additionalArgs() throws Exception {
SonarInstallation inst = new SonarInstallation("default", null,
null, null, null, "/x:a=b", "key=value", null);
null, null, "/x:a=b", "key=value", null);
configureSonar(inst);
configureMsBuildScanner(true);

Expand All @@ -90,8 +91,8 @@ public void additionalArgs() throws Exception {

@Test
public void testSonarProps() throws Exception {
SonarInstallation inst = spy(new SonarInstallation("default", "http://dummy-server:9090", "credentialsId", null,
null, null, null, null));
SonarInstallation inst = spy(new SonarInstallation("default", "http://dummy-server:9090", "credentialsId",
null, null, null, (TriggersConfig)null));
configureSonar(inst);
addCredential("credentialsId", "any-token");
configureMsBuildScanner(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import hudson.model.Result;
import hudson.model.Run;
import hudson.plugins.sonar.MsBuildSQRunnerEnd.DescriptorImpl;
import hudson.plugins.sonar.model.TriggersConfig;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -33,7 +34,7 @@ public class MsBuildSQRunnerEndTest extends MsBuildSQRunnerTest {

@Test
public void testToken() throws Exception {
SonarInstallation inst = spy(new SonarInstallation(SONAR_INSTALLATION_NAME, "localhost", "credentialsId", null,null, null, null, null));
SonarInstallation inst = spy(new SonarInstallation(SONAR_INSTALLATION_NAME, "localhost", "credentialsId", null, null, null, (TriggersConfig)null));
addCredential("credentialsId", "token");
configureSonar(inst);
configureMsBuildScanner(false);
Expand Down
9 changes: 4 additions & 5 deletions src/test/java/hudson/plugins/sonar/SonarBuildWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ public void testLogging() throws RunnerAbortedException, IOException, Interrupte
@Test
public void maskAuthToken() throws IOException, InterruptedException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
SonarInstallation inst = spy(new SonarInstallation("local", "http://localhost:9001", CREDENTIALSID, null, null, null,
null, new TriggersConfig()));
SonarInstallation inst = spy(new SonarInstallation("local", "http://localhost:9001", CREDENTIALSID, null, null, null, new TriggersConfig()));
addCredential(CREDENTIALSID, MYTOKEN);
configureSonar(inst);

Expand Down Expand Up @@ -138,7 +137,7 @@ public void testEnvironment() {

@Test
public void testEnvironmentMojoVersion() {
installation = new SonarInstallation(null, null, null, null, "2.0", null, null, null);
installation = new SonarInstallation(null, null, null, "2.0", null, null, (TriggersConfig)null);

Map<String, String> map = SonarBuildWrapper.createVars(installation, null, new EnvVars(), mock(Run.class));

Expand Down Expand Up @@ -248,14 +247,14 @@ private void enableBuildWrapper(boolean enable) {
private SonarInstallation createTestInstallation() {
addCredential(CREDENTIALSID, MYTOKEN);

return new SonarInstallation("local", "http://localhost:9001", CREDENTIALSID, null, null,
return new SonarInstallation("local", "http://localhost:9001", CREDENTIALSID, null,
"-X", "key=value", new TriggersConfig());
}

private SonarInstallation createTestInstallationForEnv() {
addCredential(CREDENTIALSID, MYTOKEN);

return new SonarInstallation("local", "http://$MY_SERVER:$MY_PORT", CREDENTIALSID, null, null,
return new SonarInstallation("local", "http://$MY_SERVER:$MY_PORT", CREDENTIALSID, null,
"-X", "key=$MY_VALUE", new TriggersConfig());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package hudson.plugins.sonar;

import hudson.plugins.sonar.model.TriggersConfig;
import hudson.util.FormValidation.Kind;
import jenkins.model.Jenkins;
import org.junit.Before;
Expand Down Expand Up @@ -47,7 +48,7 @@ private static SonarPublisher.DescriptorImpl getPublisherDescr(boolean buildWrap
}

private static SonarInstallation createInstallation(String name) {
return new SonarInstallation(name, null, null,null, null, null, null, null);
return new SonarInstallation(name, null, null, null, null, null, (TriggersConfig)null);
}

@Test
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/hudson/plugins/sonar/SonarInstallationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public void testRoundtrip() throws IOException {
"Name",
"server.url",
"credentialsId",
null,
"mojoVersion",
"props",
"key=value",
Expand Down Expand Up @@ -121,12 +120,12 @@ public void testAnalysisPropertiesUnix() {
}

private void assertAnalysisPropsWindows(String input, String... expectedEntries) {
SonarInstallation inst = new SonarInstallation(null, null, null, null, null, null, input, null);
SonarInstallation inst = new SonarInstallation(null, null, null, null, null, input, null);
assertThat(inst.getAdditionalAnalysisPropertiesWindows()).isEqualTo(expectedEntries);
}

private void assertAnalysisPropsUnix(String input, String... expectedEntries) {
SonarInstallation inst = new SonarInstallation(null, null, null, null, null, null, input, null);
SonarInstallation inst = new SonarInstallation(null, null, null, null, null, input, null);
assertThat(inst.getAdditionalAnalysisPropertiesUnix()).isEqualTo(expectedEntries);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void shouldBeEmptyInsteadOfNull() {
@Test
public void additionalArgs() {
ArgumentListBuilder args = new ArgumentListBuilder();
SonarInstallation inst = new SonarInstallation(null, null, null, null, null, "-Y", "key=value", null);
SonarInstallation inst = new SonarInstallation(null, null, null, null, "-Y", "key=value", null);
SonarRunnerBuilder builder = new SonarRunnerBuilder(null, null, "myCustomProjectSettings.properties", null, null, null, null, "-X -e");
builder.addAdditionalArguments(args, inst);
assertThat(args.toString()).isEqualTo("-Y -Dkey=value -X -e");
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/hudson/plugins/sonar/SonarTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected Maven.MavenInstallation configureDefaultMaven() throws Exception {
}

protected SonarInstallation configureDefaultSonar() {
return configureSonar(new SonarInstallation(SONAR_INSTALLATION_NAME, null, null, null, null, null, null, null));
return configureSonar(new SonarInstallation(SONAR_INSTALLATION_NAME, null, null, null, null, null, (TriggersConfig)null));
}

protected SonarInstallation configureSonar(SonarInstallation sonarInstallation) {
Expand Down Expand Up @@ -128,7 +128,7 @@ protected MavenModuleSet setupSonarMavenProject(String pomName) throws Exception
}

protected FreeStyleProject setupFreeStyleProjectWithSonarRunner() throws Exception {
return setupFreeStyleProject(new SonarRunnerBuilder(null, null, null, null, null, null, null, null));
return setupFreeStyleProject(new SonarRunnerBuilder());
}

protected FreeStyleProject setupFreeStyleProject(Builder b) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import hudson.model.Run;
import hudson.plugins.sonar.SonarInstallation;
import hudson.plugins.sonar.SonarTestCase;
import hudson.plugins.sonar.model.TriggersConfig;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
Expand Down Expand Up @@ -114,8 +115,8 @@ private void mockSQServer(Exception toThrow) throws Exception {
}

private void mockSQServer56() throws Exception {
SonarInstallation inst = spy(new SonarInstallation(SONAR_INSTALLATION_NAME, SERVER_URL, CREDENTIAL_ID, null, null, null,
null, null));
SonarInstallation inst = spy(new SonarInstallation(SONAR_INSTALLATION_NAME, SERVER_URL, CREDENTIAL_ID, null, null,
null, (TriggersConfig)null));
addCredential(CREDENTIAL_ID, TOKEN);
super.configureSonar(inst);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import hudson.model.queue.QueueTaskFuture;
import hudson.plugins.sonar.SonarGlobalConfiguration;
import hudson.plugins.sonar.SonarInstallation;
import hudson.plugins.sonar.model.TriggersConfig;
import hudson.plugins.sonar.utils.SonarUtils;
import org.apache.commons.lang.SystemUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
Expand Down Expand Up @@ -301,7 +302,7 @@ private QueueTaskFuture<WorkflowRun> submitPipeline(boolean specifyServer, boole
String serverUrl = "http://localhost:" + port + "/sonarqube";
story.j.jenkins.getDescriptorByType(SonarGlobalConfiguration.class)
.setInstallations(
new SonarInstallation(SONAR_INSTALLATION_NAME, serverUrl, null, null, null, null, null, null));
new SonarInstallation(SONAR_INSTALLATION_NAME, serverUrl, null, null, null, null, (TriggersConfig)null));
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, JOB_NAME);
String reportTaskContent1 = "dashboardUrl=" + serverUrl + "/dashboard\\n"
+ "ceTaskId=" + FAKE_TASK_ID_1 + "\\nserverUrl=" + serverUrl + "\\nprojectKey=foo";
Expand Down
13 changes: 13 additions & 0 deletions src/test/resources/hudson/plugins/sonar/CasCTest/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
unclassified:
sonarglobalconfiguration:
buildWrapperEnabled: true
installations:
- name: "TEST"
credentialsId: "test-id"
serverUrl: "http://url:9000"
mojoVersion: "mojoVersion"
additionalAnalysisProperties: "additionalAnalysisProperties"
triggers:
skipScmCause: true
skipUpstreamCause: true
envVar: "envVar"

0 comments on commit eb7ba40

Please sign in to comment.