From f264d6109a3f5583d0cfec72f65a3d9281782608 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 11 Sep 2021 20:33:20 +0200 Subject: [PATCH] refactor: Separate Gitea code Signed-off-by: Matej Focko --- gitea_comments.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 54 +++---------------------------------------- 2 files changed, 62 insertions(+), 51 deletions(-) create mode 100644 gitea_comments.go diff --git a/gitea_comments.go b/gitea_comments.go new file mode 100644 index 0000000..3795d67 --- /dev/null +++ b/gitea_comments.go @@ -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 +} diff --git a/main.go b/main.go index 2918d4d..8352e6c 100644 --- a/main.go +++ b/main.go @@ -4,37 +4,11 @@ import ( "bufio" "fmt" "os" - "sort" "strconv" "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( outputFile *os.File, 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{} func main() { @@ -111,15 +71,7 @@ func main() { prID, err := strconv.ParseInt(os.Args[1], 10, 64) ExitOnError(err) - 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) - } - - ProcessComments(&splitComments) - PatchFiles(splitComments) + comments := GetComments(client, prID) + ProcessComments(&comments) + PatchFiles(comments) }