refactor: Rename functions and use new functions
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
25059c6abf
commit
49a0221611
1 changed files with 32 additions and 20 deletions
52
main.go
52
main.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"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) 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 (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 {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
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)
|
comments, _, err := client.ListPullReviewComments(REPOSITORY_OWNER, REPOSITORY_NAME, prID, reviewID)
|
||||||
exitOnError(err)
|
ExitOnError(err)
|
||||||
|
|
||||||
return comments
|
return comments
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitByFile(comments []*gitea.PullReviewComment) map[string]([]*gitea.PullReviewComment) {
|
func SplitByFile(splitComments map[string]([]*gitea.PullReviewComment), comments []*gitea.PullReviewComment) map[string]([]*gitea.PullReviewComment) {
|
||||||
splitComments := make(map[string]([]*gitea.PullReviewComment))
|
|
||||||
|
|
||||||
for _, comment := range comments {
|
for _, comment := range comments {
|
||||||
arr, found := splitComments[comment.Path]
|
arr, found := splitComments[comment.Path]
|
||||||
|
|
||||||
|
@ -52,7 +51,7 @@ func splitByFile(comments []*gitea.PullReviewComment) map[string]([]*gitea.PullR
|
||||||
return splitComments
|
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 {
|
for filepath, comments := range commentsByFile {
|
||||||
fmt.Printf("FILE: %s\n", filepath)
|
fmt.Printf("FILE: %s\n", filepath)
|
||||||
|
|
||||||
|
@ -82,15 +81,19 @@ func BackUpSource(filepath string) {
|
||||||
_, err = io.Copy(BackUpSourceFile, inputFile)
|
_, err = io.Copy(BackUpSourceFile, inputFile)
|
||||||
ExitOnError(err)
|
ExitOnError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PatchFiles(wrapper *wrap.Wrapper, commentsByFile map[string]([]*gitea.PullReviewComment)) {
|
||||||
for filepath, comments := range commentsByFile {
|
for filepath, comments := range commentsByFile {
|
||||||
fmt.Printf("FILE: %s\n", filepath)
|
fmt.Printf("FILE: %s\n", filepath)
|
||||||
|
|
||||||
inputFile, err := os.Open(filepath)
|
BackUpSource(filepath)
|
||||||
exitOnError(err)
|
|
||||||
|
inputFile, err := os.Open(filepath + ".bck")
|
||||||
|
ExitOnError(err)
|
||||||
defer inputFile.Close()
|
defer inputFile.Close()
|
||||||
|
|
||||||
outputFile, err := os.Create(filepath + ".patched")
|
outputFile, err := os.Create(filepath)
|
||||||
exitOnError(err)
|
ExitOnError(err)
|
||||||
defer outputFile.Close()
|
defer outputFile.Close()
|
||||||
|
|
||||||
var i uint64 = 1
|
var i uint64 = 1
|
||||||
|
@ -98,7 +101,6 @@ func BackUpSource(filepath string) {
|
||||||
|
|
||||||
scanner := bufio.NewScanner(inputFile)
|
scanner := bufio.NewScanner(inputFile)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
|
|
||||||
for (nextComment < commentsLength) && comments[nextComment].LineNum == i {
|
for (nextComment < commentsLength) && comments[nextComment].LineNum == i {
|
||||||
body := comments[nextComment].Body
|
body := comments[nextComment].Body
|
||||||
|
|
||||||
|
@ -149,18 +151,28 @@ func main() {
|
||||||
token := os.Getenv("GITEA_TOKEN")
|
token := os.Getenv("GITEA_TOKEN")
|
||||||
|
|
||||||
client, err := gitea.NewClient(INSTANCE_URL, gitea.SetToken(token))
|
client, err := gitea.NewClient(INSTANCE_URL, gitea.SetToken(token))
|
||||||
exitOnError(err)
|
ExitOnError(err)
|
||||||
|
|
||||||
var prID int64 = 2
|
if len(os.Args) < 2 {
|
||||||
var reviewID int64 = 1
|
fmt.Fprintf(os.Stderr, "Usage: %s pr-id", os.Args[0])
|
||||||
comments := retrieveComments(client, prID, reviewID)
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
sort.Sort(ByLineNum(comments))
|
prID, err := strconv.ParseInt(os.Args[1], 10, 64)
|
||||||
splitComments := splitByFile(comments)
|
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 := wrap.NewWrapper()
|
||||||
wrapper.OutputLinePrefix = " ** "
|
wrapper.OutputLinePrefix = " ** "
|
||||||
|
|
||||||
printComments(&wrapper, splitComments)
|
// PrintComments(&wrapper, splitComments)
|
||||||
// patchFiles(&wrapper, splitComments)
|
PatchFiles(&wrapper, splitComments)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue