Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GitHub Code Scanning Resource and Data Source #2036

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 73 additions & 0 deletions github/data_source_github_repository_code_scanning.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package github

import (
"context"

"github.com/google/go-github/v55/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceGithubRepositoryCodeScanning() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubRepositoryCodeScanningRead,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
},
"owner": {
Type: schema.TypeString,
Required: true,
},
"languages": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"query_suite": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceGithubRepositoryCodeScanningRead(d *schema.ResourceData, meta interface{}) error {
repository := d.Get("repository").(string)
owner := meta.(*Owner).name

client := meta.(*Owner).v3client
ctx := context.Background()

config := &github.DefaultSetupConfiguration{}
config, _, err := client.CodeScanning.GetDefaultSetupConfiguration(
ctx,
owner,
repository,
)
if err != nil {
return err
}

timeString := ""

if config.UpdatedAt != nil {
timeString = config.UpdatedAt.String()
}

d.SetId(buildTwoPartID(owner, repository))
d.Set("languages", config.Languages)
d.Set("query_suite", config.GetQuerySuite())
d.Set("state", config.GetState())
d.Set("updated_at", timeString)

return nil
}
188 changes: 188 additions & 0 deletions github/data_source_github_repository_code_scanning_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package github

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccGithubRepositoryCodeScanningDataSource(t *testing.T) {
owner := os.Getenv("GITHUB_ORGANIZATION")
if owner == "" {
t.FailNow()
}

randomId := acctest.RandStringFromCharSet(6, acctest.CharSetAlphaNum)
t.Run("manages the code scanning setup for a repository", func(t *testing.T) {
config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-cs-%s"
auto_init = true
}

resource "github_repository_file" "test_py" {
repository = github_repository.test.name
branch = "main"
file = "main.py"
content = <<-EOT
if __name__ == "__main__":
print ("This is a test")
EOT
commit_message = "Managed by Terraform"
commit_author = "Terraform User"
commit_email = "terraform@example.com"
overwrite_on_create = true
}

resource "github_repository_code_scanning" "test" {
repository = github_repository.test.name
owner = "%s"

state = "configured"
query_suite = "default"

depends_on = ["github_repository_file.test_py"]
}
`, randomId, owner)

config2 := config + fmt.Sprintf(`
data "github_repository_code_scanning" "test" {
repository = github_repository.test.name
owner = "%s"
}
`, owner)

const resourceName = "data.github_repository_code_scanning.test"
check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "languages.#", "1"),
resource.TestCheckResourceAttr(resourceName, "languages.0", "python"),
resource.TestCheckResourceAttr(resourceName, "state", "configured"),
resource.TestCheckResourceAttr(resourceName, "query_suite", "default"),
)

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: resource.ComposeTestCheckFunc(),
},
{
Config: config2,
Check: check,
},
},
})
}

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("manages the code scanning setup for a repository with multiple languages", func(t *testing.T) {
config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-cs-%s"
auto_init = true
}

resource "github_repository_file" "test_py" {
repository = github_repository.test.name
branch = "main"
file = "main.py"
content = <<-EOT
if __name__ == "__main__":
print ("This is a test")
EOT
commit_message = "Managed by Terraform"
commit_author = "Terraform User"
commit_email = "terraform@example.com"
overwrite_on_create = true
}

resource "github_repository_file" "test_js" {
repository = github_repository.test.name
branch = "main"
file = "main.js"
content = <<-EOT
function main() {
console.log("This is a test");
}
EOT
commit_message = "Managed by Terraform"
commit_author = "Terraform User"
commit_email = "terraform@example.com"
overwrite_on_create = true
}

resource "github_repository_code_scanning" "test" {
repository = github_repository.test.name
owner = "%s"

state = "configured"
query_suite = "extended"

depends_on = ["github_repository_file.test_js", "github_repository_file.test_py"]
}
`, randomId, owner)

config2 := config + fmt.Sprintf(`
data "github_repository_code_scanning" "test" {
repository = github_repository.test.name
owner = "%s"
}
`, owner)

const resourceName = "data.github_repository_code_scanning.test"
check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "languages.#", "2"),
resource.TestCheckResourceAttr(resourceName, "languages.0", "python"),
resource.TestCheckResourceAttr(resourceName, "languages.1", "javascript-typescript"),
resource.TestCheckResourceAttr(resourceName, "state", "configured"),
resource.TestCheckResourceAttr(resourceName, "query_suite", "extended"),
)

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: resource.ComposeTestCheckFunc(),
},
{
Config: config2,
Check: check,
},
},
})
}

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)
})
})
}
2 changes: 2 additions & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func Provider() terraform.ResourceProvider {
"github_repository": resourceGithubRepository(),
"github_repository_autolink_reference": resourceGithubRepositoryAutolinkReference(),
"github_repository_dependabot_security_updates": resourceGithubRepositoryDependabotSecurityUpdates(),
"github_repository_code_scanning": resourceGithubRepositoryCodeScanning(),
"github_repository_collaborator": resourceGithubRepositoryCollaborator(),
"github_repository_collaborators": resourceGithubRepositoryCollaborators(),
"github_repository_deploy_key": resourceGithubRepositoryDeployKey(),
Expand Down Expand Up @@ -213,6 +214,7 @@ func Provider() terraform.ResourceProvider {
"github_repository": dataSourceGithubRepository(),
"github_repository_autolink_references": dataSourceGithubRepositoryAutolinkReferences(),
"github_repository_branches": dataSourceGithubRepositoryBranches(),
"github_repository_code_scanning": dataSourceGithubRepositoryCodeScanning(),
"github_repository_environments": dataSourceGithubRepositoryEnvironments(),
"github_repository_deploy_keys": dataSourceGithubRepositoryDeployKeys(),
"github_repository_deployment_branch_policies": dataSourceGithubRepositoryDeploymentBranchPolicies(),
Expand Down
2 changes: 1 addition & 1 deletion github/resource_github_issue_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"strings"

"github.com/google/go-github/v52/github"
"github.com/google/go-github/v55/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

Expand Down