Skip to content
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 @@ -21,14 +21,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 @@ -354,6 +361,9 @@ func dataSourceGithubRepositoryRead(d *schema.ResourceData, meta any) error {
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 @@ -428,4 +428,179 @@ EOT
testCase(t, organization)
})
})

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 @@ -351,6 +351,11 @@
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 @@ -761,6 +766,7 @@
_ = d.Set("topics", flattenStringList(repo.Topics))
_ = d.Set("node_id", repo.GetNodeID())
_ = d.Set("repo_id", repo.GetID())
_ = d.Set("owner", repo.GetOwner().GetLogin())

Check failure on line 769 in github/resource_github_repository.go

View workflow job for this annotation

GitHub Actions / Continuous Integration

File is not properly formatted (gofmt)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution @AlanCitrix!

Minor fix: please run go fmt . to format your files.


// 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 @@ -956,6 +956,49 @@
testCase(t, organization)
})
})

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

Check failure on line 1001 in github/resource_github_repository_test.go

View workflow job for this annotation

GitHub Actions / Continuous Integration

File is not properly formatted (gofumpt)
}

func TestAccGithubRepositoryPages(t *testing.T) {
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `owner` - (Optional) Owner of the repository. If not provided, the owner specified in the provider configuration will be used.
* `owner` - (Optional) Owner of the repository. If not provided, the provider's default owner is used.

This is the phrase used in other resources, since no owner is specified in the provider's configuration.


## 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 @@ -200,6 +200,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
Loading