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