github.com/reviewdog/reviewdog@v0.17.5-0.20240516205324-0cd103a83d58/service/bitbucket/server_api_helper.go (about) 1 package bitbucket 2 3 import ( 4 "fmt" 5 insights "github.com/reva2/bitbucket-insights-api" 6 "github.com/reviewdog/reviewdog" 7 ) 8 9 // ServerAPIHelper is collection of utility functions used to build requests 10 // for Bitbucket Server Code Insights API 11 type ServerAPIHelper struct{} 12 13 // BuildReport builds Code Insights API report object 14 func (h *ServerAPIHelper) BuildReport(req *ReportRequest) insights.Report { 15 data := insights.NewReport(req.Title) 16 17 data.SetReporter(req.Reporter) 18 data.SetLogoUrl(req.LogoURL) 19 data.SetResult(h.convertResult(req.Result)) 20 data.SetDetails(req.Details) 21 22 return *data 23 } 24 25 // BuildAnnotations builds list of Code Insights API annotation objects for specified comments 26 func (h *ServerAPIHelper) BuildAnnotations(comments []*reviewdog.Comment) insights.AnnotationsList { 27 annotations := make([]insights.Annotation, len(comments)) 28 for idx, comment := range comments { 29 annotations[idx] = h.buildAnnotation(comment) 30 } 31 32 list := insights.NewAnnotationsList(annotations) 33 34 return *list 35 } 36 37 func (h *ServerAPIHelper) buildAnnotation(comment *reviewdog.Comment) insights.Annotation { 38 severity := convertSeverity(comment.Result.Diagnostic.GetSeverity()) 39 if severity == "" { 40 severity = annotationSeverityLow 41 } 42 43 data := insights.NewAnnotation( 44 comment.Result.Diagnostic.GetLocation().GetPath(), 45 comment.Result.Diagnostic.GetLocation().GetRange().GetStart().GetLine(), 46 fmt.Sprintf(`[%s] %s`, comment.ToolName, comment.Result.Diagnostic.GetMessage()), 47 severity, 48 ) 49 data.SetExternalId(externalIDFromDiagnostic(comment.Result.Diagnostic)) 50 data.SetType(annotationTypeCodeSmell) 51 52 if link := comment.Result.Diagnostic.GetCode().GetUrl(); link != "" { 53 data.SetLink(link) 54 } 55 56 return *data 57 } 58 59 func (h *ServerAPIHelper) convertResult(result string) string { 60 switch result { 61 case reportResultFailed: 62 return "FAIL" 63 64 default: 65 return "PASS" 66 } 67 }