refactor: Rename functions and use new functions

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2021-05-12 15:47:55 +02:00
parent 25059c6abf
commit 49a0221611
No known key found for this signature in database
GPG key ID: CD9628474574E0B6

52
main.go
View file

@ -6,6 +6,7 @@ import (
"io"
"os"
"sort"
"strconv"
"strings"
"code.gitea.io/sdk/gitea"
@ -22,23 +23,21 @@ 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) {
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 {
func RetrieveComments(client *gitea.Client, prID, reviewID int64) []*gitea.PullReviewComment {
comments, _, err := client.ListPullReviewComments(REPOSITORY_OWNER, REPOSITORY_NAME, prID, reviewID)
exitOnError(err)
ExitOnError(err)
return comments
}
func splitByFile(comments []*gitea.PullReviewComment) map[string]([]*gitea.PullReviewComment) {
splitComments := make(map[string]([]*gitea.PullReviewComment))
func SplitByFile(splitComments map[string]([]*gitea.PullReviewComment), comments []*gitea.PullReviewComment) map[string]([]*gitea.PullReviewComment) {
for _, comment := range comments {
arr, found := splitComments[comment.Path]
@ -52,7 +51,7 @@ func splitByFile(comments []*gitea.PullReviewComment) map[string]([]*gitea.PullR
return splitComments
}
func printComments(wrapper *wrap.Wrapper, commentsByFile map[string]([]*gitea.PullReviewComment)) {
func PrintComments(wrapper *wrap.Wrapper, commentsByFile map[string]([]*gitea.PullReviewComment)) {
for filepath, comments := range commentsByFile {
fmt.Printf("FILE: %s\n", filepath)
@ -82,15 +81,19 @@ func BackUpSource(filepath string) {
_, err = io.Copy(BackUpSourceFile, inputFile)
ExitOnError(err)
}
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)
BackUpSource(filepath)
inputFile, err := os.Open(filepath + ".bck")
ExitOnError(err)
defer inputFile.Close()
outputFile, err := os.Create(filepath + ".patched")
exitOnError(err)
outputFile, err := os.Create(filepath)
ExitOnError(err)
defer outputFile.Close()
var i uint64 = 1
@ -98,7 +101,6 @@ func BackUpSource(filepath string) {
scanner := bufio.NewScanner(inputFile)
for scanner.Scan() {
for (nextComment < commentsLength) && comments[nextComment].LineNum == i {
body := comments[nextComment].Body
@ -149,18 +151,28 @@ func main() {
token := os.Getenv("GITEA_TOKEN")
client, err := gitea.NewClient(INSTANCE_URL, gitea.SetToken(token))
exitOnError(err)
ExitOnError(err)
var prID int64 = 2
var reviewID int64 = 1
comments := retrieveComments(client, prID, reviewID)
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s pr-id", os.Args[0])
os.Exit(1)
}
sort.Sort(ByLineNum(comments))
splitComments := splitByFile(comments)
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)
}
wrapper := wrap.NewWrapper()
wrapper.OutputLinePrefix = " ** "
printComments(&wrapper, splitComments)
// patchFiles(&wrapper, splitComments)
// PrintComments(&wrapper, splitComments)
PatchFiles(&wrapper, splitComments)
}