feat: Add initial version
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
e6d0486d96
commit
8ffd5ca8eb
1 changed files with 138 additions and 0 deletions
138
main.go
Normal file
138
main.go
Normal file
|
@ -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)
|
||||||
|
}
|
Loading…
Reference in a new issue