From 9cecedc62ab3a74e701fef58626d39bfd777db61 Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Mon, 9 Oct 2023 21:29:30 -0600 Subject: [PATCH 1/5] #1930 skip file-specifc schema data for directory --- github/data_source_github_repository_file.go | 7 ++- ...data_source_github_repository_file_test.go | 62 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/github/data_source_github_repository_file.go b/github/data_source_github_repository_file.go index d2d8a4a59e..a5f32c198c 100644 --- a/github/data_source_github_repository_file.go +++ b/github/data_source_github_repository_file.go @@ -95,7 +95,7 @@ func dataSourceGithubRepositoryFileRead(d *schema.ResourceData, meta interface{} opts.Ref = branch.(string) } - fc, _, _, err := client.Repositories.GetContents(ctx, owner, repo, file, opts) + fc, dc, _, err := client.Repositories.GetContents(ctx, owner, repo, file, opts) if err != nil { if err, ok := err.(*github.ErrorResponse); ok { if err.Response.StatusCode == http.StatusNotFound { @@ -107,6 +107,11 @@ func dataSourceGithubRepositoryFileRead(d *schema.ResourceData, meta interface{} return err } + // If the repo is a directory, then there is nothing to do. + if dc != nil { + return nil + } + content, err := fc.GetContent() if err != nil { return err diff --git a/github/data_source_github_repository_file_test.go b/github/data_source_github_repository_file_test.go index 8fffb1fbfe..e3140f833d 100644 --- a/github/data_source_github_repository_file_test.go +++ b/github/data_source_github_repository_file_test.go @@ -226,6 +226,7 @@ func TestDataSourceGithubRepositoryFileRead(t *testing.T) { SHA: &sha, URL: &apiUrl, }) + repoCommit := &github.RepositoryCommit{ SHA: &sha, Committer: &github.User{ @@ -420,4 +421,65 @@ func TestDataSourceGithubRepositoryFileRead(t *testing.T) { }) }) + + repoContentDirectoryRespBody := marshal(t, []github.RepositoryContent{ + { + Encoding: &enc, + Content: &b64FileContent, + SHA: &sha, + URL: &apiUrl, + }, + }) + + t.Run("extract nothing if the path is for a directory", func(t *testing.T) { + // test setup + repositoryFullName := fmt.Sprintf("%s/%s", org, repo) + + ts := githubApiMock([]*mockResponse{ + { + ExpectedUri: fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", org, repo, fileName, branch), + ResponseBody: repoContentDirectoryRespBody, + 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, "", schema.Get("content")) + assert.Equal(t, "", schema.Get("id")) + }) } From 12447175e753e8519f243c0475a803095267f68c Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Mon, 9 Oct 2023 21:42:34 -0600 Subject: [PATCH 2/5] #1930 move non-file-specific schema data before dc check --- github/data_source_github_repository_file.go | 7 ++++--- github/data_source_github_repository_file_test.go | 9 ++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/github/data_source_github_repository_file.go b/github/data_source_github_repository_file.go index a5f32c198c..ce37cdb7af 100644 --- a/github/data_source_github_repository_file.go +++ b/github/data_source_github_repository_file.go @@ -107,6 +107,10 @@ func dataSourceGithubRepositoryFileRead(d *schema.ResourceData, meta interface{} return err } + d.Set("repository", repo) + d.SetId(fmt.Sprintf("%s/%s", repo, file)) + d.Set("file", file) + // If the repo is a directory, then there is nothing to do. if dc != nil { return nil @@ -117,10 +121,7 @@ func dataSourceGithubRepositoryFileRead(d *schema.ResourceData, meta interface{} return err } - d.SetId(fmt.Sprintf("%s/%s", repo, file)) d.Set("content", content) - d.Set("repository", repo) - d.Set("file", file) d.Set("sha", fc.GetSHA()) parsedUrl, err := url.Parse(fc.GetURL()) diff --git a/github/data_source_github_repository_file_test.go b/github/data_source_github_repository_file_test.go index e3140f833d..741a4dc0cf 100644 --- a/github/data_source_github_repository_file_test.go +++ b/github/data_source_github_repository_file_test.go @@ -435,6 +435,9 @@ func TestDataSourceGithubRepositoryFileRead(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/contents/%s?ref=%s", org, repo, fileName, branch), @@ -470,8 +473,6 @@ func TestDataSourceGithubRepositoryFileRead(t *testing.T) { "file": fileName, "branch": branch, "commit_sha": sha, - "content": "", - "id": "", }) // actual call @@ -479,7 +480,9 @@ func TestDataSourceGithubRepositoryFileRead(t *testing.T) { // assertions assert.Nil(t, err) + assert.Equal(t, expectedRepo, schema.Get("repository")) + assert.Equal(t, expectedID, schema.Get("id")) assert.Equal(t, "", schema.Get("content")) - assert.Equal(t, "", schema.Get("id")) + assert.Equal(t, nil, schema.Get("sha")) }) } From 5867f29f3f852470e9140b7bcc969abdd311ab49 Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Mon, 9 Oct 2023 21:44:27 -0600 Subject: [PATCH 3/5] #1930 Clean up comment --- github/data_source_github_repository_file.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/github/data_source_github_repository_file.go b/github/data_source_github_repository_file.go index ce37cdb7af..c9ad137c3f 100644 --- a/github/data_source_github_repository_file.go +++ b/github/data_source_github_repository_file.go @@ -111,7 +111,8 @@ func dataSourceGithubRepositoryFileRead(d *schema.ResourceData, meta interface{} d.SetId(fmt.Sprintf("%s/%s", repo, file)) d.Set("file", file) - // If the repo is a directory, then there is nothing to do. + // If the repo is a directory, then there is nothing else we can include in + // the schema. if dc != nil { return nil } From 590932b598fc88ec209754ad0c8460abba5006a4 Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Mon, 9 Oct 2023 21:45:02 -0600 Subject: [PATCH 4/5] #1930 Remove blank line --- github/data_source_github_repository_file_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/github/data_source_github_repository_file_test.go b/github/data_source_github_repository_file_test.go index 741a4dc0cf..1be6936e80 100644 --- a/github/data_source_github_repository_file_test.go +++ b/github/data_source_github_repository_file_test.go @@ -226,7 +226,6 @@ func TestDataSourceGithubRepositoryFileRead(t *testing.T) { SHA: &sha, URL: &apiUrl, }) - repoCommit := &github.RepositoryCommit{ SHA: &sha, Committer: &github.User{ From 71ba6c081ddc004cd81b54657767a4f2fa3b2842 Mon Sep 17 00:00:00 2001 From: Preston Vasquez Date: Mon, 9 Oct 2023 21:45:49 -0600 Subject: [PATCH 5/5] #1930 Remove blank line --- github/data_source_github_repository_file_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/data_source_github_repository_file_test.go b/github/data_source_github_repository_file_test.go index 1be6936e80..edd5c3adec 100644 --- a/github/data_source_github_repository_file_test.go +++ b/github/data_source_github_repository_file_test.go @@ -430,7 +430,7 @@ func TestDataSourceGithubRepositoryFileRead(t *testing.T) { }, }) - t.Run("extract nothing if the path is for a directory", func(t *testing.T) { + t.Run("extract only non-file data if the path is for a directory", func(t *testing.T) { // test setup repositoryFullName := fmt.Sprintf("%s/%s", org, repo)