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 owner attribute for repository resource #1832

Open
wants to merge 6 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func resourceGithubRepository() *schema.Resource {
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[-a-zA-Z0-9_.]{1,100}$`), "must include only alphanumeric characters, underscores or hyphens and consist of 100 characters or less"),
Description: "The name of the repository.",
},
"owner": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The GitHub organization or user the repository is owned by. Defaults to the owner/organization specified in the provider configuration. If neither are given, this field defaults to the authenticated user.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -403,6 +409,14 @@ func resourceGithubRepository() *schema.Resource {
}
}

func calculateOwner(d *schema.ResourceData, meta interface{}) string {
if value, ok := d.GetOk("owner"); ok {
return value.(string)
}

return meta.(*Owner).name
}

func calculateVisibility(d *schema.ResourceData) string {

if value, ok := d.GetOk("visibility"); ok {
Expand Down Expand Up @@ -523,7 +537,7 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er
}

repoReq := resourceGithubRepositoryObject(d)
owner := meta.(*Owner).name
owner := calculateOwner(d, meta)

repoName := repoReq.GetName()
ctx := context.Background()
Expand Down Expand Up @@ -582,7 +596,9 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er
// Create without a repository template
var repo *github.Repository
var err error
if meta.(*Owner).IsOrganization {

_, explicitOwnerOk := d.GetOk("owner")
if meta.(*Owner).IsOrganization || explicitOwnerOk {
repo, _, err = client.Repositories.Create(ctx, owner, repoReq)
} else {
// Create repository within authenticated user's account
Expand Down Expand Up @@ -616,7 +632,7 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er
func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client

owner := meta.(*Owner).name
owner := calculateOwner(d, meta)
repoName := d.Id()

// When the user has not authenticated the provider, AnonymousHTTPClient is used, therefore owner == "". In this
Expand Down Expand Up @@ -648,6 +664,7 @@ func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) erro

d.Set("etag", resp.Header.Get("ETag"))
d.Set("name", repoName)
d.Set("owner", repo.GetOwner().GetLogin())
d.Set("description", repo.GetDescription())
d.Set("primary_language", repo.GetLanguage())
d.Set("homepage_url", repo.GetHomepage())
Expand Down Expand Up @@ -743,9 +760,8 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
}

repoName := d.Id()
owner := meta.(*Owner).name
owner := calculateOwner(d, meta)
ctx := context.WithValue(context.Background(), ctxId, d.Id())

repo, _, err := client.Repositories.Edit(ctx, owner, repoName, repoReq)
if err != nil {
return err
Expand Down Expand Up @@ -839,7 +855,7 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er
func resourceGithubRepositoryDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
repoName := d.Id()
owner := meta.(*Owner).name
owner := calculateOwner(d, meta)
ctx := context.WithValue(context.Background(), ctxId, d.Id())

archiveOnDestroy := d.Get("archive_on_destroy").(bool)
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/repository.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ The following arguments are supported:

* `name` - (Required) The name of the repository.

* `owner` - (Optional) The GitHub organization or user the repository is owned by.
Defaults to the owner/organization specified in the provider configuration.
If neither are given, this field defaults to the authenticated user.
* `description` - (Optional) A description of the repository.

* `homepage_url` - (Optional) URL of a page describing the project.
Expand Down