refactor: Improve error logging
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
8c2e478ae9
commit
25626f1f75
4 changed files with 16 additions and 14 deletions
|
@ -25,10 +25,10 @@ var (
|
|||
PreRun: InitializeConfig,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
client, err := gitea.NewClient(config.Gitea.InstanceURL, gitea.SetToken(config.Gitea.Token))
|
||||
core.ExitOnError(err)
|
||||
core.ExitOnError("Couldn't create gitea client", err)
|
||||
|
||||
err = GetPrID(client, &prID)
|
||||
core.ExitOnError(err)
|
||||
core.ExitOnError("Couldn't get PR ID", err)
|
||||
|
||||
comments := core.GetComments(&config, client, prID)
|
||||
core.ProcessComments(&config, &comments)
|
||||
|
@ -54,7 +54,7 @@ func GetPrID(client *gitea.Client, prID *int64) error {
|
|||
gitCmd.Stdout = &capturedOutput
|
||||
|
||||
err := gitCmd.Run()
|
||||
core.ExitOnError(err)
|
||||
core.ExitOnError("Couldn't list git branches", err)
|
||||
|
||||
branch = strings.TrimSpace(capturedOutput.String())
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ func GetPrID(client *gitea.Client, prID *int64) error {
|
|||
prs, _, err := client.ListRepoPullRequests(
|
||||
config.Gitea.Owner, config.Gitea.Repository, gitea.ListPullRequestsOptions{},
|
||||
)
|
||||
core.ExitOnError(err)
|
||||
core.ExitOnError("Couldn't list pull requests", err)
|
||||
|
||||
for _, pr := range prs {
|
||||
if pr.Head.Name == branch {
|
||||
|
@ -94,11 +94,11 @@ func ProcessFile(filepath string, comments []*gitea.PullReviewComment) {
|
|||
core.BackUpSource(filepath)
|
||||
|
||||
inputFile, err := os.Open(filepath + ".bck")
|
||||
core.ExitOnError(err)
|
||||
core.ExitOnError("Couldn't open backup file", err)
|
||||
defer inputFile.Close()
|
||||
|
||||
outputFile, err := os.Create(filepath)
|
||||
core.ExitOnError(err)
|
||||
core.ExitOnError("Couldn't overwrite original file", err)
|
||||
defer outputFile.Close()
|
||||
|
||||
var i uint64 = 1
|
||||
|
|
|
@ -23,10 +23,10 @@ type Config struct {
|
|||
|
||||
func LoadConfig(config *Config, filename string) {
|
||||
configFile, err := os.Open(filename)
|
||||
ExitOnError(err)
|
||||
ExitOnError("Couldn't open config file", err)
|
||||
defer configFile.Close()
|
||||
|
||||
decoder := yaml.NewDecoder(configFile)
|
||||
err = decoder.Decode(&config)
|
||||
ExitOnError(err)
|
||||
ExitOnError("Couldn't decode config file", err)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package core
|
|||
|
||||
import (
|
||||
"sort"
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
@ -10,7 +11,7 @@ func GetReviewsPerPR(config *Config, client *gitea.Client, prID int64) []int64 {
|
|||
reviews, _, err := client.ListPullReviews(
|
||||
config.Gitea.Owner, config.Gitea.Repository, prID, gitea.ListPullReviewsOptions{},
|
||||
)
|
||||
ExitOnError(err)
|
||||
ExitOnError("Couldn't list reviews per PR ID", err)
|
||||
|
||||
result := make([]int64, 0)
|
||||
for _, review := range reviews {
|
||||
|
@ -24,7 +25,7 @@ func RetrieveComments(config *Config, client *gitea.Client, prID, reviewID int64
|
|||
comments, _, err := client.ListPullReviewComments(
|
||||
config.Gitea.Owner, config.Gitea.Repository, prID, reviewID,
|
||||
)
|
||||
ExitOnError(err)
|
||||
ExitOnError("Couldn't list review comments for review", err)
|
||||
return comments
|
||||
}
|
||||
|
||||
|
|
|
@ -14,8 +14,9 @@ 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(msg string, err error) {
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s: ", msg)
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
@ -23,13 +24,13 @@ func ExitOnError(err error) {
|
|||
|
||||
func BackUpSource(filepath string) {
|
||||
inputFile, err := os.Open(filepath)
|
||||
ExitOnError(err)
|
||||
ExitOnError("Couldn't open input file for backup", err)
|
||||
defer inputFile.Close()
|
||||
|
||||
BackUpSourceFile, err := os.Create(filepath + ".bck")
|
||||
ExitOnError(err)
|
||||
ExitOnError("Couldn't create backup file", err)
|
||||
defer BackUpSourceFile.Close()
|
||||
|
||||
_, err = io.Copy(BackUpSourceFile, inputFile)
|
||||
ExitOnError(err)
|
||||
ExitOnError("Couldn't copy into backup file", err)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue