From cecbbddd07a267381f6e2051e3014ed138b4bffc Mon Sep 17 00:00:00 2001 From: RaviAnand Mohabir Date: Thu, 11 Jul 2024 17:09:36 +0200 Subject: [PATCH] feat: :loud_sound: add logs --- main.go | 20 ++++++++++---------- plugin/plugin.go | 12 +++++++++++- plugin/util.go | 5 +++++ 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 4cdd080..7c1a311 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,7 @@ import ( "gitea.dikurium.ch/InnoPeak/drone-gitea-release/plugin" "github.com/kelseyhightower/envconfig" - "github.com/sirupsen/logrus" + log "github.com/sirupsen/logrus" flag "github.com/spf13/pflag" ) @@ -15,7 +15,7 @@ var args plugin.Args func init() { if err := envconfig.Process("", &args); err != nil { - logrus.Fatalln(err) + log.Fatalln(err) } var ( @@ -50,19 +50,19 @@ func init() { } func main() { - logrus.SetFormatter(new(formatter)) + log.SetFormatter(new(formatter)) switch args.Level { case "debug": - logrus.SetFormatter(textFormatter) - logrus.SetLevel(logrus.DebugLevel) + log.SetFormatter(textFormatter) + log.SetLevel(log.DebugLevel) case "trace": - logrus.SetFormatter(textFormatter) - logrus.SetLevel(logrus.TraceLevel) + log.SetFormatter(textFormatter) + log.SetLevel(log.TraceLevel) } if err := plugin.Exec(context.Background(), args); err != nil { - logrus.Fatalln(err) + log.Fatalln(err) } } @@ -70,11 +70,11 @@ func main() { // or level information. type formatter struct{} -func (*formatter) Format(entry *logrus.Entry) ([]byte, error) { +func (*formatter) Format(entry *log.Entry) ([]byte, error) { return []byte(entry.Message), nil } // text formatter that writes logs with level information -var textFormatter = &logrus.TextFormatter{ +var textFormatter = &log.TextFormatter{ DisableTimestamp: true, } diff --git a/plugin/plugin.go b/plugin/plugin.go index bb5e98d..e809099 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -9,6 +9,7 @@ import ( "text/template" "code.gitea.io/sdk/gitea" + log "github.com/sirupsen/logrus" ) var ( @@ -71,7 +72,7 @@ func Exec(ctx context.Context, args Args) error { if err != nil { if os.IsNotExist(err) && args.SkipIfNoNotesFile { - fmt.Println("No notes file found, skipping release") + log.Info("No notes file found, skipping release") return nil } return fmt.Errorf("error reading notes file %w", err) @@ -96,6 +97,8 @@ func Exec(ctx context.Context, args Args) error { return fmt.Errorf("error getting git tag %w", err) } } else if args.TagFile != "" { + log.WithField("file", args.TagFile).Info("Reading tag from file") + var pattern = defaultTagRegex if args.TagRegex != "" { pattern = regexp.MustCompile(args.TagRegex) @@ -118,6 +121,8 @@ func Exec(ctx context.Context, args Args) error { tag = matches[i] } } + + log.WithField("tag", tag).Info("Found tag") } else { return fmt.Errorf("latest git tag or tag file must be given") } @@ -142,6 +147,11 @@ func Exec(ctx context.Context, args Args) error { title = titleBuffer.String() + log.WithFields(log.Fields{ + "template": titleTmpl.DefinedTemplates(), + "title": title, + }).Info("Generated title with template") + client, err := gitea.NewClient(args.GiteaUrl, gitea.SetBasicAuth(args.GiteaUsername, args.GiteaPassword)) if err != nil { diff --git a/plugin/util.go b/plugin/util.go index 4ae7ced..ae5d617 100644 --- a/plugin/util.go +++ b/plugin/util.go @@ -9,6 +9,7 @@ import ( "encoding/json" "fmt" "io" + "log" "net/http" "os" "strings" @@ -34,6 +35,8 @@ func (bat BasicAuthTransport) RoundTrip(req *http.Request) (resp *http.Response, func SetBasicAuth(username, password string) func(repo *git.Repository) error { return func(repo *git.Repository) error { + log.Println("Configuring basic auth for https") + customClient := &http.Client{ Transport: BasicAuthTransport{username, password, http.DefaultTransport}, } @@ -47,6 +50,8 @@ func SetBasicAuth(username, password string) func(repo *git.Repository) error { func FetchTags() func(repo *git.Repository) error { return func(repo *git.Repository) error { + log.Println("Fetching latest git tags") + err := repo.Fetch(&git.FetchOptions{Tags: git.AllTags}) if err == git.NoErrAlreadyUpToDate {