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