2021-04-30 18:44:02 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
2021-05-12 15:46:01 +02:00
|
|
|
"io"
|
2021-04-30 18:44:02 +02:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 15:46:01 +02:00
|
|
|
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)
|
|
|
|
}
|
2021-04-30 18:44:02 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 15:45:22 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:44:02 +02:00
|
|
|
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)
|
|
|
|
}
|