Skip to content

Commit

Permalink
Fix: Use *HookConfig instead map for Hook Config
Browse files Browse the repository at this point in the history
  • Loading branch information
nayuta committed Mar 7, 2024
1 parent ebc59ad commit 26350a1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
22 changes: 7 additions & 15 deletions github/resource_github_organization_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,7 @@ func resourceGithubOrganizationWebhookObject(d *schema.ResourceData) *github.Hoo

config := d.Get("configuration").([]interface{})
if len(config) > 0 {
configMap := config[0].(map[string]interface{})
hook.Config = &github.HookConfig{
ContentType: github.String(configMap["content_type"].(string)),
InsecureSSL: func() *string {
if configMap["insecure_ssl"].(bool) {
return github.String("1")
} else {
return github.String("0")
}
}(),
URL: github.String(configMap["url"].(string)),
Secret: github.String(configMap["secret"].(string)),
}
hook.Config = webhookConfigurationSchemaElemToObject(config)
}

return hook
Expand Down Expand Up @@ -110,7 +98,9 @@ func resourceGithubOrganizationWebhookCreate(d *schema.ResourceData, meta interf
if hook.Config.Secret != nil {
hook.Config.Secret = webhookObj.Config.Secret
}
if err = d.Set("configuration", []interface{}{hook.Config}); err != nil {

config := webhookConfigurationObjectToSchemaElem(hook.Config)
if err = d.Set("configuration", config); err != nil {
return err
}

Expand Down Expand Up @@ -176,7 +166,9 @@ func resourceGithubOrganizationWebhookRead(d *schema.ResourceData, meta interfac
}
}

if err = d.Set("configuration", []interface{}{hook.Config}); err != nil {
config := webhookConfigurationObjectToSchemaElem(hook.Config)

if err = d.Set("configuration", config); err != nil {
return err
}

Expand Down
52 changes: 37 additions & 15 deletions github/resource_github_repository_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,7 @@ func resourceGithubRepositoryWebhookObject(d *schema.ResourceData) *github.Hook

config := d.Get("configuration").([]interface{})
if len(config) > 0 {
configMap := config[0].(map[string]interface{})
hook.Config = &github.HookConfig{
ContentType: github.String(configMap["content_type"].(string)),
InsecureSSL: func() *string {
if configMap["insecure_ssl"].(bool) {
return github.String("1")
} else {
return github.String("0")
}
}(),
URL: github.String(configMap["url"].(string)),
Secret: github.String(configMap["secret"].(string)),
}
hook.Config = webhookConfigurationSchemaElemToObject(config)
}

return hook
Expand All @@ -125,7 +113,8 @@ func resourceGithubRepositoryWebhookCreate(d *schema.ResourceData, meta interfac
hook.Config.Secret = hk.Config.Secret
}

if err = d.Set("configuration", []interface{}{hook.Config}); err != nil {
config := webhookConfigurationObjectToSchemaElem(hook.Config)
if err = d.Set("configuration", config); err != nil {
return err
}

Expand Down Expand Up @@ -183,7 +172,8 @@ func resourceGithubRepositoryWebhookRead(d *schema.ResourceData, meta interface{
}
}

if err = d.Set("configuration", []interface{}{hook.Config}); err != nil {
config := webhookConfigurationObjectToSchemaElem(hook.Config)
if err = d.Set("configuration", config); err != nil {
return err
}

Expand Down Expand Up @@ -224,3 +214,35 @@ func resourceGithubRepositoryWebhookDelete(d *schema.ResourceData, meta interfac
_, err = client.Repositories.DeleteHook(ctx, owner, repoName, hookID)
return err
}

func webhookConfigurationObjectToSchemaElem(hookConfig *github.HookConfig) []interface{} {
configuration := []interface{}{
map[string]interface{}{
"url": hookConfig.URL,
"content_type": hookConfig.ContentType,
"insecure_ssl": func() bool {
return *hookConfig.InsecureSSL == "1"
}(),
"secret": hookConfig.Secret,
},
}

return configuration
}

func webhookConfigurationSchemaElemToObject(hookConfigSchemaElem []interface{}) *github.HookConfig {
configuration := &github.HookConfig{
URL: github.String(hookConfigSchemaElem[0].(map[string]interface{})["url"].(string)),
ContentType: github.String(hookConfigSchemaElem[0].(map[string]interface{})["content_type"].(string)),
Secret: github.String(hookConfigSchemaElem[0].(map[string]interface{})["secret"].(string)),
InsecureSSL: func() *string {
if hookConfigSchemaElem[0].(map[string]interface{})["insecure_ssl"].(bool) {
return github.String("1")
} else {
return github.String("0")
}
}(),
}

return configuration
}

0 comments on commit 26350a1

Please sign in to comment.