github.com/vipcoin-gold/reviewdog@v1.0.2/service/bitbucket/cloud_api_helper.go (about)

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