pr-resolution #3

Merged
mfocko merged 7 commits from pr-resolution into main 2021-09-11 22:36:41 +02:00
2 changed files with 65 additions and 47 deletions
Showing only changes of commit a25dabe20b - Show all commits

58
comments.go Normal file
View file

@ -0,0 +1,58 @@
package main
import (
"fmt"
"strings"
"unicode"
"code.gitea.io/sdk/gitea"
"github.com/bbrks/wrap/v2"
)
func GetSeparatorForMultiline() string {
if config.Language.OpeningOnSeparateLine {
return "\n"
}
return " "
}
func FormatComment(body string, beenWrapped bool) string {
if beenWrapped && !config.Language.OpeningOnSeparateLine {
body = " " + strings.TrimPrefix(body, " "+config.Language.Continuation)
}
if !beenWrapped && !config.Language.OpeningOnSeparateLine && !strings.HasSuffix(body, "\n") {
body = body + "\n"
}
return fmt.Sprintf("%s%s%s", config.Language.Opening, body, config.Language.Closing)
}
func ProcessComment(wrapper *wrap.Wrapper, comment *gitea.PullReviewComment) {
body := comment.Body
beenWrapped := len(body)+len(config.Language.Opening)+len(config.Language.Closing) > 100
if beenWrapped {
body = GetSeparatorForMultiline() + wrapper.Wrap(body, 100)
} else {
body = " " + body
}
body = strings.Replace(body, "\r", "", -1)
body = strings.Replace(
body,
config.Language.Continuation+"\n",
strings.TrimRightFunc(config.Language.Continuation, unicode.IsSpace)+"\n",
-1,
)
comment.Body = FormatComment(body, beenWrapped)
}
func ProcessComments(splitComments *map[string]([]*gitea.PullReviewComment)) {
wrapper := wrap.NewWrapper()
wrapper.OutputLinePrefix = config.Language.Continuation
for _, comments := range *splitComments {
for _, comment := range comments {
ProcessComment(&wrapper, comment)
}
}
}

54
main.go
View file

@ -7,11 +7,8 @@ import (
"os"
"sort"
"strconv"
"strings"
"unicode"
"code.gitea.io/sdk/gitea"
"github.com/bbrks/wrap/v2"
"gopkg.in/yaml.v2"
)
@ -82,49 +79,14 @@ func BackUpSource(filepath string) {
ExitOnError(err)
}
func GetSeparatorForMultiline() string {
if config.Language.OpeningOnSeparateLine {
return "\n"
}
return " "
}
func FormatComment(body string, beenWrapped bool) string {
if beenWrapped && !config.Language.OpeningOnSeparateLine {
body = " " + strings.TrimPrefix(body, " "+config.Language.Continuation)
}
if !beenWrapped && !config.Language.OpeningOnSeparateLine && !strings.HasSuffix(body, "\n") {
body = body + "\n"
}
return fmt.Sprintf("%s%s%s", config.Language.Opening, body, config.Language.Closing)
}
func WriteCommentsInFile(
wrapper *wrap.Wrapper,
outputFile *os.File,
lineNum uint64,
comments []*gitea.PullReviewComment,
nextComment, commentsLength int,
) int {
for (nextComment < commentsLength) && comments[nextComment].LineNum == lineNum {
body := comments[nextComment].Body
beenWrapped := len(body)+len(config.Language.Opening)+len(config.Language.Closing) > 100
if beenWrapped {
body = GetSeparatorForMultiline() + wrapper.Wrap(body, 100)
} else {
body = " " + body
}
body = strings.Replace(body, "\r", "", -1)
body = strings.Replace(
body,
config.Language.Continuation+"\n",
strings.TrimRightFunc(config.Language.Continuation, unicode.IsSpace)+"\n",
-1,
)
comment := FormatComment(body, beenWrapped)
comment := comments[nextComment].Body
fmt.Fprint(outputFile, comment)
fmt.Printf("L%04d:\n%s\n", comments[nextComment].LineNum, comment)
nextComment++
@ -132,7 +94,7 @@ func WriteCommentsInFile(
return nextComment
}
func ProcessFile(wrapper *wrap.Wrapper, filepath string, comments []*gitea.PullReviewComment) {
func ProcessFile(filepath string, comments []*gitea.PullReviewComment) {
fmt.Printf("FILE: %s\n", filepath)
BackUpSource(filepath)
@ -150,15 +112,15 @@ func ProcessFile(wrapper *wrap.Wrapper, filepath string, comments []*gitea.PullR
scanner := bufio.NewScanner(inputFile)
for scanner.Scan() {
nextComment = WriteCommentsInFile(wrapper, outputFile, i, comments, nextComment, commentsLength)
nextComment = WriteCommentsInFile(outputFile, i, comments, nextComment, commentsLength)
fmt.Fprintln(outputFile, scanner.Text())
i++
}
}
func PatchFiles(wrapper *wrap.Wrapper, commentsByFile map[string]([]*gitea.PullReviewComment)) {
func PatchFiles(commentsByFile map[string]([]*gitea.PullReviewComment)) {
for filepath, comments := range commentsByFile {
ProcessFile(wrapper, filepath, comments)
ProcessFile(filepath, comments)
}
}
@ -214,8 +176,6 @@ func main() {
splitComments = SplitByFile(splitComments, comments)
}
wrapper := wrap.NewWrapper()
wrapper.OutputLinePrefix = config.Language.Continuation
PatchFiles(&wrapper, splitComments)
ProcessComments(&splitComments)
PatchFiles(splitComments)
}