Skip to content

Commit

Permalink
fix(contrib): fix log on artifactory plugins (#6426)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored Jan 23, 2023
1 parent c7defed commit 87dbb36
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/golang/protobuf/ptypes/empty"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/rockbears/log"

"github.com/ovh/cds/contrib/grpcplugins"
art "github.com/ovh/cds/contrib/integrations/artifactory"
Expand Down Expand Up @@ -57,6 +58,9 @@ func (e *artifactoryBuildInfoPlugin) Manifest(_ context.Context, _ *empty.Empty)
}

func (e *artifactoryBuildInfoPlugin) Run(ctx context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
log.Factory = log.NewStdWrapper(log.StdWrapperOptions{DisableTimestamp: true, Level: log.LevelInfo})
log.UnregisterField(log.FieldCaller, log.FieldSourceFile, log.FieldSourceLine, log.FieldStackTrace)

artifactoryURL := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigURL)]
token := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigToken)]
tokenName := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigTokenName)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package main
import (
"context"
"fmt"
"os"

"regexp"
"strings"
"time"

"github.com/golang/protobuf/ptypes/empty"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/rockbears/log"

"github.com/ovh/cds/contrib/grpcplugins"
art "github.com/ovh/cds/contrib/integrations/artifactory"
Expand Down Expand Up @@ -56,6 +56,9 @@ func (e *artifactoryPromotePlugin) Manifest(_ context.Context, _ *empty.Empty) (
}

func (e *artifactoryPromotePlugin) Run(ctx context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
log.Factory = log.NewStdWrapper(log.StdWrapperOptions{DisableTimestamp: true, Level: log.LevelInfo})
log.UnregisterField(log.FieldCaller, log.FieldSourceFile, log.FieldSourceLine, log.FieldStackTrace)

artifactoryURL := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigURL)]
token := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigToken)]

Expand All @@ -80,8 +83,6 @@ func (e *artifactoryPromotePlugin) Run(ctx context.Context, opts *integrationplu
return fail("unable to list run results: %v", err)
}

log.SetLogger(log.NewLogger(log.INFO, os.Stdout))

ctx, cancel := context.WithTimeout(ctx, 15*time.Minute)
defer cancel()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"os"
"regexp"
"strings"
"time"
Expand All @@ -12,7 +11,7 @@ import (
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/distribution/services"
distriUtils "github.com/jfrog/jfrog-client-go/distribution/services/utils"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/rockbears/log"

"github.com/ovh/cds/contrib/grpcplugins"
art "github.com/ovh/cds/contrib/integrations/artifactory"
Expand Down Expand Up @@ -59,6 +58,9 @@ func (e *artifactoryReleasePlugin) Manifest(_ context.Context, _ *empty.Empty) (
}

func (e *artifactoryReleasePlugin) Run(ctx context.Context, opts *integrationplugin.RunQuery) (*integrationplugin.RunResult, error) {
log.Factory = log.NewStdWrapper(log.StdWrapperOptions{DisableTimestamp: true, Level: log.LevelInfo})
log.UnregisterField(log.FieldCaller, log.FieldSourceFile, log.FieldSourceLine, log.FieldStackTrace)

artifactoryURL := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigURL)]
distributionURL := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigDistributionURL)]
token := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactoryConfigToken)]
Expand Down Expand Up @@ -94,13 +96,12 @@ func (e *artifactoryReleasePlugin) Run(ctx context.Context, opts *integrationplu

fmt.Printf("Found %d run results\n", len(runResult))

log.SetLogger(log.NewLogger(log.INFO, os.Stdout))
if distributionURL == "" {
fmt.Printf("Using %s to release\n", artifactoryURL)
distributionURL = artifactoryURL
}
if releaseToken == "" {
fmt.Println("Using distribution token to release")
fmt.Println("Using artifactory token to release")
releaseToken = token
}

Expand Down
4 changes: 0 additions & 4 deletions sdk/artifact_manager/interface.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package artifact_manager

import (
"os"

buildinfo "github.com/jfrog/build-info-go/entities"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/utils/log"
arti "github.com/ovh/cds/sdk/artifact_manager/artifactory"

"github.com/ovh/cds/sdk"
Expand Down Expand Up @@ -38,7 +35,6 @@ func NewClient(managerType, url, token string) (ArtifactManager, error) {
}

func newClient(managerType, url, token string) (ArtifactManager, error) {
log.SetLogger(log.NewLogger(log.INFO, os.Stdout))
switch managerType {
case "artifactory":
asm, err := sdk.NewArtifactoryClient(url, token)
Expand Down
31 changes: 25 additions & 6 deletions sdk/artifactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,46 @@ import (
type artifactoryLogger struct{}

func (artifactoryLogger) GetLogLevel() rtlog.LevelType {
return rtlog.LevelType(log.Factory().GetLevel())
switch log.Factory().GetLevel() {
case log.LevelDebug:
return rtlog.DEBUG
case log.LevelInfo:
return rtlog.INFO
case log.LevelWarn:
return rtlog.WARN
case log.LevelError, log.LevelFatal, log.LevelPanic:
return rtlog.ERROR
default:
return rtlog.INFO
}
}

func (artifactoryLogger) SetLogLevel(rtlog.LevelType) {}
func (artifactoryLogger) SetOutputWriter(writer io.Writer) {}
func (artifactoryLogger) SetLogsWriter(writer io.Writer) {}

func (l artifactoryLogger) Debug(a ...interface{}) {
log.Debug(context.Background(), fmt.Sprint(a...))
log.Debug(context.Background(), l.BuildMsg(a))
}
func (l artifactoryLogger) Info(a ...interface{}) {
log.Info(context.Background(), fmt.Sprint(a...))
log.Info(context.Background(), l.BuildMsg(a...))
}
func (l artifactoryLogger) Warn(a ...interface{}) {
log.Warn(context.Background(), fmt.Sprint(a...))
log.Warn(context.Background(), l.BuildMsg(a...))
}
func (l artifactoryLogger) Error(a ...interface{}) {
log.Error(context.Background(), fmt.Sprint(a...))
log.Error(context.Background(), l.BuildMsg(a...))
}
func (l artifactoryLogger) Output(a ...interface{}) {
log.Info(context.Background(), fmt.Sprint(a...))
log.Info(context.Background(), l.BuildMsg(a...))
}

func (l artifactoryLogger) BuildMsg(a ...interface{}) string {
msg := make([]string, 0, len(a))
for _, m := range a {
msg = append(msg, fmt.Sprint(m))
}
return strings.Join(msg, " ")
}

func NewArtifactoryClient(url string, token string) (artifactory.ArtifactoryServicesManager, error) {
Expand Down

0 comments on commit 87dbb36

Please sign in to comment.