frag-review/main.go
Matej Focko f264d6109a
refactor: Separate Gitea code
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-09-11 20:33:20 +02:00

77 lines
1.7 KiB
Go

package main
import (
"bufio"
"fmt"
"os"
"strconv"
"code.gitea.io/sdk/gitea"
)
func WriteCommentsInFile(
outputFile *os.File,
lineNum uint64,
comments []*gitea.PullReviewComment,
nextComment, commentsLength int,
) int {
for (nextComment < commentsLength) && comments[nextComment].LineNum == lineNum {
comment := comments[nextComment].Body
fmt.Fprint(outputFile, comment)
fmt.Printf("L%04d:\n%s\n", comments[nextComment].LineNum, comment)
nextComment++
}
return nextComment
}
func ProcessFile(filepath string, comments []*gitea.PullReviewComment) {
fmt.Printf("FILE: %s\n", filepath)
BackUpSource(filepath)
inputFile, err := os.Open(filepath + ".bck")
ExitOnError(err)
defer inputFile.Close()
outputFile, err := os.Create(filepath)
ExitOnError(err)
defer outputFile.Close()
var i uint64 = 1
nextComment, commentsLength := 0, len(comments)
scanner := bufio.NewScanner(inputFile)
for scanner.Scan() {
nextComment = WriteCommentsInFile(outputFile, i, comments, nextComment, commentsLength)
fmt.Fprintln(outputFile, scanner.Text())
i++
}
}
func PatchFiles(commentsByFile map[string]([]*gitea.PullReviewComment)) {
for filepath, comments := range commentsByFile {
ProcessFile(filepath, comments)
}
}
var config Config = Config{}
func main() {
token := os.Getenv("GITEA_TOKEN")
LoadConfig(&config, os.Getenv("FRAG_REVIEW_CONFIG"))
client, err := gitea.NewClient(config.Gitea.InstanceURL, gitea.SetToken(token))
ExitOnError(err)
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Usage: %s pr-id\n", os.Args[0])
os.Exit(1)
}
prID, err := strconv.ParseInt(os.Args[1], 10, 64)
ExitOnError(err)
comments := GetComments(client, prID)
ProcessComments(&comments)
PatchFiles(comments)
}