Skip to content

Commit 8b94576

Browse files
authored
Merge pull request #4 from vtex/add-comment
Add Sonarqube status in the summary
2 parents 37b0456 + 776bec1 commit 8b94576

File tree

4 files changed

+129
-18
lines changed

4 files changed

+129
-18
lines changed

dist/index.js

Lines changed: 43 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { context, getOctokit } from '@actions/github'
33
import type { GitHub } from '@actions/github/lib/utils'
44
import * as exec from '@actions/exec'
55

6+
import type { ProjectStatus } from './sonarqube'
67
import Sonarqube from './sonarqube'
78
import type { Annotation } from './utils'
89
import { issuesToAnnotations } from './utils'
@@ -13,12 +14,12 @@ const MAX_ANNOTATIONS_PER_REQUEST = 50
1314
const createCheckRun = async ({
1415
octokit,
1516
repo,
16-
SQDetailsURL,
17+
summary,
1718
annotations,
1819
}: {
1920
octokit: InstanceType<typeof GitHub>
2021
repo: { owner: string; repo: string }
21-
SQDetailsURL: string
22+
summary: string
2223
annotations?: Annotation[]
2324
}) => {
2425
info('Creating check')
@@ -34,10 +35,9 @@ const createCheckRun = async ({
3435
head_sha: ref,
3536
status: 'completed',
3637
conclusion: 'neutral',
37-
details_url: SQDetailsURL,
3838
output: {
3939
title: 'SonarQube',
40-
summary: `See more details in [SonarQube](${SQDetailsURL})`,
40+
summary,
4141
annotations,
4242
},
4343
})
@@ -48,21 +48,60 @@ const createCheckRun = async ({
4848
}
4949
}
5050

51+
const generateSummary = (status: ProjectStatus, url: string) => {
52+
const conditions = status.conditions.reduce<string>((acc, current) => {
53+
switch (current.metricKey) {
54+
case 'reliability_rating':
55+
return `${acc}Reability ${
56+
current.status === 'ERROR' ? ':x:' : ':white_check_mark:'
57+
} \n`
58+
59+
case 'security_rating':
60+
return `${acc}Security ${
61+
current.status === 'ERROR' ? ':x:' : ':white_check_mark:'
62+
} \n`
63+
64+
case 'sqale_rating':
65+
return `${acc}Security review ${
66+
current.status === 'ERROR' ? ':x:' : ':white_check_mark:'
67+
} \n`
68+
69+
case 'security_hotspots_reviewed':
70+
return `${acc}Security hotspots ${
71+
current.status === 'ERROR' ? ':x:' : ':white_check_mark:'
72+
} \n`
73+
74+
default:
75+
return ''
76+
}
77+
}, '')
78+
79+
return `
80+
### Quality Gate ${
81+
status.status === 'ERROR' ? 'failed :x:' : 'passed :white_check_mark:'
82+
}.
83+
See more details in [SonarQube](${url}).
84+
85+
### Conditions
86+
${conditions}
87+
88+
`
89+
}
90+
5191
const updateCheckRun = async ({
5292
octokit,
5393
repo,
5494
checkRunId,
5595
annotations,
56-
SQDetailsURL,
96+
summary,
5797
}: {
5898
octokit: InstanceType<typeof GitHub>
5999
repo: { owner: string; repo: string }
60100
checkRunId: number
61101
annotations: Annotation[]
62-
SQDetailsURL: string
102+
summary: string
63103
}) => {
64104
info('Updating check with annotations')
65-
66105
try {
67106
await octokit.checks.update({
68107
...repo,
@@ -71,7 +110,7 @@ const updateCheckRun = async ({
71110
conclusion: 'neutral',
72111
output: {
73112
title: 'SonarQube',
74-
summary: `See more details in [SonarQube](${SQDetailsURL})`,
113+
summary,
75114
annotations,
76115
},
77116
})
@@ -99,7 +138,12 @@ async function run() {
99138

100139
const SQDetailsURL = `${sonarqube.host}/dashboard?id=${sonarqube.project.projectKey}`
101140

102-
const checkRunId = await createCheckRun({ octokit, repo, SQDetailsURL })
141+
const status = await sonarqube.getStatus()
142+
const summary = status
143+
? generateSummary(status, SQDetailsURL)
144+
: `See more details in [SonarQube](${SQDetailsURL})`
145+
146+
const checkRunId = await createCheckRun({ octokit, repo, summary })
103147

104148
issues.map(async (batch) => {
105149
const annotations = issuesToAnnotations(batch)
@@ -109,7 +153,7 @@ async function run() {
109153
repo,
110154
checkRunId,
111155
annotations,
112-
SQDetailsURL,
156+
summary,
113157
})
114158
})
115159
}

src/sonarqube.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ interface IssuesResponseAPI {
4242
debtTotal: number
4343
issues: [Issue]
4444
}
45+
46+
export interface ProjectStatus {
47+
status: 'OK' | 'ERROR'
48+
conditions: Array<{
49+
status: 'OK' | 'ERROR'
50+
metricKey: string
51+
comparator: string
52+
errorThreshold: number
53+
actualValue: number
54+
}>
55+
}
56+
57+
export interface ProjectStatusResponseAPI {
58+
projectStatus: ProjectStatus
59+
}
4560
export default class Sonarqube {
4661
private http: AxiosInstance
4762
public host: string
@@ -120,6 +135,22 @@ export default class Sonarqube {
120135
: ''
121136
}`
122137

138+
public getStatus = async (): Promise<ProjectStatus | null> => {
139+
const response = await this.http.get<ProjectStatusResponseAPI>(
140+
`/api/qualitygates/project_status?projectKey=${this.project.projectKey}`
141+
)
142+
143+
if (response.status !== 200 || !response.data) {
144+
return null
145+
}
146+
147+
const {
148+
data: { projectStatus },
149+
} = response
150+
151+
return projectStatus
152+
}
153+
123154
private getInfo = (repo: { owner: string; repo: string }) => ({
124155
project: {
125156
projectKey: getInput('projectKey')

0 commit comments

Comments
 (0)