Skip to content

Commit 8753d3c

Browse files
authored
[fix]: skip file-specifc schema data for directory
2 parents e8a69d6 + 1162166 commit 8753d3c

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

github/data_source_github_repository_file.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func dataSourceGithubRepositoryFileRead(d *schema.ResourceData, meta interface{}
9595
opts.Ref = branch.(string)
9696
}
9797

98-
fc, _, _, err := client.Repositories.GetContents(ctx, owner, repo, file, opts)
98+
fc, dc, _, err := client.Repositories.GetContents(ctx, owner, repo, file, opts)
9999
if err != nil {
100100
if err, ok := err.(*github.ErrorResponse); ok {
101101
if err.Response.StatusCode == http.StatusNotFound {
@@ -107,15 +107,22 @@ func dataSourceGithubRepositoryFileRead(d *schema.ResourceData, meta interface{}
107107
return err
108108
}
109109

110+
d.Set("repository", repo)
111+
d.SetId(fmt.Sprintf("%s/%s", repo, file))
112+
d.Set("file", file)
113+
114+
// If the repo is a directory, then there is nothing else we can include in
115+
// the schema.
116+
if dc != nil {
117+
return nil
118+
}
119+
110120
content, err := fc.GetContent()
111121
if err != nil {
112122
return err
113123
}
114124

115-
d.SetId(fmt.Sprintf("%s/%s", repo, file))
116125
d.Set("content", content)
117-
d.Set("repository", repo)
118-
d.Set("file", file)
119126
d.Set("sha", fc.GetSHA())
120127

121128
parsedUrl, err := url.Parse(fc.GetURL())

github/data_source_github_repository_file_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,68 @@ func TestDataSourceGithubRepositoryFileRead(t *testing.T) {
420420
})
421421

422422
})
423+
424+
repoContentDirectoryRespBody := marshal(t, []github.RepositoryContent{
425+
{
426+
Encoding: &enc,
427+
Content: &b64FileContent,
428+
SHA: &sha,
429+
URL: &apiUrl,
430+
},
431+
})
432+
433+
t.Run("extract only non-file data if the path is for a directory", func(t *testing.T) {
434+
// test setup
435+
repositoryFullName := fmt.Sprintf("%s/%s", org, repo)
436+
437+
expectedID := fmt.Sprintf("%s/%s", repo, fileName)
438+
expectedRepo := "test-repo"
439+
440+
ts := githubApiMock([]*mockResponse{
441+
{
442+
ExpectedUri: fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", org, repo, fileName, branch),
443+
ResponseBody: repoContentDirectoryRespBody,
444+
StatusCode: http.StatusOK,
445+
},
446+
})
447+
defer ts.Close()
448+
449+
httpCl := http.DefaultClient
450+
httpCl.Transport = http.DefaultTransport
451+
452+
client := github.NewClient(httpCl)
453+
u, _ := url.Parse(ts.URL + "/")
454+
client.BaseURL = u
455+
456+
meta := &Owner{
457+
name: owner,
458+
v3client: client,
459+
}
460+
461+
testSchema := map[string]*schema.Schema{
462+
"repository": {Type: schema.TypeString},
463+
"file": {Type: schema.TypeString},
464+
"branch": {Type: schema.TypeString},
465+
"commit_sha": {Type: schema.TypeString},
466+
"content": {Type: schema.TypeString},
467+
"id": {Type: schema.TypeString},
468+
}
469+
470+
schema := schema.TestResourceDataRaw(t, testSchema, map[string]interface{}{
471+
"repository": repositoryFullName,
472+
"file": fileName,
473+
"branch": branch,
474+
"commit_sha": sha,
475+
})
476+
477+
// actual call
478+
err := dataSourceGithubRepositoryFileRead(schema, meta)
479+
480+
// assertions
481+
assert.Nil(t, err)
482+
assert.Equal(t, expectedRepo, schema.Get("repository"))
483+
assert.Equal(t, expectedID, schema.Get("id"))
484+
assert.Equal(t, "", schema.Get("content"))
485+
assert.Equal(t, nil, schema.Get("sha"))
486+
})
423487
}

0 commit comments

Comments
 (0)