Skip to content

Commit

Permalink
make github_ref respect owner (#1651)
Browse files Browse the repository at this point in the history
* make `github_ref` respect `owner` (instead of deriving it from the token)

* `id` is also exported and should be mentioned

* If no owner specified, use owner of token

---------

Co-authored-by: Keegan Campbell <me@kfcampbell.com>
  • Loading branch information
ksatirli and kfcampbell committed Apr 18, 2023
1 parent 89aa2c5 commit e9bb788
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This project is used to read and write to/from GitHub (repositories, teams, file

## Usage

Detailed documentation for the GitHub provider can be found [here](https://www.terraform.io/docs/providers/github/index.html).
Detailed documentation for the GitHub provider can be found [here](https://registry.terraform.io/providers/integrations/github).

## Contributing

Expand Down
13 changes: 10 additions & 3 deletions github/data_source_github_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func dataSourceGithubRef() *schema.Resource {
Required: true,
ForceNew: true,
},
"owner": {
Type: schema.TypeString,
Optional: true,
},
"etag": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -38,15 +42,18 @@ func dataSourceGithubRef() *schema.Resource {

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

refData, resp, err := client.Git.GetRef(context.TODO(), orgName, repoName, ref)
refData, resp, err := client.Git.GetRef(context.TODO(), owner, 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)
log.Printf("[DEBUG] Missing GitHub ref %s/%s (%s)", owner, repoName, ref)
d.SetId("")
return nil
}
Expand Down
7 changes: 6 additions & 1 deletion website/docs/d/ref.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ Use this data source to retrieve information about a repository ref.

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

## Argument Reference

The following arguments are supported:

* `owner` - (Required) Owner of the repository.

* `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.
Expand All @@ -32,4 +35,6 @@ The following additional attributes are exported:

* `etag` - An etag representing the ref.

* `id` - A string storing a reference to the repository name and ref.

* `sha` - A string storing the reference's `HEAD` commit's SHA1.

0 comments on commit e9bb788

Please sign in to comment.