1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-03-03 15:02:35 +02:00

Fix reading of hook configuration (#1854)

* Update piper.go

* Update piper_test.go

* Update config.go

* Update sentryHook.go

* Update piper.go

* go fmt

Co-authored-by: Stephan Aßmus <stephan.assmus@sap.com>
This commit is contained in:
Oliver Nocon 2020-07-28 17:19:33 +02:00 committed by GitHub
parent bcab73d332
commit d691750931
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 8 deletions

View File

@ -199,16 +199,21 @@ func PrepareConfig(cmd *cobra.Command, metadata *config.StepData, stepName strin
config.MarkFlagsWithValue(cmd, stepConfig)
for name, v := range stepConfig.HookConfig {
if name == "sentry" {
hookConfig, _ := v.MarshalJSON()
_ = json.Unmarshal(hookConfig, &GeneralConfig.HookConfig.SentryConfig)
}
}
retrieveHookConfig(stepConfig.HookConfig, &GeneralConfig.HookConfig)
return nil
}
func retrieveHookConfig(source *json.RawMessage, target *HookConfiguration) {
if source != nil {
log.Entry().Info("Retrieving hook configuration")
err := json.Unmarshal(*source, target)
if err != nil {
log.Entry().Warningf("Failed to retrieve hook configuration: %v", err)
}
}
}
func checkTypes(config map[string]interface{}, options interface{}) map[string]interface{} {
optionsType := getStepOptionsStructType(options)

View File

@ -96,6 +96,27 @@ func TestPrepareConfig(t *testing.T) {
})
}
func TestRetrieveHookConfig(t *testing.T) {
tt := []struct {
hookJSON []byte
expectedHookConfig HookConfiguration
}{
{hookJSON: []byte(""), expectedHookConfig: HookConfiguration{}},
{hookJSON: []byte(`{"sentry":{"dsn":"https://my.sentry.dsn"}}`), expectedHookConfig: HookConfiguration{SentryConfig: SentryConfiguration{Dsn: "https://my.sentry.dsn"}}},
}
for _, test := range tt {
var target HookConfiguration
var hookJSONRaw json.RawMessage
if len(test.hookJSON) > 0 {
err := json.Unmarshal(test.hookJSON, &hookJSONRaw)
assert.NoError(t, err)
}
retrieveHookConfig(&hookJSONRaw, &target)
assert.Equal(t, test.expectedHookConfig, target)
}
}
func TestGetProjectConfigFile(t *testing.T) {
tt := []struct {

View File

@ -23,7 +23,7 @@ type Config struct {
General map[string]interface{} `json:"general"`
Stages map[string]map[string]interface{} `json:"stages"`
Steps map[string]map[string]interface{} `json:"steps"`
Hooks map[string]*json.RawMessage `json:"hooks,omitempty"`
Hooks *json.RawMessage `json:"hooks,omitempty"`
defaults PipelineDefaults
initialized bool
openFile func(s string) (io.ReadCloser, error)
@ -32,7 +32,7 @@ type Config struct {
// StepConfig defines the structure for merged step configuration
type StepConfig struct {
Config map[string]interface{}
HookConfig map[string]*json.RawMessage
HookConfig *json.RawMessage
}
// ReadConfig loads config and returns its content

View File

@ -34,6 +34,7 @@ type SentryHook struct {
// NewSentryHook initializes sentry sdk with dsn and creates new hook
func NewSentryHook(sentryDsn, correlationID string) SentryHook {
Entry().Debugf("Initializing Sentry with DSN %v", sentryDsn)
if err := sentry.Init(sentry.ClientOptions{
Dsn: sentryDsn,
AttachStacktrace: true,