pr-resolution #3

Merged
mfocko merged 7 commits from pr-resolution into main 2021-09-11 22:36:41 +02:00
2 changed files with 62 additions and 51 deletions
Showing only changes of commit f264d6109a - Show all commits

59
gitea_comments.go Normal file
View file

@ -0,0 +1,59 @@
package main
import (
"sort"
"code.gitea.io/sdk/gitea"
)
func GetReviewsPerPR(client *gitea.Client, prID int64) []int64 {
reviews, _, err := client.ListPullReviews(
config.Gitea.Owner, config.Gitea.Repository, prID, gitea.ListPullReviewsOptions{},
)
ExitOnError(err)
result := make([]int64, 0)
for _, review := range reviews {
result = append(result, review.ID)
}
return result
}
func RetrieveComments(client *gitea.Client, prID, reviewID int64) []*gitea.PullReviewComment {
comments, _, err := client.ListPullReviewComments(
config.Gitea.Owner, config.Gitea.Repository, prID, reviewID,
)
ExitOnError(err)
return comments
}
func SplitByFile(
splitComments map[string]([]*gitea.PullReviewComment),
comments []*gitea.PullReviewComment,
) map[string]([]*gitea.PullReviewComment) {
for _, comment := range comments {
arr, found := splitComments[comment.Path]
if !found {
arr = make([]*gitea.PullReviewComment, 0)
}
splitComments[comment.Path] = append(arr, comment)
}
return splitComments
}
func GetComments(client *gitea.Client, prID int64) map[string]([]*gitea.PullReviewComment) {
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)
}
return splitComments
}

54
main.go
View file

@ -4,37 +4,11 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"os" "os"
"sort"
"strconv" "strconv"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
) )
func RetrieveComments(client *gitea.Client, prID, reviewID int64) []*gitea.PullReviewComment {
comments, _, err := client.ListPullReviewComments(
config.Gitea.Owner, config.Gitea.Repository, prID, reviewID,
)
ExitOnError(err)
return comments
}
func SplitByFile(
splitComments map[string]([]*gitea.PullReviewComment),
comments []*gitea.PullReviewComment,
) map[string]([]*gitea.PullReviewComment) {
for _, comment := range comments {
arr, found := splitComments[comment.Path]
if !found {
arr = make([]*gitea.PullReviewComment, 0)
}
splitComments[comment.Path] = append(arr, comment)
}
return splitComments
}
func WriteCommentsInFile( func WriteCommentsInFile(
outputFile *os.File, outputFile *os.File,
lineNum uint64, lineNum uint64,
@ -80,20 +54,6 @@ func PatchFiles(commentsByFile map[string]([]*gitea.PullReviewComment)) {
} }
} }
func GetReviewsPerPR(client *gitea.Client, prID int64) []int64 {
reviews, _, err := client.ListPullReviews(
config.Gitea.Owner, config.Gitea.Repository, prID, gitea.ListPullReviewsOptions{},
)
ExitOnError(err)
result := make([]int64, 0)
for _, review := range reviews {
result = append(result, review.ID)
}
return result
}
var config Config = Config{} var config Config = Config{}
func main() { func main() {
@ -111,15 +71,7 @@ func main() {
prID, err := strconv.ParseInt(os.Args[1], 10, 64) prID, err := strconv.ParseInt(os.Args[1], 10, 64)
ExitOnError(err) ExitOnError(err)
reviews := GetReviewsPerPR(client, prID) comments := GetComments(client, prID)
ProcessComments(&comments)
splitComments := make(map[string]([]*gitea.PullReviewComment)) PatchFiles(comments)
for _, reviewID := range reviews {
comments := RetrieveComments(client, prID, reviewID)
sort.Sort(ByLineNum(comments))
splitComments = SplitByFile(splitComments, comments)
}
ProcessComments(&splitComments)
PatchFiles(splitComments)
} }