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 all commits
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
5 changes: 5 additions & 0 deletions _testdata/custom.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@
function f()
{
}

# Severity test
test INFO

Check notice on line 20 in _testdata/custom.txt

View workflow job for this annotation

GitHub Actions / custom-rdjson

[custom-rdjson] _testdata/custom.txt#L20

severity test 1 (INFO)
Raw output
message:"severity test 1 (INFO)" location:{path:"_testdata/custom.txt" range:{start:{line:20 column:5}}} severity:INFO

Check failure on line 20 in _testdata/custom.txt

View workflow job for this annotation

GitHub Actions / custom-rdjson

[custom-rdjson] _testdata/custom.txt#L20

severity test 2 (INFO by number)
Raw output
message:"severity test 2 (INFO by number)" location:{path:"_testdata/custom.txt" range:{start:{line:20 column:4}}} severity:ERROR

Check warning on line 20 in _testdata/custom.txt

View workflow job for this annotation

GitHub Actions / custom-rdjson

[custom-rdjson] _testdata/custom.txt#L20

severity test 3 (WARNING)
Raw output
message:"severity test 3 (WARNING)" location:{path:"_testdata/custom.txt" range:{start:{line:20 column:5}}} severity:WARNING

Check failure on line 20 in _testdata/custom.txt

View workflow job for this annotation

GitHub Actions / custom-rdjson

[custom-rdjson] _testdata/custom.txt#L20

severity test 4 (ERROR)
Raw output
message:"severity test 4 (ERROR)" location:{path:"_testdata/custom.txt" range:{start:{line:20 column:5}}} severity:ERROR
Copy link
Contributor

Choose a reason for hiding this comment

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

Warning

[custom-rdjson]
severity test INFO

reported by reviewdog 🐶

Copy link
Contributor

Choose a reason for hiding this comment

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

Note

[custom-rdjson]
severity test 1 (INFO)

reported by reviewdog 🐶

Copy link
Contributor

Choose a reason for hiding this comment

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

Warning

[custom-rdjson]
severity test 2 (INFO by number)

reported by reviewdog 🐶

Copy link
Contributor

Choose a reason for hiding this comment

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

Warning

[custom-rdjson]
severity test 3 (WARNING)

reported by reviewdog 🐶

Copy link
Contributor

Choose a reason for hiding this comment

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

Caution

[custom-rdjson]
severity test 4 (ERROR)

reported by reviewdog 🐶

Copy link
Contributor

Choose a reason for hiding this comment

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

Note

[custom-rdjson]
severity test 1 (INFO)

reported by [reviewdog](https://github.com/reviewdog/reviewdog) 🐶

Copy link
Contributor

Choose a reason for hiding this comment

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

Warning

[custom-rdjson]
severity test 2 (INFO by number)

reported by [reviewdog](https://github.com/reviewdog/reviewdog) 🐶

Copy link
Contributor

Choose a reason for hiding this comment

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

Warning

[custom-rdjson]
severity test 3 (WARNING)

reported by [reviewdog](https://github.com/reviewdog/reviewdog) 🐶

Copy link
Contributor

Choose a reason for hiding this comment

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

Caution

[custom-rdjson]
severity test 4 (ERROR)

reported by [reviewdog](https://github.com/reviewdog/reviewdog) 🐶

Copy link
Contributor

Choose a reason for hiding this comment

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

Note

📝 [custom-rdjson] reported by reviewdog 🐶
severity test 1 (INFO)

Copy link
Contributor

Choose a reason for hiding this comment

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

Warning

⚠️ [custom-rdjson] reported by reviewdog 🐶
severity test 2 (INFO by number)

Copy link
Contributor

Choose a reason for hiding this comment

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

Warning

⚠️ [custom-rdjson] reported by reviewdog 🐶
severity test 3 (WARNING)

Copy link
Contributor

Choose a reason for hiding this comment

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

Caution

🚫 [custom-rdjson] reported by reviewdog 🐶
severity test 4 (ERROR)

Copy link
Contributor

Choose a reason for hiding this comment

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

Caution

🚫 [custom-rdjson] reported by reviewdog 🐶
severity test 2 (INFO by number)

test WARNING
Copy link
Contributor

Choose a reason for hiding this comment

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

Warning

[custom-rdjson]
severity test WARNING

reported by reviewdog 🐶

test ERROR
Copy link
Contributor

Choose a reason for hiding this comment

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

Warning

[custom-rdjson]
severity test ERROR

reported by reviewdog 🐶

12 changes: 6 additions & 6 deletions _testdata/custom_rdjson.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,33 +82,33 @@
"path": "_testdata/custom.txt",
"range": {
"start": {
"line": 3,
"line": 20,
"column": 5
}
}
},
"severity": "INFO"
},
{
"message": "severity test 2 (INFO)",
"message": "severity test 2 (INFO by number)",
"location": {
"path": "_testdata/custom.txt",
"range": {
"start": {
"line": 3,
"line": 20,
"column": 4
}
}
},
"severity": 2
"severity": 1
},
{
"message": "severity test 3 (WARNING)",
"location": {
"path": "_testdata/custom.txt",
"range": {
"start": {
"line": 3,
"line": 20,
"column": 5
}
}
Expand All @@ -121,7 +121,7 @@
"path": "_testdata/custom.txt",
"range": {
"start": {
"line": 3,
"line": 20,
"column": 5
}
}
Expand Down
34 changes: 34 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,36 @@ func severity(c *reviewdog.Comment) string {
return ""
}
}

// GitHubAlertComment creates a markdown comment using GitHub Alerts syntax.
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 GitHubAlertComment(c *reviewdog.Comment) string {
return "> " + githubAlert(c) + "\n" + toMarkdownQuote(MarkdownComment(c))
}

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.GitHubAlertComment(c)
if suggestion := buildSuggestions(c); suggestion != "" {
cbody += "\n" + suggestion
}
Expand Down