gitea-sonarqube-bot/internal/clients/sonarqube/measures.go
justusbunsi 02ad0c0bf0
Improve error handling of SonarQube client
Due to unhandled errors within the SonarQube client, users may be
presented with Go panics or just don't know what the root cause of a
non-working bot is.

Now it is possible to identify network errors, authentication issues or
an incorrect bot configuration regarding SonarQube.

Fixes: #20

Signed-off-by: Steven Kriegler <sk.bunsenbrenner@gmail.com>
2022-06-17 20:19:59 +02:00

55 lines
1.3 KiB
Go

package sonarqube
import (
"fmt"
"strings"
)
type period struct {
Value string `json:"value"`
}
type MeasuresComponentMeasure struct {
Metric string `json:"metric"`
Value string `json:"value"`
Period *period `json:"period,omitempty"`
}
type MeasuresComponentMetric struct {
Key string `json:"key"`
Name string `json:"name"`
}
type MeasuresComponent struct {
PullRequest string `json:"pullRequest"`
Measures []MeasuresComponentMeasure `json:"measures"`
}
type MeasuresResponse struct {
Component MeasuresComponent `json:"component"`
Metrics []MeasuresComponentMetric `json:"metrics"`
Errors []Error `json:"errors"`
}
func (mr *MeasuresResponse) GetRenderedMarkdownTable() string {
metricsTranslations := map[string]string{}
for _, metric := range mr.Metrics {
metricsTranslations[metric.Key] = metric.Name
}
measures := make([]string, len(mr.Component.Measures))
for i, measure := range mr.Component.Measures {
value := measure.Value
if measure.Period != nil {
value = measure.Period.Value
}
measures[i] = fmt.Sprintf("| %s | %s |", metricsTranslations[measure.Metric], value)
}
table := `
| Metric | Current |
| -------- | -------- |
%s`
return fmt.Sprintf(table, strings.Join(measures, "\n"))
}