2021-04-30 18:44:02 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-05-12 15:47:55 +02:00
|
|
|
"strconv"
|
2021-04-30 18:44:02 +02:00
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
)
|
|
|
|
|
2021-09-11 19:44:30 +02:00
|
|
|
func WriteCommentsInFile(
|
|
|
|
outputFile *os.File,
|
|
|
|
lineNum uint64,
|
|
|
|
comments []*gitea.PullReviewComment,
|
|
|
|
nextComment, commentsLength int,
|
|
|
|
) int {
|
|
|
|
for (nextComment < commentsLength) && comments[nextComment].LineNum == lineNum {
|
2021-09-11 20:00:48 +02:00
|
|
|
comment := comments[nextComment].Body
|
2021-09-11 19:44:30 +02:00
|
|
|
fmt.Fprint(outputFile, comment)
|
|
|
|
fmt.Printf("L%04d:\n%s\n", comments[nextComment].LineNum, comment)
|
|
|
|
nextComment++
|
|
|
|
}
|
|
|
|
return nextComment
|
|
|
|
}
|
|
|
|
|
2021-09-11 20:00:48 +02:00
|
|
|
func ProcessFile(filepath string, comments []*gitea.PullReviewComment) {
|
2021-09-11 19:44:30 +02:00
|
|
|
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() {
|
2021-09-11 20:00:48 +02:00
|
|
|
nextComment = WriteCommentsInFile(outputFile, i, comments, nextComment, commentsLength)
|
2021-09-11 19:44:30 +02:00
|
|
|
fmt.Fprintln(outputFile, scanner.Text())
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-11 20:00:48 +02:00
|
|
|
func PatchFiles(commentsByFile map[string]([]*gitea.PullReviewComment)) {
|
2021-04-30 18:44:02 +02:00
|
|
|
for filepath, comments := range commentsByFile {
|
2021-09-11 20:00:48 +02:00
|
|
|
ProcessFile(filepath, comments)
|
2021-04-30 18:44:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-11 20:25:20 +02:00
|
|
|
var config Config = Config{}
|
2021-09-11 19:14:06 +02:00
|
|
|
|
2021-04-30 18:44:02 +02:00
|
|
|
func main() {
|
|
|
|
token := os.Getenv("GITEA_TOKEN")
|
2021-09-11 20:25:20 +02:00
|
|
|
LoadConfig(&config, os.Getenv("FRAG_REVIEW_CONFIG"))
|
2021-04-30 18:44:02 +02:00
|
|
|
|
2021-09-11 19:14:06 +02:00
|
|
|
client, err := gitea.NewClient(config.Gitea.InstanceURL, gitea.SetToken(token))
|
2021-05-12 15:47:55 +02:00
|
|
|
ExitOnError(err)
|
2021-04-30 18:44:02 +02:00
|
|
|
|
2021-05-12 15:47:55 +02:00
|
|
|
if len(os.Args) < 2 {
|
2021-09-11 19:14:06 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "Usage: %s pr-id\n", os.Args[0])
|
2021-05-12 15:47:55 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
prID, err := strconv.ParseInt(os.Args[1], 10, 64)
|
|
|
|
ExitOnError(err)
|
2021-04-30 18:44:02 +02:00
|
|
|
|
2021-09-11 20:33:20 +02:00
|
|
|
comments := GetComments(client, prID)
|
|
|
|
ProcessComments(&comments)
|
|
|
|
PatchFiles(comments)
|
2021-04-30 18:44:02 +02:00
|
|
|
}
|