From 611ed13343384770ffdebaded1b17245f661deaf Mon Sep 17 00:00:00 2001 From: Steve Hipwell Date: Thu, 5 Dec 2024 17:17:10 +0000 Subject: [PATCH] fix: Fixed repository resource churn Signed-off-by: Steve Hipwell --- github/resource_github_repository.go | 45 +++++++++++++++++++--------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index f28cc4c6e8..c6ef6e32f1 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -320,6 +320,7 @@ func resourceGithubRepository() *schema.Resource { "vulnerability_alerts": { Type: schema.TypeBool, Optional: true, + Computed: true, Description: "Set to 'true' to enable security alerts for vulnerable dependencies. Enabling requires alerts to be enabled on the owner level. (Note for importing: GitHub enables the alerts on public repos but disables them on private repos by default). Note that vulnerability alerts have not been successfully tested on any GitHub Enterprise instance and may be unavailable in those settings.", }, "ignore_vulnerability_alerts_during_read": { @@ -412,7 +413,6 @@ func resourceGithubRepository() *schema.Resource { } func calculateVisibility(d *schema.ResourceData) string { - if value, ok := d.GetOk("visibility"); ok { return value.(string) } @@ -619,6 +619,11 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er } } + err := updateVulnerabilityAlerts(d, client, ctx, owner, repoName) + if err != nil { + return err + } + return resourceGithubRepositoryUpdate(d, meta) } @@ -817,12 +822,7 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er } if d.HasChange("vulnerability_alerts") { - updateVulnerabilityAlerts := client.Repositories.DisableVulnerabilityAlerts - if vulnerabilityAlerts, ok := d.GetOk("vulnerability_alerts"); ok && vulnerabilityAlerts.(bool) { - updateVulnerabilityAlerts = client.Repositories.EnableVulnerabilityAlerts - } - - _, err = updateVulnerabilityAlerts(ctx, owner, repoName) + err = updateVulnerabilityAlerts(d, client, ctx, owner, repoName) if err != nil { return err } @@ -957,13 +957,19 @@ func flattenPages(pages *github.Pages) []interface{} { return []interface{}{} } - sourceMap := make(map[string]interface{}) - sourceMap["branch"] = pages.GetSource().GetBranch() - sourceMap["path"] = pages.GetSource().GetPath() - pagesMap := make(map[string]interface{}) - pagesMap["source"] = []interface{}{sourceMap} - pagesMap["build_type"] = pages.GetBuildType() + buildType := pages.GetBuildType() + pagesMap["build_type"] = buildType + + if buildType == "legacy" { + sourceMap := make(map[string]interface{}) + sourceMap["branch"] = pages.GetSource().GetBranch() + sourceMap["path"] = pages.GetSource().GetPath() + pagesMap["source"] = []interface{}{sourceMap} + } else { + pagesMap["source"] = nil + } + pagesMap["url"] = pages.GetURL() pagesMap["status"] = pages.GetStatus() pagesMap["cname"] = pages.GetCNAME() @@ -1038,7 +1044,8 @@ func flattenSecurityAndAnalysis(securityAndAnalysis *github.SecurityAndAnalysis) // resourceGithubParseFullName will return "myorg", "myrepo", true when full_name is "myorg/myrepo". func resourceGithubParseFullName(resourceDataLike interface { GetOk(string) (interface{}, bool) -}) (string, string, bool) { +}, +) (string, string, bool) { x, ok := resourceDataLike.GetOk("full_name") if !ok { return "", "", false @@ -1062,3 +1069,13 @@ func customDiffFunction(_ context.Context, diff *schema.ResourceDiff, v interfac } return nil } + +func updateVulnerabilityAlerts(d *schema.ResourceData, client *github.Client, ctx context.Context, owner, repoName string) error { + updateVulnerabilityAlerts := client.Repositories.DisableVulnerabilityAlerts + if vulnerabilityAlerts, ok := d.GetOk("vulnerability_alerts"); ok && vulnerabilityAlerts.(bool) { + updateVulnerabilityAlerts = client.Repositories.EnableVulnerabilityAlerts + } + + _, err := updateVulnerabilityAlerts(ctx, owner, repoName) + return err +}