Skip to content

Commit ff8ae8d

Browse files
authored
Merge branch 'main' into patch-2
2 parents 085f143 + e126a43 commit ff8ae8d

File tree

121 files changed

+2357
-2405
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+2357
-2405
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
}

github/provider.go

+12
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,18 @@ func tokenFromGhCli(baseURL string, isGithubDotCom bool) (string, error) {
407407
}
408408
hostname = parsedURL.Host
409409
}
410+
// GitHub CLI uses different base URLs in ~/.config/gh/hosts.yml, so when
411+
// we're using the standard base path of this provider, it doesn't align
412+
// with the way `gh` CLI stores the credentials. The following doesn't work:
413+
//
414+
// $ gh auth token --hostname api.github.com
415+
// > no oauth token
416+
//
417+
// ... but the following does work correctly
418+
//
419+
// $ gh auth token --hostname github.com
420+
// > gh..<valid token>
421+
hostname = strings.TrimPrefix(hostname, "api.")
410422
out, err := exec.Command(ghCliPath, "auth", "token", "--hostname", hostname).Output()
411423
if err != nil {
412424
// GH CLI is either not installed or there was no `gh auth login` command issued,

go.mod

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ require (
1010
github.com/hashicorp/terraform-plugin-sdk v1.17.2
1111
github.com/shurcooL/githubv4 v0.0.0-20221126192849-0b5c4c7994eb
1212
github.com/stretchr/testify v1.8.4
13-
golang.org/x/crypto v0.13.0
14-
golang.org/x/oauth2 v0.12.0
13+
golang.org/x/crypto v0.14.0
14+
golang.org/x/oauth2 v0.13.0
1515
gopkg.in/square/go-jose.v2 v2.6.0
1616
)
1717

@@ -198,9 +198,9 @@ require (
198198
go.opencensus.io v0.24.0 // indirect
199199
golang.org/x/exp/typeparams v0.0.0-20220827204233-334a2380cb91 // indirect
200200
golang.org/x/mod v0.8.0 // indirect
201-
golang.org/x/net v0.15.0 // indirect
201+
golang.org/x/net v0.16.0 // indirect
202202
golang.org/x/sync v0.2.0 // indirect
203-
golang.org/x/sys v0.12.0 // indirect
203+
golang.org/x/sys v0.13.0 // indirect
204204
golang.org/x/text v0.13.0 // indirect
205205
golang.org/x/tools v0.6.0 // indirect
206206
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect

0 commit comments

Comments
 (0)