feat: 🔊 add logs

This commit is contained in:
RaviAnand Mohabir 2024-07-11 17:09:36 +02:00
parent 3f0e9dd908
commit cecbbddd07
3 changed files with 26 additions and 11 deletions

20
main.go
View File

@ -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,
}

View File

@ -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 {

View File

@ -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 {