Skip to content

Adding a computed attribute for the repository owner to the github_repository #2675

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

Open
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion github/data_source_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ func dataSourceGithubRepository() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"name"},
ConflictsWith: []string{"name", "owner"},
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"full_name"},
},
"owner": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"full_name"},
Description: "Owner of the repository. If not provided, the owner specified in the provider configuration will be used.",
},
"description": {
Type: schema.TypeString,
Default: nil,
Expand Down Expand Up @@ -353,6 +360,9 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) er
if name, ok := d.GetOk("name"); ok {
repoName = name.(string)
}
if ownerName, ok := d.GetOk("owner"); ok {
owner = ownerName.(string)
}

if repoName == "" {
return fmt.Errorf("one of %q or %q has to be provided", "full_name", "name")
Expand Down
175 changes: 175 additions & 0 deletions github/data_source_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,179 @@ EOT
})

})

t.Run("queries a repository using owner and name", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-%s"
}

data "github_repository" "test" {
name = github_repository.test.name
owner = "%s"
}
`, randomID, testOrganization)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"data.github_repository.test", "owner",
testOrganization,
),
)

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: check,
},
},
})
}

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

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("validates conflicts between full_name, name, and owner", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-%[1]s"
vulnerability_alerts = true
}
`, randomID)

// Test invalid combinations
invalidConfigs := []string{
// full_name with name
fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-%[1]s"
vulnerability_alerts = true
}

data "github_repository" "test" {
full_name = "%[2]s/tf-acc-%[1]s"
name = "tf-acc-%[1]s"
}
`, randomID, testOrganization),
// full_name with owner
fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-%[1]s"
}

data "github_repository" "test" {
full_name = "%[2]s/tf-acc-%[1]s"
owner = "%[2]s"
}
`, randomID, testOrganization),
// full_name with both name and owner
fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-%[1]s"
}

data "github_repository" "test" {
full_name = "%[2]s/tf-acc-%[1]s"
name = "tf-acc-%[1]s"
owner = "%[2]s"
}
`, randomID, testOrganization),
}

// Test valid combinations
validConfigs := []string{
// Just full_name
fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-%[1]s"
}

data "github_repository" "test" {
full_name = "%[2]s/tf-acc-%[1]s"
}
`, randomID, testOrganization),
// Just name (uses provider owner)
fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-%[1]s"
}

data "github_repository" "test" {
name = "tf-acc-%[1]s"
}
`, randomID),
// name with owner
fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-%[1]s"
}

data "github_repository" "test" {
name = "tf-acc-%[1]s"
owner = "%[2]s"
}
`, randomID, testOrganization),
}

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
// Create the repository first
{
Config: config,
},
// Test that invalid configs fail
{
Config: invalidConfigs[0],
ExpectError: regexp.MustCompile("(?i)conflicts with"),
},
{
Config: invalidConfigs[1],
ExpectError: regexp.MustCompile("(?i)conflicts with"),
},
{
Config: invalidConfigs[2],
ExpectError: regexp.MustCompile("(?i)conflicts with"),
},
// Test that valid configs succeed
{
Config: validConfigs[0],
},
{
Config: validConfigs[1],
},
{
Config: validConfigs[2],
},
},
})
}

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)
})
})
}
6 changes: 6 additions & 0 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ func resourceGithubRepository() *schema.Resource {
Computed: true,
Description: "A string of the form 'orgname/reponame'.",
},
"owner": {
Type: schema.TypeString,
Computed: true,
Description: "The owner of the repository.",
},
"html_url": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -678,6 +683,7 @@ func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) erro
d.Set("topics", flattenStringList(repo.Topics))
d.Set("node_id", repo.GetNodeID())
d.Set("repo_id", repo.GetID())
d.Set("owner", repo.GetOwner().GetLogin())

// GitHub API doesn't respond following parameters when repository is archived
if !d.Get("archived").(bool) {
Expand Down
43 changes: 43 additions & 0 deletions github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,50 @@ func TestAccGithubRepositories(t *testing.T) {

})

t.Run("creates repository and returns owner field", func(t *testing.T) {
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-owner-%[1]s"
description = "Terraform acceptance tests %[1]s"
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"github_repository.test", "owner",
),
)

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: 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)
})
})

}

func TestAccGithubRepositoryPages(t *testing.T) {

randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ The following arguments are supported:

* `full_name` - (Optional) Full name of the repository (in `org/name` format).

* `owner` - (Optional) Owner of the repository. If not provided, the owner specified in the provider configuration will be used.

## Attributes Reference

* `node_id` - the Node ID of the repository.
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ The following additional attributes are exported:

* `full_name` - A string of the form "orgname/reponame".

* `owner` - The owner of the repository.

* `html_url` - URL to the repository on the web.

* `ssh_clone_url` - URL that can be provided to `git clone` to clone the repository via SSH.
Expand Down