/
server_api_helper.go
67 lines (53 loc) 路 1.83 KB
/
server_api_helper.go
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
package bitbucket
import (
"fmt"
insights "github.com/reva2/bitbucket-insights-api"
"github.com/reviewdog/reviewdog"
)
// ServerAPIHelper is collection of utility functions used to build requests
// for Bitbucket Server Code Insights API
type ServerAPIHelper struct{}
// BuildReport builds Code Insights API report object
func (h *ServerAPIHelper) BuildReport(req *ReportRequest) insights.Report {
data := insights.NewReport(req.Title)
data.SetReporter(req.Reporter)
data.SetLogoUrl(req.LogoURL)
data.SetResult(h.convertResult(req.Result))
data.SetDetails(req.Details)
return *data
}
// BuildAnnotations builds list of Code Insights API annotation objects for specified comments
func (h *ServerAPIHelper) BuildAnnotations(comments []*reviewdog.Comment) insights.AnnotationsList {
annotations := make([]insights.Annotation, len(comments))
for idx, comment := range comments {
annotations[idx] = h.buildAnnotation(comment)
}
list := insights.NewAnnotationsList(annotations)
return *list
}
func (h *ServerAPIHelper) buildAnnotation(comment *reviewdog.Comment) insights.Annotation {
severity := convertSeverity(comment.Result.Diagnostic.GetSeverity())
if severity == "" {
severity = annotationSeverityLow
}
data := insights.NewAnnotation(
comment.Result.Diagnostic.GetLocation().GetPath(),
comment.Result.Diagnostic.GetLocation().GetRange().GetStart().GetLine()-1,
fmt.Sprintf(`[%s] %s`, comment.ToolName, comment.Result.Diagnostic.GetMessage()),
severity,
)
data.SetExternalId(externalIDFromDiagnostic(comment.Result.Diagnostic))
data.SetType(annotationTypeCodeSmell)
if link := comment.Result.Diagnostic.GetCode().GetUrl(); link != "" {
data.SetLink(link)
}
return *data
}
func (h *ServerAPIHelper) convertResult(result string) string {
switch result {
case reportResultFailed:
return "FAIL"
default:
return "PASS"
}
}