Skip to content

Commit

Permalink
Merge branch 'main' into repository_file_removed_branch_bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfloyd authored Oct 11, 2023
2 parents 66a212c + f9ae9b4 commit 9c9aaad
Show file tree
Hide file tree
Showing 121 changed files with 2,346 additions and 2,405 deletions.
15 changes: 11 additions & 4 deletions github/data_source_github_repository_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -107,15 +107,22 @@ 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 else we can include in
// the schema.
if dc != nil {
return nil
}

content, err := fc.GetContent()
if err != nil {
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())
Expand Down
64 changes: 64 additions & 0 deletions github/data_source_github_repository_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,68 @@ func TestDataSourceGithubRepositoryFileRead(t *testing.T) {
})

})

repoContentDirectoryRespBody := marshal(t, []github.RepositoryContent{
{
Encoding: &enc,
Content: &b64FileContent,
SHA: &sha,
URL: &apiUrl,
},
})

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)

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),
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,
})

// actual call
err := dataSourceGithubRepositoryFileRead(schema, meta)

// 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, nil, schema.Get("sha"))
})
}
1 change: 1 addition & 0 deletions github/resource_github_repository_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func resourceGithubRepositoryEnvironmentRead(d *schema.ResourceData, meta interf
return nil
}
}
return err
}

d.Set("repository", repoName)
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ require (
github.com/hashicorp/terraform-plugin-sdk v1.17.2
github.com/shurcooL/githubv4 v0.0.0-20221126192849-0b5c4c7994eb
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.13.0
golang.org/x/oauth2 v0.12.0
golang.org/x/crypto v0.14.0
golang.org/x/oauth2 v0.13.0
gopkg.in/square/go-jose.v2 v2.6.0
)

Expand Down Expand Up @@ -198,9 +198,9 @@ require (
go.opencensus.io v0.24.0 // indirect
golang.org/x/exp/typeparams v0.0.0-20220827204233-334a2380cb91 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/net v0.16.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
Expand Down
745 changes: 736 additions & 9 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions vendor/golang.org/x/crypto/chacha20/chacha_arm64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/golang.org/x/crypto/chacha20/chacha_arm64.s

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/golang.org/x/crypto/chacha20/chacha_noasm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/golang.org/x/crypto/cryptobyte/builder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions vendor/golang.org/x/crypto/cryptobyte/string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions vendor/golang.org/x/crypto/sha3/sha3.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions vendor/golang.org/x/crypto/sha3/sha3_s390x.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 14 additions & 15 deletions vendor/golang.org/x/crypto/sha3/shake.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9c9aaad

Please sign in to comment.