Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/cnes report execution for 3.x branch #739

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fixed regression on project dump in release manager ([#666](https://github.com/opendevstack/ods-jenkins-shared-library/issues/666))
- MRO promotion to P/Q fails in cornercases when using monorepo ([#688](https://github.com/opendevstack/ods-jenkins-shared-library/issues/688))
- Fix RA table overflow replacing unicode character in jira keys ([#730](https://github.com/opendevstack/ods-jenkins-shared-library/pull/730))
- Fix Cnes report is executed too soon in the Sonar Scanner stage ([#732](https://github.com/opendevstack/ods-jenkins-shared-library/issues/732))
## [3.0] - 2020-08-11

### Added
Expand Down
30 changes: 30 additions & 0 deletions src/org/ods/component/ScanWithSonarStage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class ScanWithSonarStage extends Stage {
logger.startClocked("${sonarProjectKey}-sq-scan")
scan(sonarProperties)
logger.debugClocked("${sonarProjectKey}-sq-scan")
retryComputeEngineStatusCheck()

generateAndArchiveReports(
sonarProjectKey,
Expand Down Expand Up @@ -186,4 +187,33 @@ class ScanWithSonarStage extends Stage {
}
}

private String retryComputeEngineStatusCheck() {
def waitTime = 5 // seconds
def retries = 5
def taskProperties = sonarQube.readTask()

for (def i = 0; i < retries; i++) {
def computeEngineTaskResult
try {
computeEngineTaskResult = sonarQube.getComputeEngineTaskResult(taskProperties['ceTaskId'])
} catch (Exception ex) {
script.error 'Compute engine status could not be retrieved. ' +
"Status was: '${computeEngineTaskResult}'. Error was: ${ex}"
}
if (computeEngineTaskResult == 'IN_PROGRESS' || computeEngineTaskResult == 'PENDING') {
logger.info "SonarQube background task has not finished yet."
script.sleep(waitTime)
} else if (computeEngineTaskResult == 'SUCCESS') {
logger.info "SonarQube background task has finished successfully."
break
} else if (computeEngineTaskResult == 'FAILED') {
logger.info "SonarQube background task has failed!"
steps.error 'SonarQube Scanner stage has ended with errors'
} else {
logger.info "Unknown status for the background task"
steps.error 'SonarQube Scanner stage has ended with errors'
}
}
}

}
55 changes: 55 additions & 0 deletions src/org/ods/services/SonarQubeService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ class SonarQubeService {
script.readProperties(file: filename)
}

/*
Example of the file located in .scannerwork/report-task.txt:
projectKey=XXXX-python
serverUrl=https://sonarqube-ods.XXXX.com
serverVersion=8.2.0.32929
branch=dummy
dashboardUrl=https://sonarqube-ods.XXXX.com/dashboard?id=XXXX-python&branch=dummy
ceTaskId=AXxaAoUSsjAMlIY9kNmn
ceTaskUrl=https://sonarqube-ods.XXXX.com/api/ce/task?id=AXxaAoUSsjAMlIY9kNmn
*/
def readTask(String filename = '.scannerwork/report-task.txt') {
script.readProperties(file: filename)
}

def scan(Map properties, String gitCommit, Map pullRequestInfo = [:], String sonarQubeEdition,
boolean debug = false) {
withSonarServerConfig { hostUrl, authToken ->
Expand Down Expand Up @@ -78,6 +92,47 @@ class SonarQubeService {
}
}

/*
Example of the data returned in the api call api/ce/task:
"task": {
"organization": "my-org-1",
"id": "AVAn5RKqYwETbXvgas-I",
"type": "REPORT",
"componentId": "AVAn5RJmYwETbXvgas-H",
"componentKey": "project_1",
"componentName": "Project One",
"componentQualifier": "TRK",
"analysisId": "123456",
"status": "FAILED",
"submittedAt": "2015-10-02T11:32:15+0200",
"startedAt": "2015-10-02T11:32:16+0200",
"executedAt": "2015-10-02T11:32:22+0200",
"executionTimeMs": 5286,
"errorMessage": "Fail to extract report AVaXuGAi_te3Ldc_YItm from database",
"logs": false,
"hasErrorStacktrace": true,
"errorStacktrace": "java.lang.IllegalStateException: Fail to extract report from database",
"scannerContext": "SonarQube plugins:\n\t- Git 1.0 (scmgit)\n\t- Java 3.13.1 (java)",
"hasScannerContext": true
}
*/
def getComputeEngineTaskJSON(String taskid) {
withSonarServerConfig { hostUrl, authToken ->
script.sh(
label: 'Get status of compute engine task',
script: "curl -s -u ${authToken}: ${hostUrl}/api/ce/task?id=${taskid}",
returnStdout: true
)
}
}

String getComputeEngineTaskResult(String taskid) {
def computeEngineTaskJSON = getComputeEngineTaskJSON(taskid)
def computeEngineTaskResult = script.readJSON(text: computeEngineTaskJSON)
def status = computeEngineTaskResult?.task?.status ?: 'UNKNOWN'
return status.toUpperCase()
}

private String getScannerBinary() {
def scannerBinary = 'sonar-scanner'
def status = script.sh(
Expand Down
6 changes: 6 additions & 0 deletions test/groovy/vars/OdsComponentStageScanWithSonarSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class OdsComponentStageScanWithSonarSpec extends PipelineSpockTestBase {
ServiceRegistry.instance.add(BitbucketService, bitbucketService)
SonarQubeService sonarQubeService = Stub(SonarQubeService.class)
sonarQubeService.readProperties() >> ['sonar.projectKey': 'foo']
sonarQubeService.readTask() >> ['ceTaskId': 'AXxaAoUSsjAMlIY9kNmn']
sonarQubeService.getComputeEngineTaskResult(*_) >> 'SUCCESS'
sonarQubeService.scan(*_) >> null
ServiceRegistry.instance.add(SonarQubeService, sonarQubeService)

Expand All @@ -66,6 +68,8 @@ class OdsComponentStageScanWithSonarSpec extends PipelineSpockTestBase {
ServiceRegistry.instance.add(BitbucketService, bitbucketService)
SonarQubeService sonarQubeService = Mock(SonarQubeService.class)
sonarQubeService.readProperties() >> ['sonar.projectKey': 'foo']
sonarQubeService.readTask() >> ['ceTaskId': 'AXxaAoUSsjAMlIY9kNmn']
sonarQubeService.getComputeEngineTaskResult(*_) >> 'SUCCESS'
ServiceRegistry.instance.add(SonarQubeService, sonarQubeService)

when:
Expand Down Expand Up @@ -101,6 +105,8 @@ class OdsComponentStageScanWithSonarSpec extends PipelineSpockTestBase {
IContext context = new Context(null, c, logger)
SonarQubeService sonarQubeService = Stub(SonarQubeService.class)
sonarQubeService.readProperties() >> ['sonar.projectKey': 'foo']
sonarQubeService.readTask() >> ['ceTaskId': 'AXxaAoUSsjAMlIY9kNmn']
sonarQubeService.getComputeEngineTaskResult(*_) >> 'SUCCESS'
sonarQubeService.scan(*_) >> null
sonarQubeService.getQualityGateJSON(*_) >> """{"projectStatus": ${projectStatus}}"""
ServiceRegistry.instance.add(SonarQubeService, sonarQubeService)
Expand Down