Skip to content

Commit

Permalink
Add Vulnerability Alert Fixes & Testing (integrations#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Udit authored Sep 22, 2020
1 parent 10089b8 commit 68ee965
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 3 deletions.
6 changes: 3 additions & 3 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er
createVulnerabilityAlerts = client.Repositories.DisableVulnerabilityAlerts
}
if createVulnerabilityAlerts != nil {
_, err = createVulnerabilityAlerts(ctx, orgName, repoName)
_, err := createVulnerabilityAlerts(ctx, owner, repoName)
if err != nil {
return err
}
Expand Down Expand Up @@ -376,7 +376,7 @@ func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) erro
d.Set("template", []interface{}{})
}

vulnerabilityAlerts, _, err := client.Repositories.GetVulnerabilityAlerts(ctx, orgName, repoName)
vulnerabilityAlerts, _, err := client.Repositories.GetVulnerabilityAlerts(ctx, owner, repoName)
if err != nil {
return fmt.Errorf("Error reading repository vulnerability alerts: %v", err)
}
Expand Down Expand Up @@ -446,7 +446,7 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
updateVulnerabilityAlerts = client.Repositories.EnableVulnerabilityAlerts
}

_, err = updateVulnerabilityAlerts(ctx, orgName, repoName)
_, err = updateVulnerabilityAlerts(ctx, owner, repoName)
if err != nil {
return err
}
Expand Down
124 changes: 124 additions & 0 deletions github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,130 @@ func TestAccGithubRepositories(t *testing.T) {

})

t.Run("configures vulnerability alerts", func(t *testing.T) {

t.Run("for a public repository", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-pub-vuln-%s"
visibility = "public"
}
`, randomID)

checks := map[string]resource.TestCheckFunc{
"before": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "vulnerability_alerts",
"false",
),
),
"after": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "vulnerability_alerts",
"true",
),
resource.TestCheckResourceAttr(
"github_repository.test", "visibility",
"public",
),
),
}

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: checks["before"],
},
{
Config: strings.Replace(config,
`}`,
"vulnerability_alerts = true\n}", 1),
Check: checks["after"],
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})
})

t.Run("for a private repository", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-prv-vuln-%s"
visibility = "private"
}
`, randomID)

checks := map[string]resource.TestCheckFunc{
"before": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "vulnerability_alerts",
"false",
),
),
"after": resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository.test", "vulnerability_alerts",
"true",
),
resource.TestCheckResourceAttr(
"github_repository.test", "visibility",
"private",
),
),
}

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: checks["before"],
},
{
Config: strings.Replace(config,
`}`,
"vulnerability_alerts = true\n}", 1),
Check: checks["after"],
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})
})

})

}

func testSweepRepositories(region string) error {
Expand Down

0 comments on commit 68ee965

Please sign in to comment.