2021-04-30 18:44:02 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sort"
|
2021-05-12 15:47:55 +02:00
|
|
|
"strconv"
|
2021-04-30 18:44:02 +02:00
|
|
|
|
|
|
|
"code.gitea.io/sdk/gitea"
|
|
|
|
)
|
|
|
|
|
2021-05-12 15:47:55 +02:00
|
|
|
func RetrieveComments(client *gitea.Client, prID, reviewID int64) []*gitea.PullReviewComment {
|
2021-09-11 19:14:06 +02:00
|
|
|
comments, _, err := client.ListPullReviewComments(
|
|
|
|
config.Gitea.Owner, config.Gitea.Repository, prID, reviewID,
|
|
|
|
)
|
2021-05-12 15:47:55 +02:00
|
|
|
ExitOnError(err)
|
2021-04-30 18:44:02 +02:00
|
|
|
return comments
|
|
|
|
}
|
|
|
|
|
2021-09-11 19:14:06 +02:00
|
|
|
func SplitByFile(
|
|
|
|
splitComments map[string]([]*gitea.PullReviewComment),
|
|
|
|
comments []*gitea.PullReviewComment,
|
|
|
|
) map[string]([]*gitea.PullReviewComment) {
|
2021-04-30 18:44:02 +02:00
|
|
|
for _, comment := range comments {
|
|
|
|
arr, found := splitComments[comment.Path]
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
arr = make([]*gitea.PullReviewComment, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
splitComments[comment.Path] = append(arr, comment)
|
|
|
|
}
|
|
|
|
|
|
|
|
return splitComments
|
|
|
|
}
|
|
|
|
|
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-05-12 15:45:22 +02:00
|
|
|
func GetReviewsPerPR(client *gitea.Client, prID int64) []int64 {
|
2021-09-11 19:14:06 +02:00
|
|
|
reviews, _, err := client.ListPullReviews(
|
|
|
|
config.Gitea.Owner, config.Gitea.Repository, prID, gitea.ListPullReviewsOptions{},
|
|
|
|
)
|
2021-05-12 15:45:22 +02:00
|
|
|
ExitOnError(err)
|
|
|
|
|
|
|
|
result := make([]int64, 0)
|
|
|
|
for _, review := range reviews {
|
|
|
|
result = append(result, review.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
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-05-12 15:47:55 +02:00
|
|
|
reviews := GetReviewsPerPR(client, prID)
|
|
|
|
|
|
|
|
splitComments := make(map[string]([]*gitea.PullReviewComment))
|
|
|
|
for _, reviewID := range reviews {
|
|
|
|
comments := RetrieveComments(client, prID, reviewID)
|
|
|
|
sort.Sort(ByLineNum(comments))
|
|
|
|
splitComments = SplitByFile(splitComments, comments)
|
|
|
|
}
|
2021-04-30 18:44:02 +02:00
|
|
|
|
2021-09-11 20:00:48 +02:00
|
|
|
ProcessComments(&splitComments)
|
|
|
|
PatchFiles(splitComments)
|
2021-04-30 18:44:02 +02:00
|
|
|
}
|