package main import ( "bufio" "fmt" "io" "os" "sort" "strings" "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 BackUpSource(filepath string) { inputFile, err := os.Open(filepath) ExitOnError(err) defer inputFile.Close() BackUpSourceFile, err := os.Create(filepath + ".bck") ExitOnError(err) defer BackUpSourceFile.Close() _, err = io.Copy(BackUpSourceFile, inputFile) ExitOnError(err) } 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 } body = strings.Replace(body, "\r", "", -1) body = strings.Replace(body, " ** \n", " **\n", -1) 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 GetReviewsPerPR(client *gitea.Client, prID int64) []int64 { reviews, _, err := client.ListPullReviews(REPOSITORY_OWNER, REPOSITORY_NAME, prID, gitea.ListPullReviewsOptions{}) ExitOnError(err) result := make([]int64, 0) for _, review := range reviews { result = append(result, review.ID) } return result } 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) }