Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce GitHub Alerts Style comment #1668

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 57 additions & 0 deletions service/commentutil/commentutil.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commentutil

import (
"bufio"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -96,3 +97,59 @@
return ""
}
}

// GitHubAlertComment creates a markdown comment using GitHub Alerts syntax.

Check warning on line 101 in service/commentutil/commentutil.go

View workflow job for this annotation

GitHub Actions / golint-github-check

[golint-github-check] service/commentutil/commentutil.go#L101

comment on exported function GitHubAlertComemnt should be of the form "GitHubAlertComemnt ..."
Raw output
service/commentutil/commentutil.go:101:1: comment on exported function GitHubAlertComemnt should be of the form "GitHubAlertComemnt ..."

Check warning on line 101 in service/commentutil/commentutil.go

View workflow job for this annotation

GitHub Actions / golint

[golint] service/commentutil/commentutil.go#L101

comment on exported function GitHubAlertComemnt should be of the form "GitHubAlertComemnt ..."
Raw output
/home/runner/work/reviewdog/reviewdog/service/commentutil/commentutil.go:101:1: comment on exported function GitHubAlertComemnt should be of the form "GitHubAlertComemnt ..."
haya14busa marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

[golint-pr-review] comment on exported function GitHubAlertComemnt should be of the form "GitHubAlertComemnt ..."

reported by reviewdog 🐶

// https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts
func GitHubAlertComemnt(c *reviewdog.Comment) string {
var sb strings.Builder
alert := githubAlert(c)
if alert != "" {
sb.WriteString(alert)
sb.WriteString("\n")
}
if tool := toolName(c); tool != "" {
sb.WriteString(fmt.Sprintf("**[%s]** ", tool))
}
if code := c.Result.Diagnostic.GetCode().GetValue(); code != "" {
if url := c.Result.Diagnostic.GetCode().GetUrl(); url != "" {
sb.WriteString(fmt.Sprintf("<[%s](%s)> ", code, url))
} else {
sb.WriteString(fmt.Sprintf("<%s> ", code))
}
}
sb.WriteString(c.Result.Diagnostic.GetMessage())
sb.WriteString("<p align='right'>")
sb.WriteString(BodyPrefix)
sb.WriteString("</p>")
if alert == "" {
return sb.String()
}
return toMarkdownQuote(sb.String())
}

func toMarkdownQuote(str string) string {
var sb strings.Builder
scanner := bufio.NewScanner(strings.NewReader(str))
for scanner.Scan() {
sb.WriteString("> ")
sb.Write(scanner.Bytes())
sb.WriteRune('\n')
}
return sb.String()
}

func githubAlert(c *reviewdog.Comment) string {
// TODO: Maybe we should support TIP and IMPORTANT severity.
switch c.Result.Diagnostic.GetSeverity() {
case rdf.Severity_ERROR:
return "[!CAUTION]"
case rdf.Severity_WARNING:
return "[!WARNING]"
case rdf.Severity_INFO:
return "[!NOTE]"
default:
// I'm not 100% sure what's the best default alert notation, but let's use
// warning as default.
return "[!WARNING]"
}
}
2 changes: 1 addition & 1 deletion service/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func listAllPullRequestsComments(ctx context.Context, cli *github.Client,
}

func buildBody(c *reviewdog.Comment) string {
cbody := commentutil.MarkdownComment(c)
cbody := commentutil.GitHubAlertComemnt(c)
if suggestion := buildSuggestions(c); suggestion != "" {
cbody += "\n" + suggestion
}
Expand Down