Skip to content

Commit fe26d26

Browse files
authored
fix: Correct forking and vulnerability alert logic (#3127)
Signed-off-by: Steve Hipwell <[email protected]>
1 parent 5b3893c commit fe26d26

4 files changed

Lines changed: 328 additions & 438 deletions

File tree

github/data_source_github_organization_role_users_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010

1111
func TestAccDataSourceGithubOrganizationRoleUsers(t *testing.T) {
1212
t.Run("get the organization role users without error", func(t *testing.T) {
13+
if testAccConf.testOrgUser == "" {
14+
t.Skip("Skipping test because no organization user has been configured")
15+
}
16+
1317
roleId := 8134
1418
config := fmt.Sprintf(`
1519
resource "github_organization_role_user" "test" {
@@ -44,6 +48,10 @@ func TestAccDataSourceGithubOrganizationRoleUsers(t *testing.T) {
4448
})
4549

4650
t.Run("get indirect organization role users without error", func(t *testing.T) {
51+
if testAccConf.testOrgUser == "" {
52+
t.Skip("Skipping test because no organization user has been configured")
53+
}
54+
4755
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
4856
teamName := fmt.Sprintf("%steam-%s", testResourcePrefix, randomID)
4957
roleId := 8134

github/resource_github_repository.go

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ func resourceGithubRepository() *schema.Resource {
2323
UpdateContext: resourceGithubRepositoryUpdate,
2424
DeleteContext: resourceGithubRepositoryDelete,
2525
Importer: &schema.ResourceImporter{
26-
StateContext: func(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
27-
if err := d.Set("auto_init", false); err != nil {
28-
return nil, err
29-
}
30-
return []*schema.ResourceData{d}, nil
31-
},
26+
StateContext: resourceGithubRepositoryImport,
3227
},
3328

3429
SchemaVersion: 1,
@@ -409,6 +404,7 @@ func resourceGithubRepository() *schema.Resource {
409404
"ignore_vulnerability_alerts_during_read": {
410405
Type: schema.TypeBool,
411406
Optional: true,
407+
Default: false,
412408
Description: "Set to true to not call the vulnerability alerts endpoint so the resource can also be used without admin permissions during read.",
413409
},
414410
"full_name": {
@@ -634,7 +630,7 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository {
634630
}
635631

636632
// only configure allow forking if repository is not public
637-
if allowForking, ok := d.GetOk("allow_forking"); ok && visibility != "public" {
633+
if allowForking, ok := d.GetOkExists("allow_forking"); ok && visibility != "public" { //nolint:staticcheck,SA1019 // We sometimes need to use GetOkExists for booleans
638634
if val, ok := allowForking.(bool); ok {
639635
repository.AllowForking = github.Ptr(val)
640636
}
@@ -773,11 +769,6 @@ func resourceGithubRepositoryCreate(ctx context.Context, d *schema.ResourceData,
773769
}
774770
}
775771

776-
err := updateVulnerabilityAlerts(d, client, ctx, owner, repoName)
777-
if err != nil {
778-
return diag.FromErr(err)
779-
}
780-
781772
return resourceGithubRepositoryUpdate(ctx, d, meta)
782773
}
783774

@@ -1013,10 +1004,12 @@ func resourceGithubRepositoryUpdate(ctx context.Context, d *schema.ResourceData,
10131004
}
10141005
}
10151006

1016-
if d.HasChange("vulnerability_alerts") {
1017-
err = updateVulnerabilityAlerts(d, client, ctx, owner, repoName)
1018-
if err != nil {
1019-
return diag.FromErr(err)
1007+
if v, ok := d.GetOkExists("vulnerability_alerts"); ok { //nolint:staticcheck,SA1019 // We sometimes need to use GetOkExists for booleans
1008+
if val, ok := v.(bool); ok {
1009+
err := updateVulnerabilityAlerts(ctx, client, owner, repoName, val)
1010+
if err != nil {
1011+
return diag.FromErr(err)
1012+
}
10201013
}
10211014
}
10221015

@@ -1065,6 +1058,16 @@ func resourceGithubRepositoryDelete(ctx context.Context, d *schema.ResourceData,
10651058
return diag.FromErr(err)
10661059
}
10671060

1061+
func resourceGithubRepositoryImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
1062+
if err := d.Set("auto_init", false); err != nil {
1063+
return nil, err
1064+
}
1065+
if err := d.Set("ignore_vulnerability_alerts_during_read", true); err != nil {
1066+
return nil, err
1067+
}
1068+
return []*schema.ResourceData{d}, nil
1069+
}
1070+
10681071
func expandPages(input []any) *github.Pages {
10691072
if len(input) == 0 || input[0] == nil {
10701073
return nil
@@ -1240,23 +1243,17 @@ func resourceGithubParseFullName(resourceDataLike interface {
12401243
return parts[0], parts[1], true
12411244
}
12421245

1243-
func updateVulnerabilityAlerts(d *schema.ResourceData, client *github.Client, ctx context.Context, owner, repoName string) error {
1244-
updateVulnerabilityAlertsSDK := client.Repositories.DisableVulnerabilityAlerts
1245-
vulnerabilityAlerts, ok := d.GetOk("vulnerability_alerts")
1246+
func updateVulnerabilityAlerts(ctx context.Context, client *github.Client, owner, repoName string, state bool) error {
1247+
var err error
12461248

1247-
// Only if the vulnerability alerts are specifically set to true, enable them.
1248-
// Otherwise, disable them as GitHub defaults to enabled and we have not wanted to introduce a breaking change for this yet.
1249-
if ok && vulnerabilityAlerts.(bool) {
1250-
updateVulnerabilityAlertsSDK = client.Repositories.EnableVulnerabilityAlerts
1249+
if state {
1250+
_, err = client.Repositories.EnableVulnerabilityAlerts(ctx, owner, repoName)
1251+
} else {
1252+
_, err = client.Repositories.DisableVulnerabilityAlerts(ctx, owner, repoName)
12511253
}
1252-
1253-
resp, err := updateVulnerabilityAlertsSDK(ctx, owner, repoName)
12541254
if err != nil {
1255-
// Check if the error is because an Organization or Enterprise policy is preventing the change
1256-
// This is a temporary workaround while we extract Vulnerability Alerts into a separate resource.
1257-
if resp.StatusCode == http.StatusUnprocessableEntity && strings.Contains(err.Error(), "An enforced security configuration prevented modifying") && !ok {
1258-
return nil
1259-
}
1255+
return err
12601256
}
1261-
return err
1257+
1258+
return nil
12621259
}

0 commit comments

Comments
 (0)