refactor: Split processing of comments

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-09-11 19:44:30 +02:00
parent 698d39f7ad
commit 523bfcaf7d
No known key found for this signature in database
GPG key ID: 332171FADF1DB90B

54
main.go
View file

@ -99,26 +99,14 @@ 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 PatchFiles(wrapper *wrap.Wrapper, commentsByFile map[string]([]*gitea.PullReviewComment)) { func WriteCommentsInFile(
for filepath, comments := range commentsByFile { wrapper *wrap.Wrapper,
fmt.Printf("FILE: %s\n", filepath) outputFile *os.File,
lineNum uint64,
BackUpSource(filepath) comments []*gitea.PullReviewComment,
nextComment, commentsLength int,
inputFile, err := os.Open(filepath + ".bck") ) int {
ExitOnError(err) for (nextComment < commentsLength) && comments[nextComment].LineNum == lineNum {
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 body := comments[nextComment].Body
beenWrapped := len(body)+len(config.Language.Opening)+len(config.Language.Closing) > 100 beenWrapped := len(body)+len(config.Language.Opening)+len(config.Language.Closing) > 100
@ -141,11 +129,37 @@ func PatchFiles(wrapper *wrap.Wrapper, commentsByFile map[string]([]*gitea.PullR
fmt.Printf("L%04d:\n%s\n", comments[nextComment].LineNum, comment) fmt.Printf("L%04d:\n%s\n", comments[nextComment].LineNum, comment)
nextComment++ 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()) fmt.Fprintln(outputFile, scanner.Text())
i++ i++
} }
} }
func PatchFiles(wrapper *wrap.Wrapper, commentsByFile map[string]([]*gitea.PullReviewComment)) {
for filepath, comments := range commentsByFile {
ProcessFile(wrapper, filepath, comments)
}
} }
func GetReviewsPerPR(client *gitea.Client, prID int64) []int64 { func GetReviewsPerPR(client *gitea.Client, prID int64) []int64 {