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

     1  package commentutil
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/vipcoin-gold/reviewdog"
     9  	"github.com/vipcoin-gold/reviewdog/proto/rdf"
    10  )
    11  
    12  // `path` to `position`(Lnum for new file) to comment `body`s
    13  type PostedComments map[string]map[int][]string
    14  
    15  // IsPosted returns true if a given comment has been posted in code review service already,
    16  // otherwise returns false. It sees comments with same path, same position,
    17  // and same body as same comments.
    18  func (p PostedComments) IsPosted(c *reviewdog.Comment, lineNum int, body string) bool {
    19  	path := c.Result.Diagnostic.GetLocation().GetPath()
    20  	if _, ok := p[path]; !ok {
    21  		return false
    22  	}
    23  	bodies, ok := p[path][lineNum]
    24  	if !ok {
    25  		return false
    26  	}
    27  	for _, b := range bodies {
    28  		if b == body {
    29  			return true
    30  		}
    31  	}
    32  	return false
    33  }
    34  
    35  // AddPostedComment adds a posted comment.
    36  func (p PostedComments) AddPostedComment(path string, lineNum int, body string) {
    37  	if _, ok := p[path]; !ok {
    38  		p[path] = make(map[int][]string)
    39  	}
    40  	if _, ok := p[path][lineNum]; !ok {
    41  		p[path][lineNum] = make([]string, 0)
    42  	}
    43  	p[path][lineNum] = append(p[path][lineNum], body)
    44  }
    45  
    46  // DebugLog outputs posted comments as log for debugging.
    47  func (p PostedComments) DebugLog() {
    48  	for filename, f := range p {
    49  		for line := range f {
    50  			log.Printf("[debug] posted: %s:%d", filename, line)
    51  		}
    52  	}
    53  }
    54  
    55  // BodyPrefix is prefix text of comment body.
    56  const BodyPrefix = `<sub>reported by [reviewdog](https://github.com/reviewdog/reviewdog) :dog:</sub><br>`
    57  
    58  // MarkdownComment creates comment body markdown.
    59  func MarkdownComment(c *reviewdog.Comment) string {
    60  	var sb strings.Builder
    61  	if s := severity(c); s != "" {
    62  		sb.WriteString(s)
    63  		sb.WriteString(" ")
    64  	}
    65  	if tool := toolName(c); tool != "" {
    66  		sb.WriteString(fmt.Sprintf("**[%s]** ", tool))
    67  	}
    68  	if code := c.Result.Diagnostic.GetCode().GetValue(); code != "" {
    69  		if url := c.Result.Diagnostic.GetCode().GetUrl(); url != "" {
    70  			sb.WriteString(fmt.Sprintf("<[%s](%s)> ", code, url))
    71  		} else {
    72  			sb.WriteString(fmt.Sprintf("<%s> ", code))
    73  		}
    74  	}
    75  	sb.WriteString(BodyPrefix)
    76  	sb.WriteString(c.Result.Diagnostic.GetMessage())
    77  	return sb.String()
    78  }
    79  
    80  func toolName(c *reviewdog.Comment) string {
    81  	if name := c.Result.Diagnostic.GetSource().GetName(); name != "" {
    82  		return name
    83  	}
    84  	return c.ToolName
    85  }
    86  
    87  func severity(c *reviewdog.Comment) string {
    88  	switch c.Result.Diagnostic.GetSeverity() {
    89  	case rdf.Severity_ERROR:
    90  		return "🚫"
    91  	case rdf.Severity_WARNING:
    92  		return "⚠️"
    93  	case rdf.Severity_INFO:
    94  		return "📝"
    95  	default:
    96  		return ""
    97  	}
    98  }