github.com/mistwind/reviewdog@v0.0.0-20230322024206-9cfa11856d58/service/github/githubutils/utils.go (about) 1 package githubutils 2 3 import ( 4 "fmt" 5 6 "github.com/mistwind/reviewdog/proto/rdf" 7 ) 8 9 // LinkedMarkdownDiagnostic returns Markdown string which contains a link to the 10 // location in the diagnostic and the diagnostic content itself. 11 func LinkedMarkdownDiagnostic(owner, repo, sha string, d *rdf.Diagnostic) string { 12 path := d.GetLocation().GetPath() 13 msg := d.GetMessage() 14 if path == "" { 15 return msg 16 } 17 loc := BasicLocationFormat(d) 18 line := int(d.GetLocation().GetRange().GetStart().GetLine()) 19 link := PathLink(owner, repo, sha, path, line) 20 return fmt.Sprintf("[%s](%s) %s", loc, link, msg) 21 } 22 23 // PathLink build a link to GitHub path to given sha, file, and line. 24 func PathLink(owner, repo, sha, path string, line int) string { 25 if sha == "" { 26 sha = "master" 27 } 28 fragment := "" 29 if line > 0 { 30 fragment = fmt.Sprintf("#L%d", line) 31 } 32 return fmt.Sprintf("http://github.com/%s/%s/blob/%s/%s%s", 33 owner, repo, sha, path, fragment) 34 } 35 36 // BasicLocationFormat format a diagnostic to %f|%l col %c| errorformat. 37 func BasicLocationFormat(d *rdf.Diagnostic) string { 38 loc := d.GetLocation() 39 out := loc.GetPath() + "|" 40 lnum := int(loc.GetRange().GetStart().GetLine()) 41 col := int(loc.GetRange().GetStart().GetColumn()) 42 if lnum != 0 { 43 out = fmt.Sprintf("%s%d", out, lnum) 44 if col != 0 { 45 out = fmt.Sprintf("%s col %d", out, col) 46 } 47 } 48 return out + "|" 49 }