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 a data source for refs. #1084

Merged
merged 2 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
67 changes: 67 additions & 0 deletions github/data_source_github_ref.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package github

import (
"context"
"log"
"net/http"

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

func dataSourceGithubRef() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubRefRead,

Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"branch": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"etag": {
Type: schema.TypeString,
Computed: true,
},
"ref": {
Type: schema.TypeString,
Computed: true,
},
"sha": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceGithubRefRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
orgName := meta.(*Owner).name
repoName := d.Get("repository").(string)
ref := d.Get("ref").(string)

refData, resp, err := client.Git.GetRef(context.TODO(), orgName, repoName, ref)
if err != nil {
if err, ok := err.(*github.ErrorResponse); ok {
if err.Response.StatusCode == http.StatusNotFound {
log.Printf("[DEBUG] Missing GitHub ref %s/%s (%s)", orgName, repoName, ref)
d.SetId("")
return nil
}
}
return err
}

d.SetId(buildTwoPartID(repoName, ref))
d.Set("etag", resp.Header.Get("ETag"))
d.Set("ref", *refData.Ref)
d.Set("sha", *refData.Object.SHA)

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

import (
"fmt"
"regexp"
"testing"

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

func TestAccGithubRefDataSource(t *testing.T) {

randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

t.Run("queries an existing ref without error", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%[1]s"
auto_init = true
}
data "github_ref" "test" {
repository = github_repository.test.id
ref = "/heads/main"
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr(
"data.github_ref.test", "id", regexp.MustCompile(randomID),
),
)

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

})

t.Run("queries an invalid ref without error", func(t *testing.T) {

config := fmt.Sprintf(`
resource "github_repository" "test" {
name = "tf-acc-test-%[1]s"
auto_init = true
}
data "github_ref" "test" {
repository = github_repository.test.id
ref = "heads/xxxxxx"
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr(
"data.github_ref.test", "id",
),
)

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

})
}
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func Provider() terraform.ResourceProvider {
"github_organization": dataSourceGithubOrganization(),
"github_organization_team_sync_groups": dataSourceGithubOrganizationTeamSyncGroups(),
"github_organization_teams": dataSourceGithubOrganizationTeams(),
"github_ref": dataSourceGithubRef(),
"github_release": dataSourceGithubRelease(),
"github_repositories": dataSourceGithubRepositories(),
"github_repository": dataSourceGithubRepository(),
Expand Down
35 changes: 35 additions & 0 deletions website/docs/d/ref.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
layout: "github"
page_title: "GitHub: github_ref"
description: |-
Get information about a repository ref.
---

# github\ref

Use this data source to retrieve information about a repository ref.

## Example Usage

```hcl
data "github_ref" "development" {
repository = "example"
ref = "heads/development"
}
```

## Argument Reference

The following arguments are supported:

* `repository` - (Required) The GitHub repository name.

* `ref` - (Required) The repository ref to look up. Must be formatted `heads/<ref>` for branches, and `tags/<ref>` for tags.

## Attribute Reference

The following additional attributes are exported:

* `etag` - An etag representing the ref.

* `sha` - A string storing the reference's `HEAD` commit's SHA1.
3 changes: 3 additions & 0 deletions website/github.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<li>
<a href="/docs/providers/github/d/organization_teams.html">github_organization_teams</a>
</li>
<li>
<a href="/docs/providers/github/d/ref.html">github_ref</a>
</li>
<li>
<a href="/docs/providers/github/d/release.html">github_release</a>
</li>
Expand Down