Skip to content

Commit 4f9c85f

Browse files
committed
CCS-4 Enhance jira and Sonar evidence extraction and add documentation
1 parent c90afa3 commit 4f9c85f

10 files changed

+69
-3
lines changed

.github/workflows/sonar-evidence-example.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ jobs:
5959
-Dsonar.host.url=https://sonarcloud.io \
6060
-Dsonar.java.jdkHome=$JAVA_HOME \
6161
-Dsonar.verbose=true \
62-
-Dsonar.token=$SONAR_TOKEN -X
62+
-Dsonar.token=$SONAR_TOKEN
6363
# create evidence from sonar-scan analysis
64-
./examples/sonar-scan-example/bin/sonar-scan-extractor-linux-amd64 $PWD/.scannerwork/report-task.txt > predicate.json
64+
./examples/sonar-scan-example/bin/sonar-scan-extractor-linux-amd64 --reportTaskFile=$PWD/.scannerwork/report-task.txt --FailOnAnalysisFailure > predicate.json
6565
6666
- name: Log in to Artifactory Docker Registry
6767
uses: docker/login-action@v3
Binary file not shown.
Binary file not shown.
Binary file not shown.

examples/jira-transition-example/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"tasks": [
1717
{
1818
"jira_id": "<jira-id>",
19+
"summary": "<summary>",
1920
"transition_found": "<true/false>"
2021
"author": "<user display name>",
2122
"author_user_name": "<user email>",
@@ -36,6 +37,7 @@ type TransitionCheckResponse struct {
3637

3738
type JiraTransitionResult struct {
3839
JiraId string `json:"jira_id"`
40+
Summary string `json:"summary"`
3941
TransitionFound bool `json:"transition_found"`
4042
Author string `json:"author"`
4143
AuthorEmail string `json:"author_user_name"`
@@ -92,6 +94,7 @@ func main() {
9294
// adding the jira result to the list of results
9395
jiraTransitionResult := JiraTransitionResult{
9496
JiraId: jiraId,
97+
Summary: issue.Fields.Summary,
9598
}
9699

97100
if len(issue.Changelog.Histories) > 0 {

examples/sonar-scan-example/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Create Sonar Scan Evidence predicate from the build CI and attach it to the build info
2+
Sonar is a code scanning tool that helps to identify bugs, vulnerabilities, and code smells in your code.
3+
It is important to track the code quality and security of the code changes done and released.
4+
To allow automation of proper code quality and security checks, we create an evidence of the Sonar scan results
5+
during the build with confirmation that the code quality and security checks passed before the code was committed.
6+
using the `FailOnAnalysisFailure` argument the customer can decide if to create the sonar scan evidence if the scan did not pass
7+
sonar quality gates, or fail the predicate creation with exist status 1.
8+
If the Analysis status is not 'OK', but `FailOnAnalysisFailure` was not set, then the predicate is created with analysis.status = 'ERROR' which
9+
should be checked using a policy.
10+
11+
## Environment variables
12+
- `SONAR_TOKEN` - The sonar server token.
13+
14+
## Arguments
15+
--reportTaskFile=<path> - The path to the sonar report task file.
16+
--FailOnAnalysisFailure - Fail with exit code 1 if the sonar analysis failed in sonar quality gate.
17+
18+
19+
## The example is based on the following steps:
20+
1. set sonar token as an environment variable
21+
2. call sonar scan
22+
for example:
23+
``
24+
$PWD/sonar-scanner-6.2.1.4610/bin/sonar-scanner \
25+
-Dsonar.projectKey=my-sonar-project-key \
26+
-Dsonar.organization=my-sonar-org \
27+
-Dsonar.host.url=https://sonarcloud.io \
28+
-Dsonar.java.jdkHome=$JAVA_HOME \
29+
-Dsonar.verbose=true \
30+
-Dsonar.token=$SONAR_TOKEN
31+
``
32+
3. call the jira-transition-checker utility (use the binary for your build platform) with these arguments: "transition name" JIRA-ID [,JIRA-ID]
33+
for example:
34+
``./examples/sonar-scan-example/bin/sonar-scan-extractor-linux-amd64 --reportTaskFile=$PWD/.scannerwork/report-task.txt --FailOnAnalysisFailure > predicate.json
35+
``
36+
4. call the evidence create cli with the predicate.json file
37+
for example:
38+
``
39+
jf evd create \
40+
--build-name $GITHUB_WORKFLOW \
41+
--build-number "${{ github.run_number }}" \
42+
--predicate ./predicate.json \
43+
--predicate-type https://jfrog.com/evidence/sonar-scan/v1 \
44+
--key "${{ secrets.JIRA_TEST_PKEY }}" \
45+
--key-alias ${{ vars.JIRA_TEST_KEY }}
46+
``
Binary file not shown.
Binary file not shown.
Binary file not shown.

examples/sonar-scan-example/main.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,21 @@ func main() {
9292
///home/runner/work/Evidence-Examples/Evidence-Examples/.scannerwork/report-task.txt
9393
//get the sonar report file location or details to .scannerwork/.report-task.txt
9494
reportTaskFile := ".scannerwork/.report-task.txt"
95+
failOnAnalysisFailure := false
9596
if len(os.Args) > 0 {
96-
reportTaskFile = os.Args[1]
97+
// loop over all args
98+
for i, arg := range os.Args {
99+
if i == 0 {
100+
continue
101+
}
102+
if strings.HasPrefix(arg, "--reportTaskFile=") {
103+
reportTaskFile = strings.TrimPrefix(arg, "--reportTaskFile=")
104+
} else if strings.HasPrefix(arg, "--FailOnAnalysisFailure") {
105+
failOnAnalysisFailure = true
106+
}
107+
}
108+
logger.Println("reportTaskFile:", reportTaskFile)
109+
logger.Println("FailOnAnalysisFailure:", failOnAnalysisFailure)
97110
}
98111
// fmt.Println("reportTaskFile: ", reportTaskFile)
99112
// Open the reportTaskFile
@@ -156,6 +169,10 @@ func main() {
156169
logger.Println("Error getting sonar analysis report: ", err)
157170
os.Exit(1)
158171
}
172+
if analysis.ProjectStatus.Status != "OK" && failOnAnalysisFailure {
173+
logger.Println("Sonar analysis failed, exiting according to failOnAnalysisFailure argument")
174+
os.Exit(1)
175+
}
159176

160177
response := SonarResponse{
161178
Task: taskResponse.Task,

0 commit comments

Comments
 (0)