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

Fixes accidental deletion of Repository resource from state #1750

Merged
Merged
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
27 changes: 27 additions & 0 deletions github/resource_github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,9 +593,16 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er

func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client

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

// When the user has not authenticated the provider, AnonymousHTTPClient is used, therefore owner == "". In this
// case lookup the owner in the data, and use that, if present.
if explicitOwner, _, ok := resourceGithubParseFullName(d); ok && owner == "" {
owner = explicitOwner
}

ctx := context.WithValue(context.Background(), ctxId, d.Id())
if !d.IsNewResource() {
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
Expand Down Expand Up @@ -942,3 +949,23 @@ func flattenSecurityAndAnalysis(securityAndAnalysis *github.SecurityAndAnalysis)

return []interface{}{securityAndAnalysisMap}
}

// In case full_name can be determined from the data, parses it into an org and repo name proper. For example,
// resourceGithubParseFullName will return "myorg", "myrepo", true when full_name is "myorg/myrepo".
func resourceGithubParseFullName(resourceDataLike interface {
GetOk(string) (interface{}, bool)
}) (string, string, bool) {
x, ok := resourceDataLike.GetOk("full_name")
if !ok {
return "", "", false
}
s, ok := x.(string)
if !ok || s == "" {
return "", "", false
}
parts := strings.Split(s, "/")
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return "", "", false
}
return parts[0], parts[1], true
}
19 changes: 19 additions & 0 deletions github/resource_github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

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

func TestAccGithubRepositories(t *testing.T) {
Expand Down Expand Up @@ -1407,3 +1408,21 @@ func reconfigureVisibility(config, visibility string) string {
)
return newConfig
}

type resourceDataLike map[string]interface{}

func (d resourceDataLike) GetOk(key string) (interface{}, bool) {
v, ok := d[key]
return v, ok
}

func TestResourceGithubParseFullName(t *testing.T) {
repo, org, ok := resourceGithubParseFullName(resourceDataLike(map[string]interface{}{"full_name": "myrepo/myorg"}))
assert.True(t, ok)
assert.Equal(t, "myrepo", repo)
assert.Equal(t, "myorg", org)
_, _, ok = resourceGithubParseFullName(resourceDataLike(map[string]interface{}{}))
assert.False(t, ok)
_, _, ok = resourceGithubParseFullName(resourceDataLike(map[string]interface{}{"full_name": "malformed"}))
assert.False(t, ok)
}