Skip to content
This repository was archived by the owner on Jul 17, 2018. It is now read-only.

Commit 5da559d

Browse files
authored
Merge pull request #190 from danieljamesrees/sonar-github-integration-6
Sonar GitHub integration 6
2 parents be71029 + 0e58f24 commit 5da559d

File tree

5 files changed

+71
-32
lines changed

5 files changed

+71
-32
lines changed

jobs/sonarqube/spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ templates:
1010
bin/sonarqube_ctl: bin/sonarqube_ctl
1111
bin/monit_debugger: bin/monit_debugger
1212
bin/pre-start: bin/pre-start
13+
bin/post-deploy: bin/post-deploy
1314
config/sonar.properties: config/sonar.properties
1415
config/configure-groups.groovy: config/configure-groups.groovy
1516
config/configure-settings.groovy.erb: config/configure-settings.groovy
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
set -e # exit immediately if a simple command exits with a non-zero status
4+
set -u # report the usage of uninitialized variables
5+
6+
export JAVA_HOME="/var/vcap/packages/oraclejdk"
7+
export GROOVY_HOME="/var/vcap/packages/groovy"
8+
export GROOVY="${GROOVY_HOME}/bin/groovy"
9+
10+
cd /var/vcap/jobs/sonarqube/config
11+
12+
${GROOVY} configure-settings.groovy
13+
${GROOVY} configure-groups.groovy

jobs/sonarqube/templates/bin/pre-start

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,3 @@ set -u # report the usage of uninitialized variables
55

66
cp /var/vcap/jobs/sonarqube/config/sonar.properties /var/vcap/packages/sonarqube/conf/sonar.properties
77
chown vcap:vcap -R /var/vcap/packages/sonarqube/*
8-
9-
export GROOVY_HOME="/var/vcap/packages/groovy"
10-
export GROOVY="${GROOVY_HOME}/bin/groovy"
11-
12-
cd /var/vcap/jobs/sonarqube/config
13-
14-
${GROOVY} configure-settings.groovy
15-
${GROOVY} configure-groups.groovy

jobs/sonarqube/templates/config/SonarApiClient.groovy.erb

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,69 @@ class SonarApiClient
4848

4949
private static String generateAuthValue() {
5050
return "${username}:${password}".getBytes().encodeBase64().toString()
51-
}
52-
53-
static boolean postQueryString(String url, String queryString) {
51+
}
52+
53+
private static HttpURLConnection openPostConnection(String url) {
5454
def connection = new URL(url).openConnection() as HttpURLConnection
55-
55+
5656
connection.setRequestProperty('Accept', 'application/json')
57-
connection.setRequestProperty('Authorization', "Basic ${getAuthValue()}")
57+
connection.setRequestProperty('Authorization', "Basic ${generateAuthValue()}")
5858
connection.setRequestMethod('POST')
5959
connection.doOutput = true
60-
61-
def writer = new OutputStreamWriter(connection.outputStream)
62-
63-
try {
64-
writer.write(queryString)
65-
writer.flush()
66-
67-
if (connection.responseCode == HttpURLConnection.HTTP_OK ||
68-
connection.responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
69-
println "Request to ${url} with query string ${queryString} succeeded"
70-
} else {
71-
println "Request to ${url} with query string ${queryString} failed with status " +
72-
"${connection.responseCode} and response ${connection.responseMessage}"
60+
61+
return connection
62+
}
63+
64+
static boolean postQueryString(
65+
String url,
66+
String queryString,
67+
int numberOfTimesToRetry = 60,
68+
int delayBetweenRetriesInMilliseconds = 1000) {
69+
70+
def finalResponseReceived = false
71+
72+
while ((!finalResponseReceived) && (numberOfTimesToRetry-- > 0)) {
73+
74+
def connection
75+
def writer
76+
77+
try {
78+
connection = openPostConnection(url)
79+
writer = new OutputStreamWriter(connection.outputStream)
80+
81+
writer.write(queryString)
82+
writer.flush()
83+
84+
def responseCode = connection.responseMessage
85+
86+
if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
87+
sleep(delayBetweenRetriesInMilliseconds)
88+
println "Waiting ${delayBetweenRetriesInMilliseconds} milliseconds to post to ${url} with " +
89+
"${numberOfTimesToRetry} retries remaining"
90+
} else if (responseCode == HttpURLConnection.HTTP_OK ||
91+
responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
92+
println "Request to ${url} with query string ${queryString} succeeded"
93+
finalResponseReceived = true
94+
} else {
95+
println "Request to ${url} with query string ${queryString} failed with status " +
96+
"${responseCode} and response ${connection.responseMessage}"
97+
finalResponseReceived = true
98+
}
99+
} catch (IOException ioe) {
100+
println "Request failed with error: ${ioe}"
73101
return false
102+
} finally {
103+
if (writer != null) writer.close()
104+
if (connection != null) connection.disconnect()
74105
}
75-
} catch (IOException ioe) {
76-
println "Request failed with error: ${ioe}"
106+
}
107+
108+
if (!finalResponseReceived) {
109+
println "Timed out waiting for response from ${url}"
77110
return false
78-
} finally {
79-
writer.close()
80111
}
81-
112+
82113
return true
83114
}
84115
}
116+

packages/groovy/spec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
name: groovy
33

4-
dependencies: []
4+
dependencies:
5+
- oraclejdk
56

67
files:
78
- groovy/apache-groovy-binary-2.4.13.zip

0 commit comments

Comments
 (0)