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

Fix suggestion includes fenced code blocks #1017

Merged
merged 8 commits into from
Aug 7, 2021

Conversation

shogo82148
Copy link
Contributor

@shogo82148 shogo82148 commented Aug 5, 2021

When reviewdog suggests the following code

original code

modified as

reviewdog suggestion
```
some code
```

the suggestion comment should be

-original code
+reviewdog suggestion
+```
+some code
+```

but actually, reviewdog suggests the following comment:

-original code
+reviewdog suggestion

some code

This is because the markdown parser creates wrong pairs of code fences.

```suggestion 
reviewdog suggestion
```
some code
```
```

The document of GitHub markdown says that it is needed to wrap them inside quadruple backticks in this case.
https://docs.github.com/en/github/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks#fenced-code-blocks

To display triple backticks in a fenced code block, wrap them inside quadruple backticks.

````suggestion 
reviewdog suggestion
```
some code
```
````

This pull request checks whether the suggestion includes code fences, and use quadruple backticks code fences if it is necessary.

Fixes #999

  • Updated Unreleased section in CHANGELOG or it's not notable changes.

@shogo82148 shogo82148 requested a review from a team August 5, 2021 14:17
service/github/github.go Outdated Show resolved Hide resolved
Comment on lines 326 to 332
backticks := countBackticks(txt) + 1
if backticks < 3 {
// At least three backticks are required.
// https://github.github.com/gfm/#fenced-code-blocks
// > A code fence is a sequence of at least three consecutive backtick characters (`) or tildes (~). (Tildes and backticks cannot be mixed.)
backticks = 3
}
Copy link
Member

Choose a reason for hiding this comment

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

Can you create a helper function to get # of backticks for suggestions considering there are duplicated code?
You can move the comment to that function as well.

sb.WriteString("```suggestion\n")
if txt := s.GetText(); txt != "" {
sb.Grow(backticks + len("suggestion\n") + len(txt) + len("\n") + backticks)
for i := 0; i < backticks; i++ {
Copy link
Member

Choose a reason for hiding this comment

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

How about creating a short helper function to write n backtics?

Co-authored-by: haya14busa <haya14busa@gmail.com>
service/github/github.go Outdated Show resolved Hide resolved
@shogo82148
Copy link
Contributor Author

Thank you for your review.

Can you create a helper function to get # of backticks for suggestions considering there are duplicated code?

I created it in the commentutil package.
Because we might need same fix with Gitlab suggestions in merge request comments(#1012).
I think using quadruple backticks(````) is very very rarely, but linters can.
I'm going to do it in another pull request.

Copy link
Member

@haya14busa haya14busa left a comment

Choose a reason for hiding this comment

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

Thank you for your work! LGTM 👍

service/github/github.go Outdated Show resolved Hide resolved
sb.WriteString("```suggestion\n")
if txt := s.GetText(); txt != "" {
sb.Grow(backticks + len("suggestion\n") + len(txt) + len("\n") + backticks)
commentutil.WriteCodeFence(&sb, backticks)
Copy link
Member

Choose a reason for hiding this comment

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

Can you check the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this warning is false positive.
We don't need to check return values because the methods of strings.Builder always return nil error.

https://pkg.go.dev/strings#Builder.Write

Write appends the contents of p to b's buffer. Write always returns len(p), nil.

https://pkg.go.dev/strings#Builder.WriteByte

WriteByte appends the byte c to b's buffer. The returned error is always nil.

The return values exist only to satisfy well-known interfaces such as io.Writer, io.ByteWriter, etc.

Should I suppress the warning?

Suggested change
commentutil.WriteCodeFence(&sb, backticks)
_ = commentutil.WriteCodeFence(&sb, backticks)

Copy link
Member

Choose a reason for hiding this comment

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

ok, sounds good. You can leave it as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks!

sb.WriteString(txt)
sb.WriteString("\n")
}
sb.WriteString("```")
commentutil.WriteCodeFence(&sb, backticks)
Copy link
Member

Choose a reason for hiding this comment

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

ditto

sb.WriteString(endLineContent[max(end.GetColumn()-1, 0):])
sb.WriteString("\n```")
sb.Grow(backticks + len("suggestion\n") + len(txt) + len("\n") + backticks)
commentutil.WriteCodeFence(&sb, backticks)
Copy link
Member

Choose a reason for hiding this comment

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

ditto

sb.WriteString("```suggestion\n")
if txt := s.GetText(); txt != "" {
sb.Grow(backticks + len("suggestion\n") + len(txt) + len("\n") + backticks)
commentutil.WriteCodeFence(&sb, backticks)
Copy link
Member

Choose a reason for hiding this comment

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

ok, sounds good. You can leave it as is.

@shogo82148 shogo82148 merged commit d3768e5 into master Aug 7, 2021
@shogo82148 shogo82148 deleted the fix-suggestion-includes-fenced-code-blocks branch August 7, 2021 11:15
@shogo82148 shogo82148 mentioned this pull request Aug 7, 2021
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reviewdog gets confused when proposing changes of code that include markdown
2 participants