refactor: Split processing of comments
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
698d39f7ad
commit
523bfcaf7d
1 changed files with 58 additions and 44 deletions
102
main.go
102
main.go
|
@ -99,52 +99,66 @@ func FormatComment(body string, beenWrapped bool) string {
|
||||||
return fmt.Sprintf("%s%s%s", config.Language.Opening, body, config.Language.Closing)
|
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)
|
||||||
|
|
||||||
|
fmt.Fprint(outputFile, comment)
|
||||||
|
fmt.Printf("L%04d:\n%s\n", comments[nextComment].LineNum, comment)
|
||||||
|
nextComment++
|
||||||
|
}
|
||||||
|
return nextComment
|
||||||
|
}
|
||||||
|
|
||||||
|
func ProcessFile(wrapper *wrap.Wrapper, filepath string, comments []*gitea.PullReviewComment) {
|
||||||
|
fmt.Printf("FILE: %s\n", filepath)
|
||||||
|
|
||||||
|
BackUpSource(filepath)
|
||||||
|
|
||||||
|
inputFile, err := os.Open(filepath + ".bck")
|
||||||
|
ExitOnError(err)
|
||||||
|
defer inputFile.Close()
|
||||||
|
|
||||||
|
outputFile, err := os.Create(filepath)
|
||||||
|
ExitOnError(err)
|
||||||
|
defer outputFile.Close()
|
||||||
|
|
||||||
|
var i uint64 = 1
|
||||||
|
nextComment, commentsLength := 0, len(comments)
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(inputFile)
|
||||||
|
for scanner.Scan() {
|
||||||
|
nextComment = WriteCommentsInFile(wrapper, outputFile, i, comments, nextComment, commentsLength)
|
||||||
|
fmt.Fprintln(outputFile, scanner.Text())
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func PatchFiles(wrapper *wrap.Wrapper, commentsByFile map[string]([]*gitea.PullReviewComment)) {
|
func PatchFiles(wrapper *wrap.Wrapper, commentsByFile map[string]([]*gitea.PullReviewComment)) {
|
||||||
for filepath, comments := range commentsByFile {
|
for filepath, comments := range commentsByFile {
|
||||||
fmt.Printf("FILE: %s\n", filepath)
|
ProcessFile(wrapper, filepath, comments)
|
||||||
|
|
||||||
BackUpSource(filepath)
|
|
||||||
|
|
||||||
inputFile, err := os.Open(filepath + ".bck")
|
|
||||||
ExitOnError(err)
|
|
||||||
defer inputFile.Close()
|
|
||||||
|
|
||||||
outputFile, err := os.Create(filepath)
|
|
||||||
ExitOnError(err)
|
|
||||||
defer outputFile.Close()
|
|
||||||
|
|
||||||
var i uint64 = 1
|
|
||||||
nextComment, commentsLength := 0, len(comments)
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(inputFile)
|
|
||||||
for scanner.Scan() {
|
|
||||||
for (nextComment < commentsLength) && comments[nextComment].LineNum == i {
|
|
||||||
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)
|
|
||||||
|
|
||||||
fmt.Fprint(outputFile, comment)
|
|
||||||
fmt.Printf("L%04d:\n%s\n", comments[nextComment].LineNum, comment)
|
|
||||||
nextComment++
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Fprintln(outputFile, scanner.Text())
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue