2021-09-11 22:24:39 +02:00
|
|
|
package core
|
2021-09-11 20:00:48 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
"github.com/bbrks/wrap/v2"
|
|
|
|
)
|
|
|
|
|
2021-09-11 22:24:39 +02:00
|
|
|
func GetSeparatorForMultiline(config *Config) string {
|
2021-09-11 23:34:25 +02:00
|
|
|
if config.Language.OnSeparateLine {
|
2021-09-11 20:00:48 +02:00
|
|
|
return "\n"
|
|
|
|
}
|
2021-09-11 23:20:35 +02:00
|
|
|
return ""
|
2021-09-11 20:00:48 +02:00
|
|
|
}
|
|
|
|
|
2021-09-11 22:24:39 +02:00
|
|
|
func FormatComment(config *Config, body string, beenWrapped bool) string {
|
2021-09-11 23:34:25 +02:00
|
|
|
if beenWrapped {
|
|
|
|
if !config.Language.OnSeparateLine {
|
|
|
|
body = strings.TrimPrefix(body, config.Language.Continuation)
|
|
|
|
}
|
|
|
|
body = strings.TrimRightFunc(body, unicode.IsSpace)
|
2021-09-11 20:00:48 +02:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s%s%s", config.Language.Opening, body, config.Language.Closing)
|
|
|
|
}
|
|
|
|
|
2021-09-11 22:24:39 +02:00
|
|
|
func ProcessComment(config *Config, wrapper *wrap.Wrapper, comment *gitea.PullReviewComment) {
|
2021-09-11 20:00:48 +02:00
|
|
|
body := comment.Body
|
|
|
|
|
|
|
|
beenWrapped := len(body)+len(config.Language.Opening)+len(config.Language.Closing) > 100
|
|
|
|
if beenWrapped {
|
2021-09-11 22:24:39 +02:00
|
|
|
body = GetSeparatorForMultiline(config) + wrapper.Wrap(body, 100)
|
2021-09-11 20:00:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
body = strings.Replace(body, "\r", "", -1)
|
|
|
|
body = strings.Replace(
|
|
|
|
body,
|
|
|
|
config.Language.Continuation+"\n",
|
|
|
|
strings.TrimRightFunc(config.Language.Continuation, unicode.IsSpace)+"\n",
|
|
|
|
-1,
|
|
|
|
)
|
2021-09-11 23:34:25 +02:00
|
|
|
body = strings.Replace(
|
|
|
|
body,
|
|
|
|
config.Language.Opening+"\n",
|
|
|
|
strings.TrimRightFunc(config.Language.Opening, unicode.IsSpace)+"\n",
|
|
|
|
-1,
|
|
|
|
)
|
2021-09-11 22:24:39 +02:00
|
|
|
comment.Body = FormatComment(config, body, beenWrapped)
|
2021-09-11 20:00:48 +02:00
|
|
|
}
|
|
|
|
|
2021-09-11 22:24:39 +02:00
|
|
|
func ProcessComments(config *Config, splitComments *map[string]([]*gitea.PullReviewComment)) {
|
2021-09-11 20:00:48 +02:00
|
|
|
wrapper := wrap.NewWrapper()
|
|
|
|
wrapper.OutputLinePrefix = config.Language.Continuation
|
|
|
|
|
|
|
|
for _, comments := range *splitComments {
|
|
|
|
for _, comment := range comments {
|
2021-09-11 22:24:39 +02:00
|
|
|
ProcessComment(config, &wrapper, comment)
|
2021-09-11 20:00:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|