From 8ffd5ca8eb8741cfabfbee1c50dd0b23b5359b55 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Fri, 30 Apr 2021 18:44:02 +0200 Subject: [PATCH] feat: Add initial version Signed-off-by: Matej Focko --- main.go | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..ab3ea61 --- /dev/null +++ b/main.go @@ -0,0 +1,138 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "sort" + + "code.gitea.io/sdk/gitea" + "github.com/bbrks/wrap" +) + +const INSTANCE_URL string = "https://git.mfocko.xyz" +const REPOSITORY_OWNER string = "mfocko" +const REPOSITORY_NAME string = "2021_pb161_reviews" + +type ByLineNum []*gitea.PullReviewComment + +func (a ByLineNum) Len() int { return len(a) } +func (a ByLineNum) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a ByLineNum) Less(i, j int) bool { return a[i].LineNum < a[j].LineNum } + +func exitOnError(err error) { + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func retrieveComments(client *gitea.Client, prID, reviewID int64) []*gitea.PullReviewComment { + comments, _, err := client.ListPullReviewComments(REPOSITORY_OWNER, REPOSITORY_NAME, prID, reviewID) + exitOnError(err) + + return comments +} + +func splitByFile(comments []*gitea.PullReviewComment) map[string]([]*gitea.PullReviewComment) { + splitComments := make(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 printComments(wrapper *wrap.Wrapper, commentsByFile map[string]([]*gitea.PullReviewComment)) { + for filepath, comments := range commentsByFile { + fmt.Printf("FILE: %s\n", filepath) + + for _, comment := range comments { + body := comment.Body + + if len(body) >= 80 { + body = "\n" + wrapper.Wrap(body, 80) + } else { + body = " " + body + } + + fmt.Printf("L%04d:\n/**%s **/\n\n", comment.LineNum, body) + } + } +} + +func patchFiles(wrapper *wrap.Wrapper, commentsByFile map[string]([]*gitea.PullReviewComment)) { + for filepath, comments := range commentsByFile { + fmt.Printf("FILE: %s\n", filepath) + + inputFile, err := os.Open(filepath) + exitOnError(err) + defer inputFile.Close() + + outputFile, err := os.Create(filepath + ".patched") + 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 + + if len(body) >= 80 { + body = "\n" + wrapper.Wrap(body, 80) + } else { + body = " " + body + } + + fmt.Fprintf(outputFile, "/**%s **/\n", body) + nextComment++ + } + + fmt.Fprintln(outputFile, scanner.Text()) + i++ + } + + for _, comment := range comments { + body := comment.Body + + if len(body) >= 80 { + body = "\n" + wrapper.Wrap(body, 80) + } else { + body = " " + body + } + + fmt.Printf("L%04d:\n/**%s **/\n\n", comment.LineNum, body) + } + } +} + +func main() { + token := os.Getenv("GITEA_TOKEN") + + client, err := gitea.NewClient(INSTANCE_URL, gitea.SetToken(token)) + exitOnError(err) + + var prID int64 = 2 + var reviewID int64 = 1 + comments := retrieveComments(client, prID, reviewID) + + sort.Sort(ByLineNum(comments)) + splitComments := splitByFile(comments) + + wrapper := wrap.NewWrapper() + wrapper.OutputLinePrefix = " ** " + + printComments(&wrapper, splitComments) + // patchFiles(&wrapper, splitComments) +}