Skip to content

Commit

Permalink
Merge pull request #39 from cloudogu/feature/sonar_configurable_timeout
Browse files Browse the repository at this point in the history
Make SonarQube timeout configurable
  • Loading branch information
schnatterer authored May 25, 2020
2 parents 143b178 + a5c11c3 commit 6ecc912
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ stage('Statical Code Analysis') {
def sonarQube = new SonarQube(this, [sonarQubeEnv: 'sonarQubeServerSetupInJenkins'])
sonarQube.analyzeWith(new MavenInDocker(this, "3.5.0-jdk-8"))
sonarQube.timeoutInMinutes = 4
if (!sonarQube.waitForQualityGateWebhookToBeCalled()) {
unstable("Pipeline unstable due to SonarQube quality gate failure")
Expand All @@ -689,6 +690,7 @@ Note that
* Calling `waitForQualityGateWebhookToBeCalled()` requires a WebHook to be setup in your SonarQube server (globally or
per project), that notifies Jenkins (url: `https://yourJenkinsInstance/sonarqube-webhook/`).
See [SonarQube Scanner for Jenkins](https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins#AnalyzingwithSonarQubeScannerforJenkins-AnalyzinginaJenkinspipeline).
* Jenkins will wait for the webhook with a default timeout of 2 minutes, for big projects this might be to short and can be configured with the `timeoutInMinutes` property.
* Calling `waitForQualityGateWebhookToBeCalled()` will only work when an analysis has been performed in the current job,
i.e. `analyzeWith()` has been called and in conjuction with `sonarQubeEnv`.
* When used in conjunction with [SonarQubeCommunity/sonar-build-breaker](https://github.com/SonarQubeCommunity/sonar-build-breaker),
Expand Down
3 changes: 2 additions & 1 deletion src/com/cloudogu/ces/cesbuildlib/SonarQube.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class SonarQube implements Serializable {
// If enabled uses the branch plugin, available for developer edition and above
boolean isUsingBranchPlugin = false
boolean isIgnoringBranches = false
int timeoutInMinutes = 2
protected Map config

@Deprecated
Expand Down Expand Up @@ -64,7 +65,7 @@ class SonarQube implements Serializable {
}

protected boolean doWaitForQualityGateWebhookToBeCalled() {
script.timeout(time: 2, unit: 'MINUTES') { // Needed when there is no webhook for example
script.timeout(time: timeoutInMinutes, unit: 'MINUTES') { // Needed when there is no webhook for example
def qGate = script.waitForQualityGate()
script.echo "SonarQube Quality Gate status: ${qGate.status}"
if (qGate.status != 'OK') {
Expand Down
4 changes: 4 additions & 0 deletions test/com/cloudogu/ces/cesbuildlib/ScriptMock.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class ScriptMock {
def expectedShRetValueForScript = [:]

String actualSonarQubeEnv

Map actualTimeoutParams = [:]

List<Map> actualUsernamePasswordArgs = []
List<String> actualShStringArgs = new LinkedList<>()
List<String> actualEcho = new LinkedList<>()
Expand Down Expand Up @@ -55,6 +58,7 @@ class ScriptMock {
}

def timeout(Map params, closure) {
this.actualTimeoutParams = params
return closure.call()
}

Expand Down
24 changes: 23 additions & 1 deletion test/com/cloudogu/ces/cesbuildlib/SonarQubeTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class SonarQubeTest {
assert mavenMock.additionalArgs == '-X -Dsonar.branch.name=develop '
}

@Test
@Test
void analyzeWithBranchPluginWithConfiguredIntegrationBranch() throws Exception {
scriptMock.env = [
BRANCH_NAME: 'feature'
Expand Down Expand Up @@ -273,6 +273,28 @@ class SonarQubeTest {
assert !qualityGate
}

@Test
void waitForQualityGateWithDefaultTimeout() {
scriptMock.expectedQGate = [status: 'OK']

new SonarQube(scriptMock, 'sqEnv').waitForQualityGateWebhookToBeCalled()

assert scriptMock.actualTimeoutParams.time == 2
assert scriptMock.actualTimeoutParams.unit == 'MINUTES'
}

@Test
void waitForQualityGateWithConfiguredTimeout() {
scriptMock.expectedQGate = [status: 'OK']

def sonarQube = new SonarQube(scriptMock, 'sqEnv')
sonarQube.timeoutInMinutes = 10
sonarQube.waitForQualityGateWebhookToBeCalled()

assert scriptMock.actualTimeoutParams.time == 10
assert scriptMock.actualTimeoutParams.unit == 'MINUTES'
}

private void assertSonarHostUrlError(configMapg) {
def exception = shouldFail {
new SonarQube(scriptMock, configMapg).analyzeWith(mavenMock)
Expand Down

0 comments on commit 6ecc912

Please sign in to comment.