-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing
github_repository_file
owner/org handling (#1494)
* Fixing `github_repository_file` owner handling, if `full_name` is passed, split it into `owner` and `repo`, use user as an `owner` otherwise Signed-off-by: Valery Masiutsin <[email protected]> * Update github/data_source_github_repository_file_test.go Co-authored-by: Keegan Campbell <[email protected]> * Update website/docs/d/repository_file.html.markdown Co-authored-by: Keegan Campbell <[email protected]> * Small docs fixup * Take into account @vmasutin's suggestion Signed-off-by: Valery Masiutsin <[email protected]> Co-authored-by: Keegan Campbell <[email protected]>
- Loading branch information
1 parent
8b93e4e
commit 592edcc
Showing
3 changed files
with
210 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
package github | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/google/go-github/v49/github" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func TestAccGithubRepositoryFileDataSource(t *testing.T) { | ||
|
||
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) | ||
|
||
t.Run("read files", func(t *testing.T) { | ||
|
||
config := fmt.Sprintf(` | ||
resource "github_repository" "test" { | ||
|
@@ -85,6 +91,191 @@ func TestAccGithubRepositoryFileDataSource(t *testing.T) { | |
t.Run("with an organization account", func(t *testing.T) { | ||
testCase(t, organization) | ||
}) | ||
}) | ||
} | ||
|
||
func TestDataSourceGithubRepositoryFileRead(t *testing.T) { | ||
// helper function to simplify marshalling. | ||
marshal := func(t *testing.T, msg interface{}) string { | ||
data, err := json.MarshalIndent(msg, "", " ") | ||
if err != nil { | ||
t.Fatalf("cant encode to json: %v", err) | ||
} | ||
|
||
return string(data) | ||
} | ||
|
||
sha := "some-test-sha" | ||
committerName := "some-test-user" | ||
committerEmail := "[email protected]" | ||
commitMessage := "test commit message" | ||
|
||
enc := "base64" | ||
fileContent := "here-goes-content-of-our-glorious-config.json" | ||
b64FileContent := base64.StdEncoding.EncodeToString([]byte(fileContent)) | ||
|
||
fileName := "test-file.json" | ||
branch := "main" | ||
|
||
// setting up some org/owner info | ||
owner := "test-owner" | ||
org := "test-org" | ||
repo := "test-repo" | ||
|
||
// preparing mashalled objects | ||
branchRespBody := marshal(t, &github.Branch{Name: &branch}) | ||
repoContentRespBody := marshal(t, &github.RepositoryContent{ | ||
Encoding: &enc, | ||
Content: &b64FileContent, | ||
SHA: &sha, | ||
}) | ||
repoCommitRespBody := marshal(t, &github.RepositoryCommit{ | ||
SHA: &sha, | ||
Committer: &github.User{ | ||
Name: &committerName, | ||
Email: &committerEmail, | ||
}, | ||
Commit: &github.Commit{ | ||
Message: &commitMessage, | ||
}, | ||
}) | ||
|
||
t.Run("extracting org and repo if full_name is passed", func(t *testing.T) { | ||
// test setup | ||
repositoryFullName := fmt.Sprintf("%s/%s", org, repo) | ||
expectedID := fmt.Sprintf("%s/%s", repo, fileName) | ||
expectedRepo := "test-repo" | ||
|
||
ts := githubApiMock([]*mockResponse{ | ||
{ | ||
ExpectedUri: fmt.Sprintf("/repos/%s/%s/branches/%s", org, repo, branch), | ||
ResponseBody: branchRespBody, | ||
StatusCode: http.StatusOK, | ||
}, | ||
{ | ||
ExpectedUri: fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", org, repo, fileName, branch), | ||
ResponseBody: repoContentRespBody, | ||
StatusCode: http.StatusOK, | ||
}, | ||
{ | ||
ExpectedUri: fmt.Sprintf("/repos/%s/%s/commits/%s", org, repo, sha), | ||
ResponseBody: repoCommitRespBody, | ||
StatusCode: http.StatusOK, | ||
}, | ||
{ | ||
ExpectedUri: fmt.Sprintf("/repos/%s/%s/commits", org, repo), | ||
ResponseBody: repoCommitRespBody, | ||
StatusCode: http.StatusOK, | ||
}, | ||
}) | ||
defer ts.Close() | ||
|
||
httpCl := http.DefaultClient | ||
httpCl.Transport = http.DefaultTransport | ||
|
||
client := github.NewClient(httpCl) | ||
u, _ := url.Parse(ts.URL + "/") | ||
client.BaseURL = u | ||
|
||
meta := &Owner{ | ||
name: owner, | ||
v3client: client, | ||
} | ||
|
||
testSchema := map[string]*schema.Schema{ | ||
"repository": {Type: schema.TypeString}, | ||
"file": {Type: schema.TypeString}, | ||
"branch": {Type: schema.TypeString}, | ||
"commit_sha": {Type: schema.TypeString}, | ||
"content": {Type: schema.TypeString}, | ||
"id": {Type: schema.TypeString}, | ||
} | ||
|
||
schema := schema.TestResourceDataRaw(t, testSchema, map[string]interface{}{ | ||
"repository": repositoryFullName, | ||
"file": fileName, | ||
"branch": branch, | ||
"commit_sha": sha, | ||
"content": "", | ||
"id": "", | ||
}) | ||
|
||
// actual call | ||
err := dataSourceGithubRepositoryFileRead(schema, meta) | ||
|
||
// assertions | ||
assert.Nil(t, err) | ||
assert.Equal(t, expectedRepo, schema.Get("repository")) | ||
assert.Equal(t, fileContent, schema.Get("content")) | ||
assert.Equal(t, expectedID, schema.Get("id")) | ||
}) | ||
t.Run("using user as owner if just name is passed", func(t *testing.T) { | ||
// test setup | ||
repositoryFullName := repo | ||
expectedID := fmt.Sprintf("%s/%s", repo, fileName) | ||
expectedRepo := "test-repo" | ||
|
||
ts := githubApiMock([]*mockResponse{ | ||
{ | ||
ExpectedUri: fmt.Sprintf("/repos/%s/%s/branches/%s", owner, repo, branch), | ||
ResponseBody: branchRespBody, | ||
StatusCode: http.StatusOK, | ||
}, | ||
{ | ||
ExpectedUri: fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", owner, repo, fileName, branch), | ||
ResponseBody: repoContentRespBody, | ||
StatusCode: http.StatusOK, | ||
}, | ||
{ | ||
ExpectedUri: fmt.Sprintf("/repos/%s/%s/commits/%s", owner, repo, sha), | ||
ResponseBody: repoCommitRespBody, | ||
StatusCode: http.StatusOK, | ||
}, | ||
{ | ||
ExpectedUri: fmt.Sprintf("/repos/%s/%s/commits", owner, repo), | ||
ResponseBody: repoCommitRespBody, | ||
StatusCode: http.StatusOK, | ||
}, | ||
}) | ||
defer ts.Close() | ||
|
||
httpCl := http.DefaultClient | ||
httpCl.Transport = http.DefaultTransport | ||
|
||
client := github.NewClient(httpCl) | ||
u, _ := url.Parse(ts.URL + "/") | ||
client.BaseURL = u | ||
|
||
meta := &Owner{ | ||
name: owner, | ||
v3client: client, | ||
} | ||
|
||
testSchema := map[string]*schema.Schema{ | ||
"repository": {Type: schema.TypeString}, | ||
"file": {Type: schema.TypeString}, | ||
"branch": {Type: schema.TypeString}, | ||
"commit_sha": {Type: schema.TypeString}, | ||
"content": {Type: schema.TypeString}, | ||
"id": {Type: schema.TypeString}, | ||
} | ||
|
||
schema := schema.TestResourceDataRaw(t, testSchema, map[string]interface{}{ | ||
"repository": repositoryFullName, | ||
"file": fileName, | ||
"branch": branch, | ||
"commit_sha": sha, | ||
"content": "", | ||
"id": "", | ||
}) | ||
|
||
// actual call | ||
err := dataSourceGithubRepositoryFileRead(schema, meta) | ||
|
||
// assertions | ||
assert.Nil(t, err) | ||
assert.Equal(t, expectedRepo, schema.Get("repository")) | ||
assert.Equal(t, fileContent, schema.Get("content")) | ||
assert.Equal(t, expectedID, schema.Get("id")) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters