github.com/reviewdog/reviewdog@v0.17.5-0.20240516205324-0cd103a83d58/service/bitbucket/cloud_api_helper.go (about)

     1  package bitbucket
     2  
     3  import (
     4  	"fmt"
     5  
     6  	bbapi "github.com/reviewdog/go-bitbucket"
     7  	"github.com/reviewdog/reviewdog"
     8  )
     9  
    10  // CloudAPIHelper is collection of utility functions used to build requests
    11  // for Bitbucket Cloud Code Insights API
    12  type CloudAPIHelper struct{}
    13  
    14  // BuildReport builds Code Insights API report object
    15  func (c *CloudAPIHelper) BuildReport(req *ReportRequest) bbapi.Report {
    16  	data := bbapi.NewReport()
    17  	data.SetTitle(req.Title)
    18  	data.SetReportType(req.Type)
    19  	data.SetReporter(req.Reporter)
    20  	data.SetLogoUrl(req.LogoURL)
    21  	data.SetResult(req.Result)
    22  	data.SetDetails(req.Details)
    23  
    24  	return *data
    25  }
    26  
    27  // BuildAnnotations builds list of Code Insights API annotation objects for specified comments
    28  func (c *CloudAPIHelper) BuildAnnotations(comments []*reviewdog.Comment) []bbapi.ReportAnnotation {
    29  	annotations := make([]bbapi.ReportAnnotation, len(comments))
    30  	for idx, comment := range comments {
    31  		annotations[idx] = c.buildAnnotation(comment)
    32  	}
    33  
    34  	return annotations
    35  }
    36  
    37  func (c *CloudAPIHelper) buildAnnotation(comment *reviewdog.Comment) bbapi.ReportAnnotation {
    38  	data := bbapi.NewReportAnnotation()
    39  	data.SetExternalId(externalIDFromDiagnostic(comment.Result.Diagnostic))
    40  	data.SetAnnotationType(annotationTypeCodeSmell)
    41  	data.SetSummary(comment.Result.Diagnostic.GetMessage())
    42  	data.SetDetails(fmt.Sprintf(`[%s] %s`, comment.ToolName, comment.Result.Diagnostic.GetMessage()))
    43  	data.SetLine(comment.Result.Diagnostic.GetLocation().GetRange().GetStart().GetLine())
    44  	data.SetPath(comment.Result.Diagnostic.GetLocation().GetPath())
    45  
    46  	if severity := convertSeverity(comment.Result.Diagnostic.GetSeverity()); severity != "" {
    47  		data.SetSeverity(severity)
    48  	}
    49  
    50  	if link := comment.Result.Diagnostic.GetCode().GetUrl(); link != "" {
    51  		data.SetLink(link)
    52  	}
    53  
    54  	return *data
    55  }