forked from undecidedzogvisvitalispotent8stars360/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (159 loc) · 4.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Component } from 'react'
import styled from 'styled-components'
import fetch from 'isomorphic-unfetch'
import { object, string, number } from 'yup'
import Widget from '../../widget'
import Table, { Th, Td } from '../../table'
import Badge from '../../badge'
import { basicAuthHeader } from '../../../lib/auth'
const alertColor = ({ theme, children }) => {
switch (children) {
case 'ERROR':
return theme.palette.errorColor
case 'WARN':
return theme.palette.warnColor
default: // OK
return theme.palette.successColor
}
}
const Alert = styled.span`
color: ${alertColor};
`
const sonarBadgeColor = ({ theme, children }) => {
switch (children) {
case 'A':
return theme.palette.successColor
case 'B':
return theme.palette.successSecondaryColor
case 'C':
return theme.palette.warnColor
case 'D':
return theme.palette.warnSecondaryColor
case 'E':
return theme.palette.errorColor
default:
return 'transparent'
}
}
const SonarBadge = styled(Badge)`
background-color: ${sonarBadgeColor};
`
const schema = object().shape({
url: string().url().required(),
componentKey: string().required(),
interval: number(),
title: string(),
authKey: string()
})
export default class SonarQube extends Component {
static defaultProps = {
interval: 1000 * 60 * 5,
title: 'SonarQube'
}
state = {
measures: [],
loading: true,
error: false
}
componentDidMount () {
schema.validate(this.props)
.then(() => this.fetchInformation())
.catch((err) => {
console.error(`${err.name} @ ${this.constructor.name}`, err.errors)
this.setState({ error: true, loading: false })
})
}
componentWillUnmount () {
clearTimeout(this.timeout)
}
async fetchInformation () {
const { authKey, url, componentKey } = this.props
const opts = authKey ? { headers: basicAuthHeader(authKey) } : {}
// https://docs.sonarqube.org/display/SONAR/Metric+Definitions
const metricKeys = [
'alert_status', 'reliability_rating', 'bugs', 'security_rating',
'vulnerabilities', 'sqale_rating', 'code_smells', 'coverage',
'duplicated_lines_density'
].join(',')
try {
const res = await fetch(`${url}/api/measures/component?componentKey=${componentKey}&metricKeys=${metricKeys}`, opts)
const json = await res.json()
this.setState({ error: false, loading: false, measures: json.component.measures })
} catch (error) {
this.setState({ error: true, loading: false })
} finally {
this.timeout = setTimeout(() => this.fetchInformation(), this.props.interval)
}
}
getMetricValue = (measures, metricKey) => {
const result = measures.filter(measure => measure.metric === metricKey)
return result.length ? result[0].value : ''
}
getRatingValue = (measures, metricKey) => {
const value = this.getMetricValue(measures, metricKey)
switch (value) {
case '1.0':
return 'A'
case '2.0':
return 'B'
case '3.0':
return 'C'
case '4.0':
return 'D'
case '5.0':
return 'E'
}
return '?'
}
render () {
const { error, loading, measures } = this.state
const { title } = this.props
const alertStatus = this.getMetricValue(measures, 'alert_status')
const reliabilityRating = this.getRatingValue(measures, 'reliability_rating')
const bugs = this.getMetricValue(measures, 'bugs')
const securityRating = this.getRatingValue(measures, 'security_rating')
const vulnerabilities = this.getMetricValue(measures, 'vulnerabilities')
const sqaleRating = this.getRatingValue(measures, 'sqale_rating')
const codeSmells = this.getMetricValue(measures, 'code_smells')
const coverage = this.getMetricValue(measures, 'coverage')
const duplicatedLinesDensity = this.getMetricValue(measures, 'duplicated_lines_density')
return (
<Widget title={title} loading={loading} error={error}>
<Table>
<tbody>
<tr>
<Th>Quality Gate:</Th>
<Td><Alert>{alertStatus}</Alert></Td>
</tr>
<tr>
<Th>Reliability:</Th>
<Td>
<SonarBadge>{reliabilityRating}</SonarBadge> <small>({bugs})</small>
</Td>
</tr>
<tr>
<Th>Security:</Th>
<Td>
<SonarBadge>{securityRating}</SonarBadge> <small>({vulnerabilities})</small>
</Td>
</tr>
<tr>
<Th>Maintainability:</Th>
<Td>
<SonarBadge>{sqaleRating}</SonarBadge> <small>({codeSmells})</small>
</Td>
</tr>
<tr>
<Th>Coverage:</Th>
<Td>{coverage}%</Td>
</tr>
<tr>
<Th>Duplications:</Th>
<Td>{duplicatedLinesDensity}%</Td>
</tr>
</tbody>
</Table>
</Widget>
)
}
}