Skip to content

Commit

Permalink
fix: Fixed repository resource churn
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Hipwell <[email protected]>
  • Loading branch information
stevehipwell committed Dec 5, 2024
1 parent 1c11053 commit 9d729e8
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -412,7 +413,6 @@ func resourceGithubRepository() *schema.Resource {
}

func calculateVisibility(d *schema.ResourceData) string {

if value, ok := d.GetOk("visibility"); ok {
return value.(string)
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -817,15 +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)
if err != nil {
return err
}
err = updateVulnerabilityAlerts(d, client, ctx, owner, repoName)

Check failure on line 825 in github/resource_github_repository.go

View workflow job for this annotation

GitHub Actions / ci

SA4006: this value of `err` is never used (staticcheck)
}

if d.HasChange("visibility") {
Expand Down Expand Up @@ -957,13 +954,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()
Expand Down Expand Up @@ -1038,7 +1041,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
Expand All @@ -1062,3 +1066,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
}

0 comments on commit 9d729e8

Please sign in to comment.